SlideShare a Scribd company logo
8/10/13 SQL IntegrityConstraints
beginner-sql-tutorial.com/sql-integrity-constraints.htm 1/5
SQL Integrity Constraints
Integrity Constraints are used to apply business rules for the database tables.
The constraints available in SQL are Foreign Key, Not Null, Unique, Check.
Constraints can be defined in two ways
1) The constraints can be specified immediately after the column definition. This is called column-level
definition.
2) The constraints can be specified after all the columns are defined. This is called table-level definition.
1) SQL Primary key:
This constraint defines a column or combination of columns which uniquely identifies each row in the table.
Syntax to define a Primary key at column level:
column name datatype [CONSTRAINT constraint_name] PRIMARY KEY
Syntax to define a Primary key at table level:
[CONSTRAINT constraint_name] PRIMARY KEY (column_name1,column_name2,..)
column_name1, column_name2 are the names of the columns which define the primary Key.
The syntax within the bracket i.e. [CONSTRAINT constraint_name] is optional.
For Example: To create an employee table with Primary Key constraint, the query would be like.
Primary Key at column level:
CREATE TABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10)
);
or
CREATE TABLE employee
( id number(5) CONSTRAINT emp_id_pk PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10)
);
Primary Key at column level:
8/10/13 SQL IntegrityConstraints
beginner-sql-tutorial.com/sql-integrity-constraints.htm 2/5
CREATE TABLE employee
( id number(5),
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10),
CONSTRAINT emp_id_pk PRIMARY KEY (id)
);
Primary Key at table level:
CREATE TABLE employee
( id number(5), NOT NULL,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10),
ALTER TABLE employee ADD CONSTRAINT PK_EMPLOYEE_ID PRIMARY KEY (id)
);
2) SQL Foreign key or Referential Integrity :
This constraint identifies any column referencing the PRIMARY KEY in another table. It establishes a relationship
between two columns in the same table or between different tables. For a column to be defined as a Foreign
Key, it should be a defined as a Primary Key in the table which it is referring. One or more columns can be
defined as Foreign key.
Syntax to define a Foreign key at column level:
[CONSTRAINT constraint_name] REFERENCES Referenced_Table_name(column_name)
Syntax to define a Foreign key at table level:
[CONSTRAINT constraint_name] FOREIGN KEY(column_name) REFERENCES
referenced_table_name(column_name);
For Example:
1) Lets use the "product" table and "order_items".
Foreign Key at column level:
CREATE TABLE product
( product_id number(5) CONSTRAINT pd_id_pk PRIMARY KEY,
product_name char(20),
supplier_name char(20),
unit_price number(10)
);
8/10/13 SQL IntegrityConstraints
beginner-sql-tutorial.com/sql-integrity-constraints.htm 3/5
CREATE TABLE order_items
( order_id number(5) CONSTRAINT od_id_pk PRIMARY KEY,
product_id number(5) CONSTRAINT pd_id_fk REFERENCES, product(product_id),
product_name char(20),
supplier_name char(20),
unit_price number(10)
);
Foreign Key at table level:
CREATE TABLE order_items
( order_id number(5) ,
product_id number(5),
product_name char(20),
supplier_name char(20),
unit_price number(10)
CONSTRAINT od_id_pk PRIMARY KEY(order_id),
CONSTRAINT pd_id_fk FOREIGN KEY(product_id) REFERENCES product(product_id)
);
2) If the employee table has a 'mgr_id' i.e, manager id as a foreign key which references primary key 'id' within
the same table, the query would be like,
CREATE TABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
mgr_id number(5) REFERENCES employee(id),
salary number(10),
location char(10)
);
3) SQL Not Null Constraint :
This constraint ensures all rows in the table contain a definite value for the column which is specified as not null.
Which means a null value is not allowed.
Syntax to define a Not Null constraint:
[CONSTRAINT constraint name] NOT NULL
For Example: To create a employee table with Null value, the query would be like
CREATE TABLE employee
( id number(5),
name char(20) CONSTRAINT nm_nn NOT NULL,
dept char(10),
age number(2),
salary number(10),
location char(10)
);
8/10/13 SQL IntegrityConstraints
beginner-sql-tutorial.com/sql-integrity-constraints.htm 4/5
4) SQL Unique Key:
This constraint ensures that a column or a group of columns in each row have a distinct value. A column(s) can
have a null value but the values cannot be duplicated.
Syntax to define a Unique key at column level:
[CONSTRAINT constraint_name] UNIQUE
Syntax to define a Unique key at table level:
[CONSTRAINT constraint_name] UNIQUE(column_name)
For Example: To create an employee table with Unique key, the query would be like,
Unique Key at column level:
CREATE TABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10) UNIQUE
);
or
CREATE TABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10) CONSTRAINT loc_un UNIQUE
);
Unique Key at table level:
CREATE TABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10),
CONSTRAINT loc_un UNIQUE(location)
);
5) SQL Check Constraint :
8/10/13 SQL IntegrityConstraints
beginner-sql-tutorial.com/sql-integrity-constraints.htm 5/5
This constraint defines a business rule on a column. All the rows must satisfy this rule. The constraint can be
applied for a single column or a group of columns.
Syntax to define a Check constraint:
[CONSTRAINT constraint_name] CHECK (condition)
For Example: In the employee table to select the gender of a person, the query would be like
Check Constraint at column level:
CREATE TABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
gender char(1) CHECK (gender in ('M','F')),
salary number(10),
location char(10)
);
Check Constraint at table level:
CREATE TABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
gender char(1),
salary number(10),
location char(10),
CONSTRAINT gender_ck CHECK (gender in ('M','F'))
);

