Matthew P. Johnson, OCL4, CISDD CUNY, Sept OCL4 Oracle 10g: SQL & PL/SQL Session #5 Matthew P. Johnson CISDD, CUNY June, 2005
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Agenda Grouping & Aggregation Updates Creating tables This afternoon: PL/SQL
Matthew P. Johnson, OCL4, CISDD CUNY, Sept New topic: Grouping & Aggregation 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
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Aggregation functions Numerical: SUM, AVG, MIN, MAX Char: MIN, MAX In lexocographic/alphabetic order Any attribute: COUNT Number of values SUM(B) = 10 AVG(A) = 1.5 MIN(A) = 1 MAX(A) = 3 COUNT(A) = 4 AB
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Acc(name,bal,type) Q: Who has the largest balance? Can we do this with aggregation functions?
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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) total FROM R SELECT SUM(x) total FROM R
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Straight aggregation example COUNT applies to duplicates, unless otherwise stated: Better: Can we say: same as Count(*), except excludes nulls 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 SELECT category, COUNT(category) FROM Product WHERE year > 1995 SELECT category, COUNT(category) FROM Product WHERE year > 1995
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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'
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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 product FROM Purchase GROUP BY product SELECT product FROM Purchase GROUP BY product SELECT DISTINCT product FROM Purchase SELECT DISTINCT product FROM Purchase
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Illustrated G&A example Sometimes want to group and then compute aggregations by group Aggregation op applied to rows in group, not to all rows in table Purchase(product, date, price, quantity) Find total sales for products that sold for > 0.50: SELECT product, SUM(price*quantity) total FROM Purchase WHERE price >.50 GROUP BY product SELECT product, SUM(price*quantity) total FROM Purchase WHERE price >.50 GROUP BY product
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Illustrated G&A example Purchase
Matthew P. Johnson, OCL4, CISDD CUNY, Sept First compute the FROM-WHERE then GROUP BY product: Illustrated G&A example
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Finally, aggregate and select: Illustrated G&A example SELECT product, SUM(price*quantity) total FROM Purchase WHERW price >.50 GROUP BY product SELECT product, SUM(price*quantity) total FROM Purchase WHERW price >.50 GROUP BY product
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Illustrated G&A example GROUP BY may be reduced to (maybe more complicated) subquery: SELECT product, SUM(price*quantity) total FROM Purchase WHERE price >.50 GROUP BY product SELECT product, SUM(price*quantity) total FROM Purchase WHERE price >.50 GROUP BY product SELECT DISTINCT x.product, (SELECT SUM(y.price*y.quantity) FROM Purchase y WHERE x.product = y.product AND y.price >.50) total FROM Purchase x WHERE x.price >.50 SELECT DISTINCT x.product, (SELECT SUM(y.price*y.quantity) FROM Purchase y WHERE x.product = y.product AND y.price >.50) total FROM Purchase x WHERE x.price >.50
Matthew P. Johnson, OCL4, CISDD CUNY, Sept For every product, what is the total sales and max quantity sold? Multiple aggregations SELECT product, SUM(price * quantity) SumSales, MAX(quantity) MaxQuantity FROM Purchase WHERE price >.50 GROUP BY product SELECT product, SUM(price * quantity) SumSales, MAX(quantity) MaxQuantity FROM Purchase WHERE price >.50 GROUP BY product
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Another grouping/aggregation e.g. Movie(title, year, length, studioName) Q: How many total minutes of film have been produced by each studio? Strategy: Divide movies into groups per studio, then add lengths per group
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Another grouping/aggregation e.g. TitleYearLengthStudio Star Wars Fox Jedi Fox Aviator Miramax Pulp Fiction Miramax Lost in Translation Universal SELECT studio, sum(length) totalLength FROM Movies GROUP BY studio SELECT studio, sum(length) totalLength FROM Movies GROUP BY studio
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Another grouping/aggregation e.g. TitleYearLengthStudio Star Wars Fox Jedi Fox Aviator Miramax Pulp Fiction Miramax Lost in Translation Universal SELECT studio, sum(length) totalLength FROM Movies GROUP BY studio SELECT studio, sum(length) totalLength FROM Movies GROUP BY studio
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Another grouping/aggregation e.g. TitleYearLengthStudio Star Wars Fox Jedi Fox Aviator Miramax Pulp Fiction Miramax Lost in Translation Universal StudioLength Fox225 Miramax910 Universal95 SELECT studio, sum(length) totalLength FROM Movies GROUP BY studio SELECT studio, sum(length) totalLength FROM Movies GROUP BY studio
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Grouping/aggregation example StarsIn(SName,Title,Year) Q: Find the year of each star’s first movie year Q: Find the span of each star’s career Look up first and last movies SELECT sname, min(year) firstyear FROM StarsIn GROUP BY sname SELECT sname, min(year) firstyear FROM StarsIn GROUP BY sname
Matthew P. Johnson, OCL4, CISDD CUNY, Sept G & A for constructed relations Movie(title,year,producerSsn,length) 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) total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name SELECT name, sum(length) total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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 > ) make? Q: Is HAVING necessary here? A: No, could just add rich req. to WHERE SELECT name, sum(length) total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name, netWorth HAVING netWorth > SELECT name, sum(length) total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name, netWorth HAVING netWorth >
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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., who started before 1930) make? Q: Is HAVING necessary here? SELECT name, sum(length) total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name HAVING min(year) < 1930 SELECT name, sum(length) total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name HAVING min(year) < 1930
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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)” (Ullman, p283). Why? SELECTS FROMR1,…,Rn WHERE C1 GROUP BYAs HAVINGC2 SELECTS FROMR1,…,Rn WHERE C1 GROUP BYAs HAVINGC2
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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 WHERE C 1 GROUP BYa 1,…,a k HAVINGC 2 SELECTS FROMR 1,…,R n WHERE C 1 GROUP BYa 1,…,a k HAVINGC 2
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Web page examples Find all authors who wrote at least 10 documents Authors(login, name) Webpages(url, title, login) Attempt 1: with nested queries Bad! SELECT DISTINCT name FROM Authors WHERE (SELECT COUNT(url) FROM Webpages WHERE Authors.login=Webpages.login) > 10 SELECT DISTINCT name FROM Authors WHERE (SELECT COUNT(url) FROM Webpages WHERE Authors.login=Webpages.login) > 10
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Web page examples Find all authors who wrote at least 10 documents: Attempt 2: Simplify with GROUP BY Good! No need for DISTINCT: get for free from GROUP BY SELECT name FROM Authors, Webpages WHERE Authors.login=Webpages.login GROUP BY name HAVING count(Webpages.url) > 10 SELECT name FROM Authors, Webpages WHERE Authors.login=Webpages.login GROUP BY name HAVING count(Webpages.url) > 10
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Web page examples Find all authors who have a vocabulary over words: Authors(login, name) Webpages(url, title, login) Mentions(url, word) SELECT name FROM Authors, Webpages, Mentions WHERE Authors.login=Wrote.login AND Webpages.url=Mentions.url GROUP BY name HAVING count(distinct word) > SELECT name FROM Authors, Webpages, Mentions WHERE Authors.login=Wrote.login AND Webpages.url=Mentions.url GROUP BY name HAVING count(distinct word) > 10000
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Summary: SQL queries Only SELECT, FROM required Can’t have HAVING without GROUP BY Can have GROUP BY without HAVING Any clauses used must appear in this order: SELECTL FROMRs WHEREs GROUP BYL2 HAVINGs2 ORDER BYL3 SELECTL FROMRs WHEREs GROUP BYL2 HAVINGs2 ORDER BYL3
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Break Work on lab 4 (join, sum/count) Examples from sqlzoo.netsqlzoo.net
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Agenda More SQL 1. Modifications 2. Defining schemata 3. Views 4. Constraints
Matthew P. Johnson, OCL4, CISDD CUNY, Sept New topic: Modifications Three kinds of modifications 1. Insertions 2. Deletions 3. Updates Sometimes “update” used as a synonym for “modification”
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Insertions General form: Missing attribute NULL (or other default value) Example: Insert a new purchase to the database: INSERT INTO R(A1,…., An) VALUES(v1,….,vn) INSERT INTO Knights(name, britnatl, title) VALUES('Bill Gates', 'n', 'KBE')
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Insertions If we’re sure we have all values in the right order, can just say: Only do this if you’re sure of order in which the table fields were defined INSERT INTO R VALUES(v1,….,vn) INSERT INTO Knights VALUES('R. Giuliani', 'n', 'KBE'); INSERT INTO Knights VALUES('Bernard Kerik', 'n', 'CBE'); INSERT INTO Knights VALUES('R. Giuliani', 'n', 'KBE'); INSERT INTO Knights VALUES('Bernard Kerik', 'n', 'CBE');
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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)
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Insertion example Product(name, listPrice, category) Purchase(prodName, buyerName, price) Premise: data corruption lose some Product data every product referred to in Purchase should exist in Product, but some are missing namelistPricecategory Canon D101000Camera Canon D202000Camera prodNamebuyerName Canon D10Bill Canon D10Hilary Canon D20George ProductPurchase
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Insertion example namelistPricecategory Canon D10NULL Canon D20NULL namelistPricecategory ProductProduct’ prodNamebuyerName Canon D10Bill Canon D20Hilary Canon D20George Purchase Canon D20NULL Q: Or do we get: A: Depends on implementation! 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)
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Deletions General form: E.g.: As usual, WHERE can contain subqueries Depending on the DBMS Q: How do you delete just one row with SQL simpliciter? Oracle has the ROWID/ROWNUM pseudo-field… DELETE FROM Table WHERE condition DELETE FROM Table WHERE condition INSERT INTO Knights VALUES('R. Giuliani', 'n', 'KBE'); INSERT INTO Knights VALUES('Bernard Kerik', 'n', 'CBE'); DELETE FROM Knights WHERE name = 'Bernard Kerik'; INSERT INTO Knights VALUES('R. Giuliani', 'n', 'KBE'); INSERT INTO Knights VALUES('Bernard Kerik', 'n', 'CBE'); DELETE FROM Knights WHERE name = 'Bernard Kerik';
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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 = 'Oct, 25, 1999') UPDATE Product SET price = price/2 WHERE Product.name IN (SELECT product FROM Purchase WHERE Date = 'Oct, 25, 1999')
Matthew P. Johnson, OCL4, CISDD CUNY, Sept New topic: Defining schemata 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 numbers: number(n,d): E.g. number l(5,2): five decimal digits, with the decimal point two positions from the right: e.g DATE and TIME Character strings Fixed length: CHAR(n) Variable length: VARCHAR(n)
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Creating tables Form: E.g.: CREATE TABLE Table-name ( field1 field-type, field2 field-type, … fieldn field-type ) CREATE TABLE Table-name ( field1 field-type, field2 field-type, … fieldn field-type ) No comma! CREATE TABLE People ( name VARCHAR(30), ssn CHAR(9), age INT, city VARCHAR(255), gender CHAR(1), dob DATE ) CREATE TABLE People ( name VARCHAR(30), ssn CHAR(9), age INT, city VARCHAR(255), gender CHAR(1), dob DATE )
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Default Values Specify defaults when creating table: The default default: NULL CREATE TABLE People ( name VARCHAR(30), ssn CHAR(9), age SHORTINT DEFAULT 100, city VARCHAR(255) DEFAULT 'New York', gender CHAR(1), dob DATE DEFAULT DATE ' ' ) CREATE TABLE People ( name VARCHAR(30), ssn CHAR(9), age SHORTINT DEFAULT 100, city VARCHAR(255) DEFAULT 'New York', gender CHAR(1), dob DATE DEFAULT DATE ' ' )
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Deleting and modifying schemata Delete data, indices, schema: Delete data and indices: Either way, exercise extreme caution! Add or delete attributes: Q: What’s put in the new fields? DROP TABLE Person TRUNCATE TABLE Person ALTER TABLE Person ADD phone CHAR(12) ALTER TABLE Person ADD phone CHAR(12) ALTER TABLE Person DROP age ALTER TABLE Person DROP age
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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, Sam' SELECT * FROM Person WHERE name = 'Waksal, Sam'
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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: log 2 ( ) ~= 23 (i.e., 2 23 ~= ) CREATE INDEX index-name ON R(fields) CREATE INDEX nameIndex ON Person(name)
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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 takes log time (why?) DBMSs actually use a variant: B+Tree See Ullman’s book or data structures texts…
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Creating Indices Indexes can be useful in range queries too: CREATE INDEX ageIndex ON Person (age) SELECT * FROM Person WHERE age > 25
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Using indices Indices can be created on multiple attributes: Helps in: And in: But not in: Idea: our sorted list is sorted on age;city, not city;age Q: In Movie, should index be on year;title or title;year? CREATE INDEX doubleNdx ON Person (lname, fname) SELECT * FROM Person WHERE fname='Sam' AND lname='Waksal' SELECT * FROM Person WHERE lname='Waksal' SELECT * FROM Person WHERE fname='Sam'
Matthew P. Johnson, OCL4, CISDD CUNY, Sept The Index Selection Problem Big Q: Why not just index all (sequences of) fields? how does the list/B+Tree stay up to date? 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? Answer: Attributes in WHERE clauses (queries) favor an index Attributes in INSERT/UPDATE/DELETEs discourage an index In many DBMSs: your primary key fields get indexed automatically (why?)
Matthew P. Johnson, OCL4, CISDD CUNY, Sept New topic: Views Stored relations physically exist and persist Views are relations that don’t in some texts, “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 = 'Dev' CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = 'Dev'
Matthew P. Johnson, OCL4, CISDD CUNY, Sept A Different View Person(name, city) Purchase(buyer, seller, product, store) Product(name, maker, category) We have a new virtual table: NYCview(buyer, seller, product, store) CREATE VIEW NYCview AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = 'New York' AND Person.name = Purchase.buyer CREATE VIEW NYCview AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = 'New York' AND Person.name = Purchase.buyer
Matthew P. Johnson, OCL4, CISDD CUNY, Sept A Different View Now we can query the view: CREATE VIEW NYCview AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = 'New York' AND Person.name = Purchase.buyer CREATE VIEW NYCview AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = 'New York' AND Person.name = Purchase.buyer SELECT name, NYCview.store FROM NYCview, Product WHERE NYCview.product = Product.name AND Product.category = 'Camera' SELECT name, NYCview.store FROM NYCview, Product WHERE NYCview.product = Product.name AND Product.category = 'Camera'
Matthew P. Johnson, OCL4, CISDD CUNY, Sept What happens when we query a view? SELECT name, NYCview.store FROM NYCview, Product WHERE NYCview.product = Product.name AND Product.category = 'Camera' SELECT name, NYCview.store FROM NYCview, Product WHERE NYCview.product = Product.name AND Product.category = 'Camera' SELECT name, Purchase.store FROM Person, Purchase, Product WHERE Person.city = 'New York' AND Person.name = Purchase.buyer AND Purchase.poduct = Product.name AND Product.category = 'Camera' SELECT name, Purchase.store FROM Person, Purchase, Product WHERE Person.city = 'New York' AND Person.name = Purchase.buyer AND Purchase.poduct = Product.name AND Product.category = 'Camera'
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Can rename view fields CREATE VIEW NYCview(NYCbuyer, NYCseller, prod, store) AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = 'New York' AND Person.name = Purchase.buyer CREATE VIEW NYCview(NYCbuyer, NYCseller, prod, store) AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = 'New York' AND Person.name = Purchase.buyer
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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 Maybe more later…
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Updating Views How to insert a tuple into a table that doesn’t exist? Employee(ssn, name, department, project, salary) If we make the following insertion: It becomes: 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('Bill', 'Word') INSERT INTO Employee(ssn, name, dept, project, sal) VALUES(NULL, 'Bill', NULL, 'Word', NULL) INSERT INTO Employee(ssn, name, dept, project, sal) VALUES(NULL, 'Bill', NULL, 'Word', NULL)
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Non-Updatable Views Person(name, city) Purchase(buyer, seller, product, store) How can we add the following tuple to the view? ('NYC', 'The Wiz') We don’t know the name of the person who made the purchase cannot set to NULL (why?) CREATE VIEW CityStore AS SELECT Person.city, Purchase.store FROM Person, Purchase WHERE Person.name = Purchase.buyer CREATE VIEW CityStore AS SELECT Person.city, Purchase.store FROM Person, Purchase WHERE Person.name = Purchase.buyer
Matthew P. Johnson, OCL4, CISDD CUNY, Sept New topic: 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
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Constraints in SQL Constraints in SQL: Keys, foreign keys Attribute-level constraints Tuple-level constraints Global constraints: assertions (triggers) The more complex the constraint, the harder it is to check and to enforce simplest Most complex
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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) )
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Keys with Multiple Attributes NameCategoryPrice Canon D10Camera1200 Canon D20Camera2000 iPod G4MP3 Player250 Canon D20Camera800 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) )
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Other Keys There is at most one PRIMARY KEY; there can be many UNIQUE Primary key v. candidate 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) )
Matthew P. Johnson, OCL4, CISDD CUNY, Sept prodName is a foreign key to Product(name) name should be a key in Product Purchase ~ Product is many-one NB: referenced field specified with parentheses, not dot CREATE TABLE Purchase ( prodName CHAR(30) REFERENCES Product(name), date DATETIME ) CREATE TABLE Purchase ( prodName CHAR(30) REFERENCES Product(name), date DATETIME ) Referential integrity in SQL Foreign Key Constraints
Matthew P. Johnson, OCL4, CISDD CUNY, Sept NameCategory Canon D10Camera Canon D20Camera iPod 4GMP3 Player ProdNameStore Canon D10Wiz Canon D10Wiz Canon D20Best Buy ProductPurchase Foreign Key
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Foreign Key Constraints Or: (name, category) must be a key (primary/unique) in Product (why?) 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)
Matthew P. Johnson, OCL4, CISDD CUNY, Sept NameCategory Canon D10Camera Canon D20Camera iPod 4GMP3 Player ProdNameStore Canon D10Wiz Canon D10Wiz Canon D20Best Buy ProductPurchase What happens during updates? Types of updates: In Purchase: insert/update In Product: delete/update
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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 But can disallow nulls from that field
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Constraints on Attributes and Tuples Constraints on attributes: NOT NULL-- obvious meaning... CHECK condition-- any condition on row itself Some DBMS support subqueries here, but many don’t Constraints on tuples CHECK condition
Matthew P. Johnson, OCL4, CISDD CUNY, Sept 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 ) How is this different from a Foreign-Key?
Matthew P. Johnson, OCL4, CISDD CUNY, Sept General Assertions Supported in SQL standard, but not in Oracle: Implemented/approximated in Oracle as triggers PL/SQL in Oracle 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) )
Matthew P. Johnson, OCL4, CISDD CUNY, Sept New topic: SQL Programming Can write SQL queries in a SQL interpreter Command prompt SQL*Plus (sqlplus) in Oracle mysql in MySQL Good for experimenting, not for anything non-trivial Better: use a standard programming language Host language talks to SQL/DB
Matthew P. Johnson, OCL4, CISDD CUNY, Sept SQL/host interface in embedded SQL So Q: how to transfer data between? A: Shared variables Some vars in the program can be used by SQL Prefix var with a : After query, look here for received data SQL commands embedded in app. code Identified by EXEC SQL Source code is preprocessed before regular compilation Result is (e.g.) a C program with library calls
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Programs with Embedded SQL Host language + Embedded SQL Preprocessor Host Language + function calls Host language compiler Executable Preprocessor Host language compiler Oracle’s Pro*C gcc prog.pc prog.c a.out
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Embedded SQL example: insert void simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char pn[20], cn[30]; /* product-name, company-name */ double p, int q; /* price, quantity */ char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* get values for name, price and company somehow */ EXEC SQL INSERT INTO Product(pname, price, quantity, maker) VALUES (:pn, :p, :q, :cn); } void simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char pn[20], cn[30]; /* product-name, company-name */ double p, int q; /* price, quantity */ char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* get values for name, price and company somehow */ EXEC SQL INSERT INTO Product(pname, price, quantity, maker) VALUES (:pn, :p, :q, :cn); }
Matthew P. Johnson, OCL4, CISDD CUNY, Sept CLI: Java Host language + Embedded SQL Preprocessor Host Language + function calls Host language compiler Executable Preprocessor Host language compiler Oracle’s Pro*C javac + jar prog.pc Prog.java Proj.class
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Next topic: JDBC (Java’s CLI) As expected: Java too can talk to SQL In some ways: much nicer JDBC is an interface Changes very little Each vendor writes own plug-in Dev. Strategy: write to API, compile with jar See for 219 (!) JDBC drivershttp://servlet.java.sun.com/products/jdbc/drivers
Matthew P. Johnson, OCL4, CISDD CUNY, Sept JDBC Obtain a statement object: Run a query: Or an update: Statement stmt = con.createStatement(); stmt.executeQuery(“SELECT * FROM table”); stmt.executeUpdate(“INSERT INTO tables” + “VALUES(‘abc’, ‘def’)”); stmt.executeUpdate(“INSERT INTO tables” + “VALUES(‘abc’, ‘def’)”);
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Step back Recall basic problem: need SQL plus stronger programming lang need to connect the two langs In all these cases (and in the web app case), idea is: put SQL in (traditional-lang) programs Another way: put programs in SQL i.e., store programs on the DBMS “stored procedures”
Matthew P. Johnson, OCL4, CISDD CUNY, Sept Labs 4,5