The Relational Model Constraints and SQL DDL

Similar documents
CSE532 Theory of Database Systems The Relational a Data Model

download instant at The Relational Data Model

Relational Algebra. Relational Query Languages

Database Design. Goal: specification of database schema Methodology:

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

CS275 Intro to Databases

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

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

Chapter 4. The Relational Model

Database Applications (15-415)

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

The Relational Model

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

Database Design. Database Design I: The Entity-Relationship Model. Entity Type (con t) Representation in Relational Model.

SQL DML and DB Applications, JDBC

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

The Relational Data Model. Data Model

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

Relational Algebra and SQL. Relational Query Languages

Relational data model

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

The Relational Model

Relational Query Languages. What is an Algebra? Roadmap. Relational Algebra. Chapter 5. The Role of Relational Algebra in a DBMS

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

Database Systems ( 資料庫系統 )

The Relational Model of Data (ii)

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

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

Relational Query Languages. Relational Algebra and SQL. What is an Algebra? Select Operator. The Role of Relational Algebra in a DBMS

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

CS 2451 Database Systems: Relational Data Model

The Relational Model. Chapter 3

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

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

Introduction to Relational Databases. Introduction to Relational Databases cont: Introduction to Relational Databases cont: Relational Data structure

The Relational Model 2. Week 3

Chapter 1: Introduction

The Relational Model. Week 2

King Fahd University of Petroleum and Minerals

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

CSE532 Relational Algebra and SQL

The Relational Model

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

Chapter 5. Relational Algebra and SQL

Chapter 3: Introduction to SQL

Chapter 1 SQL and Data

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

CS143: Relational Model

Chapter 4 Conceptual Modeling

Introduction to Databases

Database Management Systems MIT Introduction By S. Sabraz Nawaz

Database Systems: An Application-Oriented Approach (Complete Version) Practice Problems and Solutions for Students

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

Database Management Systems. Chapter 3 Part 1

The SQL data-definition language (DDL) allows defining :

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

Relational Database Systems Part 01. Karine Reis Ferreira

Database Management System 9

CS317 File and Database Systems

SQL - Basics. SQL Overview

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

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

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

Database Systems. Course Administration

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

Chapter 3: Introduction to SQL

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

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

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

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

CMPT 354: Database System I. Lecture 2. Relational Model

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

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition)

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

CISC 3140 (CIS 20.2) Design & Implementation of Software Application II

Database Technology Introduction. Heiko Paulheim

Data about data is database Select correct option: True False Partially True None of the Above

DATABASE MANAGEMENT SYSTEMS. UNIT I Introduction to Database Systems

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

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

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

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

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

CS 377 Database Systems

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

1/24/2012. Chapter 7 Outline. Chapter 7 Outline (cont d.) CS 440: Database Management Systems

Keys, SQL, and Views CMPSCI 645

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

Let s briefly review important EER inheritance concepts

CIS 330: Applied Database Systems

CS425 Midterm Exam Summer C 2012

Relational Model. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

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

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

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

Lecture 2: Introduction to SQL

CS425 Fall 2016 Boris Glavic Chapter 2: Intro to Relational Model

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

The Relational Model. Database Management Systems

Transcription:

The Relational Model Constraints and SQL DDL Week 2-3 Weeks 2-3 MIE253-Consens 1

Schedule Week Date Lecture Topic 1 Jan 9 Introduction to Data Management 2 Jan 16 The Relational Model 3 Jan. 23 Constraints and SQL DDL 4 Jan. 30 DB Applications, JDBC 5 Feb 6 Relational Algebra 6 Feb 13 Advanced SQL - Feb 20 [Reading Week] 7 Feb 27 Review and Midterm (Mar 1) 8 Mar 5 OLAP 9 Mar 12 ER Conceptual Modelling 10 Mar 19 Normalization 11 Mar 26 XML and Data Integration 12 Apr 2 Transactions and the Internet, Query Processing 13 Apr 9 Final Review This week s reading: Chapter 3 Last week s reading: Chapters 1-2 Weeks 2-3 MIE253-Consens 2

Relational Database Instance Finite set of relations Each relation consists of a schema and an instance Database schema set of relation schemas constraints among relations (inter-relational constraints) Database instance set of (corresponding) relation instances Weeks 2-3 MIE253-Consens 3

Types of DBs in an Organization Weeks 2-3 MIE253-Consens 4

DB Budgets Platform Average Cost Excel $ 500 Access Individual $ 3,000 Access Simple Multi-user $ 10,000 Access Workgroup/Department $ 50,000 VB/VS.NET/Java and SQL Server $ 500,000 Oracle, IBM DB2 $ 2,000,000 SAP, etc. $ 10,000,000 Weeks 2-3 MIE253-Consens 5

Number of DBs in Organizations Platform Quantity Excel 50,000 Access Individual 5,000 Access Simple Multi-user 1,000 Access Department 500 VB/VS.NET/Java and SQL Server 50 Oracle, IBM DB2 25 SAP, etc. 10 Weeks 2-3 MIE253-Consens 6

