Presentation is loading. Please wait.

Presentation is loading. Please wait.

M.P. Johnson, DBMS, Stern/NYU, Sp20041 C20.0046: Database Management Systems Lecture #12 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.

Similar presentations


Presentation on theme: "M.P. Johnson, DBMS, Stern/NYU, Sp20041 C20.0046: Database Management Systems Lecture #12 Matthew P. Johnson Stern School of Business, NYU Spring, 2004."— Presentation transcript:

1 M.P. Johnson, DBMS, Stern/NYU, Sp20041 C20.0046: Database Management Systems Lecture #12 Matthew P. Johnson Stern School of Business, NYU Spring, 2004

2 M.P. Johnson, DBMS, Stern/NYU, Sp2004 2 Agenda Last time: Started SQL This time: More SQL 1. Grouping & aggregation 2. Modifications 3. Defining schemata 4. Views Homework 2 is due soon Future: 1. Constraints & Triggers 2. Programming for SQL

3 M.P. Johnson, DBMS, Stern/NYU, Sp2004 3 Quick review TRUE AND UNKNOWN = ? TRUE OR UNKNOWN = ? UNKNOWN OR UNKNOWN = ? Examples from sqlzoo.netsqlzoo.net

4 M.P. Johnson, DBMS, Stern/NYU, Sp2004 4 Grouping & Aggregation (6.4) In R.A.:  operator In SQL:  aggregation operators in SELECT,  Grouping in GROUP BY clause Recall aggregation operators:  sum, avg, min, max, count strings, numbers, dates  Each applies to scalars  Count also applies to row: count(*)  Can DISTINCT inside aggregation op: count(DISTINCT x) Grouping: group rows that agree on single value  Each group becomes one row in result

5 M.P. Johnson, DBMS, Stern/NYU, Sp2004 5 Straight aggregation In R.A.  sum(x)  total (R) In SQL: Just put the aggregation op in SELECT NB: aggreg. ops applied to each non-null val  count(x) counts the number of nun-null vals in field x  Use count(*) to count the number of rows SELECT SUM(x) AS total FROM R SELECT SUM(x) AS total FROM R

6 M.P. Johnson, DBMS, Stern/NYU, Sp2004 6 Straight Aggregation example COUNT applies to duplicates, unless otherwise stated: Better: SELECT Count(category) FROM Product WHERE year > 1995 SELECT Count(category) FROM Product WHERE year > 1995 SELECT Count(DISTINCT category) FROM Product WHERE year > 1995 SELECT Count(DISTINCT category) FROM Product WHERE year > 1995 same as Count(*), except excludes nulls

7 M.P. Johnson, DBMS, Stern/NYU, Sp2004 7 Straight Aggregation example Purchase(product, date, price, quantity) Q: Find total sales for the entire database: Q: Find total sales of bagels: SELECT SUM(price * quantity) FROM Purchase SELECT SUM(price * quantity) FROM Purchase SELECT SUM(price * quantity) FROM Purchase WHERE product = ‘bagel’ SELECT SUM(price * quantity) FROM Purchase WHERE product = ‘bagel’

8 M.P. Johnson, DBMS, Stern/NYU, Sp2004 8 Straight grouping Group rows together by field values Produces one row for each group  I.e., by each (combin of) grouped val(s)  Don’t select non-grouped fields Reduces to DISTINCT selections: SELECT region FROM cia GROUP BY region SELECT region FROM cia GROUP BY region SELECT DISTINCT region FROM cia SELECT DISTINCT region FROM cia

9 M.P. Johnson, DBMS, Stern/NYU, Sp2004 9 Grouping & aggregation Sometimes want to group and compute aggregations by group  Aggreg. op applied to rows in group, not rows in table Q: How many countries in each region? SELECT region, count(name) AS number FROM cia GROUP BY region SELECT region, count(name) AS number FROM cia GROUP BY region

10 M.P. Johnson, DBMS, Stern/NYU, Sp2004 10 Evaluation of G&A Evaluation steps: Compute the FROM-WHERE part as usual to obtain a table with all attributes in R 1,…,R n Group by the attributes a 1,…,a k Compute the aggregates in C 2 and keep only groups satisfying C 2 Compute aggregates in S and return the result SELECTS FROMR 1,…,R n WHEREC 1 GROUP BYa 1,…,a k HAVINGC 2 SELECTS FROMR 1,…,R n WHEREC 1 GROUP BYa 1,…,a k HAVINGC 2

