Updates - Insert SQL Other Features and ommands Davood afiei emp(sin, name, phone, city) INSET INSET INTO emp VLUES ( 999, John Doe, 444-555, Edmonton ) INSET INTO emp(sin,name,city) VLUES ( 999, Jim Gray, Ottawa ) Same as: INSET INTO emp VLUES ( 999, Jim Gray,NULL, Ottawa ) 1 3 Outline Update commands Views: more details NULL values onstraints Updates ulk Insert emp (sin, name, phone, city) Edmonton_phonebook (name, phone) INSET INTO Edmonton_phonebook SELET name, phone FOM emp WHEE city = Edmonton 2 4
Updates Delete & Update (a 1,, a n ) DELETE DELETE FOM WHEE condition UPDTE UPDTE SET a i =v i,, a k =v k WHEE condition Update Examples Increase by 5% the balance of every customer who lives in Edmonton and has a balance of more than $5,000. UPDTE deposit SET balance = balance*1.05 WHEE balance > 5000 ND cname IN (SELET cname FOM customer WHEE city = Edmonton ) 5 7 Update Examples Insert a new customer record for John Smith who lives on 345 Jasper venue, Edmonton. INSET INTO customer VLUES ( John Smith, 345 Jasper venue, Edmonton ) Delete all customers who have less than $1,000 in their accounts. DELETE FOM customer WHEE cname IN (SELET cname FOM deposit WHEE balance < 1000) View Definition Examples reate a view of customers who live in Jasper and name it jasper_customers. ETE VIEW jasper_customers S SELET * FOM customer WHEE city = Jasper List the names of all customers in Jasper. SELET cname FOM jasper_customers In queries, a view is exactly like a base table. 6 8
View Definition Examples reate a view (called cust_info) which gives for each customer the name, city, the number of deposit accounts owned and the total balance. ETE VIEW cust_info(name, city, Num, Total) S SELET c.cname, city, OUNT(accno), SUM(balance) FOM deposit d, customer c WHEE d.cname=c.cname GOUP Y c.cname, city Views: Summary View = query table derived table whose definition, not the table itself, is stored. Provides a degree of data independence. Queried like a table. Updated under some conditions. 9 11 View Definition Examples reate a view (called deposit_holders) which includes the name and the city of every deposit account holder. ETE VIEW deposit_holders(name, city) S SELET distinct c.cname, city FOM deposit d, customer c WHEE d.cname=c.cname View Updates onsider the following inserts: INSET INTO jasper_customers VLUES ( John Smith, 111-87 ve, Jasper ) INSET INTO jasper_customers VLUES ( Joe Smith, 112-99 ve, Edmonton ) Is Joe Smith in jasper_customers? To prevent this, add WITH HEK OPTION to the view definition. What can we say about the following insert: INSET INTO cust_info VLUES ( Jim arey, algary, 2, 30000) 10 12
View Update - Example Insert into deposit_holders values ( John, Edmonton ) Insert into deposit_holders values ( Mary, algary ) accno cname bname balanace 1123 John Main St 2000 1144 Joe Whyte ve 2200 1126 John Pine St 1100 cname Joe John city cname street city Edmonton Edmonton deposit_holders Joe 111-22 St Edmonton Mary 98-88 ve algary John 33-34 St Edmonton Unknown Value - NULL Useful when we don t know a column value very common in practice. Simple usage in queries: WHEE phone IS NULL WHEE phone IS NOT NULL SELET cname FOM USTOME WHEE city IS NOT NULL omplication: SELET cname FOM USTOME WHEE city = Edmonton What happens if city for some customers is NULL? deposit customer 13 15 Updatable Views in SQL/92 view defined on a single table is updatable if the view attributes contain the primary key or some other candidate key. Views defined on multiple tables using joins are generally not updatable. Views defined using aggregate functions are not updatable. asic idea: each row and each column in an updatable view must correspond to a distinct row and column in a base table NULL (ont.) The predicate city= Edmonton evaluates to UNKNOWN when city is NULL. the customer name will not be printed. What if the WHEE clause consists of several predicates? E.g. city= Edmonton O street LIKE 100% use three-valued logic: values TUE, FLSE, UNKNOWN. NULL is much different from a constant! 14 16
Left Outer Join S Full Outer Join S select,., from left outer join S using (); Select * from full outer join S using (); 17 19 ight Outer Join Select * from right outer join S using (); S Set Operations Operations: UNION, INTESET, MINUS Duplicates: y default, duplicates are removed from the result of a set operation. To keep duplicates, use UNION LL (Oracle doesn t accept INTESET LL or MINUS LL!) What if and S have more than one join columns? an list them all in the using clause. 18 5 4 25 select from union all select from where < 10 5 4 25 5 4 20
onstraints in SQL/92 User-Defined Domains lready seen: primary key and foreign key constraints. NOT NULL specifies that an attribute cannot contain values should be specified for all primary keys (if it is not default) UNIQUE specifies the alternate keys Domain onstraints HEK onstraints Example ETE DOMIN Gender S H(1) dd some domain constraints ETE DOMIN Gender S H(1) HEK (VLUE = M O VLUE = F ) ETE DOMIN Gender S H(1) HEK (VLUE IN ( M, F )) ETE TLE TEMP (, sex Gender, ) 21 23 Example Domain onstraints ETE TLE branch ( bname H(15) UNIQUE, address VH(20), city H(9) NOT NULL, assets DEIML(10,2) DEFULT 0.00) specifies the condition that each row has to satisfy Example: ETE TLE branch (bname H(15)NOT NULL, address VH(20), city H(9), assets DEIML(10,2) DEFULT 0.00 HEK (assets >= 0)); 22 24
Tuple onstraints hecked every time a tuple is inserted or updated violations are rejected. Example ETE TLE deposit ( accno H(9)NOT NULL, cname VH(15), bname VH(15), balance DEIML(10,2)DEFULT 0.00) HEK (cname = ill linton O balance > 100000); Summary overed asic Queries, nested queries and aggregate Queries Update commands, constraints, Nulls, Views Will over Embedded SQL Note minor syntactic differences between SQL/92 and Oracle SQL. 25 27 ssertions Global constraints of the form ETE SSETION name HEK (condition) Example: No branch can have a customer who has more than 2 loans over $100,000. ETE SSETION deposit HEK (NOT EXISTS (SELET bname, cname FOM loan WHEE amount > 100000 GOUP Y bname, cname HVING OUNT(*) > 2)); 26