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

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

The Relational Model of Data (ii)

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

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

The Relational Model. Chapter 3

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

Relational data model

Database Management Systems. Chapter 3 Part 1

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

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

The Relational Data Model. Data Model

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

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

Database Applications (15-415)

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

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

The Relational Model 2. Week 3

DATABASE TECHNOLOGY - 1MB025 (also 1DL029, 1DL300+1DL400)

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

DATABASE DESIGN - 1DL400

Database Systems ( 資料庫系統 )

CS425 Midterm Exam Summer C 2012

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

DATABASE DESIGN - 1DL400

The Relational Model. Week 2

CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

Domain Constraints Referential Integrity Assertions Triggers. Authorization Authorization in SQL

The Relational Model

CIS 330: Applied Database Systems

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

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

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

DATABASDESIGN FÖR INGENJÖRER - 1DL124

Announcements If you are enrolled to the class, but have not received the from Piazza, please send me an . Recap: Lecture 1.

Keys, SQL, and Views CMPSCI 645

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

The Relational Model

Database Systems. Lecture2:E-R model. Juan Huo( 霍娟 )

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

Database Systems. Course Administration

Introduction to Data Management. Lecture #3 (E-R Design, Cont d.)

DBMS. Relational Model. Module Title?

Integrity and Security

Database System Concepts

The Relational Model. Database Management Systems

Database Management Systems. Session 2. Instructor: Vinnie Costa

Review: Where have we been?

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

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

SQL Constraints and Triggers. Week 11

CS W Introduction to Databases Spring Computer Science Department Columbia University

Relational Query Languages

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

Introduction to Databases

e e Conceptual design begins with the collection of requirements and results needed from the database (ER Diag.)

The Relational Model (ii)

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

SQL: The Query Language Part 1. Relational Query Languages

Relational Algebra Homework 0 Due Tonight, 5pm! R & G, Chapter 4 Room Swap for Tuesday Discussion Section Homework 1 will be posted Tomorrow

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

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

CS 2451 Database Systems: Relational Data Model

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

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

Views, assertions, and triggers

Introduction to Data Management. Lecture #4 E-R Model, Still Going

Introduction to Data Management. Lecture #17 (SQL, the Never Ending Story )

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Conceptual Design. The Entity-Relationship (ER) Model

Introduction to Data Management. Lecture #5 (E-R Relational, Cont.)

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

High-Level Database Models (ii)

Outer Join, More on SQL Constraints

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

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

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

The Relational Model Constraints and SQL DDL

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

Modern Database Systems Lecture 1

Chapter 4: SQL. Basic Structure

CSC 453 Database Technologies. Tanu Malik DePaul University

The Relational Model

QQ Group

CS5300 Database Systems

Page 1. Quiz 18.1: Flow-Control" Goals for Today" Quiz 18.1: Flow-Control" CS162 Operating Systems and Systems Programming Lecture 18 Transactions"

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

Integrity Constraints (Reminder)

Page 1. Goals for Today" What is a Database " Key Concept: Structured Data" CS162 Operating Systems and Systems Programming Lecture 13.

Databases. Jörg Endrullis. VU University Amsterdam

1 (10) 2 (8) 3 (12) 4 (14) 5 (6) Total (50)

DATABASE TECHNOLOGY - 1MB025

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

DATABASTEKNIK - 1DL116

Chapter 6: Integrity and Security.! Domain Constraints! Referential Integrity! Assertions! Triggers! Security! Authorization! Authorization in SQL

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

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

DATABASE TECHNOLOGY - 1MB025

01/01/2017. Chapter 5: The Relational Data Model and Relational Database Constraints: Outline. Chapter 5: Relational Database Constraints

Transcription:

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

SQL as Data Definition Language Creates the Students relation. The type (domain) of each field is specified, and enforced by the DBMS whenever tuples are added or modified. CREATE TABLE Students (sid: CHAR(20), name: CHAR(20), login: CHAR(10), age: INT, gpa: REAL) As another example, the Enrolled table holds information about courses that students take. CREATE TABLE Enrolled (sid: CHAR(20), cid: CHAR(20), grade: CHAR(2)) 2

