Relational Algebra and SQL
|
|
|
- Jessie Newton
- 9 years ago
- Views:
Transcription
1 Relational Algebra and SQL Johannes Gehrke Slides from Database Management Systems, 3 rd Edition, Ramakrishnan and Gehrke. Database Management Systems, R. Ramakrishnan and J. Gehrke 1
2 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports simple, powerful QLs: Strong formal foundation based on logic. Allows for much optimization. v Query Languages!= programming languages! QLs not expected to be Turing complete. QLs not intended to be used for complex calculations. QLs support easy, efficient access to large data sets. Database Management Systems, R. Ramakrishnan and J. Gehrke 2
3 Formal Relational Query Languages v Two mathematical Query Languages form the basis for real languages (e.g. SQL), and for implementation: Relational Algebra: More operational, very useful for representing execution plans. Relational Calculus: Lets users describe what they want, rather than how to compute it. (Nonoperational, declarative.) Database Management Systems, R. Ramakrishnan and J. Gehrke 3
4 Preliminaries v A query is applied to relation instances, and the result of a query is also a relation instance. Schemas of input relations for a query are fixed (but query will run regardless of instance!) The schema for the result of a given query is also fixed! Determined by definition of query language constructs. v Positional vs. named-field notation: Positional notation easier for formal definitions, named-field notation more readable. Both used in SQL Database Management Systems, R. Ramakrishnan and J. Gehrke 4
5 Example Instances R1 sid bid day /10/ /12/96 v Sailors and Reserves relations for our examples. v We ll use positional or named field notation, assume that names of fields in query results are `inherited from names of fields in query input relations. S1 S2 sid sname rating age 22 dustin lubber rusty sid sname rating age 28 yuppy lubber guppy rusty Database Management Systems, R. Ramakrishnan and J. Gehrke 5
6 Relational Algebra Database Management Systems, R. Ramakrishnan and J. Gehrke 6
7 Relational Algebra v Basic operations: σ π Selection ( ) Selects a subset of rows from relation. Projection ( ) Deletes unwanted columns from relation. Cross-product ( ) Allows us to combine two relations. Set-difference ( ) Tuples in reln. 1, but not in reln. 2. Union ( ) Tuples in reln. 1 and in reln. 2. v Additional operations: Intersection, join, division, renaming: Not essential, but (very!) useful. v Since each operation returns a relation, operations can be composed! (Algebra is closed.) Database Management Systems, R. Ramakrishnan and J. Gehrke 7
8 Projection v Deletes attributes that are not in projection list. v Schema of result contains exactly the fields in the projection list, with the same names that they had in the (only) input relation. v Projection operator has to eliminate duplicates! (Why??) Note: real systems typically don t do duplicate elimination unless the user explicitly asks for it. (Why not?) sname yuppy 9 lubber 8 guppy 5 rusty 10 rating π ( S2) sname, rating age π age ( S2) Database Management Systems, R. Ramakrishnan and J. Gehrke 8
9 Selection v Selects rows that satisfy selection condition. v No duplicates in result! (Why?) v Schema of result identical to schema of (only) input relation. v Result relation can be the input for another relational algebra operation! (Operator composition.) sid sname rating age 28 yuppy rusty π σ rating S >8 ( 2) sname rating yuppy 9 rusty 10 σ ( ( S )) sname, rating rating>8 2 Database Management Systems, R. Ramakrishnan and J. Gehrke 9
10 Union, Intersection, Set-Difference v All of these operations take two input relations, which must be union-compatible: Same number of fields. `Corresponding fields have the same type. v What is the schema of result? sid sname rating age 22 dustin S1 S2 sid sname rating age 22 dustin lubber rusty guppy yuppy S1 S2 sid sname rating age 31 lubber rusty S1 S2 Database Management Systems, R. Ramakrishnan and J. Gehrke 10
11 Cross-Product v Each row of S1 is paired with each row of R1. v Result schema has one field per field of S1 and R1, with field names `inherited if possible. Conflict: Both S1 and R1 have a field called sid. (sid) sname rating age (sid) bid day 22 dustin /10/96 22 dustin /12/96 31 lubber /10/96 31 lubber /12/96 58 rusty /10/96 58 rusty /12/96 Renaming operator: ρ ( C( 1 sid15, sid2), S1 R1) Database Management Systems, R. Ramakrishnan and J. Gehrke 11
12 Joins v Condition Join: R c S = σ c ( R S) (sid) sname rating age (sid) bid day 22 dustin /12/96 31 lubber /12/96 S 1 R 1 S1. sid < R1. sid v Result schema same as that of cross-product. v Fewer tuples than cross-product, might be able to compute more efficiently v Sometimes called a theta-join. Database Management Systems, R. Ramakrishnan and J. Gehrke 12
13 Joins v Equi-Join: A special case of condition join where the condition c contains only equalities. sid sname rating age bid day 22 dustin /10/96 58 rusty /12/96 S1 R1 sid v Result schema similar to cross-product, but only one copy of fields for which equality is specified. v Natural Join: Equijoin on all common fields. Database Management Systems, R. Ramakrishnan and J. Gehrke 13
14 Division v Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved all boats. v Let A have 2 fields, x and y; B have only field y: A/B { } = x y B x, y A i.e., A/B contains all x tuples (sailors) such that for every y tuple (boat) in B, there is an xy tuple in A. Or: If the set of y values (boats) associated with an x value (sailor) in A contains all y values in B, the x value is in A/B. v In general, x and y can be any lists of fields; y is the list of fields in B, and x y is the list of fields of A. Database Management Systems, R. Ramakrishnan and J. Gehrke 14
15 Examples of Division A/B sno s1 s1 s1 s1 s2 s2 s3 s4 s4 A pno p1 p2 p3 p4 p1 p2 p2 p2 p4 pno p2 B1 sno s1 s2 s3 s4 pno p2 p4 B2 sno s1 s4 pno p1 p2 p4 B3 sno s1 A/B1 A/B2 A/B3 Database Management Systems, R. Ramakrishnan and J. Gehrke 15
16 Expressing A/B Using Basic Operators v Division is not essential op; just a useful shorthand. (Also true of joins, but joins are so common that systems implement joins specially.) v Idea: For A/B, compute all x values that are not `disqualified by some y value in B. x value is disqualified if by attaching y value from B, we obtain an xy tuple that is not in A. Disqualified x values: A/B: Database Management Systems, R. Ramakrishnan and J. Gehrke 16
17 Find names of sailors who ve reserved boat #103 v Solution 1: π sname (( σ Re serves) Sailors) bid =103 Solution 2: ρ ( Temp1, σ Re serves) bid =103 ρ ( Temp2, Temp1 Sailors) π sname ( Temp2) Solution 3: π sname( σ (Re serves Sailors)) bid =103 Database Management Systems, R. Ramakrishnan and J. Gehrke 17
18 Find names of sailors who ve reserved a red boat v Information about boat color only available in Boats; so need an extra join: π sname (( σ Boats) Re serves Sailors) color = ' red' A more efficient solution: π sname ( π π σ sid (( bid color = ' red ' Boats) Re s) Sailors) A query optimizer can find this, given the first solution! Database Management Systems, R. Ramakrishnan and J. Gehrke 18
19 Find sailors who ve reserved a red or a green boat v Can identify all red or green boats, then find sailors who ve reserved one of these boats: ρ ( Tempboats,( σ )) color = ' red ' color = ' green' Boats π sname ( Tempboats Re serves Sailors) Can also define Tempboats using union! (How?) What happens if is replaced by in this query? Database Management Systems, R. Ramakrishnan and J. Gehrke 19
20 Find sailors who ve reserved a red and a green boat v Previous approach won t work! Must identify sailors who ve reserved red boats, sailors who ve reserved green boats, then find the intersection (note that sid is a key for Sailors): ρ ( Tempred, π (( σ sid color = ' red ' ρ ( Tempgreen, π (( σ sid color = ' green' Boats) Re serves)) Boats) Re serves)) π sname (( Tempred Tempgreen) Sailors) Database Management Systems, R. Ramakrishnan and J. Gehrke 20
21 Find the names of sailors who ve reserved all boats v Uses division; schemas of the input relations to / must be carefully chosen: ρ ( Tempsids,( π Re serves) / ( π )) sid, bid bid Boats π sname ( Tempsids Sailors) To find sailors who ve reserved all Interlake boats:... / π ( σ ) bid bname= ' Interlake' Boats Database Management Systems, R. Ramakrishnan and J. Gehrke 21
22 SQL Database Management Systems, R. Ramakrishnan and J. Gehrke 22
23 Basic SQL Query SELECT [DISTINCT] target-list FROM relation-list [WHERE condition] SELECT S.Name FROM Sailors S WHERE S.Age > 25 SELECT DISTINCT S.Name FROM Sailors S WHERE S.Age > 25 Default is that duplicates are not eliminated! Need to explicitly say DISTINCT Database Management Systems, R. Ramakrishnan and J. Gehrke 23
24 SQL Query SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103 Sailors sid sname rating age 22 dustin lubber rusty sid bid Reserves day /10/ /12/96 Database Management Systems, R. Ramakrishnan and J. Gehrke 24
25 Conceptual Evaluation Strategy Semantics of an SQL query defined in terms of the following conceptual evaluation strategy: Compute the cross-product of relation-list Discard resulting tuples if they fail condition. Delete attributes that are not in target-list If DISTINCT is specified, eliminate duplicate rows. This strategy is probably the least efficient way to compute a query! An optimizer will find more efficient strategies to compute the same answers. Database Management Systems, R. Ramakrishnan and J. Gehrke 25
26 Example of Conceptual Evaluation SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103 (sid) sname rating age (sid) bid day 22 dustin /10/96 22 dustin /12/96 31 lubber /10/96 31 lubber /12/96 58 rusty /10/96 58 rusty /12/96 Database Management Systems, R. Ramakrishnan and J. Gehrke 26
27 A Slightly Modified Query SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103 Would adding DISTINCT to this query make a difference? Database Management Systems, R. Ramakrishnan and J. Gehrke 27
28 Find sid s of sailors who ve reserved a red or a green boat SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND (B.color= red OR B.color= green ) SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= red UNION SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= green Database Management Systems, R. Ramakrishnan and J. Gehrke 28
29 What does this query compute? SELECT S.sid FROM Sailors S, Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE S.sid=R1.sid AND R1.bid=B1.bid AND S.sid=R2.sid AND R2.bid=B2.bid AND B1.color= red AND B2.color= green Database Management Systems, R. Ramakrishnan and J. Gehrke 29
30 Find sid s of sailors who ve reserved a red and a green boat Key field! SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= red INTERSECT SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= green What if INTERSECT were replaced by EXCEPT? EXCEPT is set difference Database Management Systems, R. Ramakrishnan and J. Gehrke 30
31 Expressions and Strings SELECT S.age, S.age-5 AS age2, 2*S.age AS age2 FROM Sailors S WHERE S.sname LIKE B_%B Find triples (of ages of sailors and two fields defined by expressions) for sailors whose names begin and end with B and contain at least three characters. AS is used to name fields in result. LIKE is used for string matching `_ stands for any one character `% stands for 0 or more arbitrary characters. Database Management Systems, R. Ramakrishnan and J. Gehrke 31
32 Nested Queries (with Correlation) Find names of sailors who have reserved boat #103: SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) Database Management Systems, R. Ramakrishnan and J. Gehrke 32
33 Nested Queries (with Correlation) Find names of sailors who have not reserved boat #103: SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) Database Management Systems, R. Ramakrishnan and J. Gehrke 33
34 Division in SQL Find sailors who ve reserved all boats SELECT S.sname FROM Sailors S WHERE NOT EXISTS ((SELECT B.bid FROM Boats B) EXCEPT (SELECT R.bid FROM Reserves R WHERE R.sid=S.sid)) Database Management Systems, R. Ramakrishnan and J. Gehrke 34
35 Division in SQL (without Except!) Find sailors who ve reserved all boats. SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE NOT EXISTS (SELECT Sailors S such that... R.bid FROM Reserves R there is no boat B without... WHERE R.bid=B.bid AND R.sid=S.sid)) a Reserves tuple showing S reserved B Database Management Systems, R. Ramakrishnan and J. Gehrke 35
36 More on Set-Comparison Operators op ANY, op ALL op can be ><=,,,,, Find sailors whose rating is greater than that of all sailors called Horatio: SELECT * FROM Sailors S WHERE S.rating > ALL (SELECT S2.rating FROM Sailors S2 WHERE S2.sname= Horatio ) Database Management Systems, R. Ramakrishnan and J. Gehrke 36
37 Aggregate Operators Significant extension of relational algebra. COUNT (*) COUNT ( [DISTINCT] A) SUM ( [DISTINCT] A) AVG ( [DISTINCT] A) MAX (A) MIN (A) single column SELECT COUNT (*) FROM Sailors S SELECT AVG (S.age) FROM Sailors S WHERE S.rating=10 SELECT COUNT (DISTINCT S.rating) FROM Sailors S WHERE S.sname= Bob Database Management Systems, R. Ramakrishnan and J. Gehrke 37
38 Find name and age of the oldest sailor(s) with rating > 7 SELECT S.sname, S.age FROM Sailors S WHERE S.Rating > 7 AND S.age = (SELECT MAX (S2.age) FROM Sailors S2 WHERE S2.Rating > 7) Database Management Systems, R. Ramakrishnan and J. Gehrke 38
39 Aggregate Operators v So far, we ve applied aggregate operators to all (qualifying) tuples v Sometimes, we want to apply them to each of several groups of tuples. v Consider: Find the age of the youngest sailor for each rating level. If rating values go from 1 to 10; we can write 10 queries that look like this: SELECT MIN (S.age) For i = 1, 2,..., 10: FROM Sailors S WHERE S.rating = i Database Management Systems, R. Ramakrishnan and J. Gehrke 39
40 GROUP BY SELECT [DISTINCT] target-list FROM relation-list [WHERE condition] GROUP BY grouping-list Find the age of the youngest sailor for each rating level SELECT S.rating, MIN(S.Age) FROM Sailors S GROUP BY S.rating Database Management Systems, R. Ramakrishnan and J. Gehrke 40
41 Conceptual Evaluation Strategy Semantics of an SQL query defined as follows: Compute the cross-product of relation-list Discard resulting tuples if they fail condition. Delete attributes that are not in target-list Remaining tuples are partitioned into groups by the value of the attributes in grouping-list One answer tuple is generated per group Note: Does not imply query will actually be evaluated this way! Database Management Systems, R. Ramakrishnan and J. Gehrke 41
42 Find the age of the youngest sailor with age 18, for each rating with at least one such sailor SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating sid sname rating age 29 brutus dustin horatio rusty sid sname rating age 22 dustin lubber zorba horatio brutus rusty rating Answer relation Database Management Systems, R. Ramakrishnan and J. Gehrke 42
43 Are These Queries Correct? SELECT MIN(S.Age) FROM Sailors S GROUP BY S.rating SELECT S.name, S.rating, MIN(S.Age) FROM Sailors S GROUP BY S.rating Database Management Systems, R. Ramakrishnan and J. Gehrke 43
44 What does this query compute? SELECT B.bid, COUNT (*) AS scount FROM Reserves R, Boats B WHERE R.bid=B.bid AND B.color= red GROUP BY B.bid Database Management Systems, R. Ramakrishnan and J. Gehrke 44
45 Find those ratings for which the average age is the minimum over all ratings SELECT Temp.rating, Temp.avgage FROM (SELECT S.rating, AVG (S.age) AS avgage FROM Sailors S GROUP BY S.rating) AS Temp WHERE Temp.avgage = (SELECT MIN (Temp2.avgage) FROM (SELECT AVG(S.age) as avgage FROM Sailors S GROUP BY S.rating) AS Temp2 ) Database Management Systems, R. Ramakrishnan and J. Gehrke 45
46 What does this query compute? SELECT Temp.rating, Temp.minage FROM (SELECT S.rating, MIN (S.age) AS minage, COUNT(*) AS cnt FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating) AS Temp WHERE Temp.cnt >= 2 Database Management Systems, R. Ramakrishnan and J. Gehrke 46
47 Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list [WHERE qualification] GROUP BY grouping-list HAVING group-qualification Find the age of the youngest sailor with age >= 18 for each rating level with at least 2 such sailors SELECT S.rating, MIN(S.Age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT(*) >= 2 Database Management Systems, R. Ramakrishnan and J. Gehrke 47
48 Conceptual Evaluation Strategy Semantics of an SQL query defined as follows: Compute the cross-product of relation-list Discard resulting tuples if they fail condition. Delete attributes that are not in target-list Remaining tuples are partitioned into groups by the value of the attributes in grouping-list The group-qualification is applied to eliminate some groups One answer tuple is generated per qualifying group Note: Does not imply query will actually be evaluated this way! Database Management Systems, R. Ramakrishnan and J. Gehrke 48
49 Find the age of the youngest sailor with age 18, for each rating with at least 2 such sailors SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 v Only S.rating and S.age are mentioned in the SELECT, GROUP BY or HAVING clauses; other attributes `unnecessary. v 2nd column of result is unnamed. (Use AS to name it.) sid sname rating age 22 dustin lubber zorba horatio brutus rusty rating age rating Answer relation Database Management Systems, R. Ramakrishnan and J. Gehrke 49
50 Find the age of the youngest sailor with age >= 18, for each rating with at least 2 sailors (of any age) SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING 1 < (SELECT COUNT (*) FROM Sailors S2 WHERE S.rating=S2.rating) Database Management Systems, R. Ramakrishnan and J. Gehrke 50
51 Find the average age for each rating, and order results in ascending order on avg. age SELECT S.rating, AVG (S.age) AS avgage FROM Sailors S GROUP BY S.rating ORDER BY avgage ORDER BY can only appear in top-most query Otherwise results are unordered! Database Management Systems, R. Ramakrishnan and J. Gehrke 51
52 Null Values Field values in a tuple are sometimes unknown e.g., a rating has not been assigned Field values are sometimes inapplicable e.g., no spouse s name SQL provides a special value null for such situations. Database Management Systems, R. Ramakrishnan and J. Gehrke 52
53 Queries and Null Values SELECT S.Name FROM Sailors S WHERE S.Age > 25 What if S.Age is NULL? S.Age > 25 returns NULL! Implies a predicate can return 3 values True, false, NULL Three valued logic! Where clause eliminates rows that do not return true (i.e., which are false or NULL) Database Management Systems, R. Ramakrishnan and J. Gehrke 53
54 Three-valued Logic SELECT S.Name FROM Sailors S WHERE NOT(S.Age > 25) OR S.rating > 7 What if one or both of S.age and S.rating are NULL? NOT Truth Table A NOT(A) True False False True NULL NULL OR Truth Table A/B True False NULL True True True True False True False NULL NULL True NULL NULL Database Management Systems, R. Ramakrishnan and J. Gehrke 54
55 General Constraints v Useful when more general ICs than keys are involved v Can use queries to express constraint v Constraints can be named CREATE TABLE Reserves ( sname CHAR(10), bid INTEGER, day DATE, PRIMARY KEY (bid,day), CONSTRAINT nointerlakeres CHECK (`Interlake <> ( SELECT B.bname FROM Boats B WHERE B.bid=bid))) Database Management Systems, R. Ramakrishnan and J. Gehrke 55
56 Constraints Over Multiple Relations Number of boats plus number of sailors is < 100 CREATE ASSERTION smallclub CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 ) Database Management Systems, R. Ramakrishnan and J. Gehrke 56
57 Summary v The relational model has rigorously defined query languages that are simple and powerful. v Relational algebra is more operational; useful as internal representation for query evaluation plans. v Several ways of expressing a given query; a query optimizer should choose the most efficient version. v SQL is the lingua franca for accessing database systems today. Database Management Systems, R. Ramakrishnan and J. Gehrke 57
Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1
Relational Algebra Module 3, Lecture 1 Database Management Systems, R. Ramakrishnan 1 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model
Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables
SQL: Queries, Programming, Triggers Chapter 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Example Instances We will use these instances of the Sailors and Reserves relations in our
SQL: Queries, Programming, Triggers
SQL: Queries, Programming, Triggers Chapter 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 R1 Example Instances We will use these instances of the Sailors and Reserves relations in
SQL: Queries, Programming, Triggers
SQL: Queries, Programming, Triggers CSC343 Introduction to Databases - A. Vaisman 1 R1 Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for
Chapter 5. SQL: Queries, Constraints, Triggers
Chapter 5 SQL: Queries, Constraints, Triggers 1 Overview: aspects of SQL DML: Data Management Language. Pose queries (Ch. 5) and insert, delete, modify rows (Ch. 3) DDL: Data Definition Language. Creation,
Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves
Tutorial 5: SQL By Chaofa Gao Tables used in this note: Sailors(sid: integer, sname: string, rating: integer, age: real); Boats(bid: integer, bname: string, color: string); Reserves(sid: integer, bid:
Θεµελίωση Βάσεων εδοµένων
Θεµελίωση Βάσεων εδοµένων Βασίλης Βασσάλος 1 What do we need to produce good software? Good programmers, software engineers, project managers, business experts, etc. (People) Good software development
COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries
COMP 5138 Relational Database Management Systems Week 5 : Basic COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-1 Today s Agenda Overview Basic Queries Joins Queries Aggregate Functions
The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL
COS 597A: Principles of Database and Information Systems SQL: Overview and highlights The SQL Query Language Structured Query Language Developed by IBM (system R) in the 1970s Need for a standard since
Proposed solutions Project Assignment #1 (SQL)
Proposed solutions Project Assignment #1 (SQL) (a) Database creation: create table sailor (sname CHAR(30), rating INTEGER) create table boat (bname CHAR (30), color CHAR (15), rating INTEGER) create table
UNIT 6. Structured Query Language (SQL) Text: Chapter 5
UNIT 6 Structured Query Language (SQL) Text: Chapter 5 Learning Goals Given a database (a set of tables ) you will be able to express a query in SQL, involving set operators, subqueries and aggregations
Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Outline More Complex SQL Retrieval Queries
Database Management Systems. Chapter 1
Database Management Systems Chapter 1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2 What Is a Database/DBMS? A very large, integrated collection of data. Models real-world scenarios
3. Relational Model and Relational Algebra
ECS-165A WQ 11 36 3. Relational Model and Relational Algebra Contents Fundamental Concepts of the Relational Model Integrity Constraints Translation ER schema Relational Database Schema Relational Algebra
Query-by-Example (QBE)
Query-by-Example (QBE) Module 3, Lecture 6 Example is the school of mankind, and they will learn at no other. -- Edmund Burke (1729-1797) Database Management Systems, R. Ramakrishnan 1 QBE: Intro A GUI
Relational Databases
Relational Databases Jan Chomicki University at Buffalo Jan Chomicki () Relational databases 1 / 18 Relational data model Domain domain: predefined set of atomic values: integers, strings,... every attribute
Relational Database: Additional Operations on Relations; SQL
Relational Database: Additional Operations on Relations; SQL Greg Plaxton Theory in Programming Practice, Fall 2005 Department of Computer Science University of Texas at Austin Overview The course packet
Relational Calculus. Module 3, Lecture 2. Database Management Systems, R. Ramakrishnan 1
Relational Calculus Module 3, Lecture 2 Database Management Systems, R. Ramakrishnan 1 Relational Calculus Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC). Calculus
The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3
The Relational Model Chapter 3 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase,
Lecture 4: More SQL and Relational Algebra
CPSC 421 Database Management Systems Lecture 4: More SQL and Relational Algebra * Some material adapted from R. Ramakrishnan, L. Delcambre, and B. Ludaescher Today s Agenda Go over last week s quiz New
SQL: QUERIES, CONSTRAINTS, TRIGGERS
5 SQL: QUERIES, CONSTRAINTS, TRIGGERS Online material is available for all exercises in this chapter on the book s webpage at http://www.cs.wisc.edu/~dbbook This includes scripts to create tables for each
INFO/CS 330: Applied Database Systems
INFO/CS 330: Applied Database Systems Introduction to Database Security Johannes Gehrke [email protected] http://www.cs.cornell.edu/johannes Introduction to DB Security Secrecy:Users should not be
6 QUERY-BY-EXAMPLE (QBE)
6 QUERY-BY-EXAMPLE (QBE) Example is always more efficacious than precept. Samuel Johnson 6.1 INTRODUCTION Query-by-Example (QBE) is another language for querying (and, like SQL, for creating and modifying)
Introduction to Microsoft Jet SQL
Introduction to Microsoft Jet SQL Microsoft Jet SQL is a relational database language based on the SQL 1989 standard of the American Standards Institute (ANSI). Microsoft Jet SQL contains two kinds of
Database Security. Chapter 21
Database Security Chapter 21 Introduction to DB Security Secrecy: Users should not be able to see things they are not supposed to. E.g., A student can t see other students grades. Integrity: Users should
More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan
More on SQL Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan SELECT A1, A2,, Am FROM R1, R2,, Rn WHERE C1, C2,, Ck Interpreting a Query
The Relational Model. Why Study the Relational Model? Relational Database: Definitions
The Relational Model Database Management Systems, R. Ramakrishnan and J. Gehrke 1 Why Study the Relational Model? Most widely used model. Vendors: IBM, Microsoft, Oracle, Sybase, etc. Legacy systems in
History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)
Relational Database Languages Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG) Domain relational calculus QBE (used in Access) History of SQL Standards:
Lecture 6. SQL, Logical DB Design
Lecture 6 SQL, Logical DB Design Relational Query Languages A major strength of the relational model: supports simple, powerful querying of data. Queries can be written intuitively, and the DBMS is responsible
Relational Algebra. Basic Operations Algebra of Bags
Relational Algebra Basic Operations Algebra of Bags 1 What is an Algebra Mathematical system consisting of: Operands --- variables or values from which new values can be constructed. Operators --- symbols
4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)
ECS-165A WQ 11 66 4. SQL Contents Basic Queries in SQL (select statement) Set Operations on Relations Nested Queries Null Values Aggregate Functions and Grouping Data Definition Language Constructs Insert,
SQL NULL s, Constraints, Triggers
CS145 Lecture Notes #9 SQL NULL s, Constraints, Triggers Example schema: CREATE TABLE Student (SID INTEGER PRIMARY KEY, name CHAR(30), age INTEGER, GPA FLOAT); CREATE TABLE Take (SID INTEGER, CID CHAR(10),
Databases 2011 The Relational Model and SQL
Databases 2011 Christian S. Jensen Computer Science, Aarhus University What is a Database? Main Entry: da ta base Pronunciation: \ˈdā-tə-ˌbās, ˈda- also ˈdä-\ Function: noun Date: circa 1962 : a usually
Oracle SQL. Course Summary. Duration. Objectives
Oracle SQL Course Summary Identify the major structural components of the Oracle Database 11g Create reports of aggregated data Write SELECT statements that include queries Retrieve row and column data
SQL Simple Queries. Chapter 3.1 V3.0. Copyright @ Napier University Dr Gordon Russell
SQL Simple Queries Chapter 3.1 V3.0 Copyright @ Napier University Dr Gordon Russell Introduction SQL is the Structured Query Language It is used to interact with the DBMS SQL can Create Schemas in the
IT2305 Database Systems I (Compulsory)
Database Systems I (Compulsory) INTRODUCTION This is one of the 4 modules designed for Semester 2 of Bachelor of Information Technology Degree program. CREDITS: 04 LEARNING OUTCOMES On completion of this
The Relational Model. Why Study the Relational Model?
The Relational Model Chapter 3 Instructor: Vladimir Zadorozhny [email protected] Information Science Program School of Information Sciences, University of Pittsburgh 1 Why Study the Relational Model?
Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst
Outline Data Modeling Yanlei Diao UMass Amherst v Conceptual Design: ER Model v Relational Model v Logical Design: from ER to Relational Slides Courtesy of R. Ramakrishnan and J. Gehrke 1 2 Conceptual
A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX
ISSN: 2393-8528 Contents lists available at www.ijicse.in International Journal of Innovative Computer Science & Engineering Volume 3 Issue 2; March-April-2016; Page No. 09-13 A Comparison of Database
IT2304: Database Systems 1 (DBS 1)
: Database Systems 1 (DBS 1) (Compulsory) 1. OUTLINE OF SYLLABUS Topic Minimum number of hours Introduction to DBMS 07 Relational Data Model 03 Data manipulation using Relational Algebra 06 Data manipulation
Databases and BigData
Eduardo Cunha de Almeida [email protected] Outline of the course Introduction Database Systems (E. Almeida) Distributed Hash Tables and P2P (C. Cassagnes) NewSQL (D. Kim and J. Meira) NoSQL (D. Kim)
The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1
The Relational Model Ramakrishnan&Gehrke, Chapter 3 CS4320 1 Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. Legacy systems in older models
Answer Key. UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division
Answer Key UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division CS186 Fall 2003 Eben Haber Midterm Midterm Exam: Introduction to Database Systems This exam has
Database Design Overview. Conceptual Design ER Model. Entities and Entity Sets. Entity Set Representation. Keys
Database Design Overview Conceptual Design. The Entity-Relationship (ER) Model CS430/630 Lecture 12 Conceptual design The Entity-Relationship (ER) Model, UML High-level, close to human thinking Semantic
Introduction to database design
Introduction to database design KBL chapter 5 (pages 127-187) Rasmus Pagh Some figures are borrowed from the ppt slides from the book used in the course, Database systems by Kiefer, Bernstein, Lewis Copyright
Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems
CSC 74 Database Management Systems Topic #0: SQL Part A: Data Definition Language (DDL) Spring 00 CSC 74: DBMS by Dr. Peng Ning Spring 00 CSC 74: DBMS by Dr. Peng Ning Schema and Catalog Schema A collection
Oracle Database: SQL and PL/SQL Fundamentals
Oracle University Contact Us: 1.800.529.0165 Oracle Database: SQL and PL/SQL Fundamentals Duration: 5 Days What you will learn This course is designed to deliver the fundamentals of SQL and PL/SQL along
CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model
CS2Bh: Current Technologies Introduction to XML and Relational Databases Spring 2005 The Relational Model CS2 Spring 2005 (LN6) 1 The relational model Proposed by Codd in 1970. It is the dominant data
Lecture 6: Query optimization, query tuning. Rasmus Pagh
Lecture 6: Query optimization, query tuning Rasmus Pagh 1 Today s lecture Only one session (10-13) Query optimization: Overview of query evaluation Estimating sizes of intermediate results A typical query
Security and Authorization. Introduction to DB Security. Access Controls. Chapter 21
Security and Authorization Chapter 21 Database Management Systems, 3ed, R. Ramakrishnan and J. Gehrke 1 Introduction to DB Security Secrecy: Users should not be able to see things they are not supposed
Introduction to tuple calculus Tore Risch 2011-02-03
Introduction to tuple calculus Tore Risch 2011-02-03 The relational data model is based on considering normalized tables as mathematical relationships. Powerful query languages can be defined over such
Oracle Database: SQL and PL/SQL Fundamentals NEW
Oracle University Contact Us: + 38516306373 Oracle Database: SQL and PL/SQL Fundamentals NEW Duration: 5 Days What you will learn This Oracle Database: SQL and PL/SQL Fundamentals training delivers the
Introduction to SQL: Data Retrieving
Introduction to SQL: Data Retrieving Ruslan Fomkin Databasdesign för Ingenjörer 1056F Structured Query Language (SQL) History: SEQUEL (Structured English QUery Language), earlier 70 s, IBM Research SQL
Oracle Database: SQL and PL/SQL Fundamentals
Oracle University Contact Us: +966 12 739 894 Oracle Database: SQL and PL/SQL Fundamentals Duration: 5 Days What you will learn This Oracle Database: SQL and PL/SQL Fundamentals training is designed to
Information Systems SQL. Nikolaj Popov
Information Systems SQL Nikolaj Popov Research Institute for Symbolic Computation Johannes Kepler University of Linz, Austria [email protected] Outline SQL Table Creation Populating and Modifying
Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff
D80198GC10 Oracle Database 12c SQL and Fundamentals Summary Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff Level Professional Delivery Method Instructor-led
CSE 530A Database Management Systems. Introduction. Washington University Fall 2013
CSE 530A Database Management Systems Introduction Washington University Fall 2013 Overview Time: Mon/Wed 7:00-8:30 PM Location: Crow 206 Instructor: Michael Plezbert TA: Gene Lee Websites: http://classes.engineering.wustl.edu/cse530/
Data Modeling. Database Systems: The Complete Book Ch. 4.1-4.5, 7.1-7.4
Data Modeling Database Systems: The Complete Book Ch. 4.1-4.5, 7.1-7.4 Data Modeling Schema: The structure of the data Structured Data: Relational, XML-DTD, etc Unstructured Data: CSV, JSON But where does
SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6
SUBQUERIES AND VIEWS CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6 String Comparisons and GROUP BY 2! Last time, introduced many advanced features of SQL, including GROUP BY! Recall:
Week 4 & 5: SQL. SQL as a Query Language
Week 4 & 5: SQL The SQL Query Language Select Statements Joins, Aggregate and Nested Queries Insertions, Deletions and Updates Assertions, Views, Triggers and Access Control SQL 1 SQL as a Query Language
Review Entity-Relationship Diagrams and the Relational Model. Data Models. Review. Why Study the Relational Model? Steps in Database Design
Review Entity-Relationship Diagrams and the Relational Model CS 186, Fall 2007, Lecture 2 R & G, Chaps. 2&3 Why use a DBMS? OS provides RAM and disk A relationship, I think, is like a shark, you know?
The Relational Algebra
The Relational Algebra Relational set operators: The data in relational tables are of limited value unless the data can be manipulated to generate useful information. Relational Algebra defines the theoretical
Oracle Database 10g: Introduction to SQL
Oracle University Contact Us: 1.800.529.0165 Oracle Database 10g: Introduction to SQL Duration: 5 Days What you will learn This course offers students an introduction to Oracle Database 10g database technology.
Database Systems. Lecture 1: Introduction
Database Systems Lecture 1: Introduction General Information Professor: Leonid Libkin Contact: [email protected] Lectures: Tuesday, 11:10am 1 pm, AT LT4 Website: http://homepages.inf.ed.ac.uk/libkin/teach/dbs09/index.html
SQL/PSM. Outline. Database Application Development Oracle PL/SQL. Why Stored Procedures? Stored Procedures PL/SQL. Embedded SQL Dynamic SQL
Outline Embedded SQL Dynamic SQL Many host languages: C, Cobol, Pascal, etc. JDBC (API) SQLJ (Embedded) Java Database Application Development Oracle PL/SQL Stored procedures CS430/630 Lecture 15 Slides
2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA www.sybex.com
Essential SQL 2 Essential SQL This bonus chapter is provided with Mastering Delphi 6. It is a basic introduction to SQL to accompany Chapter 14, Client/Server Programming. RDBMS packages are generally
Review: Participation Constraints
Review: Participation Constraints Does every department have a manager? If so, this is a participation constraint: the participation of Departments in Manages is said to be total (vs. partial). Every did
3. Mathematical Induction
3. MATHEMATICAL INDUCTION 83 3. Mathematical Induction 3.1. First Principle of Mathematical Induction. Let P (n) be a predicate with domain of discourse (over) the natural numbers N = {0, 1,,...}. If (1)
CSC 443 Data Base Management Systems. Basic SQL
CSC 443 Data Base Management Systems Lecture 6 SQL As A Data Definition Language Basic SQL SQL language Considered one of the major reasons for the commercial success of relational databases SQL Structured
DataBase Management Systems Lecture Notes
1 SHRI VISHNU ENGINEERING COLLEGE FOR WOMEN::BHIMAVARAM DEPARTMENT OF INFORMATION TECHNOLOGY DataBase Management Systems Lecture Notes UNIT-1 Data: It is a collection of information. The facts that can
Instant SQL Programming
Instant SQL Programming Joe Celko Wrox Press Ltd. INSTANT Table of Contents Introduction 1 What Can SQL Do for Me? 2 Who Should Use This Book? 2 How To Use This Book 3 What You Should Know 3 Conventions
Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)
Copyright 2000-2001, University of Washington Using Multiple Operations Implementing Table Operations Using Structured Query Language (SQL) The implementation of table operations in relational database
Chapter 13: Query Processing. Basic Steps in Query Processing
Chapter 13: Query Processing! Overview! Measures of Query Cost! Selection Operation! Sorting! Join Operation! Other Operations! Evaluation of Expressions 13.1 Basic Steps in Query Processing 1. Parsing
CMU - SCS 15-415/15-615 Database Applications Spring 2013, C. Faloutsos Homework 1: E.R. + Formal Q.L. Deadline: 1:30pm on Tuesday, 2/5/2013
CMU - SCS 15-415/15-615 Database Applications Spring 2013, C. Faloutsos Homework 1: E.R. + Formal Q.L. Deadline: 1:30pm on Tuesday, 2/5/2013 Reminders - IMPORTANT: Like all homeworks, it has to be done
Chapter 14: Query Optimization
Chapter 14: Query Optimization Database System Concepts 5 th Ed. See www.db-book.com for conditions on re-use Chapter 14: Query Optimization Introduction Transformation of Relational Expressions Catalog
Schema Design and Normal Forms Sid Name Level Rating Wage Hours
Entity-Relationship Diagram Schema Design and Sid Name Level Rating Wage Hours Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke 1 Database Management Systems, 2 nd Edition. R. Ramakrishnan
Structured Query Language (SQL)
Objectives of SQL Structured Query Language (SQL) o Ideally, database language should allow user to: create the database and relation structures; perform insertion, modification, deletion of data from
www.gr8ambitionz.com
Data Base Management Systems (DBMS) Study Material (Objective Type questions with Answers) Shared by Akhil Arora Powered by www. your A to Z competitive exam guide Database Objective type questions Q.1
Netezza SQL Class Outline
Netezza SQL Class Outline CoffingDW education has been customized for every customer for the past 20 years. Our classes can be taught either on site or remotely via the internet. Education Contact: John
SQL - QUICK GUIDE. Allows users to access data in relational database management systems.
http://www.tutorialspoint.com/sql/sql-quick-guide.htm SQL - QUICK GUIDE Copyright tutorialspoint.com What is SQL? SQL is Structured Query Language, which is a computer language for storing, manipulating
The Entity-Relationship Model
The Entity-Relationship Model 221 After completing this chapter, you should be able to explain the three phases of database design, Why are multiple phases useful? evaluate the significance of the Entity-Relationship
MOC 20461C: Querying Microsoft SQL Server. Course Overview
MOC 20461C: Querying Microsoft SQL Server Course Overview This course provides students with the knowledge and skills to query Microsoft SQL Server. Students will learn about T-SQL querying, SQL Server
The process of database development. Logical model: relational DBMS. Relation
The process of database development Reality (Universe of Discourse) Relational Databases and SQL Basic Concepts The 3rd normal form Structured Query Language (SQL) Conceptual model (e.g. Entity-Relationship
3.GETTING STARTED WITH ORACLE8i
Oracle For Beginners Page : 1 3.GETTING STARTED WITH ORACLE8i Creating a table Datatypes Displaying table definition using DESCRIBE Inserting rows into a table Selecting rows from a table Editing SQL buffer
Comp 5311 Database Management Systems. 3. Structured Query Language 1
Comp 5311 Database Management Systems 3. Structured Query Language 1 1 Aspects of SQL Most common Query Language used in all commercial systems Discussion is based on the SQL92 Standard. Commercial products
There are five fields or columns, with names and types as shown above.
3 THE RELATIONAL MODEL Exercise 3.1 Define the following terms: relation schema, relational database schema, domain, attribute, attribute domain, relation instance, relation cardinality, andrelation degree.
How To Create A Table In Sql 2.5.2.2 (Ahem)
Database Systems Unit 5 Database Implementation: SQL Data Definition Language Learning Goals In this unit you will learn how to transfer a logical data model into a physical database, how to extend or
Database Applications Microsoft Access
Database Applications Microsoft Access Lesson 4 Working with Queries Difference Between Queries and Filters Filters are temporary Filters are placed on data in a single table Queries are saved as individual
Chapter 13: Query Optimization
Chapter 13: Query Optimization Database System Concepts, 6 th Ed. See www.db-book.com for conditions on re-use Chapter 13: Query Optimization Introduction Transformation of Relational Expressions Catalog
BCA. Database Management System
BCA IV Sem Database Management System Multiple choice questions 1. A Database Management System (DBMS) is A. Collection of interrelated data B. Collection of programs to access data C. Collection of data
Oracle Database 12c: Introduction to SQL Ed 1.1
Oracle University Contact Us: 1.800.529.0165 Oracle Database 12c: Introduction to SQL Ed 1.1 Duration: 5 Days What you will learn This Oracle Database: Introduction to SQL training helps you write subqueries,
Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology
COS 597A: Principles of Database and Information Systems elational model elational model A formal (mathematical) model to represent objects (data/information), relationships between objects Constraints
How To Handle Missing Information Without Using NULL
How To Handle Missing Information Without Using NULL Hugh Darwen [email protected] www.thethirdmanifesto.com First presentation: 09 May 2003, Warwick University Updated 27 September 2006 Databases,
Oracle Database: SQL and PL/SQL Fundamentals NEW
Oracle University Contact Us: 001-855-844-3881 & 001-800-514-06-97 Oracle Database: SQL and PL/SQL Fundamentals NEW Duration: 5 Days What you will learn This Oracle Database: SQL and PL/SQL Fundamentals
DBMS / Business Intelligence, SQL Server
DBMS / Business Intelligence, SQL Server Orsys, with 30 years of experience, is providing high quality, independant State of the Art seminars and hands-on courses corresponding to the needs of IT professionals.
Factorizations: Searching for Factor Strings
" 1 Factorizations: Searching for Factor Strings Some numbers can be written as the product of several different pairs of factors. For example, can be written as 1, 0,, 0, and. It is also possible to write
Guide to Performance and Tuning: Query Performance and Sampled Selectivity
Guide to Performance and Tuning: Query Performance and Sampled Selectivity A feature of Oracle Rdb By Claude Proteau Oracle Rdb Relational Technology Group Oracle Corporation 1 Oracle Rdb Journal Sampled
Database Query 1: SQL Basics
Database Query 1: SQL Basics CIS 3730 Designing and Managing Data J.G. Zheng Fall 2010 1 Overview Using Structured Query Language (SQL) to get the data you want from relational databases Learning basic
Lecture 1. Basic Concepts of Set Theory, Functions and Relations
September 7, 2005 p. 1 Lecture 1. Basic Concepts of Set Theory, Functions and Relations 0. Preliminaries...1 1. Basic Concepts of Set Theory...1 1.1. Sets and elements...1 1.2. Specification of sets...2