11 M.P. Johnson, DBMS, Stern/NYU, Sp2004 11 G & A for constructed relations Can do the same thing for larger, non-atomic relations Scenario:  Movie(title,year,producerSsn)  MovieExec(name,ssn,netWorth) Q: How many mins. of film did each producer make?  What happens to non-producer movie-execs? SELECT name, sum(length) AS total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name SELECT name, sum(length) AS total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name

12 M.P. Johnson, DBMS, Stern/NYU, Sp2004 12 Illustrated G&A example Usually want aggregations on certain parts of the relation Purchase(product, date, price, quantity) Find total sales after 10/18 per product that sold > $20: SELECT product, SUM(price*quantity) AS TotalSales FROM Purchase WHERE date > DATE ’10-18-2003’ GROUP BY product SELECT product, SUM(price*quantity) AS TotalSales FROM Purchase WHERE date > DATE ’10-18-2003’ GROUP BY product

13 M.P. Johnson, DBMS, Stern/NYU, Sp2004 13 Illustrated G&A example Purchase

14 M.P. Johnson, DBMS, Stern/NYU, Sp2004 14 First compute the FROM-WHERE clauses (date > DATE ’10-18-2003’) then GROUP BY product: Illustrated G&A example

15 M.P. Johnson, DBMS, Stern/NYU, Sp2004 15 SELECT product, SUM(price*quantity) AS TotalSales FROM Purchase WHERE d date > DATE ’10-18-2003’ GROUPBY product SELECT product, SUM(price*quantity) AS TotalSales FROM Purchase WHERE d date > DATE ’10-18-2003’ GROUPBY product Finally, aggregate: Illustrated G&A example

16 M.P. Johnson, DBMS, Stern/NYU, Sp2004 16 Illustrated G&A example GROUP BY may be reduced to subquery SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > DATE ’10-18-2003’ GROUP BY product SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > DATE ’10-18-2003’ GROUP BY product SELECT DISTINCT x.product, (SELECT Sum(y.price*y.quantity) FROM Purchase y WHERE x.product = y.product AND y.date > DATE ’10-18-2003’) AS TotalSales FROM Purchase x WHERE x.date > DATE ’10-18-2003’ SELECT DISTINCT x.product, (SELECT Sum(y.price*y.quantity) FROM Purchase y WHERE x.product = y.product AND y.date > DATE ’10-18-2003’) AS TotalSales FROM Purchase x WHERE x.date > DATE ’10-18-2003’

17 M.P. Johnson, DBMS, Stern/NYU, Sp2004 17 SELECT product, Sum(price * quantity) AS SumSales Max(quantity) AS MaxQuantity FROM Purchase GROUP BY product SELECT product, Sum(price * quantity) AS SumSales Max(quantity) AS MaxQuantity FROM Purchase GROUP BY product For every product, what is the total sales and max quantity sold? Another example

18 M.P. Johnson, DBMS, Stern/NYU, Sp2004 18 HAVING clauses Sometimes we want to limit which tuples may be grouped Q: How many mins. of film did each rich producer (i.e., netWorth > 10000000) make? Q: Is HAVING necessary here? A: No, could just add rich req. to SELECT SELECT name, sum(length) AS total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name HAVING netWorth > 10000000 SELECT name, sum(length) AS total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name HAVING netWorth > 10000000

19 M.P. Johnson, DBMS, Stern/NYU, Sp2004 19 HAVING clauses Sometimes we want to limit which tuples may be grouped, based on properties of the group Q: How many mins. of film did each old producer (i.e., started before 1930) make? SELECT name, sum(length) AS total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name HAVING min(year) < 1930 SELECT name, sum(length) AS total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name HAVING min(year) < 1930

