SlideShare a Scribd company logo
1 of 9
Integrity Constraints
 Domain Constraints
 Entity Integrity
 Referential Integrity
Domain Constraints
 Integrity constraints guard against accidental damage to the
database, by ensuring that authorized changes to the database do
not result in a loss of data consistency.
 Domain constraints are the most elementary form of integrity
constraint.
 They test values inserted in the database, and test queries to
ensure that the comparisons make sense.
 New domains can be created from existing data types
 E.g. create domain Dollars numeric(12, 2)
create domain Pounds numeric(12,2)
 We cannot assign or compare a value of type Dollars to a value of
type Pounds.
 However, we can convert type as below
(cast r.A as Pounds)
(Should also multiply by the dollar-to-pound conversion-rate)
Domain Constraints (Cont.)
 The check clause in SQL-92 permits domains to be restricted:
 Use check clause to ensure that an hourly-wage domain allows only values
greater than a specified value.
create domain hourly-wage numeric(5,2)
constraint value-test check(value > = 4.00)
 The domain has a constraint that ensures that the hourly-wage is greater
than 4.00
 The clause constraint value-test is optional; useful to indicate which
constraint an update violated.
 Can have complex conditions in domain check
 create domain AccountType char(10)
constraint account-type-test
check (value in (‘Checking’, ‘Saving’))
 check (branch-name in (select branch-name from branch))
 create domain AccountNumber char(10)
constraint Account-Number-null-test check (value not null)
Referential Integrity
 Ensures that a value that appears in one relation for a given set of
attributes also appears for a certain set of attributes in another
relation.
 Example: If “Perryridge” is a branch name appearing in one of the
tuples in the account relation, then there exists a tuple in the branch
relation for branch “Perryridge”.
 Formal Definition
 Let r1(R1) and r2(R2) be relations with primary keys K1 and K2
respectively.
 The subset  of R2 is a foreign key referencing K1 in relation r1, if for
every t2 in r2 there must be a tuple t1 in r1 such that t1[K1] = t2[].
Referential Integrity in the E-R Model
 Consider relationship set R between entity sets E1 and E2. The
relational schema for R includes the primary keys K1 of E1 and
K2 of E2.
Then K1 and K2 form foreign keys on the relational schemas for
E1 and E2 respectively.
 Weak entity sets are also a source of referential integrity
constraints.
 For the relation schema for a weak entity set must include the
primary key attributes of the entity set on which it depends
R
E1 E2
Referential Integrity in SQL
 Primary and candidate keys and foreign keys can be specified as part of
the SQL create table statement:
 The primary key clause lists attributes that comprise the primary key.
 The unique key clause lists attributes that comprise a candidate key.
 The foreign key clause lists the attributes that comprise the foreign key and
the name of the relation referenced by the foreign key.
 By default, a foreign key references the primary key attributes of the
referenced table
foreign key (account-number) references account
 Short form for specifying a single column as foreign key
account-number char (10) references account
 Reference columns in the referenced table can be explicitly specified
 but must be declared as primary/candidate keys
foreign key (account-number) references account(account-number)
Referential Integrity in SQL – Example
create table customer
(customer-name char(20),
customer-street char(30),
customer-city char(30),
primary key (customer-name))
create table branch
(branch-name char(15),
branch-city char(30),
assets integer,
primary key (branch-name))
Referential Integrity in SQL – Example (Cont.)
create table account
(account-number char(10),
branch-name char(15),
balance integer,
primary key (account-number),
foreign key (branch-name) references branch)
create table depositor
(customer-name char(20),
account-number char(10),
primary key (customer-name, account-number),
foreign key (account-number) references account,
foreign key (customer-name) references customer)
Cascading Actions in SQL
create table account
. . .
foreign key(branch-name) references branch
on delete cascade
on update cascade
. . . )
 Due to the on delete cascade clauses, if a delete of a tuple in
branch results in referential-integrity constraint violation, the
delete “cascades” to the account relation, deleting the tuple that
refers to the branch that was deleted.
 Cascading updates are similar.

More Related Content

Similar to Integrity Constraints in Database Management System.ppt

relational model.pptx
relational model.pptxrelational model.pptx
relational model.pptxThangamaniR3
 
relational model in Database Management.ppt.ppt
relational model in Database Management.ppt.pptrelational model in Database Management.ppt.ppt
relational model in Database Management.ppt.pptRoshni814224
 
Relational Model on Database management PPT
Relational Model on Database management PPTRelational Model on Database management PPT
Relational Model on Database management PPTssuser3e0f731
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data ModelSmriti Jain
 
Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Ankit Dubey
 
Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Ankit Dubey
 
