The Relational Model Constraints and SQL DDL

The Relational Model Constraints and SQL DDL

The Relational Model Constraints and SQL DDL Week 2-3 Weeks 2-3 MIE253-Consens 1 Schedule Week Date Lecture Topic This week’s 1 Jan 9 Introduction to Data Management reading: 2 Jan 16 The Relational Model Chapter 3 3 Jan. 23 Constraints and SQL DDL 4 Jan. 30 DB Applications, JDBC 5 Feb 6 Relational Algebra Last week’s 6 Feb 13 Advanced SQL reading: - Feb 20 [Reading Week] Chapters 1-2 7 Feb 27 Review and Midterm (Mar 1) 8Mar5OLAP 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 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 ( Incorporates the Id INTEGER, table definition Name CHAR(20), Address CHAR(50), to the database Status CHAR(10) 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 A1,…,An (n=1 possible) of a relation schema, S, with the following property: A relation instance s of S satisfies the key constraint iff at most one row in s can contain a particular set of values, a1,…,an, for the attributes A1,…,An Minimality: no subset of A1,…,An is a key constraint Key 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 A1,…,An (n=1 possible) of a relation schema, S, with the following property: A relation instance s of S satisfies the key constraint iff at most one row in s can contain a particular set of values, a1,…,an, for the attributes A1,…,An Minimality: no subset of A1,…,An is a key constraint Key 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 A1 is a foreign key of R1 referring to attribute A2 in R2, if whenever there is a value v of A1, there is a tuple of R2 in which A2 has value v, and A2 is a key of R2 Weeks 2-3 MIE253-Consens 16 Foreign Key Constraint (Example) Department Professor DeptId DeptId v3 v1 v5 v2 v1 v3 v6 v4 v2 -- v7 v3 v4 Foreign key Candidate key Weeks 2-3 MIE253-Consens 17 Foreign Key (cont’d) Names of A1 and A2 need not be the same. With tables: Teaching(CrsCode: COURSES, Sem: SEMESTERS, ProfId: INT) Professor(Id: INT, Name: STRING, DeptId: DEPTS) ProfId attribute of Teaching references Id attribute of Professor R1 and R2 need not be distinct. 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(A1, …An) references R2(B1, …Bn) There exists a 1 - 1 correspondance between A1,…An and B1,…Bn Ai and Bi have same domains (although not necessarily the same names) B1,…,Bn 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 p Course Id Teaching 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 This week’s 1 Jan 9 Introduction to Data Management reading: 2 Jan 16 The Relational Model Chapter 5 (5.3) 3 Jan. 23 Constraints and SQL DDL Chapter 8 (8.5, 4 Jan. 30 SQL DML, DB Applications, JDBC 8.2.5) 5 Feb 6 Relational Algebra 6 Feb 13 Advanced SQL - Feb 20 [Reading Week] 7 Feb 27 Review and Midterm (Mar 1) 8Mar5OLAP Previous 9 Mar 12 ER Conceptual Modelling week’s: 10 Mar 19 Normalization Chapters 1-2 11 Mar 26 XML and Data Integration Chapter 3 12 Apr 2 Transactions and the Internet, Query Processing 13 Apr 9 Final Review 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 A1 A2 A3 B1 B2 B3 A y x xyB candidate key: A1 candidate key: B1 foreign key: A3 references B(B1) foreign key: B3 references A(A1) Problem 1: Creation of A requires existence of

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    32 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us