Database 1
Database 1
22/03/2004
3 step design
Conceptual Design
Highest level design
Issues: data types, relationships, constraints
Uses ER model
Logical Design
Implementation of conceptual model
3 ways: hierarchical, network, relational
Apply relational model
Uses RA (relational algebra) as a formal query language
Physical Design
Actual computer implementation
Issues: mem. manag., storage, indexing
ER model
The most popular conceptual data modeling technique.
(Give an example of other conceptual design tool?)
Integrates with “relational model”.
The scenario is partitioned into “entities” which are
characterized with “attributes” and interrelated via
“relationships”. “Entity set” is a set of entities of
the same type.
Entity is an independent conceptual existence in the
scenario.
An attribute (or a set of attributes) that uniquely
identifies instance of an entity is called the key.
1-) Super key 2-) candidate key (minimal
superkey) 3-) primary key (candidate key chosen by DBA)
An attribute can be single-valued or multi-valued.
cont..
At least 2 entities participate in a relationship.
(give an example of recursive relationship)
Binary relationship, ternary relationship…
(give an example of ternary relationship)
Cardinality constraints: 1-1, 1-N, N-M
Relationships may have their own attributes
Example of an ER diagram:
E1 R E2
m1 1 N d
a3
a1 Can we migrate a3? a2
IF we can, to where?
cont..
Weak Entity set: An entity set that does not have enough
attributes to form a primary key.
Think of Transaction entity set(transaction#, date, amount) assuming
that different accounts might have similar transactions.
We need a strong entity set (owner set) in order to
distinguish the entities of weak entity set.
Question: Is there a way to represent this kind of scenario
without using another entity set?
date
account R transaction
1 N amount
Acc# Tran#
Relational Model
Data representation model introduced by Codd,
1970.
E-R RM
Relation Table
Attributes Columns
Table is an unordered collection of tuples(rows).
Degree of a relation is the # of columns.
Data types of attributes: DOMAINS
int, float, character,date, large_object (lob),
user-defined data types(only for ORDBs)
cont..
Logical consistency of data is ensured by
certain constraints:
key :: every relation must have PK key
entity integrity :: no PK can be NULL
referential integrity :: value of attributes of the foreign
key either must appear as a value in PK of another table (or the
same table, give an example) or must be null.
Definitions:
PrimaryKey is chosen among the candidate
key by DBA.
ForeignKey is set of attributes in a relation
which is duplicated in another relation.
ERRM Rules
S/w packages (CASE tools) such as Erwin, Oracle
Designer 2000, Rational Rose can translate ER to
RM.
4 steps for transformation:
1.) Map each entity set in ER into a separate table in RM
(Also, map the attributes, and PK)
2.) Weak entity set with attributes (a1,..an) and owner set
attributes (b1,b2,..bm): MAP it to a table with {(a1,..an) U
(b1,b2,..bm)} attributes. (b1,b2,..bm) becomes the foreign
key. {(a1,..an) U discriminator} becomes the PK.
cont..
3.) Binary Relationship S between R1 and R2 entity
sets. Assume (a1,a2,…an) is the attributes of S.
If cardinality is 1-1: Chose either relations( say S)
and extend it with {PK(T) U (a1,a2,…an)}
If cardinality is 1-N: Chose N-side relation( say S)
and extend it with {PK(T) U (a1,a2,…an)}
If cardinality is N-M: Represent it with a new
relation with PK(T) U PK(S) U {(a1,a2,…an)}
4.) Multivalued Attribute ‘A’ of entity set R is
represented with a new relation with {A U PK(S)}.
What is the PK of new table?
Example 1- (3-step design,SQL)
PK is (PurchaseOrder#)
Corresponds to 1-N relationship
FK is(Cust#)
CUST_PHONES
Cust Phones CONTAINS STOCK_ITEMS
# Stock# Price TaxRate
Purchase Stock# quantity discount
Order#
PK is (Cust#, Phones)
PK is (PurchaseOrder#, Stock#) Corresponds to N-
M relationship
Physical Design-DDL
CREATE TABLE Customer ( CREATE TABLE PurchaseOrder (
CustNo NUMBER NOT NULL, PONo NUMBER, /* purchase order no */
SELECT P.PONo
FROM PurchaseOrder P, Contains CO, Stock S
WHERE P.PONo = CO.PONo
AND CO.StockNo = S.StockNo
GROUP BY P.PONo ;
HAVING SUM(S.Price * CO.Quantity) > ALL(SELECT SUM(S.Price * CO.Quantity)
FROM Contains CO, Stock S
WHERE CO.PONo = 1001
AND CO.StockNo = S.StockNo)
SQL
Q4: Find the Purchase Order that has the
maximum total value.
CREATE VIEW X(Purchase,Total) AS
SELECT P.PONo, SUM(S.Price * CO.Quantity)
FROM PurchaseOrder P, Contains CO, Stock S
WHERE P.PONo = CO.PONo
AND CO.StockNo = S.StockNo
GROUP BY P.PONO
---------------------------
SELECT P.PONo
FROM X
GROUP BY P.PONo ;
HAVING Total =( SELECT max(Total)
FROM X)
DML
Delete Purchase Order 1001
DELETE
FROM Contains
WHERE PONo = 1001 ;
DELETE
FROM PurchaseOrder
WHERE PONo = 1001 ;
area catch
distance depth
Park Overlap Lake
Pname M area
N Lname
Pid Lid
Relations for Parking DB
PARK LAKE PARK_LAKE
Pid Pname area distance
Lid Lname catch depth Lid Pid area
s1 I 150 52 100 L w 20 100 s1 100
200 C t 30 200 s1 255
s2 V 255 75 300 S v 45 300 s3 175
400 T b 28
s3 B 175 300