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
Constraints | Descriptions |
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 Null | Ensures that any column must not contain NULL values. |
Default | Establish 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_ID | NAME | DEPT | SALARY(₹) |
E101 | Alice | SDE | 75000.00 |
E102 | David Williams | Manager | 100000.00 |
E103 | Emma Davis | QA_Tester | 45000.00 |
E104 | Charlie Brown | Sales | 65000.00 |
E105 | Bob Smith | IT Operation | 25000.00 |
Example: Getting the
SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE, TABLE_NAME
FROM USER_CONSTRAINTS
WHERE TABLE_NAME = 'EMPLOYEES';
Output:
CONSTRAINT_NAME | CONSTRAINT_TYPE | TABLE_NAME |
SYS_C0012345 | P (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';