SQL SQL Functionality 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 data definition language data manipulation language integrity constraints view definition transaction control embedded and dynamic SQL authorisation Creating Relation Schemas Creating Relation Schemas Pubs table create table Pubs ( varchar(50) primary key, varchar(50) ); Sells table beer price Bad Habit 1.50 Rampant Ram 2.00 Shining Wit 2.75 Rampant Ram 2.50......... create table Sells ( varchar(50), beer varchar(50), price numeric(4,2), primary key (,beer) foreign key () references Pubs );
Some Types Some Integrity Constraints 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) 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 Changing a Relation Schema Sells: beer price Bad Habit 1.50 Rampant Ram 2.00 Shining Wit 2.75 Rampant Ram 2.50 Add an attribute to a relation: ALTER TABLE Sells ADD( measure varchar(10)); Change the data type of an attribute: INSERT INTO Sells values (, Shining Wit, 2.50); INSERT INTO Sells (, beer) values (, Bad Habit ); INSERT INTO Sells values (, Bad Habit, 1.75); error INSERT INTO Sells () values ( ); 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; error
Some Notes on SQL Syntax SQL: Select-From-Where Keywords (e.g., ADD, DROP) are not case sensitive Identifiers (used to tables, columns, etc.) are not case sensitive (although in MySQL table s 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) 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 (, ) Drinkers (, ) Sells (, beer, price) Visits (drinker, ) each has a and each drinker has a and where they live s sell beers at various prices drinkers visit various s Running Example Tables Pubs: Sells: beer price Bad Habit 1.50 Rampant Ram 2.00 Shining Wit 2.75 Rampant Ram 2.50 Bad Habit 1.75 Rampant Ram 2.50 Bad Habit 2.50 Shining Wit 2.25 Rampant Ram 2.50 Rampant Ram 2.75
Running Example Tables Retrieving an entire table Drinkers: drinker Visits: Stratford SELECT * FROM Drinkers; Stratford Stratford Projection Projection Find where s are located: SELECT FROM Pubs; Find unique s of s: SELECT DISTINCT FROM Pubs; SQL does not implement set semantics
Projection Projection Find what beers are sold by which s: SELECT beer, FROM Sells; beer Bad Habit Rampant Ram Shining Wit Rampant Ram Bad Habit Rampant Ram Bad Habit Shining Wit Rampant Ram Rampant Ram Find the prices of beers in euros: SELECT FROM Ale beer AS Ale, price*1.12 AS PriceInEuros Sells; 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 Selection Who visits the : SELECT drinker FROM Visits WHERE = ; drinker drinker Which s sell some beer for less than 2.50?: (See Sells table) SELECT DISTINCT FROM Sells WHERE price < 2.50;
Selection Selection Condition Which s sell Bad Habit for less than 2.50?: SELECT FROM Sells WHERE beer= Bad Habit AND price < 2.50; 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 (See Sells table) 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)