05 SQL partIII
05 SQL partIII
Announcements
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
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)
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
SQL’s solution
• A special value NULL
• For every domain (i.e., any datatype)
Three-valued logic
TRUE = 1, FALSE = 0, UNKNOWN = 0.5
𝑥 AND 𝑦 = min(𝑥, 𝑦)
𝑥 OR 𝑦 = max(𝑥, 𝑦)
NOT 𝑥 = 1 − 𝑥
Unfortunate consequences
• Q1a = Q1b?
Q1a. SELECT AVG(pop) FROM User;
• Q2a = Q2b?
Another problem
• Example: Who has NULL pop values?
t work!
o
SELECT * FROM User WHERE pop = NULL; Does n
SELECT uid FROM User where age > 5 OR pop < 0.5;
SELECT uid FROM User where age > 5 AND pop < 0.5;
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)
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
Outerjoin syntax
SELECT * FROM Group LEFT OUTER JOIN Member
≈ 𝐺𝑟𝑜𝑢𝑝 ⟕ 𝑀𝑒𝑚𝑏𝑒𝑟
ON Group.gid = Member.gid; !"#$%.'()*+,-.,".'()
SELECT u.name as uname, m.gid FROM User u LEFT OUTER JOIN Member m
ON u.uid=m.uid;
INSERT
• Insert one row
• User 789 joins Dead Putting Society
INSERT INTO Member VALUES (789, 'dps');
DELETE
• Delete everything from a table
DELETE FROM Member;
UPDATE
• Example: User 142 changes name to “Barney”
UPDATE User
SET name = 'Barney’
WHERE uid = 142;
Constraints
• Restricts what data is allowed in a database
• In addition to the simple structure and type restrictions
imposed by the table definitions
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
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
Naming constraints
• It is possible to name constraints (similar to
assertions)