SQL - DDL 2
Mark Fontenot, PhD
Northeastern University
CREATE TABLE Examples - 1
CREATE TABLE actors (
actor_id SMALLINT UNSIGNED NOT NULL,
first_name VARCHAR(45),
last_name VARCHAR(45),
last_update TIMESTAMP,
PRIMARY KEY (actor_id)
);
2
CREATE TABLE Examples - 1
CREATE TABLE actors (
actor_id SMALLINT UNSIGNED PRIMARY KEY,
first_name VARCHAR(45),
last_name VARCHAR(45),
last_update TIMESTAMP
);
3
CREATE TABLE Examples - 2
CREATE TABLE follow_follower AS
SELECT u.username AS userName,
uf.username AS followerName
FROM users u
JOIN follows f ON u.id = f.followee_id
JOIN users uf ON uf.id = f.follower_id;
4
auto_increment
5
CREATE TABLE animals (
id INTEGER AUTO_INCREMENT NOT NULL,
name VARCHAR(20),
PRIMARY KEY (id)
);
INSERT INTO animals (name)
VALUES ('dog'), ('cat'), ('penguin');
SELECT *
FROM animals;
auto_increment
6
DELETE FROM animals
WHERE name = 'penguin';
SELECT * FROM animals;
INSERT INTO animals (name)
VALUES ('husky');
SELECT * FROM animals;
Time Stamps
7
CREATE TABLE vet_visit (
visit_seq_no INT NOT NULL auto_increment,
pet_id int NOT NULL,
vet_last_name VARCHAR(50),
create_timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
update_timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT pk PRIMARY KEY (visit_seq_no),
CONSTRAINT fk_01 FOREIGN KEY (pet_id)
REFERENCES animals (id)
ON UPDATE CASCADE
ON DELETE RESTRICT
);
INSERT INTO vet_visit (pet_id,
vet_last_name)
VALUES (1, 'Temple'),
(1, 'Huang'),
(2, 'Caraway');
Time Stamps
8
Before:
UPDATE vet_visit
SET vet_last_name = 'Temple'
WHERE visit_seq_no = 3;
Then:
Time Stamps
9
Before:
UPDATE vet_visit
SET vet_last_name = 'Temple'
WHERE visit_seq_no = 3;
Then:
After:
Indexing
10
Searching for Data
11
Searching - Linear
12
13
ID Some_other_data
18
21
25
28
30
37
38
41
45
SELECT *
FROM cool_data
WHERE id = 37;
Searching - Linear
13
13
ID Some_other_data
18
21
25
28
30
37
38
41
45
SELECT *
FROM cool_data
WHERE id = 37;
Is this 37?
Is this 37?
Is this 37?
Is this 37?
Is this 37?
Is this 37?
Is this 37? ✅
Searching - Linear
14
13
ID Some_other_data
18
21
25
28
30
37
38
41
45
SELECT *
FROM cool_data
WHERE id = 37;
Is this 37?
Is this 37?
Is this 37?
Is this 37?
Is this 37?
Is this 37?
Is this 37? ✅
In the worst case, how many IDs do we have to examine to fully execute the query?
Searching - Binary Search
15
13
ID Some_other_data
18
21
25
28
30
37
38
41
45
SELECT *
FROM cool_data
WHERE id = 37;
Is this 37?
Searching - Binary Search
16
13
ID Some_other_data
18
21
25
28
30
37
38
41
45
SELECT *
FROM cool_data
WHERE id = 37;
Is this 37?
Is this 37?
Searching - Binary Search
17
13
ID Some_other_data
18
21
25
28
30
37
38
41
45
SELECT *
FROM cool_data
WHERE id = 37;
Is this 37?
Is this 37?
Is this 37?
Searching - Binary Search
18
13
ID Some_other_data
18
21
25
28
30
37
38
41
45
SELECT *
FROM cool_data
WHERE id = 37;
Is this 37?
Is this 37?
Is this 37?
Is this 37? ✅
In the worst case, how many IDs do we have to examine to fully execute the query with Binary Search?
Searching
19
Indexes
20
Indexes
21
B+ Tree Concept
22
https://en.m.wikipedia.org/wiki/File:B%2B-tree-remove-61.png
Unique Indexes and Indexes
23
CREATE TABLE IF NOT EXISTS users
(
id INTEGER AUTO_INCREMENT NOT NULL,
user_name VARCHAR(30) NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
password_hash VARCHAR(32) NOT NULL,
date_registered DATETIME NOT NULL,
last_login DATETIME,
profile TEXT,
PRIMARY KEY (id),
UNIQUE INDEX uq_idx_user_name (user_name),
UNIQUE INDEX uq_idx_email (email),� INDEX idx_name (last_name)
);
To Index or not to Index?
24
??
25