20 M.P. Johnson, DBMS, Stern/NYU, Sp2004 20 General form of G&A S = may contain attributes As and/or any aggregates but no other attributes C1 = condition on the attributes in R 1,…,R n C2 = condition on aggregations or attributes from As Why? NB: “Any attribute of relations in the FROM clause may be aggregated in the HAVING clause, but only those attributes that are in the GROUP by list may appear unaggregated in the HAVING clause (the same rule as for the SELECT clause)” (p283). SELECTS FROMR1,…,Rn WHEREC1 GROUP BYAs HAVINGC2 Why?

21 M.P. Johnson, DBMS, Stern/NYU, Sp2004 21 More A&G Examples Web pages, and their authors: Author(login,name) Document(url, title) Wrote(login,url) Mentions(url,word)

22 M.P. Johnson, DBMS, Stern/NYU, Sp2004 22 Web page examples Find all authors who wrote at least 10 documents Author(login,name) Wrote(login,url) Attempt 1: with nested queries SELECT DISTINCT Author.name FROM Author WHERE count(SELECT Wrote.url FROM Wrote WHERE Author.login=Wrote.login) > 10 SELECT DISTINCT Author.name FROM Author WHERE count(SELECT Wrote.url FROM Wrote WHERE Author.login=Wrote.login) > 10 Bad!

23 M.P. Johnson, DBMS, Stern/NYU, Sp2004 23 Web page examples Find all authors who wrote at least 10 documents: Attempt 2: Simplify with GROUP BY SELECT Author.name FROM Author, Wrote WHERE Author.login=Wrote.login GROUP BY Author.name HAVING count(wrote.url) > 10 SELECT Author.name FROM Author, Wrote WHERE Author.login=Wrote.login GROUP BY Author.name HAVING count(wrote.url) > 10 Good! No need for DISTINCT: get for free from GROUP BY

24 M.P. Johnson, DBMS, Stern/NYU, Sp2004 24 Web page examples Find all authors who have a vocabulary over 10000 words: SELECT Author.name FROM Author, Wrote, Mentions WHERE Author.login=Wrote.login AND Wrote.url=Mentions.url GROUP BY Author.name HAVING count(distinct Mentions.word) > 10000 SELECT Author.name FROM Author, Wrote, Mentions WHERE Author.login=Wrote.login AND Wrote.url=Mentions.url GROUP BY Author.name HAVING count(distinct Mentions.word) > 10000

25 M.P. Johnson, DBMS, Stern/NYU, Sp2004 25 Summary: SQL queries Only SELECT, FROM required Can’t have HAVING without GROUP BY Can have GROUP BY without HAVING Any clauses must appear in this order: SELECT L FROM Rs WHERE s GROUP BY L2 HAVING s2 ORDER BY L3 SELECT L FROM Rs WHERE s GROUP BY L2 HAVING s2 ORDER BY L3

26 M.P. Johnson, DBMS, Stern/NYU, Sp2004 26 New topic: Modifications (6.5) Three kinds of modifications 1. Insertions 2. Deletions 3. Updates Sometimes “update” used as a synonym for “modification”

27 M.P. Johnson, DBMS, Stern/NYU, Sp2004 27 Insertions General form: Missing attribute  NULL (or other default value) INSERT INTO R(A1,…., An) VALUES (v1,…., vn) INSERT INTO Purchase(buyer, seller, product, store) VALUES (‘Joe’, ‘Fred’, ‘wakeup-clock-espresso-machine’, ‘The Sharper Image’) INSERT INTO Purchase(buyer, seller, product, store) VALUES (‘Joe’, ‘Fred’, ‘wakeup-clock-espresso-machine’, ‘The Sharper Image’) Example: Insert a new purchase to the database:

28 M.P. Johnson, DBMS, Stern/NYU, Sp2004 28 Insertions If we’re sure we have all all values in the right order, can just say: Only do this if you’re sure of order INSERT INTO R VALUES (v1,…., vn) INSERT INTO Purchase VALUES (‘Joe’, ‘Fred’, ‘wakeup-clock-espresso-machine’, ‘The Sharper Image’) INSERT INTO Purchase VALUES (‘Joe’, ‘Fred’, ‘wakeup-clock-espresso-machine’, ‘The Sharper Image’)

29 M.P. Johnson, DBMS, Stern/NYU, Sp2004 29 Insertions Can insert the result of a query; Scenario:  Product(name, etc.)  Purchase(buyerssn, prodName, etc.)  Maybe some purchases name missing products   add those to the Product table  Subquery replaces VALUES INSERT INTO R(As) (query) INSERT INTO R(As) (query)

