SQL 3
SQL 3
DBMS SQL
DATAINTEGRITY& CONSTRAINTS
• The constraint in SQL is used to specify the rule that allows or restricts
what values/data will be stored in the table.
• They provide a suitable method to ensure data accuracy and integrity
inside the table.
• It also helps to limit the type of data that will be inserted inside the
table
DATAINTEGRITY& CONSTRAINTS
• The following are the most common constraints used in the Oracle:
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT
• AUTO_INCREMENT
• INDEX
• ENUM
NOT NULL Constraint
• This constraint specifies that the column cannot have NULL or empty
values.
• The NOT NULL constraint ensures that the column has a value and the
value is not a null value.
• A space or a numeric zero is not a null value
Example: Create a table Student with following schema.
Student(Id, First Name, Last Name, City) Apply Not Null constraint on
Student Name.
UNIQUE Constraint
• This constraint ensures that all values inserted into the column will be
unique.
• It means a column cannot store duplicate values.
• SQL allows us to use more than one column with UNIQUE constraints
in a table.
• Example: Create a table Product with the following schema.
Product(Id, Name, quantity, Price) Apply Unique constraint on Product
Id.
PRIMARY KEY Constraint
• This constraint is used to link two tables together. This is also known as referential integrity constraint.
It is also known as the referencing key. A foreign key column matches the primary key field of another
table. At the table level ONLY.
• It uses a column or columns as a foreign key, and it establishes a relationship with the primary key of
the same or another table.
• Foreign key and referenced primary key columns need not have the same name, but a foreign key
value must match the value in the parent table’s primary key value or be NULL
• It means a foreign key field in one table refers to the primary key field of another table.
Foreign Key Constraint Example