More Related Content

What's hot (20)

PPTX
MYSQL join
Ahmed Farag
 
PPTX
SQL Functions
ammarbrohi
 
PPTX
Lab2 ddl commands
Balqees Al.Mubarak
 
PPTX
Operación de sistema operativo monousuario sobre MSDOS/WINDOWS
Nayeli Gonzalez
 
PDF
Sql commands
Prof. Dr. K. Adisesha
 
PPTX
Types of keys in database | SQL
Sumit Pandey
 
PDF
SQL Functions and Operators
Mohan Kumar.R
 
PPT
Constraints In Sql
Anurag
 
PPTX
SQL Data types and Constarints.pptx
jaba kumar
 
PPTX
Key and its different types
Umair Shakir
 
PPTX
Sql joins
Gaurav Dhanwant
 
PPTX
Fundamentals of Database Design
Information Technology
 
PPTX
oracle Sql constraint
home
 
PPTX
Html list
sayed fathey
 
PPT
SQL Queries
Nilt1234
 
PDF
Chapter 4 Structured Query Language
Eddyzulham Mahluzydde
 
PPTX
SQL JOIN
Ritwik Das
 
ODP
CSS Basics
Sanjeev Kumar
 
PPTX
Types of keys dbms
Surkhab Shelly
 
MYSQL join
Ahmed Farag
 
SQL Functions
ammarbrohi
 
Lab2 ddl commands
Balqees Al.Mubarak
 
Operación de sistema operativo monousuario sobre MSDOS/WINDOWS
Nayeli Gonzalez
 
Sql commands
Prof. Dr. K. Adisesha
 
Types of keys in database | SQL
Sumit Pandey
 
SQL Functions and Operators
Mohan Kumar.R
 
Constraints In Sql
Anurag
 
SQL Data types and Constarints.pptx
jaba kumar
 
Key and its different types
Umair Shakir
 
Sql joins
Gaurav Dhanwant
 
Fundamentals of Database Design
Information Technology
 
oracle Sql constraint
home
 
Html list
sayed fathey
 
SQL Queries
Nilt1234
 
Chapter 4 Structured Query Language
Eddyzulham Mahluzydde
 
SQL JOIN
Ritwik Das
 
CSS Basics
Sanjeev Kumar
 
Types of keys dbms
Surkhab Shelly
 

Similar to Sql integrity constraints (20)

PPTX
Sql server ___________session_15(data integrity)
Ehtisham Ali
 
PDF
1.Implementing_Data_Integrity.pdf
diaa46
 
PPTX
data base programming chapter3 47 slides
nights1988
 
PPTX
Entigrity constraint
suman kumar
 
DOCX
Integrity and security
Surendra Karki Chettri
 
PPTX
CONSTRAINTS PPT.pptx
ThangaduraiA4
 
PDF
RDBMS Lab03 applying constraints (UIU)
Muhammad T Q Nafis
 
PPT
Constraints constraints of oracle data base management systems
SHAKIR325211
 
PPTX
apply Integrity constraints on database table
prachi gat
 
PPTX
DBMS: Week 09 - SQL Constraints and Indexing
RashidFaridChishti
 
PPT
Les10
Vijay Kumar
 
PPT
CSE311_IAH_Slide07_SQL Advanced Quries.ppt
noshinnawar31
 
PPTX
Integrity Constraints explain everything in it
MUHAMMADANSAR76
 
PDF
Assignment#07
Sunita Milind Dol
 
PPTX
Constraints in Structure Query Language.
anjanasharma77573
 
PPTX
basic of SQL constraints in database management system
anjanasharma77573
 
PPTX
MS Sql Server: Customizing Your Data Base Design
DataminingTools Inc
 
