Course : 20CS401- Database Management System
Module : Data Modeling and Relational Query Language
Topic : SQL Keys & Integrity Constraints
Faculty : Sajeev Ram. A
SQL - Keys
• It is used to uniquely identify any record or row of data from the table. It is also used to establish
and identify relationships between tables
• In Student table, ID is used as a key because it is unique for each student. In PERSON table,
passport_number, license_number, SSN are keys since they are unique for each person
SQL - Keys | Types
keys
Primary Candidate Super
Foreign Key
Key Key Key
SQL - Keys | Primary Key
• It is the first key which is used to identify one and only one instance of
an entity uniquely. An entity can contain multiple keys as we saw in
PERSON table. The key which is most suitable from those lists become
a primary key.
• In the EMPLOYEE table, ID can be primary key since it is unique for
each employee. In the EMPLOYEE table, we can even select
License_Number and Passport_Number as primary key since they are
also unique.
• For each entity, selection of the primary key is based on requirement
and developers.
SQL - Keys | Candidate Key
• A candidate key is an attribute or set of an attribute which can uniquely
identify a tuple.
• The remaining attributes except for primary key are considered as a
candidate key. The candidate keys are as strong as the primary key.
• For example: In the EMPLOYEE table, id is best suited for the primary
key. Rest of the attributes like SSN, Passport_Number, and
License_Number, etc. are considered as a candidate key.
SQL - Keys | Super Key
• Super key is a set of an attribute which can uniquely identify a tuple.
Super key is a superset of a candidate key.
• For example: In the above EMPLOYEE table, for(EMPLOEE_ID,
EMPLOYEE_NAME) the name of two employees can be the same, but
their EMPLYEE_ID can't be the same. Hence, this combination can also
be a key.
• The super key would be EMPLOYEE-ID, (EMPLOYEE_ID,
EMPLOYEE-NAME), etc.
SQL - Keys | Foreign Key
• Foreign keys are the column of the table which is used to
point to the primary key of another table.
• In a company, every employee works in a specific
department, and employee and department are two different
entities. So we can't store the information of the department
in the employee table. That's why we link these two tables
through the primary key of one table.
• We add the primary key of the DEPARTMENT table,
Department_Id as a new attribute in the EMPLOYEE table.
• Now in the EMPLOYEE table, Department_Id is the
foreign key, and both the tables are related.
SQL - Integrity Constraints
• SQL constraints are used to specify rules for the data in a table.
• Constraints are used to limit the type of data that can go into a table.
• This ensures the accuracy and reliability of the data in the table.
• If there is any violation between the constraint and the data action, the action is aborted
• Constraints can be column level or table level. Column level constraints apply to a column, and
table level constraints apply to the whole table.
• The following constraints are commonly used in SQL
NOT NULL - Ensures that a column cannot have a NULL value
UNIQUE - Ensures that all values in a column are different
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
FOREIGN KEY - Prevents actions that would destroy links between tables
CHECK - Ensures that the values in a column satisfies a specific condition
DEFAULT - Sets a default value for a column if no value is specified
CREATE INDEX - Used to create and retrieve data from the database very quick
SQL - Integrity Constraints | NOT NULL
• By default, a column can hold NULL values
• The NOT NULL constraint enforces a column to NOT accept NULL values
• This enforces a field to always contain a value, which means that you cannot insert a new record,
or update a record without adding a value to this field.
SQL NOT NULL on CREATE TABLE
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
SQL - Integrity Constraints | UNIQUE
• The UNIQUE constraint ensures that all values in a column are different.
• Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a
column or set of columns.
• A PRIMARY KEY constraint automatically has a UNIQUE constraint.
• However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY
constraint per table.
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID)
);
SQL - Integrity Constraints | Primary Key
• The PRIMARY KEY constraint uniquely identifies each record in a table.
• Primary keys must contain UNIQUE values, and cannot contain NULL values.
• A table can have only ONE primary key; and in the table, this primary key can consist of single
or multiple columns (fields).
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
SQL - Integrity Constraints | Foreign Key
• The FOREIGN KEY constraint is used to prevent actions that would destroy links between
tables.
• A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY
KEY in another table.
• The table with the foreign key is called the child table, and the table with the primary key is
called the referenced or parent table.
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
SQL - Integrity Constraints | CHECK
• The CHECK constraint is used to limit the value range that can be placed in a column.
• If you define a CHECK constraint on a column it will allow only certain values for this column.
• If you define a CHECK constraint on a table it can limit the values in certain columns based on
values in other columns in the row.
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
SQL - Integrity Constraints | DEFAULT
• The DEFAULT constraint is used to set 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 - Integrity Constraints | CREATE INDEX
• The CREATE INDEX statement is used to create indexes in tables.
• Indexes are used to retrieve data from the database more quickly than otherwise. The users
cannot see the indexes, they are just used to speed up searches/queries.
CREATE INDEX Syntax CREATE UNIQUE INDEX Syntax
CREATE INDEX index_name CREATE UNIQUE INDEX index_name
ON table_name (column1, column2, ...); ON table_name (column1, column2, ...);
CREATE INDEX idx_lastname
ON Persons (LastName);
VIDEO LECTURE