30 M.P. Johnson, DBMS, Stern/NYU, Sp2004 30 Insertion example Premise data corruption  lose some Product data  every product referred to in Purchase should exist in Product, but some are missing namelistPricecategory gizmo100gadgets prodNamebuyerName cameraJohn gizmoSmith cameraSmith Product Product(name, listPrice, category) Purchase(prodName, buyerName, price) Product(name, listPrice, category) Purchase(prodName, buyerName, price) Purchase

31 M.P. Johnson, DBMS, Stern/NYU, Sp2004 31 Insertion example namelistPricecategory gizmo100Gadgets cameraNULL INSERT INTO Product(name) SELECT prodName FROM Purchase WHERE prodName NOT IN (SELECT name FROM Product) INSERT INTO Product(name) SELECT prodName FROM Purchase WHERE prodName NOT IN (SELECT name FROM Product) namelistPricecategory gizmo100gadgets ProductProduct’ prodNamebuyerName cameraJohn gizmoSmith cameraSmith Purchase cameraNULL Q: Or do we get: A: Depends on implementation!

32 M.P. Johnson, DBMS, Stern/NYU, Sp2004 32 Deletions General form: Example: Q: How do you delete just one row with SQL simpliciter? A: You can’t! Although Oracle has the ROWID pseudo-field… As usual, WHERE can contain subqueries DELETE FROM PURCHASE WHERE seller = ‘Joe’ AND product = ‘Brooklyn Bridge’ DELETE FROM PURCHASE WHERE seller = ‘Joe’ AND product = ‘Brooklyn Bridge’ DELETE FROM Table WHERE condition DELETE FROM Table WHERE condition

33 M.P. Johnson, DBMS, Stern/NYU, Sp2004 33 Updates General form: Example: As usual, WHERE can contain subqueries UPDATE Product SET field1 = value1, field2 = value2 WHERE condition UPDATE Product SET field1 = value1, field2 = value2 WHERE condition UPDATE Product SET price = price/2 WHERE Product.name IN (SELECT product FROM Purchase WHERE Date = DATE‘Oct, 25, 1999’); UPDATE Product SET price = price/2 WHERE Product.name IN (SELECT product FROM Purchase WHERE Date = DATE‘Oct, 25, 1999’);

34 M.P. Johnson, DBMS, Stern/NYU, Sp2004 34 New topic: Defining schemata (6.6) So far, have done queries and data manipulation Now doing data definition Recall data types:  INT or INTEGER (variant: SHORTINT)  FLOAT or REAL: floating-point numbers DOUBLE PRECISION: DECIMAL(n,d):  E.g. decimal(5,2): five decimal digits, with the decimal point two positions from the right: e.g. 123.45  DATE and TIME  Character strings Fixed length: CHAR(n) Variable length: VARCHAR(n)

35 M.P. Johnson, DBMS, Stern/NYU, Sp2004 35 Creating tables Form: Example: CREATE TABLE Table-name ( field field-type, … field field-type ) CREATE TABLE Table-name ( field field-type, … field field-type ) No comma! CREATE TABLE People ( nameVARCHAR(30), ssnCHAR(9), ageSHORTINT, cityVARCHAR(30), genderBIT(1), BirthdateDATE ) CREATE TABLE People ( nameVARCHAR(30), ssnCHAR(9), ageSHORTINT, cityVARCHAR(30), genderBIT(1), BirthdateDATE ) Not supported in Oracle

36 M.P. Johnson, DBMS, Stern/NYU, Sp2004 36 Default Values Specify defaults when creating table: The default default: NULL CREATE TABLE People ( nameVARCHAR(30), ssnCHAR(9), ageSHORTINTDEFAULT 100, cityVARCHAR(30)DEFAULT ‘New York’, genderBIT(1), BirthdateDATEDEFAULT DATE ‘0000-00’00’, ) CREATE TABLE People ( nameVARCHAR(30), ssnCHAR(9), ageSHORTINTDEFAULT 100, cityVARCHAR(30)DEFAULT ‘New York’, genderBIT(1), BirthdateDATEDEFAULT DATE ‘0000-00’00’, )

