Professional Documents
Culture Documents
Database 1
Database 1
Database 1
Examples-1
22/03/2004
3 step design
Conceptual Design
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)
cont..
At least 2 entities participate in a relationship.
(give an example of recursive relationship)
m1
E1
a1
E2
a3
a2
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?
account
Acc#
date
transaction
amount
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
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)
Example-(Relational
model)
CUSTOME
PURCHASE_ORDER
Purchase Cust#
Order
#
PK is (PurchaseOrder#)
CUST_PHONE
Phones
SCus
t#
FK is(Cust#)
STOCK_ITEMS
CONTAINS
Purchase
Order
#
Stock
#
quantit
y
discoun
t
PK is (Cust#,
Phones)
PK is (PurchaseOrder#,
Stock#)
Corresponds to
N-M
Physical Design-DDL
CREATE TABLE Customer (
CustNo
NUMBER NOT NULL,
CustName VARCHAR2(200) NOT NULL,
Street
VARCHAR2(200) NOT NULL,
City
VARCHAR2(200) NOT NULL,
State
CHAR(2) NOT NULL,
Zip
VARCHAR2(20) NOT NULL,
PRIMARY KEY (CustNo)
);
CREATE TABLE Contains (
OrderDate DATE,
ShipDate
ToStreet
ToCity
ToState
StockNo
Stock,
);
NUMBER,
Discount
NUMBER,
DATE,
/* date of order */
/* date to be shipped */
VARCHAR2(200),
*/
ToZip
Quantity
/* purchase order no */
Custno
NUMBER REFERENCES
Customer,
NUMBER,
VARCHAR2(200),
CHAR(2),
VARCHAR2(20),
PRIMARY KEY(PONo)
/* shipto address
cont..
CREATE TABLE Cust_Phones (
CustNo
NUMBER REFERENCES
Customer,
Phones VARCHAR2(20),
PRIMARY KEY (CustNo, Phones)
);
NUMBER,
TaxRate NUMBER
);
cont..
INSERT INTO PurchaseOrder VALUES (1001, 1, SYSDATE, '10MAY-1997',NULL, NULL, NULL, NULL) ;
INSERT INTO PurchaseOrder VALUES (2001, 2, SYSDATE, '20
MAY-1997', '55 Madison Ave', 'Madison', 'WI', '53715') ;
**********************************************
INSERT INTO Contains VALUES( 1001, 1534, 12, 0) ;
INSERT INTO Contains VALUES(1001, 1535, 10, 10) ;
INSERT INTO Contains VALUES(2001, 1004, 1, 0) ;
INSERT INTO Contains VALUES(2001, 1011, 2, 1) ;
**********************************************
NOTE:
You can use bulk loading if the DB has this functionality. Example:
Oracle has SQL*Loader, sqlldr command for bulk loading..
SQL
Q1: Get Customer and Data Item Information for a
Specific Purchase Order
SELECT C.CustNo, C.CustName, C.Street, C.City, C.State, C.Zip,
P.PONo, P.OrderDate,
CO.StockNo, CO.Quantity, CO.Discount
FROM
Customer C, PurchaseOrder P, Contains CO
WHERE C.CustNo = P.CustNo
AND P.PONo = CO.PONo
AND P.PONo = 1001 ;
SQL
Q3: List the Purchase Orders whose total value is greater than
that of a specific Purchase Order.
SELECT
FROM
WHERE
P.PONo
PurchaseOrder P, Contains CO,
P.PONo = CO.PONo
AND CO.StockNo = S.StockNo
AND
CO.Quantity)
Stock S
SUM(S.Price *
FROM
Stock S
Contains CO,
WHERE
CO.PONo = 1001
AND CO.StockNo = S.StockNo
NOTE:
What if >1 customers can have the same PurchaseOrderNumber? Use ANY for a general solution..
SELECT
FROM
WHERE
P.PONo
PurchaseOrder P, Contains CO, Stock S
P.PONo = CO.PONo
AND CO.StockNo = S.StockNo
GROUP BY P.PONo ;
HAVING SUM(S.Price * CO.Quantity) > ALL(SELECT
CO.Quantity)
SUM(S.Price *
FROM
Stock S
WHERE
Contains CO,
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 ;
(Important: The order of commands is important..!!!!)
Delete the
drop table
drop table
drop table
drop table
drop table
database.
Cust_Phones;
Contains;
Stock;
PurchaseOrder;
Customer;
Example-2
area
distance
Pname
Park
Pid
Overlap
area
catch
depth
Lake
Lid
Lname
PARK
Pid
s1
s2
s3
area distanc
Pname
e
I
V
B
150
255
175
52
75
LAKE
Lid
100
200
30
300
45
400
28
PARK_LAKE
Lid
100
200
300
Pid
area
s1
s1
s3
100
255
175
300
RA (relational algebra
operations)
RA is a formal query language and the core of SQL.
Not implemented in commercial DBs.
RA consists of a set of operands (tables) and
operations (select, project, union,crossproduct, difference, intersection)
RA operations
select:
<selection operation>(relation R)
(relation R)
c S : c (R x S)
c:
S)
(t1, Lid=100(ParkLake))
(t2, t1 Park)
Pname (t2)
cont..
cont..
Find the names of Parks that have
b and w as the catch in their
lakes.
(t1, Pname ( catch=b (Lake) ParkLake Park))
(t2, Pname ( catch=w (Lake) ParkLake Park))
t1 t2
cont..
Find Pid of Parks that are 50 km away from
the city where catch is not t.
Pid (
distance>50
(Park)) - Pid (
(Lake)
ParkLake Park)
catch=t