SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.

Slides:



Advertisements
Similar presentations
1 Constraints, Triggers and Active Databases Chapter 9.
Advertisements

CSE 544 Constraints Lecture #3 Friday, January 13, 2011 Dan Suciu , Winter
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Student(sid, name, addr, age, GPA) Class(dept, cnum, sec, unit, title, instructor) Enroll(sid, dept, cnum, sec) siddeptcnumsec 301CS CS AT00000.
Fall 2001Arthur Keller – CS 1809–1 Schedule Today Oct. 23 (T) Constraints. u Read Sections Assignment 4 due. Project Part 3 due Oct. 24 (W). Oct.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 7 Structured Query Language.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
SQL: Constraints and Triggers Chapter 6 Ullman and Widom Certain properties we’d like our database to hold Modification of the database may break these.
Database Systems Lecture 5 Natasha Alechina
SQL Constraints & Triggers May 10 th, Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
1 CSE 480: Database Systems Lecture 5: Relational Data Model.
DBMS Spring 2014 Database Integrity Sources: Security in Computing, Pfleeger and Pfleeger, Prentice Hall, 2003 Lecture Slides, CSE6243, MSU, Rayford B.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Constraints, Triggers and Index James Wang.
1 Lecture 6: Views Friday, January 17th, Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,
Database Management COP4540, SCS, FIU Constraints and security in SQL (Ch. 8.6, Ch22.2)
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
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.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
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.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
1 Chapter 6 Constraints uForeign Keys uConstraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
Deductive Databases General idea: some relations are stored (extensional), others are defined by datalog queries (intensional). Many research projects.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Session 1 Module 1: Introduction to Data Integrity
Jennifer Widom Relational Databases The Relational Model.
Lecture 03 Constraints. Example Schema CONSTRAINTS.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
1 Introduction to Database Systems, CS420 SQL Constraints.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
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.
Fundamental of Database Systems
CS 3630 Database Design and Implementation
Constraints and Triggers
SQL: Constraints and Triggers
Referential Integrity
CPSC-310 Database Systems
The Relational Model Relational Data Model
Referential Integrity
SQL – Constraints & Triggers
CPSC-608 Database Systems
-Transactions in SQL -Constraints and Triggers
Presentation transcript:

SQL Keys and Constraints Justin Maksim

Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY KEY keyword or the UNIQUE keyword. Can have only one PRIMARY KEY can have many attributes declared UNIQUE

Methods for Key Declaration Method 1: append the keywords PRIMARY KEY after the attribute i.e. CREATE TABLE Student ( studentID VARCHAR(8) PRIMARY KEY, name CHAR(30) );

Method 2: Add element PRIMARY KEY with a parenthesized list of the attribute(s) forming the primary key i.e. CREATE TABLE Student( studentID VARCHAR(8), name CHAR(30), PRIMARY KEY (studentID) ); Can have multiple attributes using this method PRIMARY KEY (studentID, name)

Method 3: Use the keyword UNIQUE. Can appear exactly where PRIMARY KEY can appear. i.e. CREATE TABLE Student( studentID VARCHAR(8), name CHAR(30), UNIQUE (studentID) ); OR CREATE TABLE Student( studentID VARCHAR(8) UNIQUE, name CHAR(30) ); OR UNIQUE ( studentID, name)

Enforcing Key Constraints SQL system will check key constraint only when an insertion or update to the relation occurs An index on the attribute(s) declared to be keys is important to enforce key constraint efficiently.

Referential Integrity and Foreign Keys Foreign Key  Referenced attribute(s) of second relation must be declared primary key for their relation  Any value appearing in an attribute of a foreign key must appear in the corresponding attribute of the second relation

Declaring Foreign Key Method 1: CREATE TABLE Student( studentID varchar(8), name char(30), record# INT REFERENCES Record(record#) ); Method 2: CREATE TABLE Student( studentID varchar(8), name char(30), record# INT, FOREIGN KEY record# REFERENCES Record(record#) );

Maintaining Referential Integrity Default Policy:  Reject changes Cascade Policy:  Update changes Set-Null Policy CREATE TABLE Student( studentID varchar(8), name char(30), record# INT REFERENCES Record(record#) ON DELETE SET NULL ON UPDATE CASCADE );

Constraints On the values of Attributes Limits the values for attributes Can be expressed:  A constraint on the attribute  A constraint on the domain

Not-Null Constraints record# INT REFERENCES Record(record#) NOT NULL  Can’t update to null  Can’t insert null value  Can’t use the set-null policy

Attribute-Based CHECK Constraints After defining attribute, can include the CHECK constraint i.e. gender CHAR(1) CHECK (gender IN (‘F’,’M’)),

Domain Constraints Can create a domain and assign a constraint to a domain i.e. CREATE DOMAIN GenderDomain CHAR(1) CHECK (VALUE IN (‘F’,’M’)); Then gender GenderDomain,

References First Course In Database Systems, Jeffery D. Ullman and Jennifer Widom,1997 Prentice Hall, Inc.