Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 29 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
29
Dung lượng
211,29 KB
Nội dung
CS 5614: Basic Data Definition and Modification in SQL 67 Introduction to SQL V V V V Structured Query Language (‘Sequel’) V Serves as DDL as well as DML Declarative V Say what you want without specifying how to it V One of the main reasons for commercial success of DBMSs Many standards and implementations V ANSI SQL V SQL-92/SQL-2 (Null operations, Outerjoins etc.) V SQL3 (Recursion, Triggers, Objects) V Vendor specific implementations “Bag Semantics” instead of “Set Semantics” V Used in commercial RDBMSs CS 5614: Basic Data Definition and Modification in SQL Example: V Create a Relation/Table in SQL CREATE TABLE Students (sid CHAR(9), name VARCHAR(20), login CHAR(8), age INTEGER, gpa REAL); V Support for Basic Data Types V CHAR(n) V VARCHAR(n) V BIT(n) V BIT VARYING(n) V INT/INTEGER V FLOAT V REAL, DOUBLE PRECISION V DECIMAL(p,d) V DATE, TIME etc 68 CS 5614: Basic Data Definition and Modification in SQL 69 More Examples V And one for Courses CREATE TABLE Courses (courseid CHAR(6), department CHAR(20)); V And one for their relationship! CREATE TABLE takes V Why? V (sid CHAR(9), courseid CHAR(6)); Can also provide default values CREATE TABLE Students (sid CHAR(9), age INTEGER DEFAULT 21, gpa REAL); CS 5614: Basic Data Definition and Modification in SQL Examples Contd V V V V DATE and TIME V Implementations vary widely V Typically treated as strings of a special form V Allows comparisons of an ordinal nature ( etc.) DATE Example V ‘1999-03-03’ (No Y2K problems) TIME Examples V ‘15:30:29’ V ‘15:30:29.3875’ Deleting a Relation/Table in SQL DROP TABLE Students; 70 CS 5614: Basic Data Definition and Modification in SQL 71 Modifying Relation Schemas V ‘Drop’ an attribute (column) ALTER TABLE Students DROP login; V ‘Add’ an attribute (column) ALTER TABLE Students ADD phone CHAR(7); V What happens to the new entry for the old records? V Default is ‘NULL’ or say ALTER TABLE Students ADD phone CHAR(7) DEFAULT ‘unknown’; V Always begin with ‘ ALTER TABLE ’ V Can use DEFAULT even with regular definition (as in Slide 69) CS 5614: Basic Data Definition and Modification in SQL How you enter/modify data? V INSERT command INSERT INTO Students VALUES (‘53688’,’Mark’,’mark2345’,23,3.9) V Cumbersome (use bulk loading; described later) V DELETE command DELETE FROM Students S WHERE S.name = ‘Smith’ V UPDATE command UPDATE Students S SET S.age=S.age+1, S.gpa=S.gpa-1 WHERE S.sid = ‘53688’ 72 CS 5614: Basic Data Definition and Modification in SQL 73 Domains V Domains: Similar to Structs and other user-defined types CREATE DOMAIN Email AS CHAR(8) DEFAULT ‘unknown’; login Email // instead of login CHAR(8) DEFAULT ‘unknown’ V Advantages: can be reused junkaddress Email, fromaddress Email, toaddress Email, V Can DROP DOMAINS too! DROP DOMAIN Email; V Affects only future declarations CS 5614: Basic Data Definition and Modification in SQL Keys V To Specify Keys V Use PRIMARY KEY or UNIQUE V Declare alongside attribute V For multiattribute keys, declare as a separate line CREATE TABLE takes ( sid CHAR(9), courseid CHAR(6), PRIMARY KEY (sid,courseid) ); V Whats the difference between PRIMARY KEY and UNIQUE? V Typically only one PRIMARY KEY but any number of UNIQUE keys V Implementor allowed to attach special significance 74 CS 5614: Basic Data Definition and Modification in SQL 75 Creating Indices/Indexes V V Why? V Speeds up query processing time For Students CREATE INDEX indexone ON Students(sid); CREATE INDEX indextwo ON Students(login); V V V How to decide attributes to place indices on? V One is (typically) created by default on PRIMARY KEY V Creation of indices on UNIQUE attributes is implementation-dependent V In general, physical database design/tuning is very difficult! V Use Tools: Microsoft SQLServer has an index selection Wizard Why not place indices on all attributes? V Too cumbersome for insertions/deletions/updates Like all things in computer science, there is a tradeoff! :-) CS 5614: Basic Data Definition and Modification in SQL Other Properties V ‘NOT NULL’ instead of DEFAULT CREATE TABLE Students (sid CHAR(9), name VARCHAR(20), login CHAR(8), age INTEGER, gpa REAL); V V Can insert a tuple without a value for gpa V NULL will be inserted If we had specified gpa REAL NOT NULL); V insert cannot be made! 76 ✂✁☎✄✝✆✟✞✡✠☞☛✍✌✏✎✒✑✔✓✖✕✏✗✡✘✝✙✛✚✢✜✝✣ ✤☎✥✧✦✩★✫✪✭✬✯✮✱✰✳✲✫✴✶✵✭✷✩✸✺✹✻✷✼✹✽✪✭✵✭✵✾✬✿✦❀✴✫✲✔❁❂✦✩★✫❃❄✷❅✷❆✲✫✪❈❇❉✷❅❃❄✷✩✥❀✦❋❊●✴✫✷✩❃✿❁❍✵✭■❏✥✫❑▲✴✶■▲❑▲✷❅✬❄▼▲❃◆✷✩❖✫❃◆✷✩✬❄✷✩✥❀✦❀■✦❀✪❈✰▲✥✫✬◗✴✫✬◆✷✩✲✍✪✭✥❙❘✩✰▲✥❏❚ ❯ ✴✫✥✫❘❱✦❀✪✭✰❏✥❲✹✽✪❳✦❀★❲✦✩★✫✷✡❃❄✷✩✵❈■❨✦❀✪❈✰▲✥✫■❏✵▲✮✧✰✳✲✫✷✩✵❈❩❭❬❪★✫✷✡❫✫❃◆✬✒✦❴✪✭✬✾❃◆✷✩✵✭■✦❀✪❈✰▲✥✫■❏✵✳■▲✵✭❑❏✷✩❵✫❃◆■▲✸●■▲✥◗■▲✵❈❑▲✷✩❵✫❃◆■▲✪❈❘❴■❏✥✫✲❆❖✶❃❄✰✳❘✩✷✩✲✶✴✫❃❄■❏✵ ✹✻■✂❁❍❛❜✰❏❃❪❘✩❃❄✷❅■❨✦❀✪❈✥✫❑❝✥✫✷◆✹❞❃❄✷❅✵✭■❨✦✩✪✭✰❏✥✫✬❪❛❜❃❄✰❏✮❡❑▲✪❣❢❉✷✩✥✏✰▲✥✶✷✩✬❄❩❤❬❪★✫✷◗✬❄✷✩❘❅✰▲✥✫✲✟✪✭✬❪✐✯■❨✦❀■❏✵✭✰❏❑▲✸✺✹✽★✫✪✭❘❨★✟✪✭✬❪✵✭✰❏❑▲✪❈❘✩■▲✵❥■▲✥✫✲ ✲✫✷✩❘❅✵✭■▲❃◆■❨✦✩✪❳❢❉✷❤✪✭✥✟✥✫■❨✦✩✴✫❃❄✷✱❦✿✬✒✦❀✴✶✲✫✷✩✥❀✦❀✬❪❛❜■▲✮✧✪✭✵❈✪✭■❏❃❧✹✽✪❳✦✩★❋✦❀★✫✷◗♠✡♥❪♦◗♣✾♦✽qr❖✫❃❄✰❏❑▲❃◆■▲✮✧✮✱✪❈✥✫❑✱✵❈■▲✥✫❑❏✴✫■▲❑❏✷❤✹✽✪❈✵✭✵✝❫✶✥✫✲ ✦❀★✶✪✭✬❭■❏✵✭✵✢✦❀✰✳✰❝✥✫■✦❀✴✫❃◆■▲✵✭s◆❩t✤☎✥✏❛❜■❏❘◆✦❀✸✢✦❀★✫✷❅❃❄✷✉■❏❃❄✷✉❘✩✵❈✰▲✬◆✷✉❘✩✰▲❃◆❃❄✷✩✬◆❖✈✰▲✥✫✲✫✷❅✥✫❘✩✷✩✬✇❵❉✷❱✦❱✹✉✷✩✷✩✥✏✲✶■❨✦❀■❏❵✫■▲✬◆✷◗✬✒❁✝✬✒✦❀✷❅✮✱✬✇■▲✥✫✲ ♠✡♥❪♦◗♣✾♦✽q◗❩✇♠❴♥❪♦◗♣✾♦◗q①❘✩■▲✥②❵✈✷③✦❀★✶✰▲✴✫❑❏★☞✦❂✰❏❛❪■▲✬❆■❙✲✫■❨✦✩■▲❵✫■❏✬❄✷✟✬✒❁✝✬✒✦✩✷✩✮④✹✽★✫✷✩❃◆✷✱■❏✵✭✵⑤✦❀★✶✷✏✲✫■❨✦✩■⑥❫❏✦✩✬❆✪❈✥☞✦✩✰ ✮✧■▲✪✭✥⑦✮✧✷✩✮✧✰▲❃✒❁✳❩⑧✤☎✥⑨❘❅✰▲✥❀✦❀❃❄■❏✬✒✦✩✸✉■⑩✲✶✪✭✬✒✦✩✪✭✥✫❑❏✴✫✪✭✬◆★✫✪✭✥✶❑✍❛❜✷✩■❨✦✩✴✫❃❄✷❙✰▲❛✉♥❤✐✉❶❪❷✍❸✫✬❝✪❈✬◗✦✩★✫■❨✦✧✦❀★✫✷❱❁❹✰▲❖✈✷✩❃◆■❨✦❀✷✍✰❏✥ ✬❄✷❅❘✩✰▲✥✫✲✶■▲❃✒❁⑨✬✒✦✩✰▲❃◆■▲❑▲✷❅❩❺♦❲✦✩★✫✷✩❃❲✦❀★✫■❏✥❻✦❀★✫■✦②❦✒■❏✥✫✲⑦✬❄✰❏✮✱✷✏✲✶✪✭❇✈✷✩❃❄✷❅✥✫❘✩✷✩✬◗✪✭✥②❊●✴✫✷✩❃✒❁⑨❖✫❃◆✰✳❘✩✷✩✬❄✬◆✪✭✥✫❑❏s❄✸❼✦✩★✫✷✩❃❄✷✧■▲❃◆✷ ✷◆❽✝❘✩✷✩✵❈✵✭✷✩✥❀✦✼■▲✥✶■▲✵✭✰❏❑▲✬✇✦✩✰❝❵✈✰❨✦❀★❙❘✩✴✫✵❣✦❀✴✫❃◆✷✩✬❄❩❪❶❪✰✦❀★✟♠✡♥❪♦✽♣✾♦◗q❾■❏✥✫✲✟♥t✐✉❶❪❷✍❸✶✬❪■▲❃❄✷◗✲✫✷❅❘✩✵✭■❏❃❄■❨✦✩✪❳❢❉✷✩❩➀❿➁★✫■❨✦❪✹✉✷ ➂ ✥✫✰✂✹➃✦❀✰✧❵✈✷❆❃❄✷❅✵✭■❨✦✩✪✭✰❏✥✫✬❪■▲❃◆✷❆❃❄✷❅❛❜✷✩❃❄❃◆✷✩✲③✦❀✰✧■▲✬✯➄✭❖✫❃◆✷✩✲✫✪✭❘❅■❨✦❀✷❅✬❄➅✝✪❈✥✟♠❴♥❪♦◗♣✾♦◗q◗❩➇➆➈✦✩✴✫❖✫✵✭✷◗✪❈✬❪❘✩■▲✵❈✵✭✷✩✲✟■✧❑▲❃❄✰❏✴✫✥✫✲ ❛❜■▲❘❱✦❋✪❈✥⑥♠✡♥❪♦◗♣✾♦✽q◗❩➉➆❹✦❀■❏❵✫✵✭✷③✪✭✬❤❘✩■▲✵❈✵✭✷✩✲✟■▲✥✍➄❈✷◆❽●✦❀✷✩✥✫✬◆✪✭✰❏✥✫■▲✵❧✲✫✷✩❫✫✥✶✪❳✦❀✪❈✰▲✥✫➅✾✪❈✥⑥♠✡♥❪♦◗♣✾♦◗q➊■❏✥✫✲✍✬❄✰✏✰❏✥✫❩➋❦✿✐✉✰ ✥✫✰✦✽❑▲✷◆✦✉❵❉✰❏❑▲❑❏✷✩✲⑥✲✫✰✂✹✽✥✏❵❀❁✧✦❀★✫✷✩✬◆✷✉✬❄❖✈✷✩❘❅✪✭❫✫❘✩✬◆➌●✹✉✷t✪❈✥✫❘✩✵✭✴✶✲✫✷✡✦✩★✫✷✩✮➍★✫✷✩❃◆✷ ❯ ✴✫✬✒✦✉✬❄✰✯✦✩★✫■❨✦✇❁❉✰▲✴✟❘✩■▲✥➎✮✱■ ➂ ✷✡✦✩★✫✷ ❘✩✰❏✥✫✥✫✷✩❘◆✦✩✪✭✰❏✥✫✸➉✪✭❛⑤❁❉✰❏✴⑩■❏❃❄✷❝■▲✵❈❃❄✷✩■❏✲❏❁➏❛❜■❏✮✱✪❈✵✭✪❈■▲❃✇✹✽✪❣✦❀★✍♠✡♥❪♦◗♣✾♦✽q◗❩✡➐✡✵✭✬◆✷✩✸➉✥✫✰✦❀★✫✪❈✥✫❑❋✦❀✰❋✹✻✰❏❃❄❃✒❁✳❩✭s➑❬❪★✫✷✯✦✩★✫✪✭❃◆✲ ❊●✴✫✷✩❃✒❁❂❃◆✷✩❖✫❃◆✷✩✬❄✷✩✥❀✦❀■✦❀✪❈✰▲✥✧✪✭✬❄✸❥✰▲❛✾❘✩✰❏✴✫❃❄✬◆✷✩✸✝❸✶➒✽♣➎✦✩★✫■❨✦✇✹✻■❏✬❪✪✭✥❀✦❀❃❄✰✳✲✶✴✫❘✩✷✩✲✧✷✩■▲❃◆✵✭✪❈✷✩❃❄❩✇✤❱✥✻✦❀★✶✷✉❃❄✷✩✮✧■▲✪❈✥✫✲✫✷✩❃✡✰❏❛✶✦❀★✫✪❈✬ ✲✫✰✳❘✩✴✶✮✱✷✩✥❀✦❀✸❼✹✉✷❋✹✽✪✭✵✭✵✡✪❈✥☞✦✩❃❄✰✳✲✫✴✫❘❅✷❂❵✶■▲✬❄✪❈❘✟✰▲❖✈✷✩❃❄■✦❀✪❈✰▲✥✫✬✧■▲✥✫✲⑦✮✧■▲✥✫✪❈❖✫✴✫✵❈■❨✦❀✪❈✰▲✥✫✬✯✦✩★✫■❨✦❝✹✉✷✟❘✩■▲✥⑦❖✈✷✩❃❄❛❜✰❏❃❄✮➓✰❏✥ ❃❄✷❅✵✭■❨✦✩✪✭✰❏✥✫✬❄❩✧➔✈✰▲❃◗✷✩■▲❘❨★✍✬❄✴✶❘→★✍❵✫■❏✬❄✪❈❘❝✰▲❖✈✷✩❃❄■✦❀✪❈✰▲✥✫✸➀✹✻✷✯✹✽✪✭✵❈✵➇✬◆★✫✰✂✹➣★✫✰✂✹➊✪❳✦❝✪✭✬◗❃❄✷✩❖✶❃❄✷✩✬◆✷✩✥☞✦✩✷✩✲⑥✪❈✥✍✷✩■▲❘❨★✍✰▲❛⑤✦✩★✫✷ ✦❀★✶❃❄✷✩✷◗✲✫✪❈❇❉✷❅❃❄✷✩✥❀✦✻✥✫✰✦❀■✦❀✪✭✰❏✥✫✬❄❩ ↔ ❩✉↕✟➙❧➛➝➜✈➙➞➜✈➟❪➠✏➡●➢➝➤✫➥❅➛➝➜✈➙❧➦☞➧❭❬❪★✫✷③✴✫✥✫✪❈✰▲✥✟✰▲❛❥✦☎✹✉✰❝❃❄✷✩✵❈■❨✦❀✪❈✰▲✥✫✬✼➨➁■▲✥✫✲❂➩❺✪✭✬✛✦❀★✫✷◗✬❄✷❱✦✽✰▲❛❧✷✩✵✭✷✩✮✧✷✩✥❀✦❀✬➀✦❀★✫■✦ ■❏❃❄✷✉✪❈✥➎➨✖✰❏❃❭✪❈✥➋➩➫✰▲❃❪❵✈✰✦❀★✫❩❼❿⑨✷✉■▲✬◆✬❄✴✫✮✧✷✇✦✩★✫■❨✦✇✦❀★✫✷❤✬❄❘❨★✫✷✩✮✧■▲✬✇✰▲❛➀➨✖■❏✥✫✲➋➩➏■▲❃◆✷✉■▲✵❈✪ ➂ ✷✽❦✒✰❏❛✾❘✩✰▲✴✶❃❄✬❄✷❅s ■❏✥✫✲③✦❀★✶■❨✦✡✦❀★✫✷❅✪✭❃➉❘✩✰❏✵✭✴✫✮✧✥✫✬✡■▲❃◆✷t■❏✵✭✬◆✰❆✰▲❃◆✲✫✷✩❃◆✷✩✲✱■❏✵✭✪ ➂ ✷✻❦✒✰❏❛✝❘✩✰❏✴✫❃❄✬◆✷✩✸✈■▲❑❏■▲✪✭✥✶s❄❩✛❿➈✷❪✥✫✰✂✹➁❑❏✪❳❢❉✷❼✦❀★✶✷✡✦✩★✫❃❄✷✩✷ ❃◆✷✩❖✫❃❄✷❅✬❄✷✩✥❀✦❀■✦❀✪✭✰❏✥✫✬❪✰▲❛✺✦❀★✶✷✽✴✶✥✫✪✭✰❏✥⑥❃❄✷❅✵✭■❨✦✩✪✭✰❏✥✫➧ ➨ ➩✖❦✒✬❄✪❈✮✱❖✶✵✭✷✩✸❥❃❄✪✭❑❏★☞✦✩➭☞s ➯✛➲❄➳✡➵❄➸✡➵❄➺✛➵✂➻✝➼⑦➽➚➾➏➪✡➲❄➳✛➵✂➸✛➵✂➺✛➵❄➻⑤➼❼➶ ➯✛➲❄➳✡➵❄➸✡➵❄➺✛➵✂➻✝➼⑦➽➚➾➏➹✡➲❄➳✛➵✂➸✛➵✂➺✛➵❄➻⑤➼❼➶ ➘✯✰❨✦❀✪❈❘✩✷❙✦✩★✫■❨✦✍✦✩★✫✷❙❢➚■▲❃◆✪✭■▲❵✶✵✭✷✩✬ ➳ ➵✂➸✛➵❄➺✡➵❄➻ ■❏❃❄✷➑✮✧✷✩❃❄✷❅✵❳❁➁➄✭❖✶✵✭■▲❘❅✷✩★✫✰▲✵❈✲✫✷✩❃◆✬❄➅◗✴✫✬❄✷❅✲➁❛❜✰❏❃✍❖✫■❨✦◆✦❀✷✩❃◆✥ ✛ ✮✧■❨✦✩❘→★✶✪✭✥✫❑❏➌✶✹✻✷✉❘✩✰❏✴✫✵✭✲✟★✫■✂❢❉✷❲✹✽❃❄✪❣✦❄✦✩✷✩✥❋✦❀★✫✷◗■▲❵✈✰✂❢❉✷❲✦☎✹✻✰❝■▲✬◆➧ ➯✛➲❄➴✡➵❄➷✡➵❀➬✝➵✂➮✝➼⑦➽➚➾➏➪✡➲❄➴✛➵✂➷✛➵☞➬✝➵❄➮⑤➼❼➶ ẽẹé ềểễếệễỉìĩíễĩịòỏễệõóòòọõìồổìòỗì ịốộồờịễởờĩỡệịờễốóớịòợịổùĩợễệễờóùọủũ úú ụừử ữứựừứ ẽẹé ụừử ỳ ỷỹýỵt t s ➨ ➩ ➨✯❩ ➩ ➯✛➲❄➳✡➵❄➸✡➵❄➺✛➵✂➻✝➼⑦➽➚➾➏➪✡➲❄➳✛➵✂➸✛➵✂➺✛➵❄➻⑤➼❉➵✏ø⑤õ❀➯➞➹✡➲❄➳✛➵✂➸✛➵✂➺✛➵❄➻⑤➼✛➶ ➲❄➹●❮➚❰●❮➚Ï●➯ĐÐ ơ●➪✝õ☞ư➞➪✝➼ ❮✂✁●Ï➚❮✂✄➚➯ ➲❄➹●❮➚❰●❮➚Ï●➯ĐÐ ơ●➪✝õ☞ư➞➹✝➼ ýỵ ẹ ■❏❃❄✷❭■❏✵✭✪ ✷❴■❏✥✫✲✯✦❀★✶■❨✦➉✦✩★✫✷✩✪✭❃✾❘❅✰▲✵✭✴✶✮✱✥✫✬❧■▲❃◆✷❪■▲✵❈✬❄✰❤✰▲❃❄✲✶✷✩❃❄✷✩✲❝■❏✵✭✪ ➂ ✷✩❩➉➘✉✰✦❀✪✭❘❅✷✾✦✩★✫■❨✦✼➨ ➩✏➃ ✎ ➨ ❦➝➨ ➩✛s◆❩ ➨ ➩ ➯✛➲❄➳✡➵❄➸✡➵❄➺✛➵✂➻✝➼⑦➽➚➾➏➪✡➲❄➳✛➵✂➸✛➵✂➺✛➵❄➻⑤➼❉➵✏➹✡➲❄➳✡➵❄➸✛➵✂➺✛➵✂➻❉➼✛➶ ➲❄➹●❮➚❰●❮➚Ï●➯ĐÐ ơ●➪✝õ☞ư➞➪✝➼ ù❀ø●➯➚❮●➪➚➹●❮●Ï➚➯ ➲❄➹●❮➚❰●❮➚Ï●➯ĐÐ ơ●➪✝õ☞ư➞➹✝➼ ❩ ✒◗ý☞➜✔✓ ✑ ỵ ẽ ụừử ỵ t ✦✩★✫✷❲✦❀✴✫❖✫✵❈✷✩✬❪❛❜❃❄✰❏✮➣➨❹✹✽★✶✷✩❃❄✷❲✦❀★✶✷✽✥✶■▲✮✧✷✽✪❈✬❪➄✭❷✍✪❈❘→★✫■❏✷✩✵✭➅❈❩ ó✻✺ ✼☞✘✛✚✢✜✤✣✢✽✿✾❁❀❃❂❁❄❆❅✡✚★✣✮❇❉❈ ❈ ➨ ➯✛➲❄➳✡➵❄➸✡➵❄➺✛➵✂➻✝➼⑦➽➚➾➏➪✡➲❄➳✛➵✂➸✛➵✂➺✛➵❄➻⑤➼❉➵✏➳❋❊❍●✂ư❏■●➬☞❒●➴➚➱✙❑❏▲✝➶ ➹●❮●❰➚❮●Ï➚➯➁Ð ơ●➪✝õ☞ư➞➪ ▼✂◆●❮➚➪●❮✳✲➚➴✂✴●➱✳❊❖●❄ư✷■●➬❀❒➚➴●➱✷❑❏▲ ❩ ýỵ ỗts ử ➘✉✰✦❀✪❈❘✩✷❤✦✩★✫■❨✦◗✹✻✷❝➄❈✬❄❖✫✵❈✪❳✦✩➅❥✦❀★✫✷❝❘❅✰▲✥✫✲✫✪❣✦❀✪❈✰▲✥✍■▲❘❅❃❄✰▲✬◆✬❤✦☎✹✻✰➎❃❄✴✫✵❈✷✩✬❄✸ ❯ ✴✶✬✒✦✱■▲✬❲✹✻✷✧✲✫✰✟✪✭✥➎✦✩★✫✷❝✴✫✥✫✪❈✰▲✥②❘✩■▲✬◆✷ ❦❳❯❪✰▲✮✧✷③✦❀✰ ✦❀★✫✪❈✥ ➂ ✰▲❛❤✪❳✦✩✸❧✦❀★✶✷✱♦◗♥➍✪✭✬❆✪❈✥✫✲✫✷✩✷❅✲✟✦✩★✫✷✱✴✶✥✫✪✭✰❏✥❺✰❏❛❼✦☎✹✻✰⑥❘❅✰▲✥✫✲✫✪❣✦❀✪❈✰▲✥✫✬◆s❄❩②❸✫✪✭✮✧✪✭✵❈■▲❃❄✵❣❁✢✸❥✦✩★✫✷ ❘✩✰❏✮✱✮✧■✏✪❈✥⑥✷✩■❏❘→★❙✰▲❛⑤✦❀★✫✷❝■❏❵✈✰ ❢❉✷❝✲✶■❨✦❀■❏✵✭✰❏❑❂❃◆✴✫✵✭✷❅✬t✮✧✰✳✲✫✷✩✵❈✬✇✦❀★✫✷❝➆✯➘✉✐❞❘✩✰❏✥✫✲✫✪❣✦❀✪✭✰❏✥✫❩❝♣✾✷◆✦✩➅✭✬❤❘✩✰▲✥✫✬◆✪✭✲✫✷❅❃ ■✏✮✧✰▲❃◆✷❆❘✩✰❏✮✱❖✶✵✭✪✭❘❅■❨✦❀✷❅✲❂❘❅✰▲✥✫✲✫✪❣✦❀✪❈✰▲✥✫➧◗❸✫✷✩✵❈✷✩❘◆✦✽■❏✵✭✵✺✦❀★✫✷✼✦❀✴✫❖✫✵❈✷✩✬❤❛❜❃❄✰▲✮①♥❹✦❀★✫■✦ t ửụ ỗs ụ ú ýýỵ ẹt ẹ ✦❀★✫✷❝❃◆✷✩✵✭■✦❀✪✭✰❏✥⑥✥✫■❏✮✱✷❅❩✽❬❪★✶✷❝❘✩■▲❃✿✦❀✷✩✬◆✪✭■▲✥❙❖✫❃❄✰✳✲✶✴✫❘◆✦ ✪✭✬❤❑▲✪❣❢❉✷✩✥✏❵❀❁⑤➧ ➩ ➯✛➲❄➳✷❛✝➵✂➸❏❛✝➵✂➺❏❛⑤➵❱➻❏❛❉➵❄➳❋❜❼➵❄➸❋❜❼➵❄➺✂❜❼➵✂➻☞❜✝➼⑦➽➚➾➏➪✡➲❄➳❏❛⑤➵❄➸✷❛✝➵❄➺✷❛✝➵❱➻✷❛✳➼✝➵✱➹✡➲❄➳✂❜✡➵❄➸❋❜✛➵❄➺❋❜✛➵❱➻❋❜✝➼❼➶ ➹●❮●❰➚❮●Ï➚➯➞➪❤➶✯✲➚➴✂✴➚➱✛➵➋➪❤➶✒➴➚➮●➮❋✵●➱❏✶❋✶❉➵✏➪t➶✒❐➚➱✂✲●➮➚➱✂✵✡➵✱➪❤➶➝➷❏■❝✵❋❞●❒➚➮●➴✂❞✳➱✡➵ ➹❤➶✯✲➚➴✂✴➚➱✛➵➋➹❤➶✒➴➚➮●➮❋✵●➱❏✶❋✶❉➵✏➹t➶✒❐➚➱✂✲●➮➚➱✂✵✡➵✱➹❤➶➝➷❏■❝✵❋❞●❒➚➮●➴✂❞✳➱ ơ●➪✝õ☞ư➞➪✛➵✂➹ ❡ ➘✉✰✦❀✪❈❘✩✷◗★✫✰✂✹➃✹✻✷③✲✫✪✭✬◆■▲✮❝❵✫✪❈❑▲✴✫■✦❀✷❆■✦❄✦✩❃❄✪✭❵✶✴❏✦❀✷✩✬❤✪✭✥ ✦❀✴✫❖✫✵❈✷✩✬❪■▲✥✶✲❻➩ ★✶■▲✬❲❢❻✦❀✴✫❖✫✵❈✷✩✬❄✸✶✦✩★✫✷✩✥❂➨ ✦❀★✫✷◗❸✫➒◗♣❻❢❉✷✩❃◆✬❄✪✭✰❏✥✫❩✉➆✉✵❈✬❄✰✧✥✫✰❨✦✩✪✭❘✩✷✼✦❀★✫■✦❋✪❈❛✛➨r★✶■▲✬ ➩❍✹✽✪❈✵✭✵✝★✶■ ❢❉✷ ó✻❣ ❡ ❢❻✦❀✴✶❖✫✵✭✷✩✬◆❩ ❩ ❤❥✐➇➡●➥✩➤❧❦★♠✝➜✈➛➝➙❼➧✟❬❪★✫✪✭✬❝✪❈✬ ❯ ✴✫✬✒✦❂✵❈✪ ➂ ✷✯✦✩★✫✷✟❘✩■▲❃✿✦❀✷✩✬◆✪✭■▲✥⑦❖✫❃◆✰✳✲✫✴✫❘◆✦❂❵✶✴❏✦❂❑▲✰✳✷✩✬✧■✍✬✒✦✩✷✩❖⑦❛❜✴✫❃✒✦✩★✫✷✩❃◆❩❺➆✯❛ä✦❀✷✩❃ ✺ ✝ ❛❜✰❏❃❄✮✧✪✭✥✫❑✯✦✩★✫✷ ❡ ❻ ❢ ✦❀✴✫❖✶✵✭✷✩✬◆✸✝✪❳✦✉✬❄✷✩✵❈✷✩❘◆✦✩✬❭✰▲✥✶✵❳❁❍■❝✬◆✴✫❵✫✬❄✷❱✦✽✰▲❛✺✦❀★✫✷✩✮Ñ✦❀✰❝✪❈✥✫❘✩✵❈✴✫✲✫✷✉✪❈✥✏✪❳✦✩✬❪■▲✥✫✬✿✹✻✷✩❃◆✸✾❵✫■▲✬◆✷✩✲ ✰❏✥⑧✬◆✰▲✮✧✷✍❘✩✰▲✥✫✲✶✪❳✦❀✪❈✰▲✥✫❩❾❬❪★●✴✫✬◆✸❤✦❀★✶✷➎✦❀★✫✷◆✦✩■❨❚ ❯ ✰▲✪❈✥➞✰▲❛❤✦☎✹✉✰➑❃❄✷✩✵❈■❨✦✩✪✭✰▲✥✶✬✏★✫■▲✬✽✦❀★✫✷✍✬◆■▲✮✧✷✍✥➚✴✫✮❝❵✈✷✩❃➎✰▲❛ ❘✩✰❏✵✭✴✫✮✧✥✫✬❤■▲✬✛✦❀★✫✷③❘✩■▲❃✿✦❀✷✩✬◆✪✭■❏✥⑥❖✫❃◆✰✳✲✫✴✫❘◆✦❆❵✫✴❏✦❆✥✫✰❨✦❝✥✫✷✩❘✩✷✩✬◆✬❄■❏❃❄✪✭✵❣❁✏✦❀★✶✷✽✬◆■▲✮✧✷❆✥●✴✫✮❝❵✈✷✩❃❪✰▲❛✡❃◆✰ ✹✽✬❆❦✒✬◆✰▲✮✧✷ ✰❏❛❪✦✩★✫✷⑥❃◆✰✂✹✽✬◗✹✽✪❈✵✭✵❤❵❉✷❙❃❄✷✩✮✧✰✂❢❉✷✩✲⑦❵✈✷✩❘✩■▲✴✶✬❄✷➎✦❀★✶✷◆❁❹✲✫✪❈✬❄✬❄■✦❀✪❈✬❄❛ä❁Ñ✬◆✰▲✮✧✷⑥❘✩✰❏✥✫✲✫✪❣✦❀✪✭✰❏✥✫s❄❩❫❯❪✰❏✥✫✬❄✪❈✲✫✷✩❃✏❛❜✰❏❃ ✷◆❽✝■▲✮✧❖✫✵❈✷✩✸ ✦❀★✫■✦❪✹✻✷❲✹✻■❏✥☞✦✇✦❀✰❝❫✫✥✫✲✟➄❈❖✫■▲✪❈❃❄✬❄➅✾✰❏❛➇✬✿✦❀✴✫✲✶✷✩✥☞✦✩✬❭✬◆✴✫❘→★❋✦✩★✫■❨✦❪✦✩★✫✷◗❫✫❃❄✬✿✦✽❖❉✷❅❃❄✬❄✰❏✥✟✪✭✥❋✦❀★✫✷✯❖✫■▲✪❈❃ ✪❈✬❪■▲✵❳✹✉■✂❁⑤✬❪➄❈❷✍✪✭❘❨★✫■▲✷✩✵❈➅✭❩⑤❿⑨✷✽❑❏✷◆✦❀➧ ➨♦♥✮♣✻qsr ✘t✚★✜✉✣★✽❧✈ ❀❃❂❁❄❆❅✡✚★✣✮❇❉❈ ➩ ➯✛➲❄➳✷❛✝➵✂➸❏❛✝➵✂➺❏❛⑤➵❱➻❏❛❉➵❄➳❋❜❼➵❄➸❋❜❼➵❄➺✂❜❼➵✂➻☞❜✝➼⑦➽➚➾➏➪✡➲❄➳❏❛⑤➵❄➸✷❛✝➵❄➺✷❛✝➵❱➻✷❛✳➼✝➵✱➹✡➲❄➳✂❜✡➵❄➸❋❜✛➵❄➺❋❜✛➵❱➻❋❜✝➼❉➵✱➳✷❛❝❊❍●✂ư❏■➚➬❀❒●➴➚➱❏❑✙▲⑤➶ ➹●❮●❰➚❮●Ï➚➯➞➪❤➶✯✲➚➴✂✴➚➱✛➵➋➪❤➶✒➴➚➮●➮❋✵●➱❏✶❋✶❉➵✏➪t➶✒❐➚➱✂✲●➮➚➱✂✵✡➵✱➪❤➶➝➷❏■❝✵❋❞●❒➚➮●➴✂❞✳➱✡➵ ➹❤➶✯✲➚➴✂✴➚➱✛➵➋➹❤➶✒➴➚➮●➮❋✵●➱❏✶❋✶❉➵✏➹t➶✒❐➚➱✂✲●➮➚➱✂✵✡➵✱➹❤➶➝➷❏■❝✵❋❞●❒➚➮●➴✂❞✳➱ ơ●➪✝õ☞ư➞➪✛➵✂➹ ▼✂◆●❮➚➪●❮➞➪t➶✯✲●➴❋✴●➱✇❊①●❄ư❏■➚➬❀❒➚➴●➱❏❑✷▲ ➘✉✰✦❀✪❈❘✩✷⑤✦❀★✫■✦➇✹✉✷❴✪❈✥☞✦✩❃❄✰✳✲✫✴✶❘✩✷❧✦✩★✫✷✡➄✭❵✈✰✂✹❪✦❀✪✭✷❅➅✳✬✒❁✝✮❝❵❉✰❏✵②♥✯♣③✬❄✴❧③✻❽➚❚❄✷✩✲③❵☞❁✉✦✩★✫✷❭❘❅✰▲✥✫✲✫✪❣✦❀✪❈✰▲✥❆❛❜✰❏❃➇✪❈✥✫✲✫✪❈❘✩■❨✦✩✪✭✥✫❑ ✦✩★✫✷❲✦❀★✫✷◆✦✩■❨❚ ❯ ✰▲✪❈✥✫❩❪➆✉✵✭✬◆✰▲✸✾❃◆✷✩■▲✵❈✪⑤④✩✷✇✦✩★✫■❨✦ ➨⑥♥✯♣⑧⑦➫➩✏✎ ❩ ⑨❙➤✫➥⑩❵❧ý✔➤✶➢✖♠✝➜✈➛✒➙➉➧❙❬❪★✫✪✭✬③✪✭✬ ❣ ✝ ✼ ⑦❪❦✒➨ ➩✛s ❯ ✴✫✬✿✦➋■✍❘✩✵❈✷◆❢❉✷✩❃◆✷✩❃✇✹✻■✂❁➑✦✩✰✍❘✩✰▲✮❝❵✶✪✭✥✫✷✻✦☎✹✻✰⑥❃◆✷✩✵✭■✦❀✪❈✰▲✥✫✬❝✪❈✥☞✦✩✰⑥✰▲✥✶✷✩❩⑦❬❪★✫✷ ❵✫■❏✬❄✪❈❘✱✪❈✲✫✷✩■✟✪✭✬❲✦❀★✶■❨✦➋✪❈❛⑤✦❀★✫✷✻✦☎✹✻✰✟❃❄✷✩✵❈■❨✦✩✪✭✰▲✥✶✬✽★✶■ ❢❉✷✧✬❄✰❏✮✱✷✧❘✩✰❏✵✭✴✫✮✧✥➀❦✒✬◆s◗✪✭✥②❘✩✰▲✮✧✮✱✰❏✥✫✸⑤✦❀★✫✷❅✥✟✹✉✷✱❘✩■❏✥ ➄❈❘✩✰▲✵❈✵✭■▲❖✶✬❄✷✩➅❥✦✩★✫✷✩✮ ✪❈✥☞✦✩✰✧✦❀★✶✷✱✬◆■▲✮✧✷✏❘✩✰▲✵❈✴✫✮✧✥⑩✪❈✥✟✦✩★✫✷✱❫✶✥✫■▲✵✇✰▲✴❏✦✩❖✫✴❏✦❀❩②❷✍✰▲❃◆✷✩✰✂❢❉✷✩❃❄✸⑤✹✉✷✱❘✩■❏✥➑✲✫✰ ✦❀★✫✪❈✬ ✰❏✥✫✵❳❁⑥✪✭❛✢✦❀★✫✷✇✦☎✹✉✰❤✦❀✴✶❖✫✵✭✷✩✬✡❛❜❃◆✰▲✮Ñ✦❀★✫✷✇✦☎✹✉✰✽❃◆✷✩✵✭■✦❀✪❈✰▲✥✫✬✇■▲❑▲❃◆✷✩✷✉✪❈✥③✦❀★✫✰❏✬❄✷✉❘❅✰▲✮✧✮✱✰❏✥✏❘✩✰▲✵❈✴✫✮✧✥✫✬❄❩✇❬❪★➚✴✫✬◆✸✝✪❣✦ ✪❈✬✉✬❄✪❈✮✱✪❈✵✭■▲❃❼✦✩✰③✦❀★✶✷❝❘✩■▲❃✿✦❀✷✩✬◆✪✭■▲✥✍❖✶❃❄✰✳✲✫✴✫❘❱✦❀✸➉❵✫✴✔✦✉✹✻✷③➄ ❯ ✰▲✪❈✥✫➅❧✰▲✥✫✵❣❁⑥✦❀★✶✰▲✬❄✷❝❖✶■▲✪✭❃◆✬✇✦❀★✶■❨✦ ✮✧■❨✦✩❘→★❙✪✭✥➎✦❀★✶✷✩✪✭❃ ❘✩✰❏✮✱✮✧✰▲✥✧■❨✦◆✦❀❃◆✪✭❵✫✴❏✦✩✷✩✬❄❩❶❯❪✰▲✥✫✬◆✪✭✲✫✷❅❃❧✦❀★✶■❨✦✡✹✻✷✛✹✻■❏✥☞✦✡✦❀✰◗❫✫✥✶✲③✦❀★✫✷❤✥✫■❏✮✱✷✩✸✈■❏✲✫✲✫❃❄✷❅✬❄✬❄✸✈❑❏✷✩✥✫✲✫✷✩❃◆✸✈❑▲❖✫■③■▲✥✫✲ ❵✫✪❈❃✒✦✩★✫✲✫■❨✦✩✷❆✰❏❛➉✬✒✦✩✴✫✲✫✷✩✥❀✦❀✬❤✪✭✥✟■✏✬◆✪✭✥✫❑❏✵✭✷◗❃❄✷✩✵❈■❨✦✩✪✭✰▲✥✶❩✉➘✉✰❨✦✩✪✭❘✩✷❴✦❀★✫■✦ ✰✦❀★✫✷✩❃✡❛❜✰❏✴✫❃➉■❨✦◆✦❀❃◆✪✭❵✫✴❏✦✩✷✩✬✡■▲❃◆✷❪❖✫❃❄✷✩✬◆✷✩✥❀✦✼✪✭✥ ❑▲❖✫■✧✪✭✬❤■✂❢➚■▲✪✭✵❈■▲❵✫✵❈✷◗❛❜❃❄✰▲✮❸❷➈❵✫✴✔✦t✦✩★✫✷ ➨✯❩❪❸✶✰▲✸ ✹✻✷❪✥✫✷❅✷✩✲❝■❲✹✻■✂❁❆✦✩✰✽✪❈✥☞✦✩✷✩✵✭✵❈✪✭❑❏✷✩✥☞✦✩✵❳❁❆❘✩✰▲✮❝❵✶✪✭✥✫✷➀✦❀★✫✷✩✬◆✷ ✦☎✹✻✰❝❃◆✷✩✵✭■✦❀✪❈✰▲✥✫✬◆➧ ➨♦♥✮♣❹❷ ➯✛➲❄➳✡➵❄➸✡➵❄➺✛➵✦❺✛➵✂➻❉➼⑦➽●➾⑦➪✛➲❄➳✡➵❄➸✡➵❄➺✛➵✂➻✝➼⑤➵✱❰✛➲✂➳✛➵❄➸✡➵✢❺⑤➼✛➶ ➹●❮●❰➚❮●Ï➚➯➞➪❤➶✯✲➚➴✂✴➚➱✛➵➋➪❤➶✧➴➚➮●➮❋✵●➱❏✶❋✶✝➵✱➪t➶✒❐➚➱✂✲●➮➚➱✂✵✡➵✏❰t➶✒❐✂❻➚➴✛➵➋➪❤➶➝➷❏■❝✵❋❞●❒➚➮●➴✂❞✳➱ ơ●➪✝õ☞ư➞➪✛➵✂❰ ▼✂◆●❮➚➪●❮➞➪t➶✯✲●➴❋✴●➱❋❊●❰❤➶✮✲✳➴✂✴➚➱ ❼ ø✂⑧ ❽ ➪❤➶➝➴●➮●➮❋●✵ ➱✷✶✂✶❾⑧ ❊ ❰❤➶✒➴➚➮●➮❋✵●➱❏✶❋✶ ↔✛❿ ❩✽➠✏➡●➙❧➤❧➀➑➛✒➙✉➁❥➧❭❬❪★✶✪✭✬❤✪✭✬ ❯ ✴✫✬✿✦❋■❝❘✩✰✳✰❏✵✈✦✩★✫✪✭✥✶❑▲✸✾✪✭✥✟❘✩■❏✬❄✷❲✹✻✷◗★✫■✂❢❉✷❲✦❀✰✳✰✧✮✧■▲✥❀❁❍✥✫■▲✮✧✪✭✥✶❑❝❘✩✰▲✥❧➂✶✪✭❘◆✦✩✬❪■▲✥✫✲ ❘✩✰❏✥✫❛❜✴✫✬◆✪✭✰▲✥✶✬✉■▲❃◆✪✭✬❄✪❈✥✫❑▲❩✼❿➈✷❝❘❅■▲✥⑥✴✶✬❄✷✯✦❀★✶✪✭✬❤✰▲❖✈✷✩❃❄■✦❀✰❏❃❪✦✩✰✏❃❄✷❅✥✫■▲✮✧✷❆■✏❃◆✷✩✵✭■✦❀✪❈✰▲✥✫➅❈✬t✥✫■❏✮✱✷③■▲✥✫✲✶▼▲✰▲❃◗✰❏✥✫✷ ✰❏❃t✮✧✰▲❃◆✷✽✰❏❛✡✪❳✦❀✬❪■✦❄✦✩❃❄✪✭❵✶✴❏✦❀✷✩✬◆❩✉➔✈✰▲❃❪✷◆❽✝■▲✮✧❖✫✵❈✷✩✸✾■▲✬◆✬❄✴✫✮✧✷❤✹✉✷❪✹✉■▲✥❀✦t✦✩✰✱❃◆✷✩✥✫■❏✮✱✷✽➨❹✦✩✰➄➃ ✪❣✦❀✬❤❘✩✰▲✵❈✴✫✮✱✥✶✬❼✦❀✰✏❵✈✷❆❘❅■▲✵✭✵❈✷✩✲ ✵❈✪ ➂ ✷❤✬❄✰▲➧ ➅ ❀➇➆➈✜➊➉✫✥ ✜❶➋★✥ ✜s➌t➍ ❡ ↔ ✸ ❡ ú ■▲✥✶✲ ❡ ☎ ❦➝➨✯s ✺ ❿ ■❏✥✫✲⑥✮✧■ ➂ ✷ ❩◗❬❪★✫✪❈✬t✪❈✬t✮✧✰▲✬✿✦❋✴✫✬◆✷✩❛❜✴✫✵✺✹✽✪❳✦✩★❂❃◆✷✩✵✭■✦❀✪❈✰▲✥✫■❏✵➇■❏✵✭❑▲✷❅❵✫❃❄■❏✸ CS 5614: Misc SQL Stuff, Safety in Queries 81 What is still to be covered (and will be) V V V V Declaring constraints V Domain Constraints V Referential Integrity (Foreign Keys) More SQL Stuff V Subqueries V Aggregation SQL Peculiarities V Strange Phenomena V More on Bag Semantics V Ifs and Buts Embedding SQL in a Programming Environment V Accessing DBs from within a PL V (will be covered in Module 3) CS 5614: Misc SQL Stuff, Safety in Queries What will be mentioned (but not covered in detail) V V V Triggers V Read Cow Book or Boat Book More SQL Gory Details Recursive Queries (SQL3) V Why we need these? V Security V Authorization and Privacy V Trends towards Object Oriented DBMSs 82 CS 5614: Misc SQL Stuff, Safety in Queries 91 Summary of SQL Syntax V General Form SELECT FROM WHERE GROUP BY HAVING V Order of Execution V FROM V WHERE V GROUP BY V HAVING V SELECT CS 5614: Misc SQL Stuff, Safety in Queries Views V V Can be viewed as temporary relations V not exist physically BUT V can be queried and modified (sometimes) just like normal relations Example: CREATE VIEW GoodStudents(id,name) AS SELECT sid,name FROM Students WHERE gpa=4.0; SELECT * FROM GoodStudents WHERE name=’Mark’; V Can we update the original relation using the GoodStudents VIEW? 92 CS 5614: Misc SQL Stuff, Safety in Queries 93 Beginning of Wierd Stuff V V V V V V SQL uses Bag Semantics V meaning: does not normally eliminate duplicates V e.g the SELECT clause BUT (a big BUT) this doesn’t apply to V UNION, INTERSECT and DIFFERENCE Either way, it provides facilities to whatever we want If you want duplicates eliminated in SELECT clause V use SELECT DISTINCT If you want to prevent elimination of duplicates in UNION etc V use (SELECT ) UNION ALL (SELECT ) V Likewise for INTERSECT and DIFFERENCE CS 5614: Misc SQL Stuff, Safety in Queries and that’s just the tip of the iceberg V What happens with the following code? SELECT R.A FROM R,S,T WHERE R.A = S.A or R.A = T.A V when R(A) has {2,3}, S(A) has {3,4} and T(A) is {} V Confusion Reigns! 94 CS 5614: Misc SQL Stuff, Safety in Queries 95 Safety in Queries V V Some queries are inherently “unsafe” V should not be permitted in DB access Example V Given only the following relation Students(id) V Find all those who are not students V Easy to distinguish unsafe queries via common-sense V Final result is not closed V Is there an automatic way to determine “safety”? CS 5614: Misc SQL Stuff, Safety in Queries Answer: Yes! V Easiest to spot when written in Datalog Answer(id)