0% found this document useful (0 votes)
8 views18 pages

Integrity Constraints

The document outlines various types of integrity constraints in SQL, including domain, entity, and referential integrity constraints. It explains specific constraints such as NOT NULL, CHECK, DEFAULT, UNIQUE, PRIMARY KEY, and FOREIGN KEY, along with examples of their implementation in SQL. Additionally, it discusses the behavior of foreign keys with cascade delete and set null options.

Uploaded by

hnpatil2821969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views18 pages

Integrity Constraints

The document outlines various types of integrity constraints in SQL, including domain, entity, and referential integrity constraints. It explains specific constraints such as NOT NULL, CHECK, DEFAULT, UNIQUE, PRIMARY KEY, and FOREIGN KEY, along with examples of their implementation in SQL. Additionally, it discusses the behavior of foreign keys with cascade delete and set null options.

Uploaded by

hnpatil2821969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

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));

 CREATE TABLE Persons (


ID number NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age number,
City varchar(255),
CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes')
);
CHECK Constraint…using Alter
 ALTER TABLE Persons
ADD CHECK (Age>=18);

 ALTER TABLE Persons


ADD CONSTRAINT CHK_PersonAge CHECK (Age>=18
AND City='Sandnes');

 ALTER TABLE Persons


DROP CONSTRAINT CHK_PersonAge;
DEFAULT Constraint
 The DEFAULT constraint is used to provide a default value for a
column.
 The default value will be added to all new records IF no other
value is specified.

 CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);
SQL DEFAULT on ALTER TABLE
 ALTER TABLE Persons
MODIFY City DEFAULT 'Sandnes';

 ALTER TABLE Persons


ALTER COLUMN City DROP DEFAULT;
Entity Integrity Constraint…Unique
 The UNIQUE constraint ensures that all values in a column are different.

 CREATE TABLE Persons (


ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
 CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT UC_Person UNIQUE (ID,LastName)
);
SQL UNIQUE Constraint on ALTER
TABLE
 ALTER TABLE Persons
ADD UNIQUE (ID);

 ALTER TABLE Persons


ADD CONSTRAINT UC_Person UNIQUE (ID,LastName);

 ALTER TABLE Persons


DROP CONSTRAINT UC_Person;
SQL PRIMARY KEY Constraint
 The PRIMARY KEY constraint uniquely identifies each record in a
database table.
 Primary keys must contain UNIQUE values, and cannot contain
NULL values.
 A table can have only one primary key, which may consist of single
or multiple fields.
 CREATE TABLE Persons (
ID int ,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (ID)
);
SQL PRIMARY KEY on ALTER TABLE
 ALTER TABLE Persons
ADD PRIMARY KEY (ID);

 ALTER TABLE Persons


DROP PRIMARY KEY;

 ALTER TABLE Persons


ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName);

 ALTER TABLE Persons


DROP CONSTRAINT PK_Person;
Referential IC…Foreign Key
 A FOREIGN KEY is a key used to link two tables together.
 A FOREIGN KEY is a field (or collection of fields) in one
table that refers to the PRIMARY KEY in another table.
 The table containing the foreign key is called the child table,
and the table containing the candidate key is called the
referenced or parent table.
Referential IC…Foreign Key
CREATE TABLE supplier
(supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id) );

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) );
Referential IC…Foreign Key
 ALTER TABLE Orders
ADD FOREIGN KEY (PersonID) REFERENCES Persons(Pe
rsonID);

 ALTER TABLE Orders


ADD CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonI
D);

 ALTER TABLE Orders


DROP CONSTRAINT FK_PersonOrder;
Foreign key with Cascade DELETE
 A foreign key with cascade delete means that if a record in the
parent table is deleted, then the corresponding records in the child
table will automatically be deleted. This is called a cascade delete

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 CASCADE);
EMP
Eid Ename Salary Did
1 AAA 10000
2 BBB 2000 10
3 CCC 30000 30
4 DDD 12000 10

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

You might also like