Module 2 - part i
Module   2 - part iModule   2 - part i
Module 2 - part iParthNavale
 
Relational model
Relational modelRelational model
Relational modelRUpaliLohar
 
Relational model
Relational modelRelational model
Relational modelRUpaliLohar
 
Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013Prosanta Ghosh
 
Upload a presentation to download APPLICATIChapter-02 -
Upload a presentation to download APPLICATIChapter-02 -Upload a presentation to download APPLICATIChapter-02 -
Upload a presentation to download APPLICATIChapter-02 -sualihmehammednur
 

Similar to Integrity Constraints in Database Management System.ppt (20)

relational model.pptx
relational model.pptxrelational model.pptx
relational model.pptx
 
Relational model
Relational modelRelational model
Relational model
 
Unit04 dbms
Unit04 dbmsUnit04 dbms
Unit04 dbms
 
Sql
SqlSql
Sql
 
relational model in Database Management.ppt.ppt
relational model in Database Management.ppt.pptrelational model in Database Management.ppt.ppt
relational model in Database Management.ppt.ppt
 
Relational Model on Database management PPT
Relational Model on Database management PPTRelational Model on Database management PPT
Relational Model on Database management PPT
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data Model
 
Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01
 
Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01
 
Integrity & security
Integrity & securityIntegrity & security
Integrity & security
 
Module 2 - part i
Module   2 - part iModule   2 - part i
Module 2 - part i
 
Relational model
Relational modelRelational model
Relational model
 
Relational model
Relational modelRelational model
Relational model
 
Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013
 
Upload a presentation to download APPLICATIChapter-02 -
Upload a presentation to download APPLICATIChapter-02 -Upload a presentation to download APPLICATIChapter-02 -
Upload a presentation to download APPLICATIChapter-02 -
 
3.ppt
3.ppt3.ppt
3.ppt
 
uniT 4 (1).pptx
uniT 4 (1).pptxuniT 4 (1).pptx
uniT 4 (1).pptx
 
Ankit
AnkitAnkit
Ankit
 
04.SQL.ppt
04.SQL.ppt04.SQL.ppt
04.SQL.ppt
 
Assignment#01
Assignment#01Assignment#01
Assignment#01
 

More from Roshni814224

Business Information Systems in firms.pptx
Business Information Systems in firms.pptxBusiness Information Systems in firms.pptx
Business Information Systems in firms.pptxRoshni814224
 
Strategic Information System in Business Firm.ppt
Strategic Information System in Business Firm.pptStrategic Information System in Business Firm.ppt
Strategic Information System in Business Firm.pptRoshni814224
 
Management Information System Applications.pptx
Management Information System Applications.pptxManagement Information System Applications.pptx
Management Information System Applications.pptxRoshni814224
 
The Concepts of Internet and Networking.pptx
The Concepts of Internet and Networking.pptxThe Concepts of Internet and Networking.pptx
The Concepts of Internet and Networking.pptxRoshni814224
 
Data models in Database Management Systems.ppt
Data models in Database Management Systems.pptData models in Database Management Systems.ppt
Data models in Database Management Systems.pptRoshni814224
 
Transaction Management, Recovery and Query Processing.pptx
Transaction Management, Recovery and Query Processing.pptxTransaction Management, Recovery and Query Processing.pptx
Transaction Management, Recovery and Query Processing.pptxRoshni814224
 
Computer System Software Component Details.pptx
Computer System Software Component Details.pptxComputer System Software Component Details.pptx
Computer System Software Component Details.pptxRoshni814224
 
Social Engineering and Identity Theft.pptx
Social Engineering and Identity Theft.pptxSocial Engineering and Identity Theft.pptx
Social Engineering and Identity Theft.pptxRoshni814224
 
Cyber Security and Data Privacy in Information Systems.pptx
Cyber Security and Data Privacy in Information Systems.pptxCyber Security and Data Privacy in Information Systems.pptx
Cyber Security and Data Privacy in Information Systems.pptxRoshni814224
 
Information Systems, Organizations and Strategy.pptx
Information Systems, Organizations and Strategy.pptxInformation Systems, Organizations and Strategy.pptx
Information Systems, Organizations and Strategy.pptxRoshni814224
 
Information Systems in Global Business Today.pptx
Information Systems in Global Business Today.pptxInformation Systems in Global Business Today.pptx
Information Systems in Global Business Today.pptxRoshni814224
 
