0% found this document useful (0 votes)
14 views47 pages

SQL DDL

The document outlines the goals and agenda for implementing a database, including creating tables, defining keys, and setting up attribute domains. It provides examples of SQL commands for creating schemas and tables, as well as defining data types and constraints. Additionally, it emphasizes the importance of relationships between tables and the behavior of foreign keys during deletions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views47 pages

SQL DDL

The document outlines the goals and agenda for implementing a database, including creating tables, defining keys, and setting up attribute domains. It provides examples of SQL commands for creating schemas and tables, as well as defining data types and constraints. Additionally, it emphasizes the importance of relationships between tables and the behavior of foreign keys during deletions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Data Science

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’;

1. What is the actor_id of Scarlett Benning?


2. What is the average length of all the films in the dvdrental database?
3. What is the name of the customer who has spent the most amount on rentals and
how much has that person spent in total?
4. How many actors do not have a unique last name?

Richard Brooks 3
2
Data Science

sdu.dk
efterår 2022

Global Relations diagrams

#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

create schema if not exists my_schema;


Sets the schema. Everything executed
afterwards is done against this
schema
set schema ‘my_schema’;

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?

Use the arrows (FK directions)


Start with tables, which do not point to
anything.

Richard Brooks 14
2
Data Science

sdu.dk
efterår 2022

Table - creating Table name


{PK}
This attribute is primary

#sdudk
create table profile ( key

id serial primary key, serial means an auto-


generated, auto-
first_name varchar(50), Good
incrementing integer.
for surrogate keys.
last_name varchar(50),
profile_name varchar(25)
);
Varchar(x) is your basic
string. Allows up to x
characters.

Richard Brooks 15
2
Data Science

sdu.dk
efterår 2022

Table - creating Normal integer

EATE TABLE book (

#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

Define composite key?


REATE TABLE book_read(

#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

What happens if a profile


is removed?

#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

When you try to delete a row in ”binding_type”, which is being


referenced, the database will not allow it. (Default behaviour)

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

Deleting a publisher will just set the FK of book to ‘null’

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

Deleting an author will also delete all rows, which


references that author

Richard Brooks 22
2
Data Science

sdu.dk
efterår 2022

#sdudk
Types

Richard Brooks
2
Data Science

sdu.dk
efterår 2022

Data type Declarations

#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)

VARCHAR(n) – is a string of max n characters


VARCHAR(4) allows both ‘abcd’ and ‘abc’, but not ‘abcde’
Be generous with the size of the type – a lot of names fit in VARCHAR(40), but some don’t.
Extend to be sure all fits, e.g. VARCHAR(100)

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

DECIMAL [precision [, scale] ]  [total number of digits, decimals]


DECIMAL (7,2)  range: [-99999.99 , 99999.99]
DECIMAL (5)  max: 99999

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

bigserial serial8 autoincrementing eight-byte integer

bit [ (n) ] fixed-length bit string


Data Science

sdu.dk
bit varying [ (n) ] varbit [ (n) ] variable-length bit string efterår 2022
boolean bool logical Boolean (true/false)

box rectangular box on a plane

bytea binary data ("byte array")

More types character [ (n) ]

character varying [ (n) ]

cidr
char [ (n) ]

varchar [ (n) ]
fixed-length character string

variable-length character string

IPv4 or IPv6 network address

circle circle on a plane

#sdudk
date calendar date (year, month, day)

double precision floating-point number


double precision float8
(8 bytes)

inet IPv4 or IPv6 host address

integer int, int4 signed four-byte integer

interval [ fields ] [ (p) ] time span

json textual JSON data

jsonb binary JSON data, decomposed

line infinite line on a plane

lseg line segment on a plane

macaddr MAC (Media Access Control) address

money currency amount

numeric [ (p, s) ] decimal [ (p, s) ] exact numeric of selectable precision

path geometric path on a plane

pg_lsn PostgreSQL Log Sequence Number

point geometric point on a plane

polygon closed geometric path on a plane

single precision floating-point number (4


real float4
bytes)

smallint int2 signed two-byte integer

smallserial serial2 autoincrementing two-byte integer

serial serial4 autoincrementing four-byte integer

text variable-length character string

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)

timestamp [ (p) ] with time zone

tsquery
timestamptz date and time, including time zone

text search query


PostgreSQL: Documentation: 9.5: Data Types
tsvector text search document

txid_snapshot user-level transaction ID snapshot

Richard Brooks uuid universally unique identifier


28
xml XML data

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

If I want to change the type I need user sub_forum


to update many places. user_name {PK} id {PK}
password title
profile_image description
owner {FK}

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

Domains - Define your own “sub-types”

#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

CREATE DOMAIN name AS VARCHAR(100); If the type needs to be


CREATE DOMAIN passport_no AS BIGINT; changed, you can do it
once
Richard Brooks 33
2
Data Science

sdu.dk
efterår 2022

Create these at the top of your ddl file Domains


They can be used throughout your tables.

#sdudk
create domain isbn_number as varchar(15);
create domain book_title as varchar(200);

CREATE TABLE book (


id INTEGER PRIMARY KEY,
isbn isbn_number,
title book_title,
page_count SMALLINT,
year_published SMALLINT,
binding_id SMALLINT REFERENCES binding_type(id) on delete restrict,
publisher_id SMALLINT REFERENCES publisher(id) on delete set null,
author_id SMALLINT REFERENCES author(author_id) on delete cascade
);

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)

CREATE TABLE profile(


id SMALLINT Primary Key ,
Cannot be null, value must
first_name VARCHAR(25), be ‘m’ or ‘f’.
last_name VARCHAR(25),
profile_name VARCHAR(25),
sex char(1) not null check(sex in ('m', 'f')),No two rows can have same
value
email varchar(50) unique
Richard Brooks 39
);
2
Data Science

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

TE DOMAIN OwnerNumber AS VARCHAR(5) CHECK (VALUE IN (SELECT ownerNo FROM PrivateOw


TE DOMAIN StaffNumber AS VARCHAR(5) CHECK (VALUE IN (SELECT staffNo FROM Staff));

#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

pertyNo PropertyNumber NOT NULL,


eet Street NOT NULL,
y City NOT NULL,
Richard Brooks
stcode PostCode, 42
2
Data Science TABLE
CREATE 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

CREATE TABLE people (


...,
height_cm numeric,
height_in numeric GENERATED ALWAYS AS (height_cm / 2.54) STORED
;

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

You might also like