SQL DDL Data Definition Language (DDL) Sublanguage of SQL for describing schema, manipulates the definition of tables stored in the DBMS catalog Current version of the ANSI/ISO standard is SQL:2008 (supports Objects, XML) SQL-92 is the most commonly used subset Database vendors generally deviate from standard, but eventually converge e.g., SQL in Access, SQL in JDBC Weeks 2-3 MIE253-Consens 7

Table Declaration CREATE TABLE Student ( Id INTEGER, Name CHAR(20), Address CHAR(50), Status CHAR(10) ) Incorporates the table definition to the database schema Student Id Name Address Status 101222333 John 10 Cedar St Freshman 234567890 Mary 22 Main St Sophomore Weeks 2-3 MIE253-Consens 8

Integrity Constraints Part of schema Restriction on state (or of sequence of states) of data base Enforced by DBMS Protects database from errors Enforces enterprise rules Intra-relational - involve only one relation Part of relation schema e.g., all Ids are unique Inter-relational - involve several relations Part of relation schema or database schema Weeks 2-3 MIE253-Consens 9

Kinds of Integrity Constraints Static restricts legal states of database Syntactic (structural) e.g., all values in a column must be unique Semantic (involve meaning of attributes) e.g., cannot register for more than 18 credits Dynamic limitation on sequences of database states e.g., cannot raise salary by more than 5% Weeks 2-3 MIE253-Consens 10

Key Constraint A key constraint is a sequence of attributes A 1,,A n (n=1 possible) of a relation schema, S, with the following property: Key A relation instance s of S satisfies the key constraint iff at most one row in s can contain a particular set of values, a 1,,a n, for the attributes A 1,,A n Minimality: no subset of A 1,,A n is a key constraint Set of attributes mentioned in a key constraint e.g., Id in Student, e.g., (StudId, CrsCode, Semester) in Transcript It is minimal: no subset of a key is a key (Id, Name) is not a key of Student Weeks 2-3 MIE253-Consens 11

Key Constraint A key constraint is a sequence of attributes A 1,,A n (n=1 possible) of a relation schema, S, with the following property: Key A relation instance s of S satisfies the key constraint iff at most one row in s can contain a particular set of values, a 1,,a n, for the attributes A 1,,A n Minimality: no subset of A 1,,A n is a key constraint Set of attributes mentioned in a key constraint e.g., Id in Student, e.g., (StudId, CrsCode, Semester) in Transcript It is minimal: no subset of a key is a key (Id, Name) is not a key of Student Weeks 2-3 MIE253-Consens 12

Key Constraint (cont d) Superkey - set of attributes containing key (Id, Name) is a superkey of Student Every relation has a key Not with SQL bags Relation can have several keys: primary key: Id in Student (can t be null) candidate key: (Name, Address) in Student Weeks 2-3 MIE253-Consens 13

Primary/Candidate Keys CREATE TABLE Course ( CrsCode CHAR(6), CrsName CHAR(20), DeptId CHAR(4), Descr CHAR(100), PRIMARY KEY (CrsCode), UNIQUE (DeptId, CrsName) -- candidate key ) Things that start with 2 dashes are comments in the SQL standard -- not in Access Weeks 2-3 MIE253-Consens 14

Example Database Schema Student (Id: INT, Name: STRING, Address: STRING, Status: STRING) Professor (Id: INT, Name: STRING, DeptId: DEPTS) Department(DeptId: DEPTS, Name: STRING) Course (DeptId: DEPTS, CrsName: STRING, CrsCode: COURSES) Transcript (CrsCode: COURSES, StudId: INT, Grade: GRADES, Semester: SEMESTERS) Weeks 2-3 MIE253-Consens 15

Foreign Key Constraint Referential integrity: Item named in one relation must refer to tuples that describe that item in another Transcript (CrsCode) references Course(CrsCode ) Professor(DeptId) references Department(DeptId) Attribute A 1 is a foreign key of R1 referring to attribute A 2 in R2, if whenever there is a value v of A 1, there is a tuple of R2 in which A 2 has value v, and A 2 is a key of R2 Weeks 2-3 MIE253-Consens 16

Foreign Key Constraint (Example) Professor DeptId v1 v2 v3 v4 -- v3 DeptId v3 v5 v1 v6 v2 v7 v4 Department Foreign key Candidate key Weeks 2-3 MIE253-Consens 17

Foreign Key (cont d) Names of A 1 and A 2 need not be the same. With tables: ProfId attribute of Teaching references Id attribute of Professor R1 and R2 need not be distinct. Teaching(CrsCode: COURSES, Sem: SEMESTERS, ProfId: INT) Professor(Id: INT, Name: STRING, DeptId: DEPTS) Employee(Id:INT, MgrId:INT,.) Employee(MgrId) references Employee(Id) Every manager is also an employee and hence has a unique row in Employee Weeks 2-3 MIE253-Consens 18