PPTX
MS SQL SERVER: Customizing Your D Base Design
sqlserver content
 
PPTX
MS SQLSERVER:Customizing Your D Base Design
sqlserver content
 
PPT
Database Keys / Referential Integrity / Propagation Constraint / Entity Integ...
Rushdi Shams
 
Sql server ___________session_15(data integrity)
Ehtisham Ali
 
1.Implementing_Data_Integrity.pdf
diaa46
 
data base programming chapter3 47 slides
nights1988
 
Entigrity constraint
suman kumar
 
Integrity and security
Surendra Karki Chettri
 
CONSTRAINTS PPT.pptx
ThangaduraiA4
 
RDBMS Lab03 applying constraints (UIU)
Muhammad T Q Nafis
 
Constraints constraints of oracle data base management systems
SHAKIR325211
 
apply Integrity constraints on database table
prachi gat
 
DBMS: Week 09 - SQL Constraints and Indexing
RashidFaridChishti
 
CSE311_IAH_Slide07_SQL Advanced Quries.ppt
noshinnawar31
 
Integrity Constraints explain everything in it
MUHAMMADANSAR76
 
Assignment#07
Sunita Milind Dol
 
Constraints in Structure Query Language.
anjanasharma77573
 
basic of SQL constraints in database management system
anjanasharma77573
 
MS Sql Server: Customizing Your Data Base Design
DataminingTools Inc
 
MS SQL SERVER: Customizing Your D Base Design
sqlserver content
 
MS SQLSERVER:Customizing Your D Base Design
sqlserver content
 
Database Keys / Referential Integrity / Propagation Constraint / Entity Integ...
Rushdi Shams
 
Ad

More from Vivek Singh (20)

PPS
C programming session 14
Vivek Singh
 
PPS
C programming session 13
Vivek Singh
 
PPS
C programming session 11
Vivek Singh
 
PPS
C programming session 10
Vivek Singh
 
PPS
C programming session 08
Vivek Singh
 
PPS
C programming session 07
Vivek Singh
 
PPS
C programming session 05
Vivek Singh
 
PPS
C programming session 04
Vivek Singh
 
PPS
C programming session 02
Vivek Singh
 
PPS
C programming session 01
Vivek Singh
 
PPS
C programming session 16
Vivek Singh
 
PDF
Niit aptitude question paper
Vivek Singh
 
PDF
Excel shortcut and tips
Vivek Singh
 
PDF
Sql where clause
Vivek Singh
 
PDF
Sql update statement
Vivek Singh
 
PDF
Sql tutorial, tutorials sql
Vivek Singh
 
PDF
Sql subquery
Vivek Singh
 
PDF
Sql select statement
Vivek Singh
 
PDF
Sql rename
Vivek Singh
 
PDF
Sql query tuning or query optimization
Vivek Singh
 
C programming session 14
Vivek Singh
 
C programming session 13
Vivek Singh
 
C programming session 11
Vivek Singh
 
C programming session 10
Vivek Singh
 
C programming session 08
Vivek Singh
 
C programming session 07
Vivek Singh
 
C programming session 05
Vivek Singh
 
C programming session 04
Vivek Singh
 
C programming session 02
Vivek Singh
 
C programming session 01
Vivek Singh
 
C programming session 16
Vivek Singh
 
Niit aptitude question paper
Vivek Singh
 
Excel shortcut and tips
Vivek Singh
 
Sql where clause
Vivek Singh
 
Sql update statement
Vivek Singh
 
Sql tutorial, tutorials sql
Vivek Singh
 
Sql subquery
Vivek Singh
 
Sql select statement
Vivek Singh
 
Sql rename
Vivek Singh
 
Sql query tuning or query optimization
Vivek Singh
 
Ad

Recently uploaded (20)

PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
community health nursing question paper 2.pdf
Prince kumar
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 

