SlideShare a Scribd company logo
1 of 5
Download to read offline
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

Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptxSherinRappai
ย 
Sql operator
Sql operatorSql operator
Sql operatorPooja Dixit
ย 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In SqlAnurag
ย 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueBalwant Gorad
ย 
Sql commands
Sql commandsSql commands
Sql commandsPooja Dixit
ย 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)Sabana Maharjan
ย 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structuresDurgaDeviCbit
ย 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sqlVARSHAKUMARI49
ย 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Salman Memon
ย 
Codd's Rules for Relational Database Management Systems
Codd's Rules for Relational Database Management SystemsCodd's Rules for Relational Database Management Systems
Codd's Rules for Relational Database Management SystemsRajeev Srivastava
ย 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERSLakshman Basnet
ย 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3Dr. C.V. Suresh Babu
ย 
Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQLEsmita Gupta
ย 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteAl-Mamun Sarkar
ย 
SQL select clause
SQL select clauseSQL select clause
SQL select clausearpit bhadoriya
ย 
Aggregate function
Aggregate functionAggregate function
Aggregate functionRayhan Chowdhury
ย 

What's hot (20)

Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
ย 
1.2 sql create and drop table
1.2 sql create and drop table1.2 sql create and drop table
1.2 sql create and drop table
ย 
Sql operator
Sql operatorSql operator
Sql operator
ย 
SQL Views
SQL ViewsSQL Views
SQL Views
ย 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
ย 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
ย 
Sql commands
Sql commandsSql commands
Sql commands
ย 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
ย 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)
ย 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
ย 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
ย 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
ย 
Codd's Rules for Relational Database Management Systems
Codd's Rules for Relational Database Management SystemsCodd's Rules for Relational Database Management Systems
Codd's Rules for Relational Database Management Systems
ย 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
ย 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3
ย 
Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQL
ย 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
ย 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
ย 
Aggregate function
Aggregate functionAggregate function
Aggregate function
ย 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
ย 

Similar to Sql integrity constraints

Entigrity constraint
Entigrity constraintEntigrity constraint
Entigrity constraintsuman kumar
ย 
Bn1017 a demo rdbms
Bn1017 a demo  rdbmsBn1017 a demo  rdbms
Bn1017 a demo rdbmsconline training
ย 
Dms 22319 micro project
Dms 22319 micro projectDms 22319 micro project
Dms 22319 micro projectARVIND SARDAR
ย 
Create table
Create tableCreate table
Create tableNitesh Singh
ย 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)Ehtisham Ali
ย 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...SakkaravarthiS1
ย 
RDBMS Lab03 applying constraints (UIU)
RDBMS Lab03 applying constraints (UIU)RDBMS Lab03 applying constraints (UIU)
RDBMS Lab03 applying constraints (UIU)Muhammad T Q Nafis
ย 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating databaseMukesh Tekwani
ย 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commandsBelle Wx
ย 
Constraints constraints of oracle data base management systems
Constraints  constraints of oracle data base management systemsConstraints  constraints of oracle data base management systems
Constraints constraints of oracle data base management systemsSHAKIR325211
ย 
Bn1037 demo oracle sql
Bn1037 demo  oracle sqlBn1037 demo  oracle sql
Bn1037 demo oracle sqlconline training
ย 
PHP mysql Sql
PHP mysql  SqlPHP mysql  Sql
PHP mysql SqlMudasir Syed
ย 

Similar to Sql integrity constraints (20)

Entigrity constraint
Entigrity constraintEntigrity constraint
Entigrity constraint
ย 
Les10
Les10Les10
Les10
ย 
Bn1017 a demo rdbms
Bn1017 a demo  rdbmsBn1017 a demo  rdbms
Bn1017 a demo rdbms
ย 
Dms 22319 micro project
Dms 22319 micro projectDms 22319 micro project
Dms 22319 micro project
ย 
Create table
Create tableCreate table
Create table
ย 
Les11 Including Constraints
Les11 Including ConstraintsLes11 Including Constraints
Les11 Including Constraints
ย 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)
ย 
Integrity and security
Integrity and securityIntegrity and security
Integrity and security
ย 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
ย 
RDBMS Lab03 applying constraints (UIU)
RDBMS Lab03 applying constraints (UIU)RDBMS Lab03 applying constraints (UIU)
RDBMS Lab03 applying constraints (UIU)
ย 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
ย 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
ย 
Constraints constraints of oracle data base management systems
Constraints  constraints of oracle data base management systemsConstraints  constraints of oracle data base management systems
Constraints constraints of oracle data base management systems
ย 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
ย 
SQL
SQLSQL
SQL
ย 
SQL
SQLSQL
SQL
ย 
Sql
SqlSql
Sql
ย 
Bn1037 demo oracle sql
Bn1037 demo  oracle sqlBn1037 demo  oracle sql
Bn1037 demo oracle sql
ย 
PHP mysql Sql
PHP mysql  SqlPHP mysql  Sql
PHP mysql Sql
ย 
Les11
Les11Les11
Les11
ย 

