Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Employees
CREATE TABLE Works_In(
In translating a relationship set to
a relation, attributes of the ssn CHAR(11),
relation must include: did INTEGER,
◦ Keys for each participating
since DATE,
entity set (as foreign keys).
PRIMARY KEY (ssn, did),
This set of attributes
forms a superkey for the FOREIGN KEY (ssn)
relation. REFERENCES Employees,
◦ All descriptive attributes. FOREIGN KEY (did)
REFERENCES Departments)
Each dept has at most
one manager, according
to the key constraint since
on Manages. name dname
ssn lot did budget
Translation to
relational model?
1-to-1
1-to Many Many-to-1 Many-to-Many
Map relationship to a
table: CREATE TABLE Manages(
◦ Note that did is the key ssn CHAR(11),
now! did INTEGER,
◦ Separate tables for since DATE,
Employees and PRIMARY KEY (did),
Departments. FOREIGN KEY (ssn) REFERENCES Employees,
FOREIGN KEY (did) REFERENCES Departments)
Since each department
has a unique manager,
we could instead CREATE TABLE Dept_Mgr(
combine Manages and did INTEGER,
Departments. dname CHAR(20),
budget REAL,
ssn CHAR(11),
since DATE,
PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES Employees)
Does every department have a manager?
◦ If so, this is a participation constraint: the participation of Departments
in Manages is said to be total (vs. partial).
Every did value in Departments table must appear in a row of
the Manages table (with a non-null ssn value!)
since
name dname
ssn lot did budget
Works_In
since
We can capture participation constraints involving one entity set in a binary
relationship, but little else (without resorting to CHECK constraints).
name
cost pname age
ssn lot
If we declare A ISA B,
hourly_wages hours_worked
every A entity is also ISA
considered to be a B contractid
entity. Contract_Emps
Hourly_Emps
policyid cost
Purchaser
Beneficiary
Better design
Policies
policyid cost
CREATE TABLE Policies (
The key
constraints allow policyid INTEGER,
us to combine cost REAL,
Purchaser with ssn CHAR(11) NOT NULL,
Policies and PRIMARY KEY (policyid).
Beneficiary with FOREIGN KEY (ssn) REFERENCES Employees,
Dependents. ON DELETE CASCADE)
Participation
constraints lead CREATE TABLE Dependents (
to NOT NULL pname CHAR(20),
constraints. age INTEGER,
What if Policies is policyid INTEGER,
a weak entity PRIMARY KEY (pname, policyid).
set?
FOREIGN KEY (policyid) REFERENCES Policies,
ON DELETE CASCADE)
customer_id customer_ customer_ customer_ loan_ amount
name street city number
An entity is represented by a set of attributes, that is
descriptive properties possessed by all members of an
entityExample:
set.
customer = (customer_id, customer_name,
customer_street, customer_city )
loan = (loan_number, amount )
Attribute types:
◦ Simple and composite attributes.
◦ Single-valued and multi-valued attributes
Example: multivalued attribute: phone_numbers
◦ Derived attributes
Can be computed from other attributes
Example: age, given date_of_birth
Express the number of entities to which another
entity can be associated via a relationship set.
Employees
ssn lot
since
name dname
ssn lot did budget Employees
super- subord
Employees Works_In Departments visor inate
Reports_To
since
name dname
ssn lot did budget
Works_In
since
A weak entity can be identified uniquely only by
considering the primary key of another (owner) entity.
◦ Owner entity set and weak entity set must participate in a
one-to-many relationship set (one owner, many weak
entities).
◦ Weak entity set must have total participation in this
identifying relationship set.
name
cost pname age
ssn lot
policyid cost
Previous example illustrated a case when two binary
relationships were better than one ternary
relationship.