Integrity Constraints
Integrity Constraints
Prepared By:
Sushma Vankhede
Constraints
The following constraints are commonly used in SQL:
Domain Integrity Constraints
NOT NULL - Ensures that a column cannot have a NULL value
CHECK - Ensures that all values in a column satisfies a specific
condition
DEFAULT - Sets a default value for a column when no value is
specified
Entity Integrity Constraints
PRIMARY KEY - A combination of a NOT NULL and UNIQUE.
Uniquely identifies each row in a table
UNIQUE - Ensures that all values in a column are different
Referential Integrity Constraints
FOREIGN KEY - Uniquely identifies a row/record in another table
Not Null
By default, a column can hold NULL values.
The NOT NULL constraint enforces a column to NOT
accept NULL values.
CREATETABLE Persons (
ID number NOT NULL,
LastName varchar(20) NOT NULL,
FirstName varchar(20) NOT NULL,
Age number
);
CHECK Constraint
The CHECK constraint is used to limit the value range that can be
placed in a column.
CREATE TABLE Persons (
ID number NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age number CHECK (Age>=18));
Child Table
Dept
Parent Table
Dno Dname Location
10 IT Surat
30 Admin BOM
Foreign key with On DELETE Set Null
A foreign key with "set null on delete" means that if a record
in the parent table is deleted, then the corresponding records
in the child table will have the foreign key fields set to
NULL. The records in the child table will not be deleted
CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE SET NULL);
END