Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Similar documents
EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Assignment 7: Integrity Constraints

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

Chapter 1 SQL and Data

Integrity Constraints (Reminder)

1- a> [5pts] Create the new table by writing and executing a full SQL DDL statement based on the following schema:

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

CSC 261/461 Database Systems Lecture 6. Fall 2017

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

CSC 453 Database Technologies. Tanu Malik DePaul University

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

The Relational Model Constraints and SQL DDL

SQL DATA DEFINITION LANGUAGE

The Relational Model

The Relational Model of Data (ii)

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

SQL DATA DEFINITION LANGUAGE

doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D.

Database Applications (15-415)

CLASS DISCUSSION AND NOTES

Lab # 4. Data Definition Language (DDL)

SQL Server. Lecture3 Cascading referential integrity constraint

The Relational Model

Structure Query Language (SQL)

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Introduction to Database Systems CSE 414

Relational data model

The Relational Model

Overview. Data Integrity. Three basic types of data integrity. Integrity implementation and enforcement. Database constraints Transaction Trigger

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

Oracle Create Table Foreign Key On Delete No

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

COSC344 Database Theory and Applications. σ a= c (P) Lecture 3 The Relational Data. Model. π A, COSC344 Lecture 3 1

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

SQL DATA DEFINITION LANGUAGE

The Relational Model. Chapter 3

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

CSE 530A. ER Model to Relational Schema. Washington University Fall 2013

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

3.1. Keys: Super Key, Candidate Key, Primary Key, Alternate Key, Foreign Key