More from Vivek Singh

C programming session 14
C programming session 14C programming session 14
C programming session 14Vivek Singh
ย 
C programming session 13
C programming session 13C programming session 13
C programming session 13Vivek Singh
ย 
C programming session 11
C programming session 11C programming session 11
C programming session 11Vivek Singh
ย 
C programming session 10
C programming session 10C programming session 10
C programming session 10Vivek Singh
ย 
C programming session 08
C programming session 08C programming session 08
C programming session 08Vivek Singh
ย 
C programming session 07
C programming session 07C programming session 07
C programming session 07Vivek Singh
ย 
C programming session 05
C programming session 05C programming session 05
C programming session 05Vivek Singh
ย 
C programming session 04
C programming session 04C programming session 04
C programming session 04Vivek Singh
ย 
C programming session 02
C programming session 02C programming session 02
C programming session 02Vivek Singh
ย 
C programming session 01
C programming session 01C programming session 01
C programming session 01Vivek Singh
ย 
C programming session 16
C programming session 16C programming session 16
C programming session 16Vivek Singh
ย 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paperVivek Singh
ย 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tipsVivek Singh
ย 
Sql where clause
Sql where clauseSql where clause
Sql where clauseVivek Singh
ย 
Sql update statement
Sql update statementSql update statement
Sql update statementVivek Singh
ย 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sqlVivek Singh
ย 
Sql subquery
Sql subquerySql subquery
Sql subqueryVivek Singh
ย 
Sql select statement
Sql select statementSql select statement
Sql select statementVivek Singh
ย 
Sql rename
Sql renameSql rename
Sql renameVivek Singh
ย 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimizationVivek Singh
ย 

More from Vivek Singh (20)

C programming session 14
C programming session 14C programming session 14
C programming session 14
ย 
C programming session 13
C programming session 13C programming session 13
C programming session 13
ย 
C programming session 11
C programming session 11C programming session 11
C programming session 11
ย 
C programming session 10
C programming session 10C programming session 10
C programming session 10
ย 
C programming session 08
C programming session 08C programming session 08
C programming session 08
ย 
C programming session 07
C programming session 07C programming session 07
C programming session 07
ย 
C programming session 05
C programming session 05C programming session 05
C programming session 05
ย 
C programming session 04
C programming session 04C programming session 04
C programming session 04
ย 
C programming session 02
C programming session 02C programming session 02
C programming session 02
ย 
C programming session 01
C programming session 01C programming session 01
C programming session 01
ย 
C programming session 16
C programming session 16C programming session 16
C programming session 16
ย 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paper
ย 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tips
ย 
Sql where clause
Sql where clauseSql where clause
Sql where clause
ย 
Sql update statement
Sql update statementSql update statement
Sql update statement
ย 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sql
ย 
Sql subquery
Sql subquerySql subquery
Sql subquery
ย 
Sql select statement
Sql select statementSql select statement
Sql select statement
ย 
Sql rename
Sql renameSql rename
Sql rename
ย 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimization
ย 

Recently uploaded

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
ย 
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
ย 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
ย 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
ย 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)โ€”โ€”โ€”โ€”IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)โ€”โ€”โ€”โ€”IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)โ€”โ€”โ€”โ€”IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)โ€”โ€”โ€”โ€”IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
ย 
โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
ย 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
ย 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
ย 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
ย 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
ย 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
ย 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
ย 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
ย 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
ย 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
ย 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
ย 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
ย 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
ย 

Recently uploaded (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
ย 
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Kamla Market (DELHI) ๐Ÿ” >เผ’9953330565๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
ย 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
ย 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
ย 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
ย 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
ย 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)โ€”โ€”โ€”โ€”IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)โ€”โ€”โ€”โ€”IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)โ€”โ€”โ€”โ€”IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)โ€”โ€”โ€”โ€”IMP.OF KSHARA ...
ย 
โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
โ€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
ย 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
ย 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
ย 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ย 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
ย 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
ย 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
ย 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
ย 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
ย 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
ย 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
ย 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
ย 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
ย 

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