CS122 Using Relational Databases and SQL

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

Maintenance Modifying the data –Add records –Delete records –Update records Modifying the design –Add fields into tables –Remove fields from a table –Change.
Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Using Relational Databases and SQL
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 9: Data Definition Language.
RELATIONSHIP  THE WAY TABLES ARE RELATED  A TABLE MUST PARTICIPATE IN AT LEAST ONE RELATIONSHIP  IN A BINARY RELATIONSHIP TWO ENTITIES PARTICIPATE 
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Data Definition Language.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Oracle Data Definition Language (DDL)
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
M1G Introduction to Database Development 2. Creating a Database.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Introduction to Database System Adisak Intana Lecturer Chapter 7 : Data Integrity.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Chapter 7 SQL: Data Definition Pearson Education © 2009.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Database Constraints ICT 011. Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
Fundamental of Database Systems
Tables & Relationships
Chapter 5 Database Design
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
 2012 Pearson Education, Inc. All rights reserved.
Constraints and Triggers
SQL Creating and Managing Tables
SQL – More Table Constraints
CS122 Using Relational Databases and SQL
Minggu 5, Pertemuan 9 SQL: Data Definition
Module 5: Implementing Data Integrity by Using Constraints
COS 346 Day 8.
SQL Creating and Managing Tables
Lecturer: Mukhtar Mohamed Ali “Hakaale”
SQL Creating and Managing Tables
CS4222 Principles of Database System
CS122 Using Relational Databases and SQL
Oracle Data Definition Language (DDL)
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
Data Definition Language
Data Definition Language
Database Design: Relational Model
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Instructor: Samia arshad
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Including Constraints
CS1222 Using Relational Databases and SQL
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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”

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

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

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

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

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

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

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