0% found this document useful (0 votes)
2 views72 pages

Lecture 3

The document provides an introduction to using SQL to create, read, update, and delete data from databases. It demonstrates basic SQL commands and syntax for common data management operations.

Uploaded by

b210586
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views72 pages

Lecture 3

The document provides an introduction to using SQL to create, read, update, and delete data from databases. It demonstrates basic SQL commands and syntax for common data management operations.

Uploaded by

b210586
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

Introduction to

Databases with SQL


Writing
Create
Read
Update
Delete
Insert
id title accession_number acquired
Profusion of owers
fl
id title accession_number acquired
1 Profusion of owers 56.257 1956-04-12
fl
Farmers working at dawn
id title accession_number acquired
1 Profusion of owers 56.257 1956-04-12
2 Farmers working at dawn 11.6152 1911-08-03
fl
Spring outing
id title accession_number acquired
1 Profusion of owers 56.257 1956-04-12
2 Farmers working at dawn 11.6152 1911-08-03
3 Spring outing 14.76 1914-01-08
fl
INSERT INTO table (column0, …)
VALUES
(value0, …);
INSERT INTO table (column0, …)
VALUES
(value0, …),
(value1, …),
…;
id,title,accession_number,acquired
1,Profusion of flowers,56.257,1956-04-12
2,Farmers working at dawn,11.6152,1911-08-03
3,Spring outing,14.76,1914-01-08
4,Imaginative landscape,56.496,
5,Peonies and butterfly,06.1899,1906-01-01
.import
.import --csv --skip 1
id,title,accession_number,acquired
1,Profusion of flowers,56.257,1956-04-12
2,Farmers working at dawn,11.6152,1911-08-03
3,Spring outing,14.76,1914-01-08
4,Imaginative landscape,56.496,
5,Peonies and butterfly,06.1899,1906-01-01
title,accession_number,acquired
Profusion of flowers,56.257,1956-04-12
Farmers working at dawn,11.6152,1911-08-03
Spring outing,14.76,1914-01-08
Imaginative landscape,56.496,
Peonies and butterfly,06.1899,1906-01-01
INSERT INTO table0 (column0, …)
SELECT column0, … FROM table1;
Delete
id title accession_number acquired
1 Profusion of owers 56.257 1956-04-12
2 Farmers working at dawn 11.6152 1911-08-03
3 Spring outing 14.76 1914-01-08
4 Imaginative landscape 56.496 NULL
5 Peonies and butter y 06.1899 1906-01-01
6 Tile Lunette 06.2437 1906-11-08
7 Statuette of a shrew 01.105 1901-02-11
fl
fl
id title accession_number acquired
1 Profusion of owers 56.257 1956-04-12
2 Farmers working at dawn 11.6152 1911-08-03
3 Spring outing 14.76 1914-01-08
4 Imaginative landscape 56.496 NULL
5 Peonies and butter y 06.1899 1906-01-01
6 Tile Lunette 06.2437 1906-11-08
7 Statuette of a shrew 01.105 1901-02-11
fl
fl
id title accession_number acquired
1 Profusion of owers 56.257 1956-04-12
2 Farmers working at dawn 11.6152 1911-08-03
4 Imaginative landscape 56.496 NULL
5 Peonies and butter y 06.1899 1906-01-01
6 Tile Lunette 06.2437 1906-11-08
7 Statuette of a shrew 01.105 1901-02-11
fl
fl
DELETE FROM table WHERE condition;
id title accession_number acquired
1 Profusion of owers 56.257 1956-04-12
2 Farmers working at dawn 11.6152 1911-08-03
5 Peonies and butter y 06.1899 1906-01-01
6 Tile Lunette 06.2437 1906-11-08
7 Statuette of a shrew 01.105 1901-02-11
fl
fl
id title accession_number acquired
1 Profusion of owers 56.257 1956-04-12
2 Farmers working at dawn 11.6152 1911-08-03
5 Peonies and butter y 06.1899 1906-01-01
6 Tile Lunette 06.2437 1906-11-08
7 Statuette of a shrew 01.105 1901-02-11
fl
fl
id title accession_number acquired
1 Profusion of owers 56.257 1956-04-12
2 Farmers working at dawn 11.6152 1911-08-03
fl
Foreign Key Constraints
artists created collections
id name artist_id collection_id id title

1 Li Yin 1 2 1 Farmers working …

2 Qian Weicheng 2 3 2 Imaginative land…

3 Unidenti ed artist 3 1 3 Profusion of …

4 Zhou Chen 4 4 4 Spring outing


fi
artists created collections
id name artist_id collection_id id title

1 Li Yin 1 2 1 Farmers working …

2 Qian Weicheng 2 3 2 Imaginative land…

3 Unidenti ed artist 3 1 3 Profusion of …

4 Zhou Chen 4 4 4 Spring outing


fi
artists created collections
id name artist_id collection_id id title

1 Li Yin 1 2 1 Farmers working …

2 Qian Weicheng 2 3 2 Imaginative land…

4 Zhou Chen 3 1 3 Profusion of …