Sql integrity constraints

  • 1. 8/10/13 SQL IntegrityConstraints beginner-sql-tutorial.com/sql-integrity-constraints.htm 1/5 SQL Integrity Constraints Integrity Constraints are used to apply business rules for the database tables. The constraints available in SQL are Foreign Key, Not Null, Unique, Check. Constraints can be defined in two ways 1) The constraints can be specified immediately after the column definition. This is called column-level definition. 2) The constraints can be specified after all the columns are defined. This is called table-level definition. 1) SQL Primary key: This constraint defines a column or combination of columns which uniquely identifies each row in the table. Syntax to define a Primary key at column level: column name datatype [CONSTRAINT constraint_name] PRIMARY KEY Syntax to define a Primary key at table level: [CONSTRAINT constraint_name] PRIMARY KEY (column_name1,column_name2,..) column_name1, column_name2 are the names of the columns which define the primary Key. The syntax within the bracket i.e. [CONSTRAINT constraint_name] is optional. For Example: To create an employee table with Primary Key constraint, the query would be like. Primary Key at column level: CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) ); or CREATE TABLE employee ( id number(5) CONSTRAINT emp_id_pk PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) ); Primary Key at column level:
  • 2. 8/10/13 SQL IntegrityConstraints beginner-sql-tutorial.com/sql-integrity-constraints.htm 2/5 CREATE TABLE employee ( id number(5), name char(20), dept char(10), age number(2), salary number(10), location char(10), CONSTRAINT emp_id_pk PRIMARY KEY (id) ); Primary Key at table level: CREATE TABLE employee ( id number(5), NOT NULL, name char(20), dept char(10), age number(2), salary number(10), location char(10), ALTER TABLE employee ADD CONSTRAINT PK_EMPLOYEE_ID PRIMARY KEY (id) ); 2) SQL Foreign key or Referential Integrity : This constraint identifies any column referencing the PRIMARY KEY in another table. It establishes a relationship between two columns in the same table or between different tables. For a column to be defined as a Foreign Key, it should be a defined as a Primary Key in the table which it is referring. One or more columns can be defined as Foreign key. Syntax to define a Foreign key at column level: [CONSTRAINT constraint_name] REFERENCES Referenced_Table_name(column_name) Syntax to define a Foreign key at table level: [CONSTRAINT constraint_name] FOREIGN KEY(column_name) REFERENCES referenced_table_name(column_name); For Example: 1) Lets use the "product" table and "order_items". Foreign Key at column level: CREATE TABLE product ( product_id number(5) CONSTRAINT pd_id_pk PRIMARY KEY, product_name char(20), supplier_name char(20), unit_price number(10) );
  • 3. 8/10/13 SQL IntegrityConstraints beginner-sql-tutorial.com/sql-integrity-constraints.htm 3/5 CREATE TABLE order_items ( order_id number(5) CONSTRAINT od_id_pk PRIMARY KEY, product_id number(5) CONSTRAINT pd_id_fk REFERENCES, product(product_id), product_name char(20), supplier_name char(20), unit_price number(10) ); Foreign Key at table level: CREATE TABLE order_items ( order_id number(5) , product_id number(5), product_name char(20), supplier_name char(20), unit_price number(10) CONSTRAINT od_id_pk PRIMARY KEY(order_id), CONSTRAINT pd_id_fk FOREIGN KEY(product_id) REFERENCES product(product_id) ); 2) If the employee table has a 'mgr_id' i.e, manager id as a foreign key which references primary key 'id' within the same table, the query would be like, CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), mgr_id number(5) REFERENCES employee(id), salary number(10), location char(10) ); 3) SQL Not Null Constraint : This constraint ensures all rows in the table contain a definite value for the column which is specified as not null. Which means a null value is not allowed. Syntax to define a Not Null constraint: [CONSTRAINT constraint name] NOT NULL For Example: To create a employee table with Null value, the query would be like CREATE TABLE employee ( id number(5), name char(20) CONSTRAINT nm_nn NOT NULL, dept char(10), age number(2), salary number(10), location char(10) );
  • 4. 8/10/13 SQL IntegrityConstraints beginner-sql-tutorial.com/sql-integrity-constraints.htm 4/5 4) SQL Unique Key: This constraint ensures that a column or a group of columns in each row have a distinct value. A column(s) can have a null value but the values cannot be duplicated. Syntax to define a Unique key at column level: [CONSTRAINT constraint_name] UNIQUE Syntax to define a Unique key at table level: [CONSTRAINT constraint_name] UNIQUE(column_name) For Example: To create an employee table with Unique key, the query would be like, Unique Key at column level: CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) UNIQUE ); or CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) CONSTRAINT loc_un UNIQUE ); Unique Key at table level: CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10), CONSTRAINT loc_un UNIQUE(location) ); 5) SQL Check Constraint :
  • 5. 8/10/13 SQL IntegrityConstraints beginner-sql-tutorial.com/sql-integrity-constraints.htm 5/5 This constraint defines a business rule on a column. All the rows must satisfy this rule. The constraint can be applied for a single column or a group of columns. Syntax to define a Check constraint: [CONSTRAINT constraint_name] CHECK (condition) For Example: In the employee table to select the gender of a person, the query would be like Check Constraint at column level: CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), gender char(1) CHECK (gender in ('M','F')), salary number(10), location char(10) ); Check Constraint at table level: CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), gender char(1), salary number(10), location char(10), CONSTRAINT gender_ck CHECK (gender in ('M','F')) );