SQL Query to Display All the Existing Constraints on a Table
Last Updated :
30 Dec, 2021
In SQL, we sometimes need to display all the currently existing constraints on a table. The whole process for doing the same is demonstrated below. For this article, we will be using the Microsoft SQL Server as our database.
Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks.
Query:
CREATE DATABASE GeeksForGeeks
Output:

Step 2: Use the GeeksForGeeks database. For this use the below command.
Query:
USE GeeksForGeeks
Output:

Step 3: Create a table STUDENT_INFO inside the database GeeksForGeeks. This table has 3 columns namely ROLL_NO, STUDENT_NAME, and BRANCH containing the roll number, name, and branch of various students.
Query:
CREATE TABLE STUDENT_INFO(
ROLL_NO INT,
STUDENT_NAME VARCHAR(10),
BRANCH VARCHAR(5)
);
Output:

Step 4: Display the current constraints applied on the table STUDENT_INFO. We use INFORMATION_SCHEMA.TABLE_CONSTRAINTS to display the constraints. Here, we display the name(CONSTRAINT_NAME) and the type of the constraint(CONSTRAINT_TYPE) for all existing constraints.
Syntax:
SELECT INFORMATION
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME='TABLE_NAME';
Query:
SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME='STUDENT_INFO';
Note - Since our table has no constraints currently, hence no rows are displayed.
Output:

Step 5: Alter the ROLL_NO column using ALTER clause table to set it to NOT NULL. We do this because it is a prerequisite for setting the ROLL_NO as a PRIMARY KEY(done in the next step).
Query:
ALTER TABLE STUDENT_INFO ALTER
COLUMN ROLL_NO INT NOT NULL;
Output:

Step 6: Add a PRIMARY KEY constraint named C1 to the ROLL_NO column using ALTER clause.
Query:
ALTER TABLE STUDENT_INFO ADD CONSTRAINT
C1 PRIMARY KEY (ROLL_NO);
Output:

Step 7: Display the current constraints applied on the table STUDENT_INFO.
Query:
SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME='STUDENT_INFO';
Note: Since our table has only 1 constraint i.e. the PRIMARY KEY constraint currently, hence only 1 row is displayed.
Output:

Step 8: Add a CHECK constraint named BRANCH_CHECK to the BRANCH column using ALTER clause.
Query:
ALTER TABLE STUDENT_INFO ADD CONSTRAINT BRANCH_CHECK
CHECK (BRANCH IN('CSE','ECE','CE','ME','ELE'));
Output:

Step 9: Display the current constraints applied on the table STUDENT_INFO.
Query:
SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME='STUDENT_INFO';
Note: Since our table has 2 constraints i.e. the PRIMARY KEY constraint and the CHECK constraint currently, hence 2 rows are displayed.
Output:

Step 10: Add a UNIQUE constraint named UNIQ to the STUDENT_NAME column using ALTER clause.
Query:
ALTER TABLE STUDENT_INFO ADD CONSTRAINT
UNIQ UNIQUE(STUDENT_NAME);
Output:

Step 11: Display the current constraints applied on the table STUDENT_INFO.
Query:
SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME='STUDENT_INFO';
Note: Since our table has 3 constraints i.e. the PRIMARY KEY constraint, the CHECK constraint, and the UNIQUE constraint currently, hence 3 rows are displayed.
Output:
Similar Reads
SQL Query to Find the Number of Columns in a Table
SQL stands for a structure query language, which is used in the database to retrieve data, update and modify data in relational databases like MySql, Oracle, etc. And a query is a question or request for data from the database, that is if we ask someone any question then the question is the query. S
4 min read
SQL Query to Update All Columns in a Table
In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database an
2 min read
SQL | Checking Existing Constraints on a Table using Data Dictionaries
Prerequisite: SQL-Constraints In SQL Server the data dictionary is a set of database tables used to store information about a database's definition. One can use these data dictionaries to check the constraints on an already existing table and to change them(if possible). USER_CONSTRAINTS Data Dictio
2 min read
Truncate Tables with Dependent Foreign Key Constraints in SQL
Truncating a table is a common database operation used to efficiently remove all table rows. Unlike the DELETE statement which logs individual row deletions, TRUNCATE operates at the table level and make it faster and less resource-intensive. However, when foreign key constraints are involved trunca
4 min read
How to Temporarily Disable a Foreign Key Constraint in MySQL?
MySQL is a popular open-source relational database management system (RDBMS) that is uniquely used to construct expandable and high-productivity databases. MySQL, which was created by MySQL AB and later acquired by its current owner Oracle Corporation, was originally introduced in 1995. MySQL is rep
4 min read
How to Delete a Foreign Key Constraint in SQL
Foreign key constraints are important for maintaining referential integrity in a relational database as they define relationships between tables. However, there are times when we may need to identify which foreign key constraints reference a specific table in SQL Server. This could be necessary for
4 min read
How to Create a Table With a Foreign Key in SQL?
A foreign key is a column or a set of columns in one table that references the primary key of another table. Foreign keys are used to establish and enforce a link between the data in two tables, ensuring referential integrity in the relational database system. In this article, we will explain how to
5 min read
SQL Query to Create a Backup Table
Relational databases play an important role in managing data, especially during complex operations like updates and deletions. To maintain data integrity, it is essential to back up tables before making changes. SQL backup tables ensure the safety of the original dataset, allow for data recovery, an
5 min read
Adding multiple constraints in a single table
Prerequisite - SQL Constraints We can create a table with more than one constraint in its columns. Following example shows how we can define different constraints on a table. Adding constraints in Create command : Sr_no is a Primary Key. Branch_no is the foreign key referencing Branch table. Company
2 min read
Find all Tables that Contain a Specific Column Name in SQL Server
When managing a database in SQL Server, it's often necessary to find out which tables include a specific column name. This task is essential for database administrators and developers, especially when updating database structures, migrating data safely, or getting a better understanding of how infor
2 min read