4 4 4 Spring outing
artists created collections
id name artist_id collection_id id title

1 Li Yin 1 2 1 Farmers working …

2 Qian Weicheng 2 3 2 Imaginative land…

4 Zhou Chen 3 1 3 Profusion of …

4 4 4 Spring outing
FOREIGN KEY("artist_id") REFERENCES "artists"("id")
FOREIGN KEY("artist_id") REFERENCES "artists"("id")
ON DELETE …
FOREIGN KEY("artist_id") REFERENCES "artists"("id")
ON DELETE RESTRICT
FOREIGN KEY("artist_id") REFERENCES "artists"("id")
ON DELETE NO ACTION
FOREIGN KEY("artist_id") REFERENCES "artists"("id")
ON DELETE SET NULL
FOREIGN KEY("artist_id") REFERENCES "artists"("id")
ON DELETE SET DEFAULT
FOREIGN KEY("artist_id") REFERENCES "artists"("id")
ON DELETE CASCADE
artists created collections
id name artist_id collection_id id title

1 Li Yin 2 3 1 Farmers working …

2 Qian Weicheng 3 1 2 Imaginative land…

3 Unidenti ed artist 1 2 3 Profusion of …

4 Zhou Chen 4 4 4 Spring outing


fi
artists created collections
id name artist_id collection_id id title

1 Li Yin 2 3 1 Farmers working …

2 Qian Weicheng 3 1 2 Imaginative land…

3 Unidenti ed artist 1 2 3 Profusion of …

4 Zhou Chen 4 4 4 Spring outing


fi
artists created collections
id name artist_id collection_id id title

1 Li Yin 2 3 1 Farmers working …

2 Qian Weicheng 3 1 2 Imaginative land…

4 Zhou Chen 1 2 3 Profusion of …

4 4 4 Spring outing
artists created collections
id name artist_id collection_id id title

1 Li Yin 2 3 1 Farmers working …

2 Qian Weicheng 1 2 2 Imaginative land…

4 Zhou Chen 4 4 3 Profusion of …

4 Spring outing
Update
artists created collections
id name artist_id collection_id id title

1 Li Yin 2 3 1 Farmers working …

2 Qian Weicheng 3 1 2 Imaginative land…

3 Unidenti ed artist 1 2 3 Profusion of …

4 Zhou Chen 4 4 4 Spring outing


fi
artists created collections
id name artist_id collection_id id title

1 Li Yin 2 3 1 Farmers working …

2 Qian Weicheng 3 1 2 Imaginative land…

3 Unidenti ed artist 1 2 3 Profusion of …

4 Zhou Chen 4 4 4 Spring outing


fi
artists created collections
id name artist_id collection_id id title

1 Li Yin 2 3 1 Farmers working …

2 Qian Weicheng 1 1 2 Imaginative land…

3 Unidenti ed artist 1 2 3 Profusion of …

4 Zhou Chen 4 4 4 Spring outing


fi
UPDATE table
SET column0 = value0, …
WHERE condition;
Triggers
collections transactions
id title id title action

1 Farmers working …

2 Imaginative land…

3 Profusion of …

4 Spring outing
collections transactions
id title id title action

1 Farmers working …

2 Imaginative land…

3 Profusion of …

4 Spring outing
collections transactions
id title id title action

1 Farmers working …

2 Imaginative land…

3 Profusion of …
collections transactions
id title id title action

1 Farmers working … 1 Spring outing sold

2 Imaginative land…

3 Profusion of …
collections transactions
id title id title action

1 Farmers working … 1 Spring outing sold

2 Imaginative land…

3 Profusion of …
collections transactions
id title id title action

1 Farmers working … 1 Spring outing sold

2 Imaginative land…

3 Profusion of …

4 Peonies and …
collections transactions
id title id title action

1 Farmers working … 1 Spring outing sold

2 Imaginative land… 2 Peonies and … bought

3 Profusion of …

4 Peonies and …
CREATE TRIGGER name
CREATE TRIGGER name
AFTER
CREATE TRIGGER name
BEFORE
CREATE TRIGGER name
BEFORE INSERT ON table
CREATE TRIGGER name
BEFORE UPDATE OF column ON table
CREATE TRIGGER name
BEFORE DELETE ON table
CREATE TRIGGER name
BEFORE DELETE ON table
FOR EACH ROW
CREATE TRIGGER name
BEFORE DELETE ON table
FOR EACH ROW
BEGIN
CREATE TRIGGER name
BEFORE DELETE ON table
FOR EACH ROW
BEGIN
…;
CREATE TRIGGER name
BEFORE DELETE ON table
FOR EACH ROW
BEGIN
…;
END;
Soft Deletions
collections
id title deleted

1 Farmers working … 0

2 Imaginative land… 0

3 Profusion of … 0

4 Peonies and … 0
collections
id title deleted

1 Farmers working … 0

2 Imaginative land… 0

3 Profusion of … 0

4 Peonies and … 0
collections
id title deleted

1 Farmers working … 1

2 Imaginative land… 0

3 Profusion of … 0

4 Peonies and … 0
Introduction to
Databases with SQL
Writing

You might also like