Display names of All constraints for a table in Oracle SQL

Display names of All constraints for a table in Oracle SQL

In Oracle SQL, the names of all the constraints for the table can be displayed using system views such as USER_CONSTRAINTS and ALL_CONSTRAINTS.

This blog explains rendering all the constraint names for the table in Oracle SQL.

Table Of Contents

Getting All Constraint Names in Oracle SQL

In SQL, the Constraints define the rules for data in a table, which are applied to table columns when the table is created, enforcing data integrity and consistency. For this reason, the data adheres to specific requirements (like unique values, valid foreign keys, or non-null values).

To analyze or debug a table’s structure, you can view the names of all constraints that are applied to a specific table. The system views such as USER_CONSTRAINTS and ALL_CONSTRAINTS, which are provided by Oracle, facilitate the retrieval of these kinds of information efficiently.

Syntax

SELECT constraint_name, constraint_type, table_name
FROM ALL_CONSTRAINTS
WHERE table_name = “EMPLOYEES”;

Explanation

Here, the constraint_name depicts the name of the constraint,  constraint_type denotes the types of constraints followed by their value, and table_name depicts the name of the table to which the constraint belongs. The ALL_CONSTRAINTS helps to get the metadata about all constraints that are accessible to the current user, regardless of the schema.

Constraints can be categorized into column level or table level. The column-level constraints are implemented to a column, and the table-level constraints are implemented to the whole table.

The user_constraints are utilized to display the names of the constraints in the database. The all_constraints provides the metadata about all constraints that are accessible to the current user, regardless of the schema.

Commonly Used Constraints in Oracle SQL

Most commonly used constraints in Oracle SQL

ConstraintsDescriptions
Primary Key (P)Each row must have a unique and non-null identifier.
Foreign Key (R)Set up the link between two tables.
Unique (U)Ensures all values in a column must be unique.
Check (C)Validates that column values must satisfy the specified condition.
Not NullEnsures that any column must not contain NULL values.
DefaultEstablish a default value for a column if no value is specified.

Example: Below example rendering names of all the constraints with the following SQL Query:

Employees Table:

EMP_IDNAMEDEPTSALARY(₹)
E101AliceSDE75000.00
E102David WilliamsManager100000.00
E103Emma DavisQA_Tester45000.00
E104Charlie BrownSales65000.00
E105Bob SmithIT Operation25000.00

Example: Getting the

SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE, TABLE_NAME
FROM USER_CONSTRAINTS
WHERE TABLE_NAME = 'EMPLOYEES';

Output:

CONSTRAINT_NAMECONSTRAINT_TYPETABLE_NAME
SYS_C0012345P (Primary Key)            EMPLOYEES

Conclusion

Constraints can be utilized to restrict the type of data that can be put into a table. For this, it certifies the accuracy and reliability of the table data. In case of any violation between the constraint and the data action, the action is aborted. Using the USER_CONSTRAINTS or ALL_CONSTRAINTS views can help to easily identify the constraints implemented in a table and their respective types. This helps the database design, debugging, and maintaining consistency across schemas.

FAQs

1. How to display constraints like a primary key only for a table?

Apply the filter to the constraint_type column for ‘P’:

SELECT constraint_name
FROM user_constraints
WHERE table_name = 'User_Data' AND constraint_type = 'P';
2. How to drop a constraint by its name?

Use the ALTER TABLE statement:

ALTER TABLE table_Name DROP CONSTRAINT constraint_name;
3. How to check if a column is part of a composite primary key?

Use the query USER_CONS_COLUMNS to find the columns involved:

SELECT column_name
FROM user_cons_columns
WHERE constraint_name = 'EMP_PK';

About the Author

Data Engineer

As a skilled Data Engineer, Sahil excels in SQL, NoSQL databases, Business Intelligence, and database management. He has contributed immensely to projects at companies like Bajaj and Tata. With a strong expertise in data engineering, he has architected numerous solutions for data pipelines, analytics, and software integration, driving insights and innovation.

business intelligence professional