SQL stands for Structured Query Language sometimes pronounced sequel a very-high-level (declarative) language user specifies what is wanted, not how to find it number of standards original ANSI SQL updated in 1992 to SQL-92 or SQL2 most recent is SQL:1999 (also called SQL3) also extensions to SQL:1999, called SQL:2003 DBMS vendors all support ANSI SQL and largely SQL-92 some of SQL:1999 and SQL:2003 with variations and own extensions
SQL Functionality data definition language data manipulation language integrity constraints view definition transaction control embedded and dynamic SQL authorisation
Creating Relation Schemas Pubs table name Horse and Hound Hound and Hare March Hare Black Horse White Horse location Bloomsbury Islington Bloomsbury Islington Bloomsbury create table Pubs ( name varchar(50) primary key, location varchar(50) );
Creating Relation Schemas Sells table pub beer price Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50......... create table Sells ( pub varchar(50), beer varchar(50), price numeric(4,2), primary key (pub,beer) foreign key (pub) references Pubs );
Some Types char(n) varchar(n) int smallint decimal(p, d) real boolean date time... character string of fixed length n character string of varying length up to n integer (usually 4 bytes) small integer (usually 2 bytes) fixed-point number comprising p digits, d of them to the right of the decimal point floating-point number true, false or unknown (in MySQL, true is non-zero, while false is zero) year, month and day values (e.g., YYYY-MM-DD) hour, minute and second values (e.g., HH:MM:SS)
Some Integrity Constraints primary key(a 1,..., A n ): attributes A 1,..., A n form a primary key of the relation foreign key(a 1,..., A n ) references R: values for attributes A 1,..., A n must correspond to values of primary key attributes in R
Some Integrity Constraints primary key(a 1,..., A n ): attributes A 1,..., A n form a primary key of the relation foreign key(a 1,..., A n ) references R: values for attributes A 1,..., A n must correspond to values of primary key attributes in R not null: null value not allowed for attribute
Some Integrity Constraints primary key(a 1,..., A n ): attributes A 1,..., A n form a primary key of the relation foreign key(a 1,..., A n ) references R: values for attributes A 1,..., A n must correspond to values of primary key attributes in R not null: null value not allowed for attribute unique: attributes values must be unique in relation (like a simple candidate key)
Some Integrity Constraints primary key(a 1,..., A n ): attributes A 1,..., A n form a primary key of the relation foreign key(a 1,..., A n ) references R: values for attributes A 1,..., A n must correspond to values of primary key attributes in R not null: null value not allowed for attribute unique: attributes values must be unique in relation (like a simple candidate key) check: check constraint on attribute value, e.g., check (price > 0)...
Inserting Data in a Relation Sells: pub beer price Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 INSERT INTO Sells values ( White Horse, Shining Wit, 2.50);
Inserting Data in a Relation Sells: pub beer price Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 INSERT INTO Sells values ( White Horse, Shining Wit, 2.50); INSERT INTO Sells (pub, beer) values ( White Horse, Bad Habit );
Inserting Data in a Relation Sells: pub beer price Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 INSERT INTO Sells values ( White Horse, Shining Wit, 2.50); INSERT INTO Sells (pub, beer) values ( White Horse, Bad Habit ); INSERT INTO Sells values ( Horse and Hound, Bad Habit, 1.75); error
Inserting Data in a Relation Sells: pub beer price Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 INSERT INTO Sells values ( White Horse, Shining Wit, 2.50); INSERT INTO Sells (pub, beer) values ( White Horse, Bad Habit ); INSERT INTO Sells values ( Horse and Hound, Bad Habit, 1.75); error INSERT INTO Sells (pub) values ( March Hare ); error
Changing a Relation Schema Add an attribute to a relation: ALTER TABLE Sells ADD( measure varchar(10));
Changing a Relation Schema Add an attribute to a relation: ALTER TABLE Sells ADD( measure varchar(10)); Change the data type of an attribute: ALTER TABLE Sells MODIFY( measure varchar(15));
Changing a Relation Schema Add an attribute to a relation: ALTER TABLE Sells ADD( measure varchar(10)); Change the data type of an attribute: ALTER TABLE Sells MODIFY( measure varchar(15)); Remove an attribute from a relation: ALTER TABLE Sells DROP(measure);
Changing a Relation Schema Add an attribute to a relation: ALTER TABLE Sells ADD( measure varchar(10)); Change the data type of an attribute: ALTER TABLE Sells MODIFY( measure varchar(15)); Remove an attribute from a relation: ALTER TABLE Sells DROP(measure); Delete a relation schema (relation must be empty): DROP TABLE Sells;
Some Notes on SQL Syntax Keywords (e.g., ADD, DROP) are not case sensitive Identifiers (used to name tables, columns, etc.) are not case sensitive (although in MySQL table names are case sensitive) Tables are defined within a (default) schema; for a non-default schema, use schema:table Comments can be enclosed in /* and */ (for multi-line comments) or started with -- (for a comment ended by the end of line)
SQL: Select-From-Where Overall form of an SQL query is SELECT desired attributes FROM one or more tables WHERE conditions on rows of the tables are satisfied
Running Example Pubs (name, location) Drinkers (name, location) Sells (pub, beer, price) Visits (drinker, pub) each pub has a name and location each drinker has a name and location where they live pubs sell beers at various prices drinkers visit various pubs
Running Example Tables Pubs: Sells: name location Horse and Hound Bloomsbury Hound and Hare Islington March Hare Bloomsbury Black Horse Islington White Horse Bloomsbury pub beer price Horse and Hound Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 March Hare Bad Habit 1.75 March Hare Rampant Ram 2.50 Black Horse Bad Habit 2.50 Black Horse Shining Wit 2.25 Black Horse Rampant Ram 2.50 White Horse Rampant Ram 2.75
Running Example Tables name location Alice Islington Drinkers: Bob Bloomsbury Carol Islington Dave Bloomsbury Eve Stratford Visits: drinker Alice Alice Bob Bob Carol Dave Eve pub Black Horse Hound and Hare Horse and Hound White Horse March Hare Hound and Hare March Hare
Retrieving an entire table SELECT * FROM Drinkers; name location Alice Islington Bob Bloomsbury Carol Islington Dave Bloomsbury Eve Stratford
Retrieving an entire table SELECT * FROM Drinkers; name location Alice Islington Bob Bloomsbury Carol Islington Dave Bloomsbury Eve Stratford name Alice Bob Carol Dave Eve location Islington Bloomsbury Islington Bloomsbury Stratford
Projection Find where pubs are located:
Projection Find where pubs are located: SELECT location FROM Pubs;
Projection Find where pubs are located: SELECT location FROM Pubs; name location Horse and Hound Bloomsbury Hound and Hare Islington March Hare Bloomsbury Black Horse Islington White Horse Bloomsbury
Projection Find where pubs are located: SELECT location FROM Pubs; name Horse and Hound Hound and Hare March Hare Black Horse White Horse location Bloomsbury Islington Bloomsbury Islington Bloomsbury location Bloomsbury Islington Bloomsbury Islington Bloomsbury SQL does not implement set semantics
Projection Find unique locations of pubs:
Projection Find unique locations of pubs: SELECT DISTINCT location FROM Pubs;
Projection Find unique locations of pubs: SELECT DISTINCT location FROM Pubs; name location Horse and Hound Bloomsbury Hound and Hare Islington March Hare Bloomsbury Black Horse Islington White Horse Bloomsbury
Projection Find unique locations of pubs: SELECT DISTINCT location FROM Pubs; name location Horse and Hound Bloomsbury Hound and Hare Islington March Hare Bloomsbury Black Horse Islington White Horse Bloomsbury location Bloomsbury Islington
Projection Find what beers are sold by which pubs:
Projection Find what beers are sold by which pubs: SELECT beer, pub FROM Sells;
Projection Find what beers are sold by which pubs: SELECT beer, pub FROM Sells; beer pub Bad Habit Horse and Hound Rampant Ram Horse and Hound Shining Wit Hound and Hare Rampant Ram Hound and Hare Bad Habit March Hare Rampant Ram March Hare Bad Habit Black Horse Shining Wit Black Horse Rampant Ram Black Horse Rampant Ram White Horse
Projection Find the prices of beers in euros:
Projection Find the prices of beers in euros: SELECT beer AS Ale, price*1.12 AS PriceInEuros FROM Sells;
Projection Find the prices of beers in euros: SELECT beer AS Ale, price*1.12 AS PriceInEuros FROM Sells; Ale Bad Habit 1.68 Rampant Ram 2.24 Shining Wit 3.08 Rampant Ram 2.80 Bad Habit 1.96 Rampant Ram 2.80 Bad Habit 2.80 Shining Wit 2.52 Rampant Ram 2.80 Rampant Ram 3.08 PriceInEuros
Selection Who visits the March Hare :
Selection Who visits the March Hare : SELECT drinker FROM Visits WHERE pub= March Hare ;
Selection Who visits the March Hare : SELECT drinker FROM Visits WHERE pub= March Hare ; drinker pub Alice Black Horse Alice Hound and Hare Bob Horse and Hound Bob White Horse Carol March Hare Dave Hound and Hare Eve March Hare
Selection Who visits the March Hare : SELECT drinker FROM Visits WHERE pub= March Hare ; drinker Alice Alice Bob Bob Carol Dave Eve pub Black Horse Hound and Hare Horse and Hound White Horse March Hare Hound and Hare March Hare drinker Carol Eve
Selection Which pubs sell some beer for less than 2.50?:
Selection Which pubs sell some beer for less than 2.50?: SELECT DISTINCT pub FROM Sells WHERE price < 2.50; (See Sells table)
Selection Which pubs sell some beer for less than 2.50?: SELECT DISTINCT pub FROM Sells WHERE price < 2.50; (See Sells table) pub Horse and Hound March Hare Black Horse
Selection Which pubs sell Bad Habit for less than 2.50?:
Selection Which pubs sell Bad Habit for less than 2.50?: SELECT pub FROM Sells WHERE beer= Bad Habit AND price < 2.50; (See Sells table)
Selection Which pubs sell Bad Habit for less than 2.50?: SELECT pub FROM Sells WHERE beer= Bad Habit AND price < 2.50; (See Sells table) pub Horse and Hound March Hare
Selection Condition A boolean expression in the WHERE clause may contain the following operators: AND: beer= Bad Habit AND price < 2.50 NOT: NOT beer= Bad Habit Equivalently: beer <> Bad Habit OR: beer= Bad Habit OR beer= Rampant Ram
Selection Condition A boolean expression in the WHERE clause may contain the following operators: AND: beer= Bad Habit AND price < 2.50 NOT: NOT beer= Bad Habit Equivalently: beer <> Bad Habit OR: beer= Bad Habit OR beer= Rampant Ram or any combination: beer= Bad Habit OR beer= Rampant Ram AND price < 2.50 which is equivalent to beer= Bad Habit OR (beer= Rampant Ram AND price < 2.50)