MySQL MariaDB

How to Show Constraints of a Table in MySQL?

In MySQL, constraints play a vital role in maintaining data integrity by restricting or controlling the data that can be inserted, updated, or deleted from a table. Constraints can be defined during the creation of a table or incorporated into an already existing table. If a constraint is enabled on a table, the data will only be manipulated according to the specified constraint.

This guide will provide in-depth information on how to show the constraints of a table in MySQL.

How to Show/Display Constraints of a Table in MySQL?

In MySQL, the constraints are utilized to set the rules to restrict or control the data to be inserted, updated, or deleted from a table. The constraints can be shown using different commands, but before that, you need to create the constraints.

The constraints can be created while creating the table as provided below in the example:

CREATE TABLE lh_table (

id INT NOT NULL,

name VARCHAR(50),

age INT,

PRIMARY KEY (id),

CHECK (age >= 18)

);

In the above example, the table name “lh_table” with the constraints on the “id” and “age” columns has been created.

Output

The output showed that the table and constraints have been created.

Alternatively, the constraints can also be added to the existing (already created) table utilizing the “ALTER TABLE” command as shown below:

ALTER TABLE lh_table ADD CONSTRAINT id_check CHECK (id < 1000);

In the above example, the new constraints name “id_check” is being created.

Output

The output showed that the constraints had been created.

After creating the constraints, there are multiple methods to show the constraints, such as by utilizing the “SHOW CREATE” command or the “information_schema”.

Method 1: Show Constraints Using the “SHOW CREATE TABLE” Command

The constraints of a particular table can be displayed by utilizing the SHOW CREATE TABLE command, with the particular table name. An example of showing the constraints of the “lh_table” is given below:

SHOW CREATE TABLE lh_table;

Output

The output depicts the constraints of the “lh_table”.

Method 2: Show Constraints Using the information_schema

The constraints of a specific table can also be displayed by utilizing the “information schema”. The information is retrieved by using the “SELECT” statement and the table is specified with the “WHERE” clause. An example of retrieving the constraints of the “lh_table” is given below:

SELECT constraint_name, constraint_type, table_name

FROM information_schema.table_constraints

WHERE table_name = 'lh_table';

In the above example, constraint_name, constraint_type, and table_name columns of the “information_schema.table_constraints” are being retrieved.

Output

The output showed the constraints name of the “lh_table” with its type and table name.

Method 3: Show Constraints of Multiple Tables

Multiple tables’ constraints can also be shown by using the information schema of the constraints table. To do that, you must utilize the “IN” operator in the “WHERE” clause to provide the table names. An example of retrieving the “lh_table” and “lh_check” tables constraints is given below:

SELECT constraint_name, constraint_type, table_name

FROM information_schema.table_constraints

WHERE table_name IN ('lh_table', 'lh_check');

Output

The output showed the available constraints of the selected tables.

Conclusion

In MySQL, constraints are used to control the data that can be manipulated in a table. They can be created while creating the table using the “CREATE” command or added to an existing (already created) table using the “ALTER TABLE” command. To retrieve the constraints, you can use multiple methods such as SHOW CREATE TABLE or querying the information_schema of table_constraints. This post presented in-depth information on retrieving the constraints of a table.

About the author

Danish Ghafoor

I am a computer science graduate with a certification in cybersecurity. My passion for technology has led me to develop a deep understanding of the challenges and solutions in the field of information security. I am dedicated to staying up-to-date with the latest industry trends and technologies, and constantly seeking out new opportunities to expand my knowledge and skills.