Lec02 Data Models SQL Basics
Lec02 Data Models SQL Basics
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 ...
• select … from … where
9
create table …
CREATE TABLE Company(
name VARCHAR(20) PRIMARY KEY,
country VARCHAR(20),
employees INT,
for_profit CHAR(1));
10
Multi-column Keys
• This makes name a key:
12
Multi-column Keys (2)
• Likewise for secondary keys:
goes away
CREATE TABLE Company( name
VARCHAR(20) UNIQUE,
country VARCHAR(20),
employees INT,
added
for_profit BOOLEAN,
UNIQUE (name, country));
13
Multi-column Keys (3)
• This makes manufacturer a foreign key:
14
Multi-column Keys (3)
• Similar syntax for foreign keys:
15
UNIQUE
• PRIMARY KEY adds implicit “NOT NULL” constraint
while UNIQUE does not
– you would have to add this explicitly for UNIQUE:
later
drop table ...
17
alter table ... add/remove ...
18
insert into ... values ...
19
One Way to Input Data
• Write a program that outputs SQL statements:
21
Warning
• Be very careful when doing this with strings:
System.out.format(
”INSERT INTO T2 VALUES (%d, ‘%s’);”,
3, ”O’Shaughnessy”);
Becomes:
INSERT INTO T2 VALUES (3, ‘O’Shaughnessy’);
which is a syntax error in this case
22
https://xkcd.com/327/
23
Warning (cont)
• Be very careful when doing this with strings:
System.out.format(
”INSERT INTO T VALUES (%d, ‘%s’);”,
3, ”O’Shaughnessy”);
• This allows a SQL injection attack!
– Must check for quotes and escape (or disallow) them.
– We’ll see safer ways to do this using JDBC
25
update ... set ... where ...
UPDATE Company
SET employees = employees + 120
where name = 'GizmoWorks';
26
select ... from ... where ...
27
DISTINCT and ORDER BY
• Query results do not have to be relations
– i.e., they can have duplicate rows
– remove them using DISTINCT
• Examples in lec03-sql-basics.sql
28
Demo on Sqlite
• E.g., type sqlite3 in Cygwin
• .exit - exit from sqlite3
29
SQLite Uses
• SQLite is just a library
30
Physical Data Independence
• SQL doesn’t specify how data is stored on disk