37 M.P. Johnson, DBMS, Stern/NYU, Sp2004 37 Deleting and modifying schemata Delete data, indices, schema: Delete data and indices: Either way, use EXTREME CAUTION! Add or delete attributes: ALTER TABLE Person ADD phone CHAR(12); ALTER TABLE Person DROP age; ALTER TABLE Person ADD phone CHAR(12); ALTER TABLE Person DROP age; Q: What’s put in the new fields? DROP TABLE Person TRUNCATE TABLE Person

38 M.P. Johnson, DBMS, Stern/NYU, Sp2004 38 New topic: Indices Very important speeding up query processing Index on field(s) = data structure that makes searches/comparisons on those fields fast Suppose we have a relation Person (name, age, city) Sequential scan of the whole Person file may take a very long time SELECT * FROM Person WHERE name = ‘Waksal’ SELECT * FROM Person WHERE name = ‘Waksal’

39 M.P. Johnson, DBMS, Stern/NYU, Sp2004 39 Creating Indices Syntax: Here: No searching by name is much faster How much faster? Log-time, say Base-what? Doesn’t matter, but say 2 If all New Yorkers, #comparisons: 8000000  log 2 (8000000) ~= 23 (i.e., 2 23 ~= 8000000) CREATE INDEX nameIndex ON Person(name) CREATE INDEX index-name ON R(field(s))

40 M.P. Johnson, DBMS, Stern/NYU, Sp2004 40 How do indices work? What the data structure?  Different possibilities 1 st intuition: index on field f is an ordered list of all values in the table’s f field  each item has address (“rowid”) of its row Where do we get the ordered list? 2 nd intuition: put all f values in a BST  searching BST take log time (why?) DBMSs actually use a variant: B+Tree  more later…

41 M.P. Johnson, DBMS, Stern/NYU, Sp2004 41 Creating Indices Indexes can be useful in range queries too: CREATE INDEX ageIndex ON Person (age) SELECT * FROM Person WHERE age > 25

42 M.P. Johnson, DBMS, Stern/NYU, Sp2004 42 Using indices Indices can be created on multiple attributes: Helps in: And in: But not in: SELECT * FROM Person WHERE age = 55 AND city = “Seattle” SELECT * FROM Person WHERE city = “Seattle” CREATE INDEX doubleindex ON Person (age, city) SELECT * FROM Person WHERE age = 55 Idea: our sorted list is sorted on age;city, not city;age Q: In Movie tbl, should index be on year;title or title;year?

43 M.P. Johnson, DBMS, Stern/NYU, Sp2004 43 The Index Selection Problem Big Q: Why not just indexes all the fields?  how does the list/B+Tree stay up to day? We are given a workload: a set of SQL queries and their frequencies Q is: What indices should we build to speed up the workload? Attributes in FROM/WHERE clauses  favor an index Attributes in INSERT/UPDATE clauses  discourage an index Usually: your primary key fields get index (why?)

44 M.P. Johnson, DBMS, Stern/NYU, Sp2004 44 New topic: Views (6.7) Stored relations physically exist and persist Views are relations that don’t  in text, “table” = stored relation = “base table” Basically names/references given to queries  maybe a relevant subset of a table Employee(ssn, name, department, project, salary) Payroll has access to Employee, others only to Developers CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = “Development” CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = “Development”

45 M.P. Johnson, DBMS, Stern/NYU, Sp2004 45 A Different View Person(name, city) Purchase(buyer, seller, product, store) Product(name, maker, category) We have a new virtual table: Seattle-view(buyer, seller, product, store) CREATE VIEW Seattle-view AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = “Seattle” AND Person.name = Purchase.buyer CREATE VIEW Seattle-view AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = “Seattle” AND Person.name = Purchase.buyer

46 M.P. Johnson, DBMS, Stern/NYU, Sp2004 46 A Different View Now we can query the view: SELECT name, store FROM Seattle-view, Product WHERE Seattle-view.product = Product.name AND Product.category = “shoes” SELECT name, store FROM Seattle-view, Product WHERE Seattle-view.product = Product.name AND Product.category = “shoes” CREATE VIEW Seattle-view AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = “Seattle” AND Person.name = Purchase.buyer CREATE VIEW Seattle-view AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = “Seattle” AND Person.name = Purchase.buyer