Integrity Constraints (IC) IC guard against accidental damage to the database, by ensuring that authorized changes to the database do not result in a loss of data consistency. Integrity constraints are based upon the semantics of the realworld enterprise that is being described in the database relations. We can check a database instance to see if an IC is violated, but we can NEVER infer that an IC is true by looking at an instance. An IC is a statement about all possible instances! From example, we know name is not a key, but the assertion that sid is a key is given to us. 3

Domain Constraints The check clause in SQL-92 permits domains to be restricted use check clause to ensure that an hourly-wage domain allows only values greater than a specified value. name of constraint 4 new domain name create domain hourly-wage numeric(5,2) constraint value-test check (value>=4.00) condition must be TRUE The domain hourly-wage is declared to be a decimal number with 5 digits, 2 of which are after the decimal point The domain has a constraint that ensures that the hourly-wage is greater than 4.00. constraint value-test is optional; useful to indicate which constraint an update violated.

Primary and Candidate Keys in SQL Possibly many candidate keys (specified using UNIQUE), one of which is chosen as the primary key. CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid) ) CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid), UNIQUE (cid, grade) ) Used carelessly, an IC can prevent the storage of database instances that arise in practice! 5

Foreign Keys in SQL Only students listed in the Students relation should be allowed to enroll for courses. CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ) Enrolled sid cid grade 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B 6 Students sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8

Enforcing Referential Integrity Consider Students and Enrolled; sid in Enrolled is a foreign key that references Students. What should be done if an Enrolled tuple with a non-existent student id is inserted? (Reject it!) What should be done if a Students tuple is deleted? Also delete all Enrolled tuples that refer to it (cascading deletion). Disallow deletion of a Student tuple that is referred to by an Enrolled tuple. Set sid in Enrolled tuples that refer to it to a default sid. Set sid in Enrolled tuples that refer to it to a special value null (not applicable in this example because sid is part of the primary key). If primary key of Students tuple is updated, you must also update the classes taken by the student 7

Referential Integrity in SQL/92 SQL/92 supports all 4 options on deletes and updates. Default is NO ACTION (delete is rejected) CASCADE (also delete all tuples that refer to deleted tuple) SET NULL / SET DEFAULT (sets foreign key value of referencing tuple) CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ON DELETE CASCADE ON UPDATE CASCADE) 8

Participation Constraints in SQL We can capture total participation constraints using NOT NULL. CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, HKID CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (HKID) REFERENCES Employees, ON DELETE NO ACTION) 9

Destroying and Altering Relations DROP TABLE Students Destroys the relation Students. The schema information and the tuples are deleted. ALTER TABLE Students ADD COLUMN firstyear: integer The schema of Students is altered by adding a new field; every tuple in the current instance is extended with a null value in the new field. 10

Views Provide a mechanism to hide certain data from the view of certain users. To create a view we use the command: create view view-name as <query expression> where: <query expression> is any legal SQL query EXAMPLE: Create a view from loan(loan-number, branch-name, amount) that hides the amount. create view branch-loan as select branch-name, loan-number from loan QUERY: Find all loans in the Perryridge branch select loan-number from branch-loan where branch-name = Perryridge A user who has access to the view, but not the loan table, cannot see the amount. 11

General Constraints Useful when more general ICs than keys are involved. Are created in the definition of table - checked whenever there is an update within the table CREATE TABLE Loan ( loan-number INTEGER, amount INTEGER, branch-name CHAR(20), PRIMARY KEY (loan-number ), FOREIGN KEY (branch-name) REFERENCES Branch, ON DELETE CASCADE CHECK ( amount >= 1 AND amount <= 10000) CHECK ( branch-name <> "Choi Hung"))

Assertions An assertion is a complex constraint that the database must always satisfy. An assertion in SQL-92 takes the form create assertion <assertion-name> check <predicate> Difference from general constraints: A constraint is associated with a single table and checked when there is an update on this specific table An assertion may be associated with several tables, and is checked every time there is an update anywhere. Assertion testing may introduce a significant amount of overhead; hence assertions should be used with great care. Any predicate allowed in SQL can be used. 13

Assertion Example The sum of all loan amounts for each branch must be less than the sum of all account balances at the branch. create assertion sum-constraint check (not exists (select * from branch where (select sum(amount) from loan where loan.branch-name=branch.branch-name) >= (select sum(amount) from account where loan.number-name=branch.branch-name) )) Note that the assertion refers to multiple tables. Therefore it cannot be included as a constraint in the definition of loan or amount. 14