CS2102: Database Systems - Adi Yoga Sidi Prabawa 1 / 73
CS2102: Database Systems - Adi Yoga Sidi Prabawa 1 / 73
Keep
dangling
Keep
dangling
Keep Keep
dangling dangling
Basic DDL
Basic DML Alter and Drop
First standard is SQL-86; the most recent standard is SQL-2019 (new standard every ~3-5 years)
New standards introduce new language concepts (e.g., support new features of RDBMS)
Many RDBMS add their own " avor" to SQL
Application is a mixture of host language (e.g., C, Java, Python) and SQL statements
Example: Embedded SQL, Dynamic SQL
psql pgAdmin
Table "Employees"
CREATE TABLE Employees (
id INTEGER, id name age role
name VARCHAR(50),
age INTEGER,
role VARCHAR(50)
);
✱
Remember, we have three-valued logic!
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 24 / 73
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 25 / 73
Reject the insertion if the condition evaluates to False.
Used in integrity constraints
Choice Comment
A Row 1
B Row 2
C Row 3
D Row 4
Choice Comment
A Row 1
B Row 2
C Row 3
D Row 4
Requirement:
Each foreign key in R1 must satisfy one of the following:
Appear as primary key in R2
Be a NULL value (or a tuple containing at least one NULL value)
Table "Cast"
id name dob
20 Sigourney Weaver 08-10-1949
21 Hugh Jackman 12-10-1968
22 Tom Hanks 09-07-1956
23 Bill Paxton 17-05-1955
Updates the foreign key of the referencing tuples to NULL value (Important:
SET NULL
corresponding column must be allowed to contain NULL values)
R(A, B)
S(C, D)
Consider the foreign key constraints (S.C, S.D) ⇝ (R.A, R.B). Which
rows violate the FOREIGN KEY constraint?
Choice Comment
A Row 1
B Row 2
C Row 3
D Row 4
⋕
Is this good enough? Can you do better?
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 49 / 73
CREATE ASSERTION statement (since SQL-92)
Used for (almost) arbitrary constraints (multiple table, multiple rows)
Example: "Each project must have at least one team member being 30 or older"
Primary Key PRIMARY KEY PRIMARY KEY(A1,A2,...) UNIQUE & NOT NULL
FOREIGN KEY (A1,A2,...) The tuple exists in R1
Foreign Key REFERENCES R1(B)
REFERENCES R1(B1,B2,...) or the tuple contains NULL value
General CHECK (c) CHECK (c) Condition c does not evaluate to False
Common Changes:
Adding/dropping columns
Adding/dropping constraints
Changing the speci cation of a single column:
Data type
Default values
⋕
https://www.postgresql.org/docs/14/sql-altertable.html
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 55 / 73
DROP TABLE
[IF EXISTS] -- no error if table does not exist
<table_name>[, <table_name> [, <table_name> [...]]] -- multiple table
[CASCADE]; -- also delete referencing table
ALTER TABLE Employee ADD CONSTRAINT ALTER TABLE Department ADD CONSTRAINT
did_fk FOREIGN KEY (did) eid_fk FOREIGN KEY (eid)
REFERENCES Department (did); REFERENCES Employee (eid);
ALTER TABLE Employee ADD CONSTRAINT ALTER TABLE Department ADD CONSTRAINT
did_fk FOREIGN KEY (did) eid_fk FOREIGN KEY (eid)
REFERENCES Department (did) REFERENCES Employee (eid)
NOT DEFERRABLE; NOT DEFERRABLE;
ALTER TABLE Employee ADD CONSTRAINT ALTER TABLE Department ADD CONSTRAINT
did_fk FOREIGN KEY (did) eid_fk FOREIGN KEY (eid)
REFERENCES Department (did) REFERENCES Employee (eid)
DEFERRABLE INITIALLY DEFERRED; DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE Employee ADD CONSTRAINT ALTER TABLE Department ADD CONSTRAINT
did_fk FOREIGN KEY (did) eid_fk FOREIGN KEY (eid)
REFERENCES Department (did) REFERENCES Employee (eid)
DEFERRABLE INITIALLY IMMEDIATE; DEFERRABLE INITIALLY DEFERRED;
UPDATE <table_name>
SET <attr> = <value> [, <attr> = <value>]
[WHERE <condition>];
Primary Key PRIMARY KEY PRIMARY KEY(A1,A2,...) UNIQUE & NOT NULL
FOREIGN KEY (A1,A2,...) The tuple exists in R1
Foreign Key REFERENCES R1(B)
REFERENCES R1(B1,B2,...) or the tuple contains NULL value
General CHECK (c) CHECK (c) Condition c does not evaluate to False