Constraints CSED421 Database Systems Lab. Constraints To define an integrity constraint (IC) –IC : condition that must be true for any instance of the.

Slides:



Advertisements
Similar presentations
CSED421 Database Systems Lab Constraints Group functions.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Mirsad Hadzikadic.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
The Relational Model CS 186, Fall 2006, Lecture 2 R & G, Chap. 3.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
The Relational Model CS 186, Spring 2007, Lecture 2 Cow book Section 1.5, Chapter 3 Mary Roth.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Model and more…
The Relational Model These slides are based on the slides of your text book.
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3 Modified by Donghui Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
 Relational database: a set of relations.  Relation: made up of 2 parts: › Instance : a table, with rows and columns. #rows = cardinality, #fields =
1 The Relational Model Instructor: Mohamed Eltabakh
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
1 Database Systems ( 資料庫系統 ) October 4, 2005 Lecture #3.
SQL: DDL John Ortiz Cs.utsa.edu.
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
CS3431-B111 The Relational Model Instructor: Mohamed Eltabakh
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.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
1 Database Systems ( 資料庫系統 ) October 4/5, 2006 Lecture #3.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
CMPT 258 Database Systems The Relationship Model (Chapter 3)
The Relational Model Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints.
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Jianping Fan.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Xin Zhang.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
 Database – collection of persistent data  Database Management System (DBMS) – software system that supports creation, population, and querying of a.
Logical DB Design: ER to Relational
Constraints and Triggers
CS 186, Fall 2006, Lecture 2 R & G, Chap. 3
Referential Integrity
Instructor: Mohamed Eltabakh
Database Systems (資料庫系統)
The Relational Model Relational Data Model
Referential Integrity
Database Systems (資料庫系統)
Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3
The Relational Model Chapter 3
Presentation transcript:

Constraints CSED421 Database Systems Lab

Constraints To define an integrity constraint (IC) –IC : condition that must be true for any instance of the database 5 types of constraints –NOT NULL constraints –UNIQUE constraints –PRIMARY KEY constraints –FOREIGN KEY constraints –CHECK constraints

Specification As part of the table definition e.g., CREATE TABLE orders ( order_id NUMBER PRIMARY KEY, price NUMBER NOT NULL, … ); CREATE TABLE ( [ CONSTRAINT ], … );

Specification (cont.) Alteration after the table definition e.g., ALTER TABLE orders ADD PRIMARY KEY (order_id); e.g., ALTER TABLE orders MODIFY ( price NOT NULL ); ALTER TABLE ADD[DROP] [ CONSTRAINT ] ; ALTER TABLE MODIFY ( [ CONSTRAINT ] );

NOT NULL Constraint Prohibits a database value from being null –null : either unknown or not applicable To satisfy a NOT NULL constraint, every row in the table must contain a value for the column CREATE TABLE student ( sid NUMBER NOT NULL, … … … ); ALTER TALBE student MODIFY sid NOT NULL;

UNIQUE Constraint Prohibits multiple rows from having the same value in the same column or combination of columns. But allows some values to be null. CREATE TABLE promotions ( promo_id NUMBER UNIQUE, … … … ); ALTER TABLE promotions ADD UNIQUE (promo_id); ALTER TABLE promotions MODIFY promo_id UNIQUE;

PRIMARY KEY Constraint Prohibits multiple rows from having the same value in the same column or combination of columns and prohibits values from being null. Combines a NOT NULL constraint and a unique constraint in a single declaration. CREATE TABLE locations ( location_id NUMBER PRIMARY KEY, … … … ); ALTER TABLE locations ADD PRIMARY KEY (location_id); ALTER TABLE locations MODIFY location_id PRIMARY KEY;

PRIMARY KEY Constraint (cont.) CREATE TABLE locations ( nameCHAR(20), addrCHAR(20), … …, CONSTRAINT name_addr PRIMARY KEY (name, addr) ); ALTER TABLE locations ADD CONSTRAINT name_addr PRIMARY KEY (name, addr)