Foreign Key (cont d) Foreign key might consist of several columns (CrsCode, Semester) of Transcript references (CrsCode, Semester) of Teaching R1(A 1, A n ) references R2(B 1, B n ) There exists a 1-1 correspondance between A 1, A n and B 1, B n A i and B i have same domains (although not necessarily the same names) B 1,,B n is a candidate key of R2 Weeks 2-3 MIE253-Consens 19

Foreign Key Constraint CREATE TABLE Teaching ( ProfId INTEGER, CrsCode CHAR (6), Semester CHAR (6), PRIMARY KEY (CrsCode, Semester), FOREIGN KEY (CrsCode) REFERENCES Course, FOREIGN KEY (ProfId) REFERENCES Professor (Id) ) Weeks 2-3 MIE253-Consens 20

Foreign Key Constraint CrsCode CrsCode ProfId c c Teaching p Id Course p Professor Weeks 2-3 MIE253-Consens 21

Constraints and SQL DDL Week 2-3 (cont.) Weeks 2-3 MIE253-Consens 22

Schedule Week Date Lecture Topic 1 Jan 9 Introduction to Data Management 2 Jan 16 The Relational Model 3 Jan. 23 Constraints and SQL DDL 4 Jan. 30 SQL DML, DB Applications, JDBC 5 Feb 6 Relational Algebra 6 Feb 13 Advanced SQL - Feb 20 [Reading Week] 7 Feb 27 Review and Midterm (Mar 1) 8 Mar 5 OLAP 9 Mar 12 ER Conceptual Modelling 10 Mar 19 Normalization 11 Mar 26 XML and Data Integration 12 Apr 2 Transactions and the Internet, Query Processing 13 Apr 9 Final Review This week s reading: Chapter 5 (5.3) Chapter 8 (8.5, 8.2.5) Previous week s: Chapters 1-2 Chapter 3 Weeks 2-3 MIE253-Consens 23

NULL Problem: Not all information might be known when row is inserted (e.g., Grade might be missing from Transcript) A column might not be applicable for a particular row (e.g., MaidenName if row describes a male) Solution: Use place holder NULL Not a value of any domain (although called null value) Indicates the absence of a value Not allowed in certain situations Primary keys and columns constrained by NOT NULL Weeks 2-3 MIE253-Consens 24

Default Value -Value to be assigned if attribute value in a row is not specified CREATE TABLE Student ( Id: INTEGER, Name CHAR(20) NOT NULL, Address CHAR(50), Status CHAR(10) DEFAULT freshman, PRIMARY KEY (Id) ) Weeks 2-3 MIE253-Consens 25

Semantic Constraints in SQL Primary key and foreign key are examples of structural (syntactic) constraints Semantic constraints Express the logic of the application at hand: e.g., number of registered students maximum enrollment Weeks 2-3 MIE253-Consens 26

Semantic Constraints (cont d) Used for application dependent conditions Example: limit attribute values CREATE TABLE Transcript ( StudId INTEGER, CrsCode CHAR(6), Semester CHAR(6), Grade CHAR(1), CHECK (Grade IN ( A, B, C, D, F )), CHECK (StudId > 0 AND StudId < 1000000000) ) Each row in table must satisfy condition Weeks 2-3 MIE253-Consens 27

Domains Possible attribute values can be specified Using a CHECK constraint or Creating a new domain Domain can be used in several declarations Domain is a schema element CREATE DOMAIN Grades CHAR (1) CHECK (VALUE IN ( A, B, C, D, F )) CREATE TABLE Transcript (., Grade Grades, ) Weeks 2-3 MIE253-Consens 28

Modifying the Schema ALTER TABLE Student ADD COLUMN Gpa INTEGER DEFAULT 0 ALTER TABLE Student ADD CONSTRAINT GpaRange CHECK (Gpa >= 0 AND Gpa <= 4) ALTER TABLE Student DROP CONSTRAINT GpaRange-- constraint names are useful DROP TABLE Employee Weeks 2-3 MIE253-Consens 29

Circularity in Foreign Key Constraint A 1 A 2 A 3 B 1 B 2 B 3 A y x x y B candidate key: A 1 foreign key: A 3 references B(B 1 ) candidate key: B 1 foreign key: B 3 references A(A 1 ) Problem 1: Creation of A requires existence of B and vice versa Solution: CREATE TABLE A ( ) -- no foreign key CREATE TABLE B ( ) -- include foreign key ALTER TABLE A ADD CONSTRAINT cons FOREIGN KEY (A 3 ) REFERENCES B (B 1 ) MIE253-Samavi 30

Inclusion Dependency Referential integrity constraint that is not a foreign key constraint Teaching(CrsCode, Semester) references Transcript(CrsCode, Semester) (no courses are taught without students) Target attributes do not form a candidate key in Transcript (StudId missing) No simple enforcement mechanism for inclusion dependencies in SQL (requires assertions) Weeks 2-3 MIE253-Consens 31

Summary: The Relational Model Schemas: physical, conceptual, external Data model: DSL, DDL, DML Relational Model, SQL Relation Instance (tuples, cardinality) Relation Schema (relation name, attributes, domains, constraints) Constraints (Keys, PK, CK, FK, ID) SQL DDL Weeks 2-3 MIE253-Consens 32