47 M.P. Johnson, DBMS, Stern/NYU, Sp2004 47 What happens when we query a view ? SELECT name, Seattle-view.store FROM Seattle-view, Product WHERE Seattle-view.product = Product.name AND Product.category = “shoes” SELECT name, Seattle-view.store FROM Seattle-view, Product WHERE Seattle-view.product = Product.name AND Product.category = “shoes” SELECT name, Purchase.store FROM Person, Purchase, Product WHERE Person.city = “Seattle” AND Person.name = Purchase.buyer AND Purchase.poduct = Product.name AND Product.category = “shoes” SELECT name, Purchase.store FROM Person, Purchase, Product WHERE Person.city = “Seattle” AND Person.name = Purchase.buyer AND Purchase.poduct = Product.name AND Product.category = “shoes”

48 M.P. Johnson, DBMS, Stern/NYU, Sp2004 48 Can rename view fields CREATE VIEW Seattle-view(seabuyer, seaseller, prod, store) AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = “Seattle” AND Person.name = Purchase.buyer CREATE VIEW Seattle-view(seabuyer, seaseller, prod, store) AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = “Seattle” AND Person.name = Purchase.buyer

49 M.P. Johnson, DBMS, Stern/NYU, Sp2004 49 Types of Views Views discussed here:  Used in databases  Computed only on-demand – slow at runtime  Always up to date Sometimes talk about “materialized” views  Used in data warehouses  Pre-computed offline – fast at runtime  May have stale data  More later…

50 M.P. Johnson, DBMS, Stern/NYU, Sp2004 50 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department, project, salary) CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = “Development” CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = “Development” INSERT INTO Developers VALUES(“Joe”, “Optimizer”) INSERT INTO Employee(ssn, name, department, project, salary) VALUES(NULL, “Joe”, NULL, “Optimizer”, NULL) If we make the following insertion: It becomes:

51 M.P. Johnson, DBMS, Stern/NYU, Sp2004 51 Non-Updatable Views Person(name, city) Purchase(buyer, seller, product, store) How can we add the following tuple to the view? (“Seattle”, “Nine West”) We don’t know the name of the person who made the purchase cannot set to NULL (why?) CREATE VIEW City-Store AS SELECT Person.city, Purchase.store FROM Person, Purchase WHERE Person.name = Purchase.buyer CREATE VIEW City-Store AS SELECT Person.city, Purchase.store FROM Person, Purchase WHERE Person.name = Purchase.buyer

52 M.P. Johnson, DBMS, Stern/NYU, Sp2004 52 Live Examples Examples from sqlzoo.netsqlzoo.net For next time: finish chapter 6 (skip last subsection of views), real chapter 7 (all)

53 M.P. Johnson, DBMS, Stern/NYU, Sp2004 53 INTERSECT and EXCEPT: Not in SQL Server (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) SELECT R.A, R.B FROM R WHERE EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B) SELECT R.A, R.B FROM R WHERE EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B) (SELECT R.A, R.B FROM R) EXCEPT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM R) EXCEPT (SELECT S.A, S.B FROM S) SELECT R.A, R.B FROM R WHERE NOT EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B) SELECT R.A, R.B FROM R WHERE NOT EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B) If R, S have no duplicates, then can write without subqueries (HOW ?)

54 M.P. Johnson, DBMS, Stern/NYU, Sp2004 54 Constraints in SQL A constraint = a property that we’d like our database to hold The system will enforce the constraint by taking some actions:  forbid an update  or perform compensating updates

55 M.P. Johnson, DBMS, Stern/NYU, Sp2004 55 Constraints in SQL Constraints in SQL: Keys, foreign keys Attribute-level constraints Tuple-level constraints Global constraints: assertions The more complex the constraint, the harder it is to check and to enforce simplest Most complex

56 M.P. Johnson, DBMS, Stern/NYU, Sp2004 56 Keys OR: CREATE TABLE Product ( name CHAR(30) PRIMARY KEY, category VARCHAR(20)) CREATE TABLE Product ( name CHAR(30) PRIMARY KEY, category VARCHAR(20)) CREATE TABLE Product ( name CHAR(30), category VARCHAR(20) PRIMARY KEY (name)) CREATE TABLE Product ( name CHAR(30), category VARCHAR(20) PRIMARY KEY (name))