FOREIGN KEY Constraint Requires values in one table to match values in another table. Also called referential integrity constraint CREATE TABLE enrolled ( sid CHAR(20), … … …, CONSTRAINT fk_enrolled FOREIGN KEY (sid) REFERENCES students(sid) ); CREATE TABLE enrolled ( sid CHAR(20) REFERENCES students(sid) … … … ); ALTER TABLE enrolled ADD CONSTRAINT fk_enrolled FOREIGN KEY (sid) REFERENCES students(sid); ALTER TABLE enrolled MODIFY sid REFERENCES students(sid);

FOREIGN KEY Constraint What if a student tuple is deleted… –NO ACTION (default) –CASCADE / SET NULL CREATE TABLE Enrolled( … … …, FOREIGN KEY (sid) REFERENCES Students(sid) ON DELETE CASCADE / SET NULL ); sidcidgrade 53666Carnatic101C 53666Reggae203B 53650Topology112A 53666'History105B sidnameloginagegpa EnrolledStudents

CHECK Constraint Requires a value in the database to comply with a specified condition CREATE TABLE emp ( … … …, age NUMBER CHECK (age > 0) ); ALTER TABLE emp ADD CHECK (age > 0); ALTER TABLE emp MODIFY age CHECK (age > 0);

Practice 1 1.Bank_Acct 와 Customer_Info 테이블을 생성 Customer_Info : name VARCHAR2(20), addr VARCHAR(60), phone CHAR(10), VARCHAR2(40) Bank_Acct : acct CHAR(4), name VARCHAR2(20), addr VARCHAR2(60), balance REAL Customer_Info’s primary key is (name, addr) Bank_Acct’s primary key is acct Bank_Acct’s (name, addr) references Customer_Info’s (name, addr)

Practice 1 2. 두 테이블에 대한 위의 인스턴스에 대해 오류 (IC) 를 찾기 3. 두 테이블에 각각의 레코드를 IC 를 만족하도록 필드 값을 수정하여 추가하기 NameAddrPhone Jones11 First Smith12 First Green14 Fourth Gates11 First Smith12 Second AcctNameAddrbalance 1005Jones11 First St Smith12 First St Smith12 First St Green14 Fourth St Gates20 Tenth St Smith15 Second St443 Bank_AcctCustomer_Info

Practice 1 4. 아래의 SQL 을 수행결과를 확인 SELECT B.name, C. FROM Bank_Acct B, Customer_Info C WHERE B.name = C.name AND B.addr = C.addr AND B.balance >

Practice 2 Simulate violation of integrity constraint 스크립트를 통해 테이블 생성. Practice2 스크립트는 Enrolled 와 Students 테이블 을 생성하고, Enrolled 의 Students 를 참조하는 foreign key constraint 를 추가한다. sidcidgrade 53666Carnatic101C 53666Reggae203B 53650Topology112A 53666'History105B sidnameloginagegpa EnrolledStudents

Practice 2 practice2.sql CREATE TABLE Students ( sid CHAR(20) PRIMARY KEY, name CHAR(20), login CHAR(10), age INTEGER, gpa REAL ); CREATE TABLE Enrolled ( sid CHAR(20), cid CHAR(20), grade CHAR(2), CONSTRAINT fk_enrolled FOREIGN KEY (sid) REFERENCES Students (sid) ); INSERT INTO Students VALUES ('53666', 'Jones', 18, 3.4); INSERT INTO Students VALUES ('53688', 'Smith', 18, 3.2); INSERT INTO Students VALUES ('53650', 'Smith', 19, 3.8); INSERT INTO Enrolled VALUES ('53666', 'Carnatic101', 'C'); INSERT INTO Enrolled VALUES ('53666', 'Reggae203', 'B'); INSERT INTO Enrolled VALUES ('53650', 'Topology112', 'A'); INSERT INTO Enrolled VALUES ('53666', 'History105', 'B');

Practice 2 2. 각각의 referential integrity enforcement 에 따라 Students 의 레코드를 삭제한다. (NO ACTION, CASCADE) 3. 결과 메시지 혹은 삭제 후 테이블의 전체 내용들 을 확인한다.

Practice 2 TAs want to see query results… –NO ACTION enforcement –CASCADE

Constraint Check Confirm constraints on the tables. –Use a system table, user_constraints. SQL> SELECT table_name, constraint_name, constraint_type FROM user_constraints WHERE table_name = ‘your table name’; SQL> SELECT table_name, constraint_name, constraint_type FROM user_constraints