2 - SQL - Part 1
2 - SQL - Part 1
Part 1
1
SQL
• Before SQL, there was SEQUEL which stands for Structured English
Query Language
2
Agenda
• So Far
• Basics of the Relational Model
• SQL DDL Statements
– CREATE TABLE Command
– Attribute Domains
– Key Constraints
– Foreign Key Constraints
– Check Constrains
– Other DDL Statements
• SQL DML Statements
– Queries
3
Question
4
Relational Model –
Do the values of different rows have to be distinct?
Sailors
sid name rating age
1 Dusty 7 45
Valid
2 Rusty 10 35
Sailors
sid name rating age Not Valid
1 Dusty 7 45
1 Dusty 7 45
2 Rusty 10 15
5
SQL Standard –
Do the values of different rows have to be distinct?
Sailors
sid name rating age
1 Dusty 7 45
Valid
2 Rusty 10 35
Sailors
sid name rating age
Valid, if no primary key is
1 Dusty 7 45
1 Dusty 7 45
defined
2 Rusty 10 15
6
Keys – Theory vs Practice
7
Primary Keys: Good to have them or not?
• Even though a primary key is not required, it is bad practice not to have
one
8
Foreign Key Constraints
9
Foreign Keys: Example
Students Enrollment
sid login name dob gpa sid cid grade
10
Declaring Foreign Keys
11
Declaring Foreign Keys
12
Declaring Foreign Keys
• You can have more than one foreign key in a table referencing the same
candidate key of another table
13
Enforcing Referential Integrity - Insertion
Students Enrollment
sid login name dob gpa sid cid grade
789 210 4
Students Enrollment
sid login name dob gpa sid cid grade
Students Enrollment
sid login name dob gpa sid cid grade
Students Enrollment
sid login name dob gpa sid cid grade
Students Enrollment
sid login name dob gpa sid cid grade
if the user deletes this record the DBMS deletes this record
20
Enforcing Referential Integrity – Deletion - Option 4
if the user deletes this record the DBMS updates the FK with
its default value (or reject if
default also violates integrity)
21
Enforcing Referential Integrity – Deletion - Option 5
if the user deletes this record the DBMS updates the FK with
NULL (allowed only if FK not
part of primary key)
22
Enforcing Referential Integrity - Update
Students Enrollment
sid login name dob gpa sid cid grade
24
NULLs in Foreign Keys
25
Nulls in Foreign Keys
Employees
create table Employees (
ssn char(12) primary key, ssn name title
name varchar(128) not null, ‘111-11-1111’ Ann ‘CEO’
title varchar(128) ’222-22-2222’ Bob ‘clerk’
);
Managers
create table Managers ( emp_ssn mgr_ssn
emp_ssn char(12) primary key,
‘222-22-2222’ ‘111-11-1111’
mgr_ssn char(12),
foreign key (emp_ssn) references Employees(ssn), ‘111-11-1111’ NULL
foreign key (mgr_ssn) references Employees(ssn)
);
26
Foreign Key Examples
27
Foreign Key Examples (Solution 1)
28
Foreign Key Examples (Solution 2)
29
Integrity Constraints continued
30
Specifying CHECK Constraints
31
Naming Constraints
32
Modifying Constraints
33
Modifying Tables
34
Inserting Records
35
Inserting Records
• You can insert into some columns only, filling with NULL
36
Updating Records
37
Deleting Records
38
SQL Queries
39
SQL Queries – SFW Queries
select Columns
from Table
where Condition
40
Operations on a single table – The SFW Query
select * select *
from Table from Table
where Condition
41
SWF Queries
select Columns
from Table
where Condition
42
SWF Queries
select Columns
from Table
where Condition
43
SQL Queries – SFW Queries
select Columns
from Table
where Condition
• is the same as
44
Examples
46
Selection in SQL
47
Selection in SQL
What are the sailors whose age is 35 and whose rating is over 7?
48
Projection in SQL
age
select age
45
from Sailors; 35 SQL queries compute
35 bags (not sets!)
18
25
49
Projection in SQL – Use DISTINCT to eliminate duplicates
age
select distinct age
45
from Sailors; 35 use distinct to remove
18 duplicates
25
50
SQL queries compute bags (not sets)
51
SQL – Combining selection and projection
name
select name
Rusty
from Sailors Horatio
where age = 35
52
Examples
53
SQL Operators
• Arithmetic: + - * / %
• Bitwise: ^ & |
• Comparison: = <> > < .. The SQL standard defines the
“not equal” operator as <>
• Compound: +=, -=, .. but != is also used
54
String expressions
Find name, age, 2*rating of all sailors whose name starts with A or
contains ‘ust’ as a substring starting from position 2
55
Expressions and strings: examples
Find names, sids of sailors whose rating * 5 is less than their age
Q2
select name, sid
from Sailors
where rating * 5 < age
56
Null values
1 Dusty 7 45 2 Rusty 10 35
2 Rusty 10 35 3 Horatio 5 35
3 Horatio 5 35 4 Zorba 8 18
4 Zorba 8 18
5 Julius null 25
Q5 select *
from Sailors
where rating is null;
sid name rating age
5 Julius null 25
57
Null Values: Cannot use =, !=, < or > with NULL
select ..
from ..
where rating is null
Valid
select ..
from ..
where rating is not null
Valid
select ..
from ..
where rating = null Not Valid
select ..
from ..
where rating <> null Not Valid
58
Null values (II)
3 Horatio 5 35 3 0
4 Zorba 8 18 4 0
5 Julius null 25 5 null
59
Question: What does this query compute?
select gpa*100
from students;
60
Question: What does this query compute?
select gpa*100
from students;
gpa
400
NULL
380
61
Question: What does this query compute?
select name
from students
where gpa > 3.5;
62
Question: What does this query compute?
select name
from students
where gpa > 3.5;
name
Moe
Joe
63
Question: What does this query compute?
select name
from students
where age > 15 or gpa > 3.5;
64
Question: What does this query compute?
select name
from students
where age > 15 or gpa > 3.5;
name
Moe
Larry
Joe
65
Question?
66
Question?
Input Output
sid name rating age sid name rating age
1 Dusty 7 45 1 Dusty 7 45
2 Rusty 10 35 2 Rusty 10 35
3 Horatio 5 35 3 Horatio 5 35
4 Zorba 8 18 4 Zorba 8 18
5 Julius null 25
67
Renaming attributes
select sid as sailor_id, rating / 10 as normalized_rating
from Sailors;
sailor_id normalized_rating
1 0.7
2 1
3 0.5
4 0.8
5 null
68
Renaming relations
sid normalized_rating
1 0.7
2 1
3 0.5
4 0.8
5 null
69
Sorting results
select Columns
from Table
where Condition
order by col_1, col_2 asc, col_3 desc
ascending descending
order order
70
Sorting Results - Example
71
Examples
(d) Find all boats that are either red or called Interlake.
72
Acknowledgements
73