Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS122 Using Relational Databases and SQL

Similar presentations


Presentation on theme: "CS122 Using Relational Databases and SQL"— Presentation transcript:

1 CS122 Using Relational Databases and SQL
7/1/2019 CS122 Using Relational Databases and SQL 9. Constraints Daniel Firpo Slides prepared by Behzad Parviz Department of Computer Science California State University, Los Angeles

2 Outline Create and drop primary key constraints
7/1/2019 Outline Create and drop primary key constraints Create and drop unique constraints Create and drop foreign key constraints 9. Constraints CS1222_F2018

3 7/1/2019 Constraints A constraint is any kind of restriction that is placed on the data that is inserted into a table Entity constraints ensure the value in a column meets some criteria compared to other rows in the table Primary Key, Unique Domain constraints ensure the value in a column meets some particular criteria without respect to any other row in the table Default, Check Referential integrity constraints ensure the value in a column matches a value in another column in a different table or (sometimes) the same table Foreign Key 9. Constraints CS1222_F2018

4 Constraint Naming Conventions
7/1/2019 Constraint Naming Conventions Begin the constraints with two letters that indicate the type of constraint (pk for primary key, fk for foreign key, and un for unique) Follow that with the table name If it is a unique constraint, follow the table name with the column being constrained If it is a foreign key constraint, follow the table name with the name of the table it refers to 9. Constraints CS1222_F2018

5 Primary Key Constraint With Alter Table
7/1/2019 Primary Key Constraint With Alter Table Primary keys are unique identifiers for each record in a database table Syntax: ALTER TABLE tablename ADD PRIMARY KEY (fieldname1, fieldname2) Or ALTER TABLE tablename ADD CONSTRAINT ConstraintName PRIMARY KEY (fieldname1, fieldname2) 9. Constraints CS1222_F2018

6 7/1/2019 Example Set the primary key of the tblContracts table to ArtistID and ContractDate ALTER TABLE tblContracts ADD CONSTRAINT pk_tblContracts PRIMARY KEY (ArtistID, ContractDate) 9. Constraints CS1222_F2018

7 Primary Key Constraint With Create Table
7/1/2019 Primary Key Constraint With Create Table Two ways with Create Table First form cannot create multi-field PK First form does not allow for naming constraint First way Create Table tablename ( fieldname1 datatype null | not null Primary Key, fieldname2 datatype null | not null ) 9. Constraints CS1222_F2018

8 Primary Key Constraint With Create Table (Cont.)
7/1/2019 Primary Key Constraint With Create Table (Cont.) Second way Create Table tablename ( Fieldname1 datatype null | not null, Fieldname2 datatype null | not null, Constraint constraintName Primary Key (fieldname1,fieldname2) ) 9. Constraints CS1222_F2018

9 7/1/2019 Example Create a table called tblContracts with the ArtistID from the Artists table and a ContractDate field. Set the primary key to ArtistID and ContractDate. Create Table tblContracts ( ArtistID Integer Not Null, ContractDate DateTime Not Null, Constraint pk_tblcontracts Primary Key (ArtistID,ContractDate) ) 9. Constraints CS1222_F2018

10 Unique Constraint With Alter Table
7/1/2019 Unique Constraint With Alter Table Sometimes we want to make sure a non-primary key column has unique values Sometimes they are called alternate keys because they could be used as the primary key if you wished Syntax Alter Table tablename Add Constraint constraintname Unique (fieldname1, fieldname2) 9. Constraints CS1222_F2018

11 Example Add a constraint to the Titles table so that UPC is unique.
7/1/2019 Example Add a constraint to the Titles table so that UPC is unique. Alter Table Titles Add Constraint un_titles_upc Unique (UPC) Add a constraint to the Titles table so that the combination of ArtistID and Title is unique. Alter Table Titles Add Constraint un_titles_artistid_title Unique (ArtistID, Title) Better to have error than to have bad data 9. Constraints CS1222_F2018

12 Unique Constraint With Create Table
7/1/2019 Unique Constraint With Create Table Two ways with Create Table First form cannot create multi-field constraint First form does not allow for naming constraint Syntax Create Table tablename ( Fieldname1 datatype null | not null Primary key, Fieldname2 datatype null | not null Unique ) 9. Constraints CS1222_F2018

13 Unique Constraint With Create Table (Cont.)
7/1/2019 Unique Constraint With Create Table (Cont.) Second way: syntax Create Table tablename ( Fieldname1 datatype null | not null, Fieldname2 datatype null | not null, Constraint constraintname Unique (fieldname1, fieldname2) ) 9. Constraints CS1222_F2018