Applications of Management Information System.pptx
Applications of Management Information System.pptxApplications of Management Information System.pptx
Applications of Management Information System.pptxRoshni814224
 
Securing Management Information Systems.ppt
Securing Management Information Systems.pptSecuring Management Information Systems.ppt
Securing Management Information Systems.pptRoshni814224
 
Database Management System Security.pptx
Database Management System  Security.pptxDatabase Management System  Security.pptx
Database Management System Security.pptxRoshni814224
 
Normalization in Database Management System.pptx
Normalization in Database Management System.pptxNormalization in Database Management System.pptx
Normalization in Database Management System.pptxRoshni814224
 
Introduction to Database Management System.ppt
Introduction to Database Management System.pptIntroduction to Database Management System.ppt
Introduction to Database Management System.pptRoshni814224
 
Computer system Hardware components.pptx
Computer system Hardware components.pptxComputer system Hardware components.pptx
Computer system Hardware components.pptxRoshni814224
 
Foundation of Business Intelligence for Business Firms .ppt
Foundation of Business Intelligence for Business Firms .pptFoundation of Business Intelligence for Business Firms .ppt
Foundation of Business Intelligence for Business Firms .pptRoshni814224
 
Global e-Business and Decision Support System.pptx
Global e-Business and Decision Support System.pptxGlobal e-Business and Decision Support System.pptx
Global e-Business and Decision Support System.pptxRoshni814224
 
Strategic Information Systems in Business Today.ppt
Strategic Information Systems in Business Today.pptStrategic Information Systems in Business Today.ppt
Strategic Information Systems in Business Today.pptRoshni814224
 

More from Roshni814224 (20)

Business Information Systems in firms.pptx
Business Information Systems in firms.pptxBusiness Information Systems in firms.pptx
Business Information Systems in firms.pptx
 
Strategic Information System in Business Firm.ppt
Strategic Information System in Business Firm.pptStrategic Information System in Business Firm.ppt
Strategic Information System in Business Firm.ppt
 
Management Information System Applications.pptx
Management Information System Applications.pptxManagement Information System Applications.pptx
Management Information System Applications.pptx
 
The Concepts of Internet and Networking.pptx
The Concepts of Internet and Networking.pptxThe Concepts of Internet and Networking.pptx
The Concepts of Internet and Networking.pptx
 
Data models in Database Management Systems.ppt
Data models in Database Management Systems.pptData models in Database Management Systems.ppt
Data models in Database Management Systems.ppt
 
Transaction Management, Recovery and Query Processing.pptx
Transaction Management, Recovery and Query Processing.pptxTransaction Management, Recovery and Query Processing.pptx
Transaction Management, Recovery and Query Processing.pptx
 
Computer System Software Component Details.pptx
Computer System Software Component Details.pptxComputer System Software Component Details.pptx
Computer System Software Component Details.pptx
 
Social Engineering and Identity Theft.pptx
Social Engineering and Identity Theft.pptxSocial Engineering and Identity Theft.pptx
Social Engineering and Identity Theft.pptx
 
Cyber Security and Data Privacy in Information Systems.pptx
Cyber Security and Data Privacy in Information Systems.pptxCyber Security and Data Privacy in Information Systems.pptx
Cyber Security and Data Privacy in Information Systems.pptx
 
Information Systems, Organizations and Strategy.pptx
Information Systems, Organizations and Strategy.pptxInformation Systems, Organizations and Strategy.pptx
Information Systems, Organizations and Strategy.pptx
 
Information Systems in Global Business Today.pptx
Information Systems in Global Business Today.pptxInformation Systems in Global Business Today.pptx
Information Systems in Global Business Today.pptx
 
Applications of Management Information System.pptx
Applications of Management Information System.pptxApplications of Management Information System.pptx
Applications of Management Information System.pptx
 
Securing Management Information Systems.ppt
Securing Management Information Systems.pptSecuring Management Information Systems.ppt
Securing Management Information Systems.ppt
 
Database Management System Security.pptx
Database Management System  Security.pptxDatabase Management System  Security.pptx
Database Management System Security.pptx
 
Normalization in Database Management System.pptx
Normalization in Database Management System.pptxNormalization in Database Management System.pptx
Normalization in Database Management System.pptx
 
Introduction to Database Management System.ppt
Introduction to Database Management System.pptIntroduction to Database Management System.ppt
Introduction to Database Management System.ppt
 
Computer system Hardware components.pptx
Computer system Hardware components.pptxComputer system Hardware components.pptx
Computer system Hardware components.pptx
 
