Database Basic Concepts
Database Basic Concepts
Database Basic Concepts
Relational Schema
• Logical Design
– Based upon the conceptual data model
– Four key steps
1. Develop a logical data model for each known user
interface for the application using normalization
principles.
2. Combine normalized data requirements from all user
interfaces into one consolidated logical database model
3. Translate the conceptual E-R data model for the
application into normalized data requirements
4. Compare the consolidated logical database design with
9.6 the translated E-R model and produce one final logical
database model for the application
9.7
Relational Database Model
• Data represented as a set of related tables or
relations
• Relation
– A named, two-dimensional table of data. Each
relation consists of a set of named columns and
an arbitrary number of unnamed rows
– Properties
• Entries in cells are simple
• Entries in columns are from the same set of values
• Each row is unique
• The sequence of columns can be interchanged without
changing the meaning or use of the relation
• The rows may be interchanged or stored in any
9.8
sequence
Relational Database Model
• Well-Structured Relation
– A relation that contains a minimum
amount of redundancy and allows users
to insert, modify and delete the rows
without errors or inconsistencies
9.9
Transforming E-R Diagrams into
Relations
9.10
Refining the ER Design for the
COMPANY Database
16
Attributes
17
Figure 4.6 - Depiction of a
Derived Attribute
18
Attribute types:
– Derived attributes
• Can be computed from other attributes
• E.g. age, given date of birth
Multivalued Attributes
• Normalization Required to
First Nomal Form
• A multivalued attribute M of an entity E
is represented by a separate table EM
– Table EM has attributes corresponding to
the primary key of E and an attribute
corresponding to multivalued attribute M
Translation to
relational model?
Represent Relationships
– Binary 1:N Relationships
• Add the Primary key attribute (or attributes) of
the entity on the one side of the relationship as a
Foreign key in the relation on the other (N) side
• The one side migrates to the many side
9.31
Constraints on Binary
Relationship Types
Cardinality ratio for a binary relationship
Specifies maximum number of relationship
instances that entity can participate in
Participation Constraint
Specifies whether existence of entity depends
on its being related to another entity
Types: total and partial
Attributes of Relationship
Types
Attributes of 1:1 or 1:N relationship types
can be migrated to one entity type
For a 1:N relationship type
Relationship attribute can be migrated only to
entity type on N-side of relationship
Identifying relationship
Relates a weak entity type to its owner
9.37
Binary 1 To 1 Relationship
Employee(Manager) MANAGES Department
EMPLOYEE
FNAME MINIT LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO
DEPARTMENT
DNAME DNUMBER MGRSSN MGRSTARTDATE
Headquarters 1 888665555 19-Jun-71
Administration 4 987654321 01-Jan-85
Research 5 333445555 22-May-78
Automation 7 123456789 06-Oct-05
Transforming E-R Diagrams into
Relations
Represent Relationships
– Binary and higher M:N relationships
• Create another relation and include
primary keys of all relations as primary
key of new relation
9.39
9.40
Association Class to Table
ORDER LINE
61384 M128 2
61384 A261 1
62807 A261 2
62807 R149 1
62009 R149 2
Transforming Binary M:N
Relationships into Relations
• Relationship Requests: CREATE TABLE ORDERLINE (
Order Requests Products Order_Number CHAR(10),
1. Create Table ORDERLINE Product_ID CHAR(10),
for Relationship Requests Quantity_Ordered Integer,
2. Add PK of each side of PRIMARY KEY (Order_Number,
Tables ( Order_Number, Product_ID),
Product_ID) as Foreign Keys FOREIGN KEY (Order_Numer)
3. Make composite of both REFERENCES
attributes as Primary Key of ORDER(Order_Number),
the Table ORDERLINE : FOREIGN KEY (Product_ID)
REFERENCES
ORDERLINE(Order_Number,
PRODUCT(Product_ID));
Product_ID,
Quantity_Ordered)
Transforming E-R Diagrams into
Relations
– Unary 1:N Relationships
• Relationship between instances of a single entity type
• Utilize a recursive foreign key
– A foreign key in a relation that references the primary key
values of that same relation
9.43
9.44
Unary 1 to M to Table:
MANAGES
• 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 (1 owner, many weak entities).
– Weak entity set must have total participation in this identifying
relationship set.
name
cost dname age
ssn lot
name
ssn lot
Employees
CREATE TABLE Employees
(ssn CHAR(11),
name CHAR(20),
lot INTEGER,
PRIMARY KEY (ssn))
Review: Key Constraints
since
• Each dept has at most name dname
Translation to
relational model?
since
name dname
ssn lot did budget
Works_In
since
Translating ER Diagrams with Key Constraints
since
name dname
ssn lot did budget
Works_In
since
Relationship Sets to Tables
• In translating a relationship CREATE TABLE Works_In(
Works_In (M-N) to a ssn CHAR(1),
did INTEGER,
relation, attributes of the since DATE,
relation must include: PRIMARY KEY (ssn, did),
FOREIGN KEY (ssn)
– Keys for each REFERENCES Employees,
participating entity set FOREIGN KEY (did)
REFERENCES Departments)
(as foreign keys).
– This set of attributes
forms a superkey for the
relation.
– All descriptive attributes.
Review: Participation Constraints
since
name dname
ssn lot did budget
Works_In
since
Participation Constraints in
SQL
• We can capture participation constraints
involving one entity set in a binary
relationship, but little else (without
resorting to CHECK constraints).
CREATE TABLE Dept_Mgr(
did INTEGER,
dname CHAR(20),
budget REAL,
ssn CHAR(11) NOT NULL,
since DATE,
PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES Employees,
ON DELETE NO ACTION)
Review: Binary vs. Ternary
Relationships
name
ssn lot dname age
• If each policy is Employees Covers Dependents
owned by just 1
employee:
Bad design Policies