Introduction to Databases
Data Retrival SELECT * FROM Students S WHERE S.age < 18
Data Retrival SELECT S.name, S.login FROM Students S WHERE S.age < 18
Entity sets to tables
Entity sets to tables CREATE TABLE ( CHAR(11), CHAR(30), Date, PRIMARY KEY () )
Relation sets to tables (no constraints) Since DID D Budget WorksIn Departments
Relation sets to tables (no constraints) Since DID D Budget WorksIn Departments CREATE TABLE WorksIn ( CHAR(11), DID INTEGER, Since DATE, PRIMARY KEY (, DID), FOREIGN KEY () REFERENCES, FOREIGN KEY (DID) REFERENCES Departments )
Relation sets to tables (no constraints) ReportsTo
Relation sets to tables (no constraints) ReportsTo CREATE TABLE ReportsTo (supervisor_ CHAR(11), subordinate_ CHAR(11), PRIMARY KEY (supervisor_, subordinate_), FOREIGN KEY (supervisor_) REFERENCES (), FOREIGN KEY (subordinate_) REFERENCES () )
Relation sets to tables (with key constraints) Since DID D Budget Manages Departments
Relation sets to tables (with key constraints) Since DID D Budget Manages Departments CREATE TABLE Manages ( CHAR(11), DID INTEGER, Since DATE, PRIMARY KEY (DID), FOREIGN KEY () REFERENCES, FOREIGN KEY (DID) REFERENCES Departments )
Relation sets to tables (with key constraints) Since DID D Budget Manages Departments CREATE TABLE Dept_Mgr (DID INTEGER, D CHAR(20), Budget REAL, CHAR(11), SINCE DATE, PRIMARY KEY (DID), FOREIGN KEY () REFERENCES )
Which way is better? Since DID D Budget Manages Departments Departments + Manages Dept_Mgr
Which way is better? Since DID D Budget Manages Departments Departments + Manages Often more space efficient Can be slow when generating the combined view Dept_Mgr Can waste space Remove the need for another table and therefore faster to query
Relation sets to tables (with participation constraints) Since DID D Budget Manages Departments CREATE TABLE Dept_Mgr (DID INTEGER, D CHAR(20), Budget REAL, CHAR(11) NOT NULL, SINCE DATE, PRIMARY KEY (DID), FOREIGN KEY () REFERENCES ON DELETE NO ACTION )
Relation sets to tables (with participation constraints) Since DID D Budget Manages Departments CREATE TABLE Manages ( CHAR(11), DID INTEGER, Since DATE, PRIMARY KEY (DID), FOREIGN KEY () REFERENCES, FOREIGN KEY (DID) REFERENCES Departments ) Can we still capture the total participation constraint?
Relation sets to tables (with participation constraints) Since DID D Budget Manages Departments CREATE TABLE Manages ( CHAR(11) NOT NULL, DID INTEGER, Since DATE, PRIMARY KEY (DID), FOREIGN KEY () REFERENCES, FOREIGN KEY (DID) REFERENCES Departments ) Does this fix the problem?
Relation sets to tables (with participation constraints) Since DID D Budget Manages Departments Participation constraints in general cannot be enforced with only table definitions. We need other more powerful tools, such as table constraints and assertions. What can be captured? (1) In a binary relationship, there is exactly one of the entity sets has a key constraints, and the total participation constraint is expressed on that entity set. (2) A relationship in which all participating entity sets have key constraints and total participation.
Weak entities to tables Cost P Age Policy Dependents CREATE TABLE Dep_Policy(P CHAR(20), Age INTEGER, Cost REAL, CHAR(11), PRIMARY KEY (P, ), FOREIGN KEY () REFERENCES ON DELETE CASCADE )
Class hierarchies First approach: Dog, Trained_Dog, In_Training_Dog
Class hierarchies Second approach: Trained_Dog and In_Training_Dog which common attributes of Dog.
Class hierarchies Which way is better? ER to Relational
Questions?