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