0% found this document useful (0 votes)
7 views

05 SQL partIII

DBMS

Uploaded by

Tinin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

05 SQL partIII

DBMS

Uploaded by

Tinin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

SQL: Part III

CS348 Spring 2024


Instructor: Sujaya Maiyya
Sections: 002 and 003 only
2

Announcements

• Project Milestone 0: due May 25th !

• Assignment 1: Due June 4th


• Marmoset will be open tomorrow
3

Basic SQL features


• Query
• SELECT-FROM-WHERE statements
• Set/bag (DISTINCT, UNION/EXCEPT/INTERSECT (ALL))
• Subqueries (table, scalar, IN, EXISTS, ALL, ANY)
• Aggregation and grouping (GROUP BY, HAVING)
• Ordering (ORDER)
• Outerjoins (and Nulls)
• Modification Lecture 5
• INSERT/DELETE/UPDATE
• Constraints
4

Incomplete information
• Example: User (uid, name, age, pop)
• Value unknown
• We do not know Nelson’s pop
• Value not applicable
• Suppose pop is based on interactions with others on our
social networking site
• Nelson is new to our site; what is their pop?
5

Solution 1
• Dedicate a value from each domain (type)
• pop cannot be −1, so use −1 as a special value to
indicate a missing or invalid pop
n s we rs
c o rre cta
SELECT AVG(pop) FROM User; In
p lica ted
SELECT AVG(pop) FROM User WHERE pop != -1; Com

• Perhaps the value is not


as special as you think!
• the Y2K bug

http://www.90s411.com/images/y2k-cartoon.jpg
6

Solution 2
• A valid-bit for every column
• User (uid,
name, name_is_valid,
age, age_is_valid,
pop, pop_is_valid)

SELECT AVG(pop) FROM User WHERE pop_is_valid=1;

• Complicates schema and queries


• Need almost double the number of columns
7

Solution 3
• Decompose the table; missing row = missing value
• UserName (uid, name) Has a tuple for Nelson
• UserAge (uid, age) No entry for Nelson
• UserPop (uid, pop) No entry for Nelson
• UserID (uid) Has a tuple for Nelson

• Conceptually the cleanest solution


• Still complicates schema and queries
• How to get all information about users in a table?
• Natural join doesn’t work!
8

SQL’s solution
• A special value NULL
• For every domain (i.e., any datatype)

• Example: User (uid, name, age, pop)


• 789, “Nelson”, NULL, NULL

• Special rules for dealing with NULL’s


SELECT * FROM User WHERE name=‘Nelson’ AND pop > 0.5 ??
9

Three-valued logic
TRUE = 1, FALSE = 0, UNKNOWN = 0.5
𝑥 AND 𝑦 = min(𝑥, 𝑦)
𝑥 OR 𝑦 = max(𝑥, 𝑦)
NOT 𝑥 = 1 − 𝑥

• Comparing a NULL with another value (including


another NULL) using =, >, etc., the result is NULL

• WHERE and HAVING clauses only select rows for


output if the condition evaluates to TRUE
• NULL is not enough

• Aggregate functions ignore NULL, except COUNT(*)


10

Will 789 be in the output?

789, “Nelson”, NULL, NULL

SELECT uid FROM User where name=‘Nelson’ AND pop>0.5;


11

Unfortunate consequences
• Q1a = Q1b?
Q1a. SELECT AVG(pop) FROM User;

Q1b. SELECT SUM(pop)/COUNT(*) FROM User;

• Q2a = Q2b?

Q2a. SELECT * FROM User;

Q2b SELECT * FROM User WHERE pop=pop;

• Be careful: NULL breaks many equivalences


12

Another problem
• Example: Who has NULL pop values?
t work!
o
SELECT * FROM User WHERE pop = NULL; Does n

(SELECT * FROM User)


but ug ly
EXCEPT
Works,
(SELECT * FROM USER WHERE pop=pop);

• SQL introduced special, built-in predicates


IS NULL and IS NOT NULL
SELECT * FROM User WHERE pop IS NULL;
13

In class exercises User Member


uid gid
uid name age pop
857 dps
Consider this db instance: 142 Bart NULL 0.9
123 gov
123 Milhouse 8 NULL
857 abc
857 Lisa 8 0.7
857 gov
456 Nelson 8 NULL
456 abc
324 Ralph NULL 0.3
456 gov

• What is the output of these queries?

SELECT uid FROM User where age > 5 OR pop < 0.5;

SELECT uid FROM User where age > 5 AND pop < 0.5;

SELECT avg(pop), count(*) FROM User GROUP BY age;

