No SQLData Modeling
No SQLData Modeling
Mike Carey
UC Irvine
[email protected]
Customer1(custid, name)
...
...
...
...
Customer(custid, name, addrid, rating) Order1(orderno, custid)
...
Address(addrid, street, city, zipcode) Lineitems1(orderno, itemno, qty)
Order(orderno, custid, order_date, ship_date) ...
Products1(itemno, category)
Lineitems(orderno, itemno, qty, price) ...
Products(itemno, category, name, descrip, manuf, listprice) Products5(itemno, listprice)
M. Carey, Spring 2023: CS122D 3
Relational DB Design Theory
Normal Form Description
1NF All attributes must be atomic (a.k.a. scalar)
2NF Eliminates partial dependencies on PK
3NF Eliminates transitive dependencies on PK
BCNF Eliminates “all” remaining redundancy
• Other considerations
• Query performance, joins, space, ....
M. Carey, Spring 2023: CS122D 4
ER-Driven Relational DB Design
ER Concept Relational Artifact
Entity Table with entity’s attributes and PK
Relationship (M:N) Table with relationship’s attributes and FKs
Relationship (1:N) Merge relationship table with N-side entity table
Composite attribute Use flattened column naming convention
Multivalued attribute Add separate side table with entity’s PK as FK
Inheritance Delta tables or mashup table
... ...
address items
item
item
...
customer
product
address
orders
order
items
item
...
order
items
item
... Caution: Is this a
... customer’s whole
order history...?!
M. Carey, Spring 2023: CS122D 9
Some Possible Nesting Designs (II)
• Or even more heavily...
ecommerce
customers
customer products
address product
product
orders ...
order
items
item
...
order
items
item
...
...
product
customer order
item
address
customer vehicle
... ...
vehicles customers
Q: Do most order
... queries also want
the line items?
M. Carey, Spring 2023: CS122D 14
Query (and Indexing) Considerations
SELECT * FROM customers c, orders o WHERE c.custid = o.custid AND ...
SELECT * FROM customers WHERE ... SELECT * FROM products WHERE ...
SELECT * FROM orders o, o.items i WHERE ...
address items
item
CREATE INDEX
item ON products (listprice)
CREATE INDEX ...
ON customers (name)
CREATE INDEX
ON customers (address.city) CREATE INDEX ON orders (shipdate)
CREATE INDEX ON orders (custid)
CREATE INDEX ON orders (items.itemno)
phone_order web_order
...
order Note: An object of a particular
subtype is free to contain
any/all fields that it needs in
phone_order the JSON world...!
Possible ways to indicate the
subtype(s) of an object include:
web_order
• Disjoint subtypes: a “type” field
containing a value of “order”,
web_order “phone_order”, or “web_order”
• Non-disjoint subtypes: a
”types” field containing an array
phone_order of values, or a set of boolean
type indicator fields, such as
“is_order”, “is_phone_order”,
... and “is_web_order”
orders
customer
order
address items
...
item
...
orders
order order
items items
item item
... ...