Data Structure: Relational Moel Relational atabases: Schema + Data Schema (also calle scheme): collection of tables (also calle relations) each table has a set of attributes no repeating relation names, no repeating attributes in one table Data (also calle instance): set of tuples tuples have one value for each attribute of the table they belong Movie Ti t l e Di rector Ac t o r Wil Lynch Winger Sky erto Winger Res eatty eatty erto rano erto Winger erto Snyer Scheule The a t e r Ti t l e Oeon Wil Forum Res Forum Sky Programming Interface: JDC/ODC How client opens connection with server How access & moification commans are issue SQL Queries: The asic From asic form SELECT a,, an FROM R,, RM WHERE conition WHERE clause is optional When more than one π a, relations, anσ conition of the (Rx FROM xrm) have an attribute name A we refer to a specific A attribute as <RelationName>.A Fin titles of currently playing movies Fin the titles of all movies by erto WHERE Director= erto Fin the titles an the irectors of all currently playing movies SELECT Movie.Title, Director, Scheule WHERE Movie.Title=Scheule.Title
SQL Queries: Aliases Use the same relation more than once in the FROM clause Tuple variables Example: fin actors who are also irectors SELECT t.actor t s WHERE t.actor=s.director SQL Queries: Nesting The WHERE clause can contain preicates of the form attr/value IN <SQL query> attr/value NOT IN <SQL query> The preicate is satisfie if the attr or value appears in the result of the neste <SQL query> Queries involving nesting but no negation can always be unneste, unlike queries with nesting an negation Typical use: fin objects that always satisfy property X, e.g., fin actors playing in every movie by erto : SELECT Actor WHERE Actor NOT IN (SELECT t.actor t s, WHERE s.director= erto AND t.actor NOT IN WHERE Title=s.Title)) The shae query fins actors NOT playing in some movie by erto The top lines complement the shae part Homework Problem Compare with shae sub-query of previous page. The sample ata may help you SELECT Actor WHERE Actor NOT IN WHERE Director= erto ) Actor Director a a b c X X Movie 3 3 6
3 Neste Queries: Existential an Universal Quantification A op ANY <neste query> is satisfie if there is a value X in the result of the <neste query> an the conition A op X is satisfie ANY aka SOME A op ALL <neste query> is satisfie if for every value X in the result of the <neste query> the conition A op X is satisfie Fin irectors of currently playing movies SELECT Director WHERE Title = ANY Fin the employees with the highest salary SELECT Name WHERE Salary >= ALL SELECT Salary Neste Queries: Set Comparison <neste query > CONTAINS <neste query > Fin actors playing in every movie by erto SELECT s.actor s WHERE ( t WHERE t.actor = s.actor) CONTAINS ( WHERE Director = erto ) SQL:Union, Intersection, Difference Union <SQL query > UNION <SQL query > Intersection <SQL query > INTERSECT <SQL query > Difference <SQL query > MINUS <SQL query > Fin all actors or irectors ) UNION (SELECT Director ) Fin all actors who are not irectors ) MINUS (SELECT Director )
4 SQL Queries: Aggregation an Grouping There is no relational algebra equivalent for aggregation an grouping Aggregate functions: AVG, COUNT, MIN, MAX, SUM, an recently user efine functions as well Group-by Employee Name De pt Sal ar y Joe Toys 45 Nick PCs 50 Jim Toys 35 Jack PCs 40 Fin the average salary of all employees SELECT AvgSal=Avg(Salary) AvgSal 4.5 Fin the average salary for each epartment SELECT Dept, AvgSal=Avg(Salary) De pt AvgSal Toys 40 PCs 45 GROUP-Y Dept SQL Grouping: Conitions that Apply on Groups HAVING clause Fin the average salary of for each epartment that has more than employee SELECT Dept, AvgSal=(Avg(Salary)) GROUP-Y Dept HAVING COUNT(Name)> SQL: More ells an Whistles... Select all attributes using * Pattern matching conitions <attr> LIKE <pattern> Retrieve all movie attributes of currently playing movies SELECT Movie.*, Scheule WHERE Movie.Title=Scheule.Title Retrieve all movies where the title starts with Ta SELECT * WHERE Title LIKE Ta
5 an a Few Dirty Points Duplicate elimination must be explicitly requeste SELECT DISTINCT FROM WHERE Null values all comparisons involving NULL are false by efinition all aggregation operations, except count, ignore NULL values SELECT DISTINCT Title Ti t l e Ti t l e Ti t l e Di rector Ac t o r Wil Lynch Winger Sky erto Winger Res NULL eatty erto rano erto Winger erto NULL SQL as a Data Manipulation Language: Insertions inserting tuples INSERT INTO R VALUES (v,,vk); some values may be left NULL use results of queries for insertion INSERT INTO R SELECT FROM WHERE INSERT INTO Movie VALUES ( rave, Gibson, Gibson ); INSERT INTO Movie(Title,Director) VALUES ( rave, Gibson ); INSERT INTO EuroMovie SELECT * WHERE Director = erto SQL as a Data Manipulation Language: Upates an Deletions Deletion basic form: elete every tuple that satisfies <con> DELETE FROM R WHERE <con> Upate basic form: upate every tuple that satisfies <con> in the way specifie by the SET clause UPDATE R SET A=<exp>,, Ak=<expk> WHERE <con> Delete the movies that are not currently playing DELETE WHERE Title NOT IN Change all erto entries to ertoluci UPDATE Movie SET Director= ertoluci WHERE Director= erto Increase all salaries in the Toys ept by 0% UPDATE Employee SET Salary =. * Salary WHERE Dept = Toys The rich get richer exercise: Increase by 0% the salary of the employee with the highest salary