14 7/1/2019 Example Create a table called tblX with an integer ID field and an integer anotherfield. Both fields are not null. Make anotherfield unique. Create Table tblX ( ID int Not Null, anotherfield int Not Null Unique ) anotherfield int Not Null, constraint un_tblx_anotherfield Unique(anotherfield) 9. Constraints CS1222_F2018

15 Setting Default Constraints
7/1/2019 Setting Default Constraints A default constraint automatically fills in a value if the user fails to supply a value. They are only used with INSERTs - once a row exists in a table the default plays no part in setting the value If the INSERT supplies a value for the column specified by a default, the INSERT’s value is used instead of the default If the INSERT does not supply a value, the default value will be inserted. 9. Constraints CS1222_F2018

16 Adding a default constraint with Alter table
7/1/2019 Adding a default constraint with Alter table Syntax: ALTER TABLE tablename ALTER fieldname SET DEFAULT value Value must be a constant 9. Constraints CS1222_F2018

17 7/1/2019 Example Make a default entry of 0 for the RespParty field of XrefArtistsMembers ALTER TABLE XrefArtistsMembers ALTER RespParty SET DEFAULT 0 9. Constraints CS1222_F2018

18 Adding a default constraint with Create table
7/1/2019 Adding a default constraint with Create table Syntax: CREATE TABLE tablename( Fieldname1 datatype, Fieldname2 datatype DEFAULT value NULL|NOT NULL 9. Constraints CS1222_F2018

19 7/1/2019 Example Create a table called tblX with an integer ID field and integer another field. Another field should have a default of 0 CREATE TABLE tblX( ID int, Another int DEFAULT 0 NOT NULL ) 9. Constraints CS1222_F2018

20 Foreign Key Constraints
Parent Table “One” Primary Key Foreign Key Foreign Key constraint requires that every CD_ID in Child table matches existing CD_ID in Parent table Child Table “Many”

21 Foreign key constraints
7/1/2019 Foreign key constraints Parent table and child table Parent table: the table whose primary key is involved in the relationship Child table: the table whose foreign key is involved in the table The purpose of foreign key constraints Make sure that all foreign keys in the child table match the primary keys in the parent table Which table to create the foreign key constraint? The child table 9. Constraints CS1222_F2018

22 Foreign Key Constraints With Create Table
7/1/2019 Foreign Key Constraints With Create Table Syntax: Create Table tablename ( Fieldname1 datatype null | not null, Fieldname2 datatype null | not null, Constraint constraintname Foreign Key (child_field1, child_field2) References parent_tablename (parent_field1, parent_field2) ) 9. Constraints CS1222_F2018

23 7/1/2019 Example Create a table called Contracts with the ArtistID from the Artists table and a ContractDate field. Create a foreign key constraint between ArtistID and the ArtistID in the Artists table. Create Table tblContracts ( ArtistID Integer Not Null, ContractDate DateTime Not Null, Constraint fk_tblcontracts_artists Foreign Key (ArtistID) References Artists (ArtistID) ) 9. Constraints CS1222_F2018

24 Foreign Key Implications
7/1/2019 Foreign Key Implications Foreign Key constraint requires that every FK field in child table matches existing PK in Parent table Constraint will not allow "orphan" records Must delete child records before deleting related parent record If updating or deleting PK value in parent table Drop constraint before updating either parent or child Or use Cascade Update or delete 9. Constraints CS1222_F2018

25 Cascading Updates & Deletes
7/1/2019 Cascading Updates & Deletes Parent Table “One” Cascade Update would change all references in Child table when a PK is changed in Parent table Cascade Delete would delete all related records in Child table when a record is deleted in Parent table Avoids orphan records MySQL supports cascade deletes with the inclusion of On Delete Cascade at the end of the constraint clause Child Table “Many” 9. Constraints CS1222_F2018

26 Dropping foreign key constraints
7/1/2019 Dropping foreign key constraints Syntax: Alter Table tablename Drop FOREIGN KEY foreign_key_name Example: Drop the fk_tblcontracts_artsists on the tblContracts table. Alter Table tblContracts Drop FOREIGN KEY fk_tblcontracts_artists 9. Constraints CS1222_F2018

27 Dropping primary key constraints
7/1/2019 Dropping primary key constraints Syntax: Alter Table tablename Drop PRIMARY KEY Example: Drop the primary key of the tblX table. Alter Table tblX Drop PRIMARY KEY 9. Constraints CS1222_F2018


Download ppt "CS122 Using Relational Databases and SQL"

Similar presentations


Ads by Google