0% found this document useful (0 votes)
5 views

constraints

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)
5 views

constraints

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/ 33

• Q:What do you mean by database ?

• A database is a collection of interrelated data stored


together to serve multiple application information
• What are the advantages of using database
system over traditional record keeping
system?
• Redundancy can be reduced
• Database facilitate sharing of data
• Integrity ( correctness)can be maintained
through databases
• How integrity (correctness) of a database can be
maintained?
Integrity constraints
• A Constraint is a condition or check applicable to a
column or table which ensures the integrity or validity
of data.
Constraints
• Constraints are of two types:
• Column constraint: Constraints which are
applicable on columns
• Table constraints : Constraints which are
applicable on more than one columns
Constraints in the Table
Constraints are also called Integrity constraints. The
following constraints are commonly used in MySQL.

S.N Constraints Description


1 NOT NULL Ensures that a column cannot have NULL value.
2 DEFAULT Provides a default value for a column, when
nothing is given.
3 UNIQUE Ensures that all values in a column are different.
4 CHECK Ensures that all values in a column satisfy
certain condition.
5 PRIMARY KEY Used to identify a row uniquely.
6 FOREIGN KEY Used to ensure Referential Integrity of the data.
NOT NULL CONSTRAINTS

• NOT NULL: Ensures that a column cannot have NULL


value.

Ex:CREATE TABLE Emp ( ecode integer NOT NULL ,


ename char(20) , gender char(1) NOT NULL ,
grade char(2), gross decimal );

• INSERT INTO EMP VALUES (NULL, 'Khan', 'M','A', 10);


• HERE ECODE CANOT BE NULL
Unique Constraint
• This constraint ensures that no two rows have the same value in the
specified column (s)

• Allows only unique Values in the database table column.

• No two rows have the same value in the specified columns.

ECODE ENAME GENDER GRADE GROSS


1101 Brian M E3 12000
1102 Maithali F M1 18000
1103 Kushagra M M3 24000
1104 Vansh M M3 24000
1105 Samaira F E2 11000
Error
INSERT INTO

1105 Zainab F M1 21000


Unique Constraint

• Ex:CREATE TABLE Emp1 ( ecode integer NOT NULL UNIQUE,


ename char(20) NOT NULL , gender char(1) NOT NULL , grade
char(2), gross decimal );

• Ex. UNIQUE constraint applied on ecode of EMP table ensures


that no rows have the same ecode value .
Primary Key Constraint

• It specifies the primary key for the database table.

• It does not allow repeated values in the column, similar to the UNIQUE
constraint.

• They cannot allow NULL values in the column.


CREATE TABLE employee
( ecode integer NOT NULL PRIMARY KEY,
ename char(20) NOT NULL,
gender char(1) NOT NULL,
grade char(2) ,
gross decimal );
Defining Primary Key
❖Defining Primary Key at Column Level:
mysql> CREATE TABLE Student1
( StCode char(3) NOT NULL PRIMARY KEY,
Stname char(20) NOT NULL,);

❖Defining Primary Key at Table Level:


mysql> CREATE TABLE Student
( StCode char(3) NOT NULL , Constraint
Stname char(20) NOT NULL, is defined
PRIMARY KEY StCode as the last
line
);
A Composite (multi-column) Primary key can be defined as only
a Table level whereas Single-column Primary key can be defined
in both way i.e. Column level or Table level.
Default Constraint

• A default value can be specified for a column using the


DEFAULT clause.

• When a user does not enter a value for the column


(having default value),automatically the defined value is
inserted in the field .
Default Constraint

• DEFAULT = Value :Defines a default value for the column.

CREATE TABLE dept1( ecode integer ,grade char(2) DEFAULT


'E1', gross integer );
Implement Constraints in the Table

Generally constraints are applied at the time of creating table.

mysql> CREATE TABLE Student6


( StCode char(3) NOT NULL PRIMARY KEY,
Stname char(20) NOT NULL,
StAdd varchar(40),
AdmNo char(5) UNIQUE,
Stgender char(1) DEFAULT = ‘M’,
StAge integer CHECK (StAge>=10),
Stream char(1) CHECK Stream IN (‘S’, ‘C’, ‘A’)
);
Implementing Foreign Key Constraints
➢ A Foreign key is non-key column in a table whose value is
derived from Primary key of some other table.
➢ Each time when record is inserted/updated the other table
is referenced. This constraints is also called Referential
Integrity Constraints.
➢ This constraints requires two tables in which Reference
table (having Primary key) called Parent table and table
having Foreign key is called Child table.
EMPLOYEE DEPARTMENT
Primary
EmpID DeptNo key
Name DeptName
Foreign
City Head
Key
Sal Location
DeptNo Parent Table
Child Table
Implementing Foreign Key Cont..
Parent
CREATE TABLE Department table
( DeptNo char(2) NOT NULL PRIMARY KEY, should be
DeptName char(10) NOT NULL, created
Head char(30) ); first.

CREATE TABLE Employee5 Foreign key as


( EmpNo char(3) NOT NULL PRIMARY KEY, table level
Name char(30) NOT NULL, constraints…
City char(20), written after
Sal decimal(8,2), all columns.
DeptNo char(2),
FOREIGN KEY (DeptNo) REFERENCES Department (DeptNo));

Alternatively, it can applied in front of column itself…


without using Foreign Key keyword.

DeptNo char(2) REFERENCES Departmet (DeptNo),


Check Constraint
• This constraint limits values that can be
inserted into a column of a table
• It is a logical construct : Is used if an error
check is needed before new values are saved
into the field.
Check Constraint

CREATE TABLE employee3 (ecode integer , ename char(20)


, gender char(1) , grade char(2), gross decimal
CHECK(gross > 2000) );

• A check constraint can consist of an expression using IN,


BETWEEN, LIKE etc. and multiple conditions using OR, AND
etc..
Example of Column Constraints

• CREATE TABLE employee3 ( ecode integer , ename


char(20), gender char(1) , grade char(2) , gross
decimal CHECK (gross>2000) );
Example of Table Constraints
• CREATE TABLE Product ( ProdNum INTEGER NOT NULL,
Name CHAR(100) NOT NULL, ProdVer DECIMAL(4,2) NOT
NULL, UnitPrice DECIMAL(6,2), PRIMARY KEY(ProdNum),
UNIQUE(Name, ProdVer)); These two are table
constraints.
Recapitulation
• What is the use of constraint?
• What are the different types of constraints?
• What is the use of UNIQUE constraint?
Assignments
Q1.Write an SQL query to create a table ‘TEAMS’ with the following
structure:
TEAMS

Field Type constraint


TeamCode Varchar(5) Primary key

TeamName Varchar(20) Unique

TeamLeader Varchar(20)

NoOfMembers integer

Team_symbol Char(1) Not Null


• Q2. Can a table have multiple primary keys?
• Q3.Explain the concept of referential integrity?

You might also like