Upload
others
View
5
Download
0
Embed Size (px)
Citation preview
Weeks 2-3 MIE253-Consens 1
The Relational ModelConstraints and SQL DDL
Week 2-3
Weeks 2-3 MIE253-Consens 2
ScheduleWeek Date Lecture Topic1 Jan 9 Introduction to Data Management2 Jan 16 The Relational Model3 Jan. 23 Constraints and SQL DDL4 Jan. 30 DB Applications, JDBC5 Feb 6 Relational Algebra6 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 Modelling10 Mar 19 Normalization11 Mar 26 XML and Data Integration12 Apr 2 Transactions and the Internet, Query Processing13 Apr 9 Final Review
This week’s reading:
Chapter 3
Last week’s reading:
Chapters 1-2
Weeks 2-3 MIE253-Consens 3
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 4
Types of DBs in an Organization
Weeks 2-3 MIE253-Consens 5
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 6
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 7
SQL DDLData 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 8
Table Declaration
CREATE TABLE Student (Id INTEGER,Name CHAR(20),Address CHAR(50),Status CHAR(10)
)
101222333 John 10 Cedar St Freshman234567890 Mary 22 Main St Sophomore
Id Name Address Status
Student
Incorporates the table definition to the database schema
Weeks 2-3 MIE253-Consens 9
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 10
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 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
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 13
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 14
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 15
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 16
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, andA2 is a key of R2
Weeks 2-3 MIE253-Consens 17
Foreign Key Constraint (Example)
DeptIdv3v5v1v6v2v7v4
DeptIdv1v2v3v4--v3
DepartmentProfessor
Foreign key Candidate key
Weeks 2-3 MIE253-Consens 18
Foreign Key (cont’d)
Names of A1 and A2 need not be the same. With tables:
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
Teaching(CrsCode: COURSES, Sem: SEMESTERS, ProfId: INT)Professor(Id: INT, Name: STRING, DeptId: DEPTS)
Weeks 2-3 MIE253-Consens 19
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 20
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 21
Foreign Key Constraint
c
CrsCode
p
c p
CrsCode ProfId
IdTeaching
Course
Professor
Weeks 2-3 MIE253-Consens 22
Constraints and SQL DDL
Week 2-3 (cont.)
Weeks 2-3 MIE253-Consens 23
ScheduleWeek Date Lecture Topic1 Jan 9 Introduction to Data Management2 Jan 16 The Relational Model3 Jan. 23 Constraints and SQL DDL4 Jan. 30 SQL DML, DB Applications, JDBC5 Feb 6 Relational Algebra6 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 Modelling10 Mar 19 Normalization11 Mar 26 XML and Data Integration12 Apr 2 Transactions and the Internet, Query Processing13 Apr 9 Final Review
Previous week’s:
Chapters 1-2
Chapter 3
This week’s reading:
Chapter 5 (5.3)
Chapter 8 (8.5, 8.2.5)
Weeks 2-3 MIE253-Consens 24
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 25
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 26
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 27
Semantic Constraints (cont’d)
Used for application dependent conditions Example: limit attribute values
Each row in table must satisfy condition
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) )
Weeks 2-3 MIE253-Consens 28
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 29
Modifying the Schema
ALTER TABLE StudentADD COLUMN Gpa INTEGER DEFAULT 0
ALTER TABLE StudentADD CONSTRAINT GpaRange
CHECK (Gpa >= 0 AND Gpa <= 4)
ALTER TABLE StudentDROP CONSTRAINT GpaRange-- constraint names are useful
DROP TABLE Employee
30
Circularity in Foreign Key Constraint
y x
A1 A2 A3 B1 B2 B3
x yA B
candidate key: A1foreign key: A3 references B(B1)
candidate key: B1foreign key: B3 references A(A1)
Problem 1: Creation of A requires existence of B and vice versaSolution: CREATE TABLE A ( ……) -- no foreign key
CREATE TABLE B ( ……) -- include foreign keyALTER TABLE A
ADD CONSTRAINT consFOREIGN KEY (A3) REFERENCES B (B1)
MIE253-Samavi
Weeks 2-3 MIE253-Consens 31
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 32
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