About Constraints in Oracle SQL

 Constraints

============

Constraints are conditions put on table columns.

Oracle provides certain constraints that can be assigned to table columns.

The total number of constraints are: 

1.CHECK 

2.UNIQUE 

3.NOT NULL 

4.PRIMARY KEY 

5.FOREIGN KEY


Check Constraint

----------------

A CHECK constraint can be used for validation, 

for example, ensuring a hire date is greater than the company's establishment date.

If a date lower than the establishment date is inserted, the constraint will throw an error.


Unique Constraint

-----------------

The UNIQUE constraint can be used to ensure a column, such as a cell number, has unique values for every person.

A column with a UNIQUE constraint can allow records without a cell number (null values).


Primary Key Constraint

----------------------

The PRIMARY KEY constraint is used to ensure values are both unique and not null.

It achieves the goal of not allowing duplicate values and not allowing null values.

If a PRIMARY KEY is imposed, trying to insert a duplicate or null value will not be allowed; a unique value must be passed.


Foreign Key Constraint

----------------------

Relational Database Management Systems (RDBMS) maintain data in multiple tables and relate them using a common column as a primary-foreign key relationship.

The department number in the department table is a primary key.

The department number in the employee table is a foreign key.

The foreign key column refers to the primary key column of the foreign (parent) table.

When a department number is inserted into the employee table, the foreign key constraint checks if that department number exists in the parent table's primary key column.

If the value exists, the record is allowed; otherwise, it is rejected with an error message.

The foreign key column in the employee table allows duplicate values, as multiple employees can belong to the same department.


Constraint Repositories

-----------------------

The repositories for constraints follow the pattern of those for tables, using the words "user" and "all" as prefixes.

USER_ constraints show only the constraints for the logged-in user.

select *from USER_constraints;

ALL_ constraints show the constraints for all users and all schemas.

Select *from ALL_constraints;

Comments

Popular posts from this blog

Importance of Incremental Data in Oracle Fusion

Mastering Parameters in Oracle Fusion BI Publisher

REST Web Service in Oracle Fusion to manage Banks