Lec02 Data Models
Lec02 Data Models
1
Data Models
• language / notation for talking about data
• other models:
– key-value pairs: used by NoSQL systems
– graph data model: used by RDF (semi-structured can also do)
– object oriented: often layered on relational, J2EE
2
Relational Model columns /
attributes /
fields
• Data is a collection of relations / tables:
4
Keys
• Key = subset of columns that uniquely identifies tuple
• Another constraint on the table
– no two tuples can have the same values for those columns
• Examples:
– Movie(title, year, length, genre): key is (title, year)
– what is a good key for Company?
• Part of the schema (book notation is underline):
Company(Name: string, Country: string,
Employees: int, For_Profit: boolean)
5
Keys (cont.)
• Can have multiple keys for a table
6
SQL (“sequel”)
• Standard query language for relational data
– used for databases in many different contexts
– inspires query languages for non-relational (e.g. SQL++)
• Everything not in quotes (‘…’) is case insensitive
• Provides standard types. Examples:
– numbers: INT, FLOAT, DECIMAL(p,s)
• DECIMAL(p,s): Exact numerical, precision p, scale s. Example:
decimal(5,2) is a number that has 3 digits before the decimal
and 2 digits after the decimal
– strings: CHAR(n), VARCHAR(n)
• CHAR(n): Fixed-length n
• VARCHAR(n): Variable length. Maximum length n
7
SQL (“sequel”) – Cont.
8
SQL statements
• create table …
• drop table ...
• alter table ... add/remove ...
• insert into ... values ...
• delete from ... where ...
• update ... set ... where ...
9
create table …
CREATE TABLE Company(
name VARCHAR(20) PRIMARY KEY,
country VARCHAR(20),
employees INT,
for_profit CHAR(1));
10
drop table ...
11
alter table ... add/remove ...
12
insert into ... values ...
13
delete from ... where ...
14
update ... set ... where ...
UPDATE Company
SET employees = employees + 120
where name = 'GizmoWorks';
15
Demo on Sqlite
• E.g., type sqlite3 in Cygwin
• .exit - exit from sqlite3
16