SELECT name FROM User WHERE age IN (SELECT age FROM User
WHERE name = 'Bart');
14
User (uid int, name string, age int, pop float)
Take home ex. Group (gid string, name string)
Member (uid int, gid string)

• For the previous db instance, what is the output for:

SELECT avg(pop), count(*) FROM User WHERE age IS NOT NULL


GROUP BY age;

SELECT MAX(pop), count(*) FROM User GROUP BY age;

• Write a query to find all users (uids) with non-null


popularity who belong to at least one group.
15

Need for a new join query


• Example: construct a master group membership list
with all groups and its members info

SELECT g.gid, g.name AS gname,


u.uid, u.name AS uname
FROM Group g, Member m, User u
WHERE g.gid = m.gid AND m.uid = u.uid;

• What if a group is empty?


• It may be reasonable for the master list to include
empty groups as well
• For these groups, uid and uname columns would be
NULL
16

Outerjoin examples
gid name uid
Group ⟗ Member abc Book Club 857
Group gov Student Government 123
gid name gov Student Government 857
abc Book Club dps Dead Putting Society 142
gov Student Government spr Sports Club NULL
dps Dead Putting Society foo NULL 789
spr Sports Club

A full outerjoin between R and S:


Member • All rows in the result of 𝑅 ⋈ 𝑆, plus
uid gid
• “Dangling” 𝑅 rows (those that do not join
142 dps
123 gov
with any 𝑆 rows) padded with NULL’s for
857 abc 𝑆’s columns
857 gov • “Dangling” 𝑆 rows (those that do not join
789 foo with any 𝑅 rows) padded with NULL’s for
𝑅’s columns
17

Outerjoin examples gid name uid

Group ⟕ Member abc Book Club 857


gov Student Government 123
gov Student Government 857
Group dps Dead Putting Society 142
gid name
spr Sports Club NULL
abc Book Club
gov Student Government
• A left outerjoin (𝑅 ⟕ 𝑆) includes rows in 𝑅 ⋈ 𝑆
dps Dead Putting Society
plus dangling 𝑅 rows padded with NULL’s
spr Sports Club
gid name uid
Group ⟖ Member abc Book Club 857
Member gov Student Government 123
uid gid
gov Student Government 857
142 dps
dps Dead Putting Society 142
123 gov
foo NULL 789
857 abc
857 gov • A right outerjoin (𝑅 ⟖ 𝑆) includes rows in 𝑅 ⋈
789 foo
𝑆 plus dangling 𝑆 rows padded with NULL’s
18

Outerjoin syntax
SELECT * FROM Group LEFT OUTER JOIN Member
≈ 𝐺𝑟𝑜𝑢𝑝 ⟕ 𝑀𝑒𝑚𝑏𝑒𝑟
ON Group.gid = Member.gid; !"#$%.'()*+,-.,".'()

SELECT * FROM Group RIGHT OUTER JOIN Member


≈ 𝐺𝑟𝑜𝑢𝑝 ⟖ 𝑀𝑒𝑚𝑏𝑒𝑟
ON Group.gid = Member.gid; !"#$%.'()*+,-.,".'()

SELECT * FROM Group FULL OUTER JOIN Member ≈ 𝐺𝑟𝑜𝑢𝑝 ⟗ 𝑀𝑒𝑚𝑏𝑒𝑟


ON Group.gid = Member.gid; !"#$%.'()*+,-.,".'()

☞A similar construct exists for regular (“inner”) joins:


SELECT * FROM Group JOIN Member ON Group.gid = Member.gid;

Theta join: gid is Natural join: gid


repeated appears once

☞For natural joins, add keyword NATURAL; don’t use ON


SELECT * FROM Group NATURAL JOIN Member;
19

In class exercises User Member


Consider this db instance: uid uname age pop uid gid
gid gname 142 Bart 10 0.9 857 dps
abc Book Club 123 Milhouse 10 NULL 123 gov
Group gov Student Government 857 Lisa 8 0.7 857 abc
dps Dead Putting Society 456 Ralph 8 NULL 123 abc
spr Sports Club

• What is the output of these queries?


SELECT u.name as uname, g.name as gname FROM User u NATURAL JOIN
Member m NATURAL JOIN Group g;

SELECT u.name as uname, m.gid FROM User u LEFT OUTER JOIN Member m
ON u.uid=m.uid;

SELECT COUNT(m.gid), COUNT(g.name) FROM Member m RIGHT OUTER JOIN


Group g ON g.gid=m.gid;
20

SQL features covered so far


• SELECT-FROM-WHERE statements
• Set and bag operations
• Table expressions, subqueries
• Aggregation and grouping
• Ordering
• NULLs and outerjoins

FNext: data modification statements, constraints


21

