Lecture02 Data Models
Lecture02 Data Models
-- Database schema:
-- -- "table name" or "relation name"
-- -- "column name" or "attribute name"
-- -- each attribute has a "type" or "domain"
--
-- Degree (or arity) of relation: nb attributes
--
-- Types or datatypes
--
-- -- Character strings: CHAR(20), VARCHAR(50), TEXT
-- -- Numbers: INT, BIGINT, SMALLINT, FLOAT
-- -- Others: MONEY, DATETIME,
--
-- -- Types are vendor specific
-- -- Types are static and strictly enforced;
-- exception: sqlite has dynamic types
-- http://www.sqlite.org/datatype3.html
--
-- Keys:
-- an attribute is called a "key" if it uniquely identifies a record
--
-- We can have a key with multiple attributes: what does this mean ?
-- It means that each unique combination of values for those
attributes
-- uniquely determines the record.
--
--
-- Comment: upper/lower case; name conflicts
-- -- Company, company, COMPANY = all the same
-- -- Company(cname, country), Person(pname, country) = repeated
'country' OK
-- -- Company(cname, country), Person(pname, company) = the
attribute 'company' not ok
-- Discussion in class:
-- tables are NOT ordered. They represent sets or bags.
-- tables do NOT prescribe how they should be implemented: PHYSICAL
DATA INDEPENDENCE!
-- tables are FLAT (all attributes are base types)
-- Add/Drop attribute(s)
-- let's drop the for_profit attribute:
PRAGMA foreign_keys=ON;
-- If we try:
insert into Product values('MultiTouch2', 199.99, 'photography',
'H2');
-- We should get an error if foreign keys got enforced
-- Error: foreign key constraint failed