You are on page 1of 37

20CS401-DATABASE MANAGEMENT SYSTEMS

Module :I
Module name :Data Modeling and Relational Query Language
Topic:Keys and Integrity constraints
Faculty :Ms.V.R.Azhaguramyaa, AP/CSE

Database Management Systems 1


Database Management Systems 2
TOPIC OBJECTIVE

• At the end of this presentation students should be


able to:

 Understand the different integrity constraints


with example

Database Management Systems 3


INTEGRITY CONSTRAINTS

• Constraints or nothing but the rules that are


to be followed while entering data into
columns of the database table
•  
• Constraints ensure that data entered by the
user into columns must be within the criteria
specified by the condition 

Database Management Systems 4


INTEGRITY CONSTRAINTS-TYPES

Database Management Systems 5


NOT NULL

• Null represents a record where data may be missing 


data or data for that record may be optional.

• Once Not null is applied to a particular column, you


cannot enter null values to that column and restricted
to maintain  only some proper value other than null.

• A not-null constraint cannot be applied at table level

Database Management Systems 6


SYNTAX

• Create table tablename(


columnname1 datatype NOT NULL,
columnname2 datatype NOT NULL,
columnname3 datatype ,
columnname4 datatype)

Database Management Systems 7


EXAMPLE

CREATE TABLE STUDENT(


 ID   INT NOT NULL,NAME VARCHAR
(20),AGE  INT ,PHNO INT)          
   

Database Management Systems 8


UNIQUE

• Unique constraint ensures that all values in a


column are unique..

• Sometimes we need to maintain only unique


data in the column of a database table, this is
possible by using a unique constraint 

Database Management Systems 9


SYNTAX

• Create table tablename(


columnname1 datatype UNIQUE,
columnname2 datatype NOT NULL,
columnname3 datatype UNIQUE,
columnname4 datatype)

Database Management Systems 10


EXAMPLE

CREATE TABLE STUDENT(


 NAME VARCHAR (20) NOT NULL,
   AGE  INT ,MARKS INT, ID   INT
UNIQUE)          
   

Database Management Systems 11


DEFAULT

• Default clause in SQL is used to add default data to the


columns

• When a column is specified as default with some value


then all the rows will use the same value i.e each and every
time while entering the data we need not enter that value 

• But default column value can be customized i.e it can be


overridden  when inserting a data for that row based on the
requirement

Database Management Systems 12


SYNTAX

• Create table tablename(


columnname1 datatype UNIQUE,
columnname2 datatype NOT NULL,
Columnname3 datatype UNIQUE,
columnname4 datatype DEFAULT ‘VALUE')

Database Management Systems 13


EXAMPLE

CREATE TABLE STUDENT(ID   INT UNIQUE ,NAME


VARCHAR(20) NOT NULL,
PHNO  INT ,COMPANY VARCHAR(20) DEFAULT
‘PREPINSTA’)          
   

Database Management Systems 14


CHECK

• Check constraint ensures that the data entered


by the user for that column is within the range
of values or possible values specified 

• EXAMLPLE
• Suppose in real-time if you want to give access to
an application only if the age entered by the user
is greater than 18 this is done at the back-end by
using a check constraint
Database Management Systems 15
SYNTAX

• Create table tablename(


columnname1 datatype UNIQUE,
columnname2 datatype,
columnname3 datatype,
columnname4 datatype CHECK(CONDITION))

Database Management Systems 16


EXAMPLE

CREATE TABLE STUDENT(


 NAME VARCHAR (20) NOT NULL,
    ID   INT UNIQUE ,MARKS INT, AGE  INT
CHECK(AGE>18))          
   

Database Management Systems 17


PRIMARY KEY

• A primary key is a constraint  in a table


which uniquely identifies each row record in a
database table.

• Primary key will restrict you to maintain unique


values and not null values in that particular
column.

• A table can have only one primary key.


Database Management Systems 18
SYNTAX

• Create table tablename(


columnname1 datatype PRIMARY KEY,
columnname2 datatype UNIQUE,
columnname3 datatype NOT NULL,
columnname4 datatype)

Database Management Systems 19


PRIMARY KEY

Database Management Systems 20


EXAMPLE

CREATE TABLE STUDENT(


  ID   INT PRIMARY KEY,NAME VARCHAR
(20) ,AGE  INT ,COURSE VARCHAR(10));   

Database Management Systems 21


FOREIGN KEY

• The foreign key constraint is a column which


points to the primary key column of another
table.

• The main purpose of the foreign key is only those


values are allowed in the present table that will
match to the primary key column of another
table 
Database Management Systems 22
RULES FOR FOREIGN KEY
 NULL is allowed in SQL Foreign key.

 The table being referenced is called the Parent Table.

 The table with the Foreign Key in SQL is called Child Table.

 The SQL Foreign Key in child table references the primary key in
the parent table.

 This parent-child relationship enforces the rule which is known


as "Referential Integrity."

Database Management Systems 23


SYNTAX

• Create table tablename(


columnname1 datatype,
columnname2 datatype,
Columnname3 datatype,
columnname4 datatype,
FOREIGN KEY (columnname) REFERENCES PARENT
TABLENAME(PRIMARY KEY))

Database Management Systems 24


FOREIGN KEY

Database Management Systems 25


EXAMPLE
STUDENT MARK_DETAILS

CREATE TABLE STUDENT( CREATE TABLE MARK_DETAILS(


  ID   INT PRIMARY KEY,NAME   ID   INT,MARKS INT,FOREIGN
VARCHAR (20) ,COURSE KEY(ID) REFERENCES
VARCHAR(10));    STUDENT(ID));   

Database Management Systems 26


Database Management Systems 27
Database Management Systems 28
KEYS-Used to identify the records uniquely

Database Management Systems 29


Database Management Systems 30
Database Management Systems 31
SUPER KEY

Set of attributes that can help us uniquely identify a record or


tuple in the database is called super key.

Database Management Systems 32


CANDIDATE KEY
Candidate keys are selected from the set of super keys, the only thing that you
should remember while selecting candidate keys is, it should not have any
redundant attributes.

Definition of candidate key: Super key with no redundant attributes known as


candidate key i.e should not contain any column that contains duplicate data 

Database Management Systems 33


CANDIDATE KEY

 {Id}: ID column will contain all unique values hence ID column is a


candidate key
 {phone}: As no two students have the same phone number, it is not
redundant data column and hence phone column is a candidate key
 {Id, phone}: As both ID and phone are unique for all students this
combination is a valid candidate key
 {Id, Name}: This combination is not a candidate key because name column
may have duplicate values
 {Id, phone, Name}: This combination is not a candidate key because name
column may have duplicate values
 {Name, Phone}: This combination is not a candidate key because name
column may have duplicate values

Database Management Systems 34


COMPOSITE KEY

If a single column alone fails to be served as a primary key


then combination  columns would help to uniquely access a
record from table such type of keys are called as composite keys 

Database Management Systems 35


ALTERNATE KEY

Alternate keys  are  columns present in the table which are not


selected as primary keys but still, they have all the capabilities to
be used as a primary key is called alternate key .

Database Management Systems 36


Database Management Systems 37

You might also like