Data Engineering
Updates
1
It’s not sufficient to query…
2
Inserting One or More Tuples
INSERT INTO Relation VALUES (<list of values>);
Example:
(5001, ‘Hispanic’, 37, ‘Rockridge'),
(5002, ‘White’, 68, ‘MacArthur');
3
Specifying Attributes in Insert
INSERT INTO Stops VALUES (5000, ‘Asian’, 24, ‘West Oakland’);
INSERT INTO Stops (id, race, age, location) VALUES
(5000, ‘Asian’, 24, ‘West Oakland’);
4
Inserting Many Tuples
INSERT INTO Relation (<Subquery>);
INSERT INTO Zips(location) VALUES (
SELECT DISTINCT S.location
FROM Stops S
WHERE NOT EXISTS
(SELECT * FROM Zips Z WHERE S.location = Z.location));
5
Deleting Tuples
To delete tuples satisfying a condition from some relation:
DELETE FROM Relation WHERE <condition>;
Stops (id, race, age, location)
Remove all juvenile stops
DELETE FROM Stops WHERE age <18;
To delete all tuples from a relation simply say:
DELETE FROM Stops;
6
Updates
To change certain attributes of certain tuples in a relation:
UPDATE Relation
SET <list of attribute assignments>
WHERE <condition>
7
Example: Update
Stops (id, race, age, location)
Set NULL ages to be 18:
UPDATE Stops
SET age = 18
WHERE age IS NULL;
8
A Tricky Issue of Semantics
UPDATE Employee SET salary = salary * 1.2
WHERE title= ‘Database Designer’ AND salary <100000
9
A Tricky Issue of Semantics
UPDATE Employee SET salary = salary * 1.2
WHERE title= ‘Database Designer’ AND salary <100000
10
A Tricky Issue of Semantics
UPDATE Employee SET Salary = Salary * 1.2
WHERE Title= ‘Database Designer’ AND Salary <100000
How do we deal with this issue?
11
Data Engineering
Relational Design
12
So far…
13
Defining a Relation
CREATE TABLE <name> (
<list of attributes and domains>
);
Recall that we created a table based on a subquery previously; this allows us to create an unrelated table with an entirely new schema
DROP TABLE <name>;
14
List of Attributes and Domains
15
Example
CREATE TABLE Zips (
location VARCHAR(20),
zipcode INTEGER
);
16
The Notion of Keys
17
Example Primary Key Usage
CREATE TABLE Stops(
stopID INTEGER, race VARCHAR(10), location VARCHAR(20),
age INTEGER, arrest BOOLEAN,
PRIMARY KEY (stopID)
);
CREATE TABLE Zips (
location VARCHAR(20),zipcode INTEGER, area REAL,
PRIMARY KEY (location, zipcode)
);
Q: How do we interpret this?
A: combo of location and zipcode is unique across Zips tuples
18
UNIQUE vs. PRIMARY KEY
19
Example Unique Usage
CREATE TABLE Stops(
stopID INTEGER, personID INTEGER, stopTime TIMESTAMP,
race VARCHAR(10), location VARCHAR(20),
age INTEGER, arrest BOOLEAN,
PRIMARY KEY (stopID),
UNIQUE (personID, stopTime)
);
20
Other Declarations
21
Example Usage of Declarations
CREATE TABLE Stops(
stopID INTEGER, personID INTEGER, stopTime TIMESTAMP,
race VARCHAR(10), location VARCHAR(20) NOT NULL,
age INTEGER, arrest BOOLEAN DEFAULT False,
PRIMARY KEY (stopID),
UNIQUE (personID, stopTime)
);
22
Adding Attributes
ALTER TABLE <name>
ADD <attribute declaration>,
ADD <attribute declaration>, …;
ALTER TABLE Zips
ADD area REAL,
ADD timezone VARCHAR(5);
23
Deleting Attributes
ALTER TABLE <name>
DROP <attribute>,
DROP <attribute>, …;
ALTER TABLE Zips
DROP area,
DROP timezone;
24
Recap
Kinds of Constraints
26
Example
27
person_id | movie_id |
1 | 23 |
3 | 45 |
id | name |
1 | Ingrid Bergman |
3 | Samuel L Jackson |
id | title |
23 | Casablanca |
45 | Snakes on a plane |
Declaring a FK Constraint
Using REFERENCES
CREATE TABLE Actor (id INTEGER, name TEXT, PRIMARY KEY actor_id);
CREATE TABLE Cast_info (person_id INTEGER, movie_id INTEGER,
FOREIGN KEY (person_id) REFERENCES Actor (id),
FOREIGN KEY (movie_id) REFERENCES Movie (id));
Or
CREATE TABLE Cast_info (
person_id INTEGER REFERENCES Actor (id),
movie_id INTEGER REFERENCES Movie (id));
Less verbose but doesn’t support multi-attribute FK constraint
28
person_id | movie_id |
1 | 23 |
3 | 45 |
id | name |
1 | Ingrid Bergman |
3 | Samuel L Jackson |
id | title |
23 | Casablanca |
45 | Snakes on a plane |
Enforcing Foreign Key Constraints
R
S
person_id | movie_id |
1 | 23 |
3 | 45 |
id | name |
1 | Ingrid Bergman |
3 | Samuel L Jackson |
id | title |
23 | Casablanca |
45 | Snakes on a plane |
Enforcing Foreign Key Constraints
30
Enforcing Foreign Key Constraints
31
person_id | movie_id |
1 | 23 |
3 | 45 |
3 | 76 |
id | name |
1 | Ingrid Bergman |
3 | Samuel L Jackson |
Example
CREATE TABLE Cast_info (person_id INTEGER, movie_id INTEGER,
FOREIGN KEY (person_id) REFERENCES Actor (id)
ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY (movie_id) REFERENCES Movie (id)
ON DELETE SET NULL);
Q: what if we
32
person_id | movie_id |
1 | 23 |
3 | 45 |
id | name |
1 | Ingrid Bergman |
3 | Samuel L Jackson |
id | title |
23 | Casablanca |
45 | Snakes on a plane |
Attribute Based Constraints
CREATE TABLE Actor (
id INTEGER CHECK (id >= 1000 AND id <= 9999),
name TEXT);
(EXISTS (SELECT * FROM Actor A WHERE person_id = A.id)), …
33
Kinds of Constraints
34
Why Constraints?
Triggers, Briefly
Quick Demo
INSERT INTO Actor VALUES (5000, 'Adam', 'Driver', 'Feb 3, 2020’);
INSERT INTO Actor VALUES (5001, 'Brenda', 'Driver', 'Feb 3, 2020'), (5002, 'Charles', 'Driver', 'Feb 3, 2020’);
INSERT INTO Actor (actor_id, first_name, last_name, last_update) VALUES (5003, 'Didi', 'Driver', 'Feb 3, 2020');
UPDATE Actor
SET actor_id = 6000
WHERE first_name = ‘Adam’ AND last_name = ‘Driver’
DELETE FROM Actor WHERE last_name = ‘Driver';
37
Quick Demo
CREATE TABLE manager (manager_name VARCHAR(20), age INTEGER DEFAULT 50, manager_id CHAR(4), PRIMARY KEY (manager_id), UNIQUE (manager_name));
INSERT INTO Manager (age, manager_id)
VALUES (23, ‘sd45’);
INSERT INTO Manager (manager_name, age)
VALUES ('John Smith', 123);
ALTER TABLE Manager
ADD address VARCHAR (20),
ADD income REAL DEFAULT 10000.0;
ALTER TABLE Manager DROP address, DROP income;
DROP TABLE Manager;
38