Week5 Lecture
Week5 Lecture
Introduction to Databases
1
Databases
Structured Query Language (SQL)
Chapter 5
2
History
• Developed at IBM in 1970’s as part of System R (vs. Quel in Ingres)
• Theoretical relational model has real practical importance
• Keep in mind that SQL does not strictly implement the relational model
• Became popular in 1980’s
• Oracle was first to sell an SQL database
• 40+ years of existence and still relevant today, despite:
• 90’s: Object-Oriented DBMS
• 2000’s: XML
• 2010’s: NoSQL and MapReduce
Most Popular Databases
4
Source: Stackoverflow, 2020
Most Popular Languages
5
Source: Stackoverflow, 2020
SQL is here to stay!
6
Review
7
Review
• A relational table
• Fixed schema
• Variable instance
• Attributes
• Unique names
• Atomic types
• Tuples:
• Set in relational, multiset in SQL (duplicates allowed)
10
SQL Language
• Four main components:
• Data Definition Language (DDL) to define/modify schemas (add/delete
tables/columns/views/indexes) and specify constraints
• Data Manipulation Language (DML) to manipulate the data
(insert/delete/modify/query rows)
• Data Control Language (DCL)
• Transaction Control Language (TCL)
• Declarative!
• The user tells the RDBMS what it wants not how to get it.
• The RDBMS decides which algorithms/data structures to use to answer a
query
11
SQL Language Overview
[1] https://www.w3schools.in/mysql/ddl-dml-dcl
12
SQL Language Overview
[1] https://www.w3schools.in/mysql/ddl-dml-dcl
13
Manipulate the data/query
SQL Language Overview
[1] https://www.w3schools.in/mysql/ddl-dml-dcl
14
SQL Language Overview
[1] https://www.w3schools.in/mysql/ddl-dml-dcl
Control transactions
15
SQL Language Overview
[1] https://www.w3schools.in/mysql/ddl-dml-dcl
Week 10
Week 6
Week4
16
Weeks 4 & 5
SQL DDL Example
• Given the following instances of the relations Sailors, Boats and Reserves
that belong to the BoatingClub database
Sailors Boats
sid sname rating age bid bname color
1 Mohamed 6 60 101 Boughaz blue
2 Amina 2 22 102 Battouta red
3 Imane 7 30 103 Intilaqa green
4 Ahmed 2 45 104 Flouqa red
Reserves
sid bid day
1 101 28/02/2023
3 101 25/02/2023
17
SQL DDL Example
• Drop/Create/Rename/Select a database
USE BoatingClub;
18
SQL DDL Example
CREATE TABLE Sailors
Sailors sid sname rating age ( sid INTEGER,
1 Mohamed 6 60 sname CHAR(30),
rating INTEGER,
2 Amina 2 22 age FLOAT,
PRIMARY KEY (sid)
3 Imane 7 30 );
4 Ahmed 2 45
19
SQL DDL Example
CREATE TABLE Sailors
Sailors sid sname rating age ( sid INTEGER,
1 Mohamed 6 60 sname CHAR(30),
rating INTEGER,
2 Amina 2 22 age FLOAT,
PRIMARY KEY (sid)
3 Imane 7 30 );
4 Ahmed 2 45
CREATE TABLE Boats
Boats bid bname color ( bid INTEGER,
bname CHAR(20),
101 Boughaz blue color CHAR (10),
PRIMARY KEY (bid)
102 Battouta red );
103 Intilaqa green
104 Flouqa red
20
SQL DDL Example
CREATE TABLE Sailors
Sailors sid sname rating age ( sid INTEGER,
1 Mohamed 6 60 sname CHAR(30),
rating INTEGER,
2 Amina 2 22 age FLOAT,
PRIMARY KEY (sid)
3 Imane 7 30 );
4 Ahmed 2 45
CREATE TABLE Boats
Boats bid bname color ( bid INTEGER,
bname CHAR(20),
101 Boughaz blue color CHAR (10),
PRIMARY KEY (bid)
102 Battouta red );
103 Intilaqa green
104 Flouqa red
Reserves sid bid day
1 101 28/02/2023
3 101 25/02/2023 21
SQL DDL Example
CREATE TABLE Sailors
Sailors sid sname rating age ( sid INTEGER,
1 Mohamed 6 60 sname CHAR(30),
rating INTEGER,
2 Amina 2 22 age FLOAT,
PRIMARY KEY (sid)
3 Imane 7 30 );
4 Ahmed 2 45
CREATE TABLE Boats
Boats bid bname color ( bid INTEGER,
bname CHAR(20),
101 Boughaz blue color CHAR (10),
PRIMARY KEY (bid)
102 Battouta red );
103 Intilaqa green
104 Flouqa red CREATE TABLE Reserves
( sid INTEGER,
Reserves bid INTEGER,
sid bid day day DATE,
1 101 28/02/2023 PRIMARY KEY (bid,day),
FOREIGN KEY (sid) REFERENCES Sailors (sid),
3 101 25/02/2023 FOREIGN KEY (bid) REFERENCES Boats (bid) 22
);
SQL DDL Example
• Some other table-related DDL statements (non-exhaustive):
23
SQL DDL Example
• Indexes/Views:
CREATE VIEW view-name AS sql-query; //a view is a virtual table defined using an SQL statement
24
Integrity Constraints (Review)
25
General Constraints
CREATE TABLE Sailors
• Useful when more ( sid INTEGER,
general ICs than keys sname CHAR(30),
rating INTEGER,
are involved. age FLOAT,
PRIMARY KEY (sid),
• Can use queries to CHECK (rating > =1 AND rating <= 10)
express constraint. );
INSERT INTO table_name (column1, column2, column3....) VALUES (value1, value2, value3.....);
DELETE FROM TABLE table-name [WHERE condition]; //delete all rows that satisfy condition
27
SQL-DML: Basic SQL Query
SELECT [DISTINCT] target-list
FROM relation-list
[WHERE qualification]
29
Query Examples – Simple Query
Sailors sid sname rating age
1 Mohamed 6 60
2 Amina 2 22
3 Imane 8 30
4 Ahmed 2 45
rating sname
2 Ahmed
2 Amina
8 Imane 31
Query Examples – ORDER BY
Sailors sid sname rating age
1 Mohamed 6 60
2 Amina 2 22
3 Imane 8 30
4 Ahmed 2 45
35
Query Examples – GROUP BY
Sailors sid sname rating age
1 Mohamed 6 60
2 Amina 2 22
3 Imane 8 30
4 Ahmed 2 45
• HAVING predicate
• Filters groups
• Can only be used with GROUP BY
• Optional and applied after grouping and aggregation
• Can contain anything that can go in the SELECT target-list
• Can be used with WHERE clause
37
HAVING vs WHERE
WHERE HAVING
Can be used without GROUP BY clause Can only be used with GROUP BY clause
Can be used with SELECT, UPDATE, DELETE Can only be used with SELECT statements
statements
Applied before GROUP BY clause Applied after the GROUP BY clause
WHERE clause cannot include SQL Aggregate HAVING clause can include SQL Aggregate
functions functions
38
Query Examples – HAVING
Sailors sid sname rating age
1 Mohamed 6 60
2 Amina 2 22
3 Imane 8 30
4 Ahmed 2 45
40
Query Examples – DISTINCT Aggregates
Sailors sid sname rating age
1 Mohamed 6 60
2 Amina 2 22
3 Imane 8 30
4 Ahmed 2 45
cnt cnt
2 3
41
Summary
• Format DML query:
SELECT [DISTINCT] target-list
FROM relation-list
[WHERE predicate]
[GROUP BY column-list]
[HAVING predicate]
[ORDER BY column-list]
[LIMIT integer];
42
Summary
• How to interpret a query of the form: Specify table
FROM
Eliminate rows
SELECT [DISTINCT] target-list WHERE (satisfying the predicate)
FROM relation-list
[WHERE predicate] Eliminate columns
[GROUP BY column-list] SELECT (keep only columns listed in the target-list)
[HAVING predicate];
• Eliminate duplicates
DISTINCT
43
A Note on Range Variables
• Really needed only if the same relation appears twice in the FROM
clause. The previous query can also be written as:
SELECT S.sname
FROM Sailors S, Reserves R It is good style,
however, to use
WHERE S.sid=R.sid AND bid=103
range variables
always!
OR SELECT sname
FROM Sailors, Reserves
WHERE Sailors.sid=Reserves.sid
AND bid=103
44
Triggers
45
Triggers: Example (SQL:1999)
CREATE TRIGGER youngSailorUpdate
AFTER INSERT ON SAILORS
REFERENCING NEW TABLE NewSailors
FOR EACH STATEMENT
INSERT
INTO YoungSailors(sid, name, age, rating)
SELECT sid, name, age, rating
FROM NewSailors N
WHERE N.age <= 18
46
Null Values
• Field values in a tuple are sometimes unknown (e.g., a rating has
not been assigned) or inapplicable (e.g., no spouse’s name).
• SQL provides a special value null for such situations.
• The presence of null complicates many issues. E.g.:
• Special operators needed to check if value is/is not null.
• Is rating>8 true or false when rating is equal to null? What about AND, OR
and NOT connectives?
• We need a 3-valued logic (true, false and unknown).
• Meaning of constructs must be defined carefully. (e.g., WHERE clause
eliminates rows that don’t evaluate to true.)
• New operators (in particular, outer joins) possible/needed.
47
Summary
• SQL was an important factor in the early acceptance of the
relational model; more natural than earlier, procedural query
languages.
• Relationally complete; in fact, significantly more expressive power
than relational algebra.
• Even queries that can be expressed in RA can often be expressed
more naturally in SQL.
• Many alternative ways to write a query; optimizer should look for
most efficient evaluation plan.
• In practice, users need to be aware of how queries are optimized and
evaluated for best results.
58
Summary (Contd.)
59