how to get the list of all constraints in MySQL database
In MySQL, you can retrieve information about constraints (rules) in a database by querying the information schema tables. The information schema contains metadata about the database, including details about tables, columns, and constraints. To get a list of constraints in MySQL, you can use the following queries.
Syntax :
SELECT
constraint_name,
constraint_type,
table_name
FROM
information_schema.table_constraints
WHERE
table_schema = 'your_database_name';
Replace 'your_database_name' with the actual database name.
Query :
select constraint_name, constraint_type, table_name from information_schema.table_constraints where table_schema = 'learn_sql';
Ouput :
+---------------------+-----------------+------------------+
| CONSTRAINT_NAME | CONSTRAINT_TYPE | TABLE_NAME |
+---------------------+-----------------+------------------+
| fk_users | FOREIGN KEY | addresses |
| email | UNIQUE | employees |
| PRIMARY | PRIMARY KEY | employees |
| employees_chk_1 | CHECK | employees |
| employees_chk_2 | CHECK | employees |
| PRIMARY | PRIMARY KEY | orders |
| fk_orders_employees | FOREIGN KEY | orders |
| fk_users_orders | FOREIGN KEY | orders |
| PRIMARY | PRIMARY KEY | sales |
| PRIMARY | PRIMARY KEY | statement_backup |
| email | UNIQUE | users |
| PRIMARY | PRIMARY KEY | users |
+---------------------+-----------------+------------------+
12 rows in set (0.00 sec)
Above query will return the constraint name, constraint type, table name from information_schema
database for the learn_sql
database.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Sql
- Cannot drop index 'fk_role_id': needed in a foreign key constraint
- How to get a list of MySQL views?
- How to drop function in MySQL ?
- How to Rename column name in MySql ?
- Difference between SQL and NoSQL databases
- How to check the MySQL version
- SQLSTATE[01000]: Warning: 1265 Data truncated for column 'visibility' at row 1
- How to get yesterday's date in MySQL ?
- 1553 Cannot drop index 'posts_user_id_foreign': needed in a foreign key constraint
- Get number of connections being used by a specific user in MySQL
- Insert multiple rows in a single MySQL query
- SQL query to delete records older than 6 months
- Get create table Query from existing table in phpMyAdmin
- Drop foreign id column (user_id) in MySQL
- MySQL create procedure example
- How to check column exists or not in table using MySQL
- How to retrieve data from two tables with one SQL statement
- Check constraint in MySQL
- Self join in mysql employee manager example
- Create Employee table in MySQL
- Database connection using mySQLi with object-oriented
- Get data in random order with limit from table using mysql
- Get comma separated ids from table using MySQL
- How to create function in MySQL ?
- ERROR 3780 (HY000): Referencing column 'order_id' and referenced column 'id' in foreign key constraint 'order_items_ibfk_1' are incompatible.