57 M.P. Johnson, DBMS, Stern/NYU, Sp2004 57 Keys with Multiple Attributes CREATE TABLE Product ( name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (name, category)) CREATE TABLE Product ( name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (name, category)) NameCategoryPrice GizmoGadget10 CameraPhoto20 GizmoPhoto30 GizmoGadget40

58 M.P. Johnson, DBMS, Stern/NYU, Sp2004 58 Other Keys CREATE TABLE Product ( productID CHAR(10), name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (productID), UNIQUE (name, category)) CREATE TABLE Product ( productID CHAR(10), name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (productID), UNIQUE (name, category)) There is at most one PRIMARY KEY; there can be many UNIQUE

59 M.P. Johnson, DBMS, Stern/NYU, Sp2004 59 Foreign Key Constraints CREATE TABLE Purchase ( prodName CHAR(30) REFERENCES Product(name), date DATETIME) CREATE TABLE Purchase ( prodName CHAR(30) REFERENCES Product(name), date DATETIME) prodName is a foreign key to Product(name) name must be a key in Product Referential integrity constraints

60 M.P. Johnson, DBMS, Stern/NYU, Sp2004 60 NameCategory Gizmogadget CameraPhoto OneClickPhoto ProdNameStore GizmoWiz CameraRitz CameraWiz ProductPurchase

61 M.P. Johnson, DBMS, Stern/NYU, Sp2004 61 Foreign Key Constraints OR (name, category) must be a PRIMARY KEY CREATE TABLE Purchase ( prodName CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category) CREATE TABLE Purchase ( prodName CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category)

62 M.P. Johnson, DBMS, Stern/NYU, Sp2004 62 NameCategory Gizmogadget CameraPhoto OneClickPhoto ProdNameStore GizmoWiz CameraRitz CameraWiz ProductPurchase What happens during updates ? Types of updates: In Purchase: insert/update In Product: delete/update

63 M.P. Johnson, DBMS, Stern/NYU, Sp2004 63 What happens during updates ? SQL has three policies for maintaining referential integrity: Reject violating modifications (default) Cascade: after a delete/update do a delete/update Set-null set foreign-key field to NULL READING ASSIGNEMNT: 7.1.5, 7.1.6

64 M.P. Johnson, DBMS, Stern/NYU, Sp2004 64 Constraints on Attributes and Tuples Constraints on attributes: NOT NULL-- obvious meaning... CHECK condition-- any condition ! Constraints on tuples CHECK condition

65 M.P. Johnson, DBMS, Stern/NYU, Sp2004 65 CREATE TABLE Purchase ( prodName CHAR(30) CHECK (prodName IN SELECT Product.name FROM Product), date DATETIME NOT NULL) CREATE TABLE Purchase ( prodName CHAR(30) CHECK (prodName IN SELECT Product.name FROM Product), date DATETIME NOT NULL) What is the difference from Foreign-Key ?

66 M.P. Johnson, DBMS, Stern/NYU, Sp2004 66 General Assertions CREATE ASSERTION myAssert CHECK NOT EXISTS( SELECT Product.name FROM Product, Purchase WHERE Product.name = Purchase.prodName GROUP BY Product.name HAVING count(*) > 200) CREATE ASSERTION myAssert CHECK NOT EXISTS( SELECT Product.name FROM Product, Purchase WHERE Product.name = Purchase.prodName GROUP BY Product.name HAVING count(*) > 200)

67 M.P. Johnson, DBMS, Stern/NYU, Sp2004 67 Final Comments on Constraints Can give them names, and alter later  Read in the book !!! We need to understand exactly when they are checked We need to understand exactly what actions are taken if they fail

68 M.P. Johnson, DBMS, Stern/NYU, Sp2004 68 For next time Begin chapter 6 Come to office hours!


Download ppt "M.P. Johnson, DBMS, Stern/NYU, Sp20041 C20.0046: Database Management Systems Lecture #12 Matthew P. Johnson Stern School of Business, NYU Spring, 2004."

Similar presentations


Ads by Google