Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
RDBMS and SQL • RDBMS = Relational Database Management System • SQL = “Structured Query Language” – Programming Language to Interact with an RDBMS – Pronounced as “sequel” 2/23/16 CprE/SE 419, Srikanta Tirthapura 1 RDBMS • An RDBMS consists of data viewed as one or more tables • Each table can be viewed as many “rows” • Each table can be viewed as many “columns” • Each row consists of values assigned to each of many attributes • Table stores a mathematical Relation 2/22/16 CprE/SE 419, Srikanta Tirthapura 2 RDBMS Schema • A specification of the set of tables in the database. • For each table, a specification of the types and names of each column in the table All rows in a table have the same schema (Compare the above with Pig) Vocabulary: Table, Row, Column, Schema 2/22/16 CprE/SE 419, Srikanta Tirthapura 3 Example Consider the following Tables 1. Sailors (sid: integer, sname: string, rating: integer, age: real) 2. Boats (bid: integer, bname: string, color: string) 3. Reservations (sid: integer, bid: integer, day: dates) 2/22/16 CprE/SE 419, Srikanta Tirthapura 4 SQL • Data Definition Language (DDL) – Define the database structure and schema – Create, Alter, Drop, etc • Data Manipulation Language (DML) – Query and update data – SELECT 2/22/16 CprE/SE 419, Srikanta Tirthapura 5 A basic query in SQL SELECT select-‐list FROM from-‐list WHERE qualification 2/22/16 CprE/SE 419, Srikanta Tirthapura 6 SQL Clauses • FROM clause: the from-‐list is a list of table names • SELECT clause: the select-‐list is a list of (expressions involving) column names of tables • WHERE clause: qualification is a predicate involving columns of the tables 2/22/16 CprE/SE 419, Srikanta Tirthapura 7 Example 1 Find the names and ages of all sailors. SELECT sname, age FROM Sailors or SELECT S.sname, S.age FROM Sailors S Note that a SQL table is a set, not an ordered list, so names will not be printed in any particular order 2/22/16 CprE/SE 419, Srikanta Tirthapura 8 Example 2 Print out information about all sailors younger than 25 years SELECT sid, sname, rating, age FROM Sailors WHERE age < 25 equivalently written as SELECT * FROM Sailors WHERE age < 25 2/22/16 CprE/SE 419, Srikanta Tirthapura 9 Example 3 Find the names and ids of sailors who have reserved boat id 103 SELECT S.sname, FROM Sailors S, Reservations R WHERE S.sid = R.sid AND R.bid = 103 equivalently, SELECT Sailors.sname, Sailors.sid FROM Sailors, Reservations WHERE Sailors.sid = Reservations.sid AND Reservations.bid = 103 2/22/16 CprE/SE 419, Srikanta Tirthapura 10 Example 4 Find the names of all sailors who have reserved at least one boat. SELECT S.sname FROM Sailors S, Reservations R WHERE S.sid = R.sid 2/22/16 CprE/SE 419, Srikanta Tirthapura 11 Example 5 Find the names of all sailors who have reserved a red boat SELECT S.sname FROM Sailors S, Boats B, Reservations R WHERE (S.sid = R.sid) AND (R.bid = B.bid) AND (B.color = ‘red’) 2/22/16 CprE/SE 419, Srikanta Tirthapura 12 Example 6 Find sids of all sailors who have a rating of 10 or have reserved boat 111 SELECT S.sid FROM Sailors S WHERE S.rating = 10 UNION SELECT R.sid FROM Reservations R WHERE R.bid = 111 2/22/16 CprE/SE 419, Srikanta Tirthapura 13 Example 7 Find the average age of sailors SELECT AVG (age) FROM Sailors Find the average age of sailors with a rating of 10 SELECT AVG (age) FROM Sailors WHERE rating = 10 2/22/16 CprE/SE 419, Srikanta Tirthapura 14 Example 8 Find the name of the sailors with the maximum age SELECT S.sname, S.age FROM Sailors S WHERE S.age = (SELECT MAX (S2.age) FROM Sailors S2) 2/22/16 CprE/SE 419, Srikanta Tirthapura 15 Example 9 Find the names of sailors who are older than the oldest sailor with a rating of 10 SELECT S.sname FROM Sailors S WHERE S.age > (SELECT MAX(S2.age) FROM Sailors S2 WHERE S2.rating = 10) 2/22/16 CprE/SE 419, Srikanta Tirthapura 16 Example 10 Find the number of reservations for each sailor SELECT S.sid, COUNT(*) FROM Reservations GROUPBY S.sid 2/22/16 CprE/SE 419, Srikanta Tirthapura 17 Data and Metadata • Data: – Actual contents of tables – Ex: Sailors, Boats, Reservations – Stored on HDFS • Metadata: – Table Schemas – Stored in a metastore 2/22/16 CprE/SE 419, Srikanta Tirthapura 18