LAB 3 Notes. Codd proposed the relational model in 70 Main advantage of Relational Model : Simple representation (relationstables(row,

Introduction to Data Management CSE 344

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The Relational Model 2. Week 3

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Relational Database Systems Part 01. Karine Reis Ferreira

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

Using a DBMS. Shan-Hung Wu & DataLab CS, NTHU

Chapter 1: Introduction

download instant at The Relational Data Model

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E)

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

The Relational Model. Week 2

Module 9: Managing Schema Objects

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Introduction to Data Management CSE 344

Database Management Systems. Chapter 3 Part 1

Databases. Jörg Endrullis. VU University Amsterdam

Intermediate SQL ( )

Let s briefly review important EER inheritance concepts

The Relational Data Model and Relational Database Constraints

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

Chapter 7 Constraints and Triggers. Spring 2011 Instructor: Hassan Khosravi

Introduction to SQL Server. nikos bikakis

Oracle Database 10g Express

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

Data Manipulation (DML) and Data Definition (DDL)

The Basic (Flat) Relational Model. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Techno India Batanagar Computer Science and Engineering. Model Questions. Subject Name: Database Management System Subject Code: CS 601

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

Basic SQL. Basic SQL. Basic SQL

CS W Introduction to Databases Spring Computer Science Department Columbia University

Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2

Full file at

DB Creation with SQL DDL

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Relational Data Model. Christopher Simpkins

Relational Model, Key Constraints

In This Lecture. SQL Data Definition SQL SQL. Non-Procedural Programming. Notes. Database Systems Lecture 5 Natasha Alechina

SQL: Data Definition Language

INTRODUCTION TO RELATIONAL DATABASE SYSTEMS

Transcription:

Data Modelling and Databases Exercise Session 7: Integrity Constraints 1

Database Design Textual Description Complete Design ER Diagram Relational Schema Conceptual Modeling Logical Modeling Physical Modeling SQL (DDL) Integrity Constraint 2

Integrity Constraints How can we define rules in a DB? 1. Attribute Constraints: intra-table rules 2. Referential Constraints: inter-table rules 3. Triggers: more complex rules Constraints are used to limit the type of data that can be inserted into a table. There exist two ways to specify constraints on a relation: When a table is created: CREATE TABLE After the table is created: ALTER TABLE 3

Integrity Constraints Name NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK DEFAULT Description This enforces a field to always contain a value. This constraint uniquely identifies each record in a table. An attribute that is unique can be null. A primary key uniquely identifies each record in a table (= UNIQUE + NOT NULL), each table can have only one primary key. A foreign key is a reference that points from one table to the primary key of another table. It prevents actions that would destroy links between tables. [Referential Integrity Constraints] This constraint is used to limit the value range that can be placed in a column. CHECK can limit the values in certain columns based on values in other columns It is used to insert a default value into a column. The default value will be added to all new records, if no other value is specified. 4

Intra-Table Constraints Example: CREATE TABLE User ( id int(11) PRIMARY KEY, group varchar(10) DEFAULT guest', username varchar(20) UNIQUE, password varchar(30) NOT NULL, email varchar(30) CHECK (email LIKE %@% ), ) CONSTRAINT chk_group CHECK group IN ( user, admin, guest ) ALTER TABLE User ADD CONSTRAINT uni_email UNIQUE (email) Use CONSTRAINT statement to name your constraints (optional) 5

Referential Constraints A foreign key is a referential constraint between two tables. This is reference that points from one (or more) attribute(s) to the primary key of another table. A foreign key value must point to an existing tuple. If this is not the case, its value must be NULL, i.e. a FK value cannot point to a non-existing row! uid username 1 jribon 3 prtrem 4 lucasa cid uid comment 1 1 Hello World! 2 1 Students are smart. 3 2 I love DB!! 6

Inter-Table Constraints Example: CREATE TABLE User ( id int(11) PRIMARY KEY, group_id int(11) FOREIGN KEY (group_id) REFERENCES Group(id), username varchar(20), password varchar(30) ) CREATE TABLE Group ( id int(11) PRIMARY KEY, name varchar(10) ) 7

Referential Actions Referential actions ensure data integrity if a row (in a referenced table) is deleted or updated. This defines what should happens if we want to delete (or update) a row that is still referenced in another table. uid username 1 jribon 2 prtrem 3 lucasa? cid uid comment 1 1 Hello World! 2 1 Students are smart. 3 2 To be or not to be. Referenced Table Referencing Table 8

Referential Actions SQL-2003 specifies 5 different referential actions: Name CASCADE RESTRICT NO ACTION SET NULL SET DEFAULT Description The respective rows of the referencing table are deleted or updated together with the referenced row. A row cannot be deleted or updated as long as there is a reference to it from a referencing table. Integrity constraint is checked before executing the statement. [Pessimistic: expect violation] As RESTRICT, but integrity constraint is checked at the end of the UPDATE or DELETE statement. [Optimistic: assume no violation] The foreign key value (in the referencing row) is set to NULL when the referenced row is updated or deleted. Note: foreign key must be null-able. Similar to SET NULL, the foreign key is set to the column s default value when the referenced row is updated or deleted. 9

Inter-Table Constraints Example: CREATE TABLE User ( id int(11) PRIMARY KEY, group_id int(11), username varchar(20), password varchar(30) ) ALTER TABLE User ADD CONSTRAINT `fk_user2group` FOREIGN KEY (group_id) REFERENCES Group(id), ON DELETE RESTRICT ON UPDATE CASCADE; Foreign Key constraint with referential actions 10

Question 1.1 Which one does not allow NULL to be inserted? o UNIQUE o NOT NULL o FOREIGN KEY o PRIMARY KEY o CHECK 11

Question 1.1 Which one does not allow NULL to be inserted? o UNIQUE -> In SQL all NULLs are unique NOT NULL o FOREIGN KEY -> Allowed to be NULL PRIMARY KEY -> Can t be NULL CHECK -> Any NULL will cause it to fail 12

Question 1.2 True or False? 13

Question 1.2 True or False? 14

Question 2.1 Schema: Initial contents: What happens when we run the following queries? 15

Question 2.1 Schema: Initial contents: 16

Question 2.1 Schema: Initial contents: Allowed! Not allowed! id 2 already exists. 17

Question 2.1 Schema: Initial contents: 18

Question 2.1 Schema: Initial contents: Allowed! Allowed! 19

Question 2.1 Schema: Initial contents: 20

Question 2.1 Schema: Initial contents: Not allowed! ON DELETE NO ACTION Allowed! ON UPDATE SET NULL 21

Question 2.1 Schema: Initial contents: 22

Question 2.1 Schema: Initial contents: Allowed! ON DELETE CASCADE 23

Question 4 Which statements are true? 24

Question 4 Which statements are true? 25

Question 5 How can we add the following constraints to the schema? 1. The start date of salary has to be before the end date. 2. The end date in dept_emp table is either NULL (meaning sometime future) or is in the past. 3. One employee can only be the manager of one department at a time. This implies that the date entries in dept_manager table can not have overlaps for a certain employee. 26

Question 5 1. The start date of salary has to be before the end date. 27

Question 5 2. The end date in dept_emp table is either NULL (meaning sometime future) or is in the past. 28

Question 5 3. One employee can only be the manager of one department at a time. 29