INSERT
• Insert one row
• User 789 joins Dead Putting Society
INSERT INTO Member VALUES (789, 'dps');

INSERT INTO User (uid, name) VALUES (389, ‘Marge');

• Insert the result of a query


• Everybody joins Dead Putting Society!
INSERT INTO Member
(SELECT uid, 'dps' FROM User
WHERE uid NOT IN (SELECT uid
FROM Member
WHERE gid = 'dps'));
22

DELETE
• Delete everything from a table
DELETE FROM Member;

• Delete according to a WHERE condition


• Example: User 789 leaves Dead Putting Society
DELETE FROM Member WHERE uid=789 AND gid=‘dps’;

• Example: Users over age 18 must be removed from Sports Club


DELETE FROM Member
WHERE uid IN (SELECT uid FROM User WHERE age > 18)
AND gid = ‘spr';

DELETE m FROM Member m NATURAL JOIN User u WHERE


u.age > 18 AND m.gid=‘spr’;
23

UPDATE
• Example: User 142 changes name to “Barney”
UPDATE User
SET name = 'Barney’
WHERE uid = 142;

• Example: We are all popular!


UPDATE User
SET pop = (SELECT AVG(pop) FROM User);

• But won’t update of every row causes average pop to


change?
FSubquery is always computed over the old table
24

In class exercises User Member


Consider this db instance: uid name age pop uid gid
gid name 142 Bart 10 0.9 857 dps
abc Book Club 123 Milhouse 10 NULL 123 gov
Group gov Student Government 857 Lisa 8 0.7 857 abc
dps Dead Putting Society 456 Ralph 8 NULL 123 abc
spr Sports Club

• What is the output of these queries?


INSERT INTO Member (SELECT u.uid, ‘spr’ FROM User u WHERE u.age >= 10
AND u.pop IS NOT NULL);

DELETE m, g FROM Member m NATURAL JOIN Group g WHERE g.gid=‘dps’;

UPDATE User u NATURAL JOIN Member m SET u.age=11, u.pop=0.4, m.gid=‘spr’


WHERE u.uid=123 and m.gid=‘gov’;
25

Constraints
• Restricts what data is allowed in a database
• In addition to the simple structure and type restrictions
imposed by the table definitions

• Why use constraints?


• Protect data integrity (catch errors)
• Tell the DBMS about the data (so it can optimize better)

• Declared as part of the schema and enforced by the


DBMS
26

Types of SQL constraints


• NOT NULL
• Key
• Referential integrity (foreign key)
• General assertion
• Tuple- and attribute-based CHECK’s
27

NOT NULL constraint examples


CREATE TABLE User
(uid INT NOT NULL,
name VARCHAR(30) NOT NULL,
twitterid VARCHAR(15) NOT NULL,
age INT,
pop DECIMAL(3,2));

CREATE TABLE Group


(gid CHAR(10) NOT NULL,
name VARCHAR(100) NOT NULL);

CREATE TABLE Member


(uid INT NOT NULL,
gid CHAR(10) NOT NULL);
28

Key declaration examples


CREATE TABLE User At most one
(uid INT NOT NULL PRIMARY KEY, primary key per
name VARCHAR(30) NOT NULL, table
twitterid VARCHAR(15) NOT NULL UNIQUE,
age INT,
pop DECIMAL(3,2)); Any number of
UNIQUE keys per
table
CREATE TABLE Group
(gid CHAR(10) NOT NULL PRIMARY KEY, This form is
name VARCHAR(100) NOT NULL); required for multi-
attribute keys
CREATE TABLE Member
(uid INT NOT NULL, orre ct !
gid CHAR(10) NOT NULL, CREATE TABLE Member Inc
PRIMARY KEY(uid,gid)); (uid INT NOT NULL PRIMARY KEY,
gid CHAR(10) NOT NULL PRIMARY KEY,
29

Referential integrity example


• If a uid appears in Member, it must appear in User
• Member.uid references User.uid
• If a gid appears in Member, it must appear in Group
• Member.gid references Group.gid
FThat is, no “dangling pointers”
User Member Group
uid name … uid gid gid name
142 Bart … 142 dps abc …
123 Milhouse … 123 gov gov …
857 Lisa … 857 abc dps …
456 Ralph … 857 gov … …
789 Nelson … 456 abc
… … … 456 gov
… …
30

Referential integrity in SQL


• Referenced column(s) must be PRIMARY KEY
• Referencing column(s) form a FOREIGN KEY
Some system allow them to be
• Example non-PK but must be UNIQUE
CREATE TABLE Member
(uid INT NOT NULL REFERENCES User(uid),
gid CHAR(10) NOT NULL,
PRIMARY KEY(uid,gid),
FOREIGN KEY (gid) REFERENCES Group(gid));
This form is required for multi-
attribute foreign keys
CREATE TABLE MemberBenefits
(…..
FOREIGN KEY (uid,gid) REFERENCES Member(uid,gid));
31

Enforcing referential integrity


Example: Member.uid references User.uid
• Insert or update a Member row so it refers to a non-
existent uid
• Reject

User Member
uid name … uid gid
142 Bart … 142 dps
123 Milhouse … 123 gov
857 Lisa … 857 abc
456 Ralph … 857 gov
789 Nelson … 456 abc
… … … 456 gov
000 gov Reject
32

Enforcing referential integrity


Example: Member.uid references User.uid
• Delete or update a User row whose uid is
referenced by some Member row
• Multiple Options (in SQL)

CREATE TABLE Member


User Member (uid INT NOT NULL
uid name … uid gid REFERENCES User(uid)
142 Bart … 142 dps ON DELETE CASCADE,
123 Milhouse … 123 gov …..);
Option
857 Lisa 1: Reject
… 857 abc
Option 2: Cascade
456 Ralph … 857 gov
789 Nelson …
(ripple changes to all
456 abc
… … … 456 gov
referring rows)
… ….
33

Enforcing referential integrity


Example: Member.uid references User.uid
• Delete or update a User row whose uid is
referenced by some Member row
• Multiple Options (in SQL) CREATE TABLE Member
(uid INT NOT NULL
REFERENCES User(uid)
User Member ON DELETE SET NULL,
uid name … uid gid …..);
142 Bart … 142 dps
123 Milhouse … 123 gov
Option 3: Set NULL
857 Lisa … 857 abc
(set all references to NULL)
456 Ralph … 857 gov
789 Nelson … NULL abc
… … … NULL gov
… ….
35

General assertion
• CREATE ASSERTION 𝑎𝑠𝑠𝑒𝑟𝑡𝑖𝑜𝑛_𝑛𝑎𝑚𝑒
CHECK assertion_condition;
• assertion_condition is checked for each
modification that could potentially violate it
Can include
• Example: Member.uid references User.uid multiple
tables
CREATE ASSERTION MemberUserRefIntegrity
CHECK (EXISTS
(SELECT * FROM Member
WHERE uid IN Assertions are
(SELECT uid FROM User))); statements
that must
always be true
36

Tuple- and attribute-based CHECK’s


• Associated with a single table
• Only checked when a tuple/attribute is
inserted/updated
• Reject if condition evaluates to FALSE
• TRUE and UNKNOWN are fine
• Examples:
CREATE TABLE User(...
age INTEGER CHECK(age IS NULL OR age > 0), Checked when
...); new tuples are
added to Member
CREATE TABLE Member
but not when User
(uid INTEGER NOT NULL,
is modified
CHECK(uid IN (SELECT uid FROM User)),
...);
37

Naming constraints
• It is possible to name constraints (similar to
assertions)

CREATE TABLE User(...


age INT, constraint minAge check(age IS NULL OR age > 0),
...);
38
User (uid int, name string, age int, pop float)
In class ex. Group (gid string, name string)
Member (uid int, gid string)

• Write a DDL statement to create the User table with a


Primary key constraint and check that pop is between
0 and 1.
39
User (uid int, name string, age int, pop float)
In class ex. Group (gid string, name string)
Member (uid int, gid string)

• Write a DDL statement to create the User table with a


Primary key constraint and check that pop is between
0 and 1.

CREATE TABLE User


(uid INT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
age INT,
pop DECIMAL(3,2) CHECK(pop IS NULL OR (age >= 0 AND pop < 1));
40
User (uid int, name string, age int, pop float)
In class ex. Group (gid string, name string)
Member (uid int, gid string)

• Say every user with pop >=0.9 must belong to the


Book Club (gid=‘abc’). Create as assertion to check
this constraint.
41
User (uid int, name string, age int, pop float)
In class ex. Group (gid string, name string)
Member (uid int, gid string)

• Say every user with pop >=0.9 must belong to the


Book Club (gid=‘abc’). Create as assertion to check
this constraint.

CREATE ASSERTION BookClubMembership


CHECK (NOT EXISTS
(SELECT uid FROM User WHERE pop >= 0.9 AND
uid NOT IN (SELECT uid FROM Member WHERE gid=‘abc’)));
42

SQL features covered so far


• Query
• SELECT-FROM-WHERE statements
• Set and bag operations
• Table expressions, subqueries
• Aggregation and grouping
• Ordering
• Outerjoins (and NULL)
• Modification
• INSERT/DELETE/UPDATE
• Constraints
FNext lecture: schema changes, triggers, views,
indexes

You might also like