Foundation of Business Intelligence for Business Firms .ppt
Foundation of Business Intelligence for Business Firms .pptFoundation of Business Intelligence for Business Firms .ppt
Foundation of Business Intelligence for Business Firms .ppt
 
Global e-Business and Decision Support System.pptx
Global e-Business and Decision Support System.pptxGlobal e-Business and Decision Support System.pptx
Global e-Business and Decision Support System.pptx
 
Strategic Information Systems in Business Today.ppt
Strategic Information Systems in Business Today.pptStrategic Information Systems in Business Today.ppt
Strategic Information Systems in Business Today.ppt
 

Recently uploaded

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
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
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 

Recently uploaded (20)

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
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
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 

Integrity Constraints in Database Management System.ppt

  • 1. Integrity Constraints  Domain Constraints  Entity Integrity  Referential Integrity
  • 2. Domain Constraints  Integrity constraints guard against accidental damage to the database, by ensuring that authorized changes to the database do not result in a loss of data consistency.  Domain constraints are the most elementary form of integrity constraint.  They test values inserted in the database, and test queries to ensure that the comparisons make sense.  New domains can be created from existing data types  E.g. create domain Dollars numeric(12, 2) create domain Pounds numeric(12,2)  We cannot assign or compare a value of type Dollars to a value of type Pounds.  However, we can convert type as below (cast r.A as Pounds) (Should also multiply by the dollar-to-pound conversion-rate)
  • 3. Domain Constraints (Cont.)  The check clause in SQL-92 permits domains to be restricted:  Use check clause to ensure that an hourly-wage domain allows only values greater than a specified value. create domain hourly-wage numeric(5,2) constraint value-test check(value > = 4.00)  The domain has a constraint that ensures that the hourly-wage is greater than 4.00  The clause constraint value-test is optional; useful to indicate which constraint an update violated.  Can have complex conditions in domain check  create domain AccountType char(10) constraint account-type-test check (value in (‘Checking’, ‘Saving’))  check (branch-name in (select branch-name from branch))  create domain AccountNumber char(10) constraint Account-Number-null-test check (value not null)
  • 4. Referential Integrity  Ensures that a value that appears in one relation for a given set of attributes also appears for a certain set of attributes in another relation.  Example: If “Perryridge” is a branch name appearing in one of the tuples in the account relation, then there exists a tuple in the branch relation for branch “Perryridge”.  Formal Definition  Let r1(R1) and r2(R2) be relations with primary keys K1 and K2 respectively.  The subset  of R2 is a foreign key referencing K1 in relation r1, if for every t2 in r2 there must be a tuple t1 in r1 such that t1[K1] = t2[].
  • 5. Referential Integrity in the E-R Model  Consider relationship set R between entity sets E1 and E2. The relational schema for R includes the primary keys K1 of E1 and K2 of E2. Then K1 and K2 form foreign keys on the relational schemas for E1 and E2 respectively.  Weak entity sets are also a source of referential integrity constraints.  For the relation schema for a weak entity set must include the primary key attributes of the entity set on which it depends R E1 E2
  • 6. Referential Integrity in SQL  Primary and candidate keys and foreign keys can be specified as part of the SQL create table statement:  The primary key clause lists attributes that comprise the primary key.  The unique key clause lists attributes that comprise a candidate key.  The foreign key clause lists the attributes that comprise the foreign key and the name of the relation referenced by the foreign key.  By default, a foreign key references the primary key attributes of the referenced table foreign key (account-number) references account  Short form for specifying a single column as foreign key account-number char (10) references account  Reference columns in the referenced table can be explicitly specified  but must be declared as primary/candidate keys foreign key (account-number) references account(account-number)
  • 7. Referential Integrity in SQL – Example create table customer (customer-name char(20), customer-street char(30), customer-city char(30), primary key (customer-name)) create table branch (branch-name char(15), branch-city char(30), assets integer, primary key (branch-name))
  • 8. Referential Integrity in SQL – Example (Cont.) create table account (account-number char(10), branch-name char(15), balance integer, primary key (account-number), foreign key (branch-name) references branch) create table depositor (customer-name char(20), account-number char(10), primary key (customer-name, account-number), foreign key (account-number) references account, foreign key (customer-name) references customer)
  • 9. Cascading Actions in SQL create table account . . . foreign key(branch-name) references branch on delete cascade on update cascade . . . )  Due to the on delete cascade clauses, if a delete of a tuple in branch results in referential-integrity constraint violation, the delete “cascades” to the account relation, deleting the tuple that refers to the branch that was deleted.  Cascading updates are similar.