SQL DDL
SQL DDL
sdu.dk
efterår 2022
Data Definition
#sdudk
Language
Implementation
Richard Brooks
2
Data Science
sdu.dk
efterår 2022
Today’s goal
#sdudk
Implement the database
Setup attribute domains (types and rules)
Create tables with attributes
Define keys and relations
Richard Brooks 2
2
Data Science
sdu.dk
efterår 2022
Start me up!
#sdudk
SET SCHEMA ‘dvdrental’;
Richard Brooks 3
2
Data Science
sdu.dk
efterår 2022
#sdudk
This is our schematic (logical model can also be used)
It shows which tables to implement
Attributes
Keys
References
Relationships can indicate in which order to create the tables
Richard Brooks 4
2
Data Science
sdu.dk
efterår 2022
Goodreads
#sdudk
Richard Brooks 5
2
Data Science
sdu.dk
efterår 2022
IMDB – tv shows
#sdudk
Richard Brooks 6
2
Data Science
sdu.dk
efterår 2022
Web shop
#sdudk
Richard Brooks 7
2
Data Science
sdu.dk
efterår 2022
Agenda
#sdudk
Schema
Table
Keys
Types
Domains
Constraints
Computed columns
Richard Brooks 8
2
Data Science
sdu.dk
efterår 2022
#sdudk
Schema
Richard Brooks
2
Data Science
sdu.dk
efterår 2022
Schema
#sdudk
What was a schema?
It contains:
Tables
Constraints
Views
Triggers
Stuff
Richard Brooks 10
2
Data Science
sdu.dk
efterår 2022
Throws error if schema
already exists
Schema - creating
create schema my_schema;
#sdudk
Does nothing if schema
already exists
Richard Brooks 11
2
Data Science
sdu.dk
efterår 2022
Agenda
#sdudk
Schema
Table
Keys
Types
Domains
Constraints
Computed columns
Richard Brooks 12
2
Data Science
sdu.dk
efterår 2022
#sdudk
Tables
Richard Brooks
2
Data Science
sdu.dk
efterår 2022
Table - creating
#sdudk
Where to start?
Richard Brooks 14
2
Data Science
sdu.dk
efterår 2022
#sdudk
create table profile ( key
Richard Brooks 15
2
Data Science
sdu.dk
efterår 2022
#sdudk
d integer primary key,
sbn varchar(13),
itle varchar(200),
page_count smallint, Foreign
year_published smallint, keys
binding_id smallint references binding_type(id),
publisher_id smallint references publisher(id),
author_id smallint references author(author_id) Possible, not
recommendeed
Richard Brooks 16
2
Data Science
sdu.dk
efterår 2022
#sdudk
profile_id SMALLINT,
book_id INTEGER,
rating SMALLINT,
date_started DATE, Can define
date_finished DATE, here
status VARCHAR(7),
PRIMARY KEY (profile_id, book_id),
FOREIGN KEY (profile_id) REFERENCES profile(id),
FOREIGN KEY (book_id) REFERENCES book(id)
Recommend defining all
keys like this, not just Can define
composite here
Richard Brooks 17
2
Data Science
sdu.dk
efterår 2022
#sdudk
A profile is referenced by book_read
Default behaviour:
You cannot delete something, which is being referenced
Richard Brooks 18
2
Data Science
sdu.dk
efterår 2022
#sdudk
ON DELETE behaviour
What should happen, when you try to delete something?
Richard Brooks
2
TE TABLE book (
Data Science
NTEGER PRIMARY KEY,
sdu.dk
efterår 2022
n VARCHAR(15),
e VARCHAR(200),
ge_count SMALLINT,
#sdudk
ar_published SMALLINT,
ding_id SMALLINT REFERENCES binding_type(id) on delete restrict,
blisher_id SMALLINT REFERENCES publisher(id) on delete set null,
hor_id SMALLINT REFERENCES author(author_id) on delete cascade
Richard Brooks 20
2
TE TABLE book (
Data Science
NTEGER PRIMARY KEY,
sdu.dk
efterår 2022
n VARCHAR(15),
e VARCHAR(200),
ge_count SMALLINT,
#sdudk
ar_published SMALLINT,
ding_id SMALLINT REFERENCES binding_type(id) on delete restrict,
blisher_id SMALLINT REFERENCES publisher(id) on delete set null,
hor_id SMALLINT REFERENCES author(author_id) on delete cascade
Richard Brooks 21
2
TE TABLE book (
Data Science
NTEGER PRIMARY KEY, This
sdu.dk
efterår 2022
i s p ot
n VARCHAR(15), pretty en t i a
d a n ge r o l yl
e VARCHAR(200),
ge_count SMALLINT,
u s!
#sdudk
ar_published SMALLINT,
ding_id SMALLINT REFERENCES binding_type(id) on delete restrict,
blisher_id SMALLINT REFERENCES publisher(id) on delete set null,
hor_id SMALLINT REFERENCES author(author_id) on delete cascade
Richard Brooks 22
2
Data Science
sdu.dk
efterår 2022
#sdudk
Types
Richard Brooks
2
Data Science
sdu.dk
efterår 2022
#sdudk
Boolean BOOLEAN
character CHAR VARCHAR
exact number NUMERIC DECIMAL INTEGER
approx. number FLOAT REAL DOUBLE PRECISION
date/time DATE TIME TIMESTAMP
interval INTERVAL
Richard Brooks 24
2
Data Science
sdu.dk
efterår 2022
String types
#sdudk
CHAR(n) - is a string of exactly n characters
CHAR(4) allows ‘abcd’, but ‘abc’ becomes ‘abc ‘ (suffixed with whitespace)
Richard Brooks 25
2
Data Science
sdu.dk
efterår 2022
Exact numbers
#sdudk
INTEGER – min/max ±2147483647
SMALLINT – min/max ±32767
BIGINT – min/max ±9223372036854775807
https://www.postgresql.org/docs/current/datatype-numeric.html
Richard Brooks 26
2
Data Science
sdu.dk
efterår 2022
Date/time
#sdudk
DATE – holds a date (without time) year, month, day
TIME – holds a time (without date) hour, minute, second
TIMESTAMP – date and time
https://www.postgresql.org/docs/current/datatype-datetime.html
Richard Brooks 27
2
bigint int8 signed eight-byte integer
sdu.dk
bit varying [ (n) ] varbit [ (n) ] variable-length bit string efterår 2022
boolean bool logical Boolean (true/false)
cidr
char [ (n) ]
varchar [ (n) ]
fixed-length character string
#sdudk
date calendar date (year, month, day)
time [ (p) ] [ without time zone ] time of day (no time zone)
time [ (p) ] with time zone timetz time of day, including time zone
timestamp [ (p) ] [ without time zone ] date and time (no time zone)
tsquery
timestamptz date and time, including time zone
2
Data Science
sdu.dk
efterår 2022
Agenda
#sdudk
Schema
Table
Keys
Types
Domains
Constraints
Computed columns
Richard Brooks 29
2
Data Science
sdu.dk
efterår 2022
#sdudk
Domains
Creating your own types
Richard Brooks
2
comment
Data Science
sdu.dk
efterår 2022
id
body
Foreign key types must match the writer {FK}
type of the referenced attribute, post {FK}
#sdudk
here: user_name
post
id
body
writer {FK}
Richard Brooks 31
sub_forum {FK}
2
Data Science
sdu.dk
efterår 2022
Domains
#sdudk
Good to create your own types to be used several places.
Reduces copying things
Richard Brooks 32
2
Data Science
sdu.dk
efterår 2022
#sdudk
Sometimes you use the same concept over and over, and need to make sure it’s consistent
Total of 12 digits. 2 of
them after the decimal
separator.
Example:
CREATE DOMAIN salary AS DECIMAL(12,2);
Salary is set to max 10 digits, and 2 decimals
The CHECK is a constraint, data might be rejected, and result in an error
sdu.dk
efterår 2022
#sdudk
create domain isbn_number as varchar(15);
create domain book_title as varchar(200);
Richard Brooks 34
2
Data Science
sdu.dk
efterår 2022
Agenda
#sdudk
Schema
Table
Keys
Types
Domains
Constraints
Computed columns
Richard Brooks 35
2
Data Science
sdu.dk
efterår 2022
Agenda
#sdudk
Schema
Table
Keys
Types
Domains
Constraints
Computed columns
Richard Brooks 36
2
Data Science
sdu.dk
efterår 2022
#sdudk
Constraints
Validation rules
Richard Brooks
2
Data Science
sdu.dk
efterår 2022
Constraints
#sdudk
You can add constraints to attributes and domains
If a value is inserted, which violates the constraint, it is rejected, and you get an error.
Richard Brooks 38
2
Data Science
sdu.dk
efterår 2022
Constraints
#sdudk
ate table book (
d integer primary key, Value cannot be
null Value must be
sbn isbn_number,
larger than 1900
itle book_title(200),
age_count smallint not null,
ear_published smallint check(year_published >= 1900)
sdu.dk
efterår 2022
#sdudk
REATE TABLE book_read(
profile_id SMALLINT,
book_id INTEGER,
rating SMALLINT,
date_started DATE,
date_finished DATE check (date_started <= date_finished),
Richard Brooks 40
2
Data Science
sdu.dk
efterår 2022
#sdudk
Dreamhouse
Richard Brooks
2
Data Science
First domains
sdu.dk
efterår 2022
#sdudk
TE DOMAIN BranchNumber AS CHAR(4) CHECK (VALUE IN (SELECT branchNo FROM Branch));
TE DOMAIN PropertyNumber AS VARCHAR(5);
TE DOMAIN Street AS VARCHAR(25);
TE DOMAIN City AS VARCHAR(15);
TE DOMAIN Postcode AS VARCHAR(8);
TE DOMAIN PropertyType AS CHAR(1) CHECK (VALUE IN (‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘M’, ‘S’));
TE DOMAIN PropertyRooms AS SMALLINT CHECK (VALUE BETWEEN 1 AND 15);
TE DOMAIN PropertyRent AS DECIMAL(6, 2) CHECK (VALUE BETWEEN 0 AND 9999.99);
TE TABLE PropertyForRent
sdu.dk
efterår 2022
Table attributes
(
propertyNo PropertyNumber NOT NULL,
street Street NOT NULL,
city City NOT NULL,
#sdudk
postcode PostCode,
type PropertyType NOT NULL DEFAULT ‘F’,
rooms PropertyRooms NOT NULL DEFAULT 4,
rent PropertyRent NOT NULL DEFAULT 600,
ownerNo OwnerNumber NOT NULL,
staffNo StaffNumber
This here makes sure that
CONSTRAINT StaffNotHandlingTooMuch no staff-member is
CHECK (NOT EXISTS( responsible for more than
SELECT staffNo 100 properties.
FROM PropertyForRent
GROUP BY staffNo
HAVING COUNT(*) > 100)),
branchNo BranchNumber NOT NULL,
PRIMARY KEY (propertyNo),
FOREIGN
RichardKEY (staffNo) REFERENCES Staff ON DELETE SET NULL ON UPDATE
Brooks 43
2
CASCADE,
ffNo StaffNumber
CONSTRAINT StaffNotHandlingTooMuch
Data Science Keys
CHECK (NOT EXISTS(
sdu.dk
efterår 2022
SELECT staffNo
FROM PropertyForRent
GROUP BY staffNo ”update”: when the referenced
HAVING COUNT(*) > 100)), key is changed, this will
#sdudk
nchNo BranchNumber NOT NULL, attribute be changed
MARY KEY (propertyNo),
REIGN KEY (staffNo) REFERENCES Staff ON DELETE SET NULL ON UPDATE CASCADE,
REIGN KEY (ownerNo) REFERENCES PrivateOwner ON DELETE NO ACTION ON UPDATE CASCADE,
REIGN KEY (branchNo) REFERENCES Branch ON DELETE NO ACTION ON UPDATE CASCADE
Richard Brooks 44
2
Data Science
sdu.dk
efterår 2022
Agenda
#sdudk
Schema
Table
Keys
Types
Domains
Constraints
Computed columns
Richard Brooks 45
2
Data Science
sdu.dk
efterår 2022
Computed columns
#sdudk
Can be used to handle some derived attributes.
Read more here PostgreSQL: Documentation: 14: 5.3. Generated Columns
Richard Brooks 46
2
Data Science
sdu.dk
efterår 2022
Agenda
#sdudk
Schema
Table
Keys
Types
Tak for i dag!
Domains
Constraints
Computed columns
Richard Brooks 47
2