Lesson 2 Data Manipulation Language
IN THIS LESSON YOU WILL LEARN To add data to the database. To remove data. To update existing data. To retrieve the information from the database that fulfil the stablished conditions.
Altering the contents of the database
ALTERING CONTENTS There re three SQL statements that allow to manipulate the data within a table: INSERT UPDATE DELETE
INSERT VALUES It s allows to insert one record into a table at a time. Insert can be used in two ways: To insert single complete row To insert a single partial row
INSERT VALUES Insert a complete row STUDENTS STUDENTS id name phone country age id name phone country age 180 230 Robert Jenn 999 111 US Scotland 23 32 180 230 Robert Jenn 999 111 US Scotland 23 32 340 Robert Ireland 35 340 Robert Ireland 35 110 Kristen 444 Scotland 28 INSERT INTO students VALUES ( 110, Kristen, 444, Scotland, 28);
INSERT VALUES Insert a partial row STUDENTS STUDENTS id name phone country age id name phone country age 180 230 Robert Jenn 999 111 US Scotland 23 32 180 230 Robert Jenn 999 111 US Scotland 23 32 340 Robert Ireland 35 340 Robert Ireland 35 110 Kristen 28 INSERT INTO students (id, name, age) VALUES ( 110, Kristen, 28); null values!!
INSERT VALUES II Three rules must be followed when inserting data: The values used must be the same data type as the fields they are being added to. The data s size must be within the column s size. The data s location in the VALUES list must correspond to the location in the column list of the column it is being added to.
UPDATE VALUES It s used to change the values of existing records. The syntax of the UPDATE statement is as follows: UPDATE table SET column = value1 [, columname2 = value2]... WHERE search_condition
UPDATE VALUES Example 28 US 999 Kristen 110 35 Ireland Robert 340 32 Scotland 111 Jenn 230 23 US 999 Robert 180 age country phone name id UPDATE students SET phone= 999, country= US WHERE name= Kristen ; STUDENTS STUDENTS 28 Scotland 444 Kristen 110 35 Ireland Robert 340 32 Scotland 111 Jenn 230 23 US 999 Robert 180 age country phone name id
DELETE FROM STUDENTS STUDENTS id 180 230 340 110 name Robert Jenn Robert Kristen phone 999 111 444 country US Scotland Ireland Scotland age 23 32 35 28 id 180 230 340 name Robert Jenn Robert phone 999 111 country US Scotland Ireland age 23 32 35 DELETE FROM students WHERE name= Kristen ; ATTENTION!!
Queries on a Table
INTRODUCTION Queries Database manipulation is quite simple. The main challenge to database manipulation is selecting the rows that you want to change, delete, or retrieve.
SELECT FROM That s the statement that you will probably use most frequently. It allows to retrieve information from tables; It returns a set of rows. It consists on selecting columns and rows from one or more tables.
SELECT FROM SELECT columns FROM table; Where - columns specifies which columns (fields) you want to display. - table specifies from which table/s you want to retrieve columns.
SELECT FROM Example id name phone country age name 180 Robe 999 US 23 Robert 230 Jenn 111 Scotland 32 Jenn 340 Robert Ireland 35 Robert 110 Kristen 444 Scotland 28 Kristen SELECT name FROM Students;
SELECT FROM DISTINCT Clause If they re duplicate values in the result, they are all displayed. The clause DISTINCT removes all duplicate values before displaying them. SELECT SELECT DISTINCT columns columns FROM table
SELECT FROM DISTINCT Clause SELECT name DISTINCT FROM name Students; FROM Students; id name phone country age name 180 Robert 999 US 23 Robert 230 Jenn 111 Scotland 32 Jenn 340 Robert Ireland 35 Robert 110 Kristen 444 Scotland 28 Kristen
SELECT FROM DISTINCT Clause SELECT DISTINCT name FROM Students; id name phone country age name 180 Robert 999 US 23 Robert 230 Jenn 111 Scotland 32 Jenn 340 Robert Ireland 35 Kristen 110 Kristen 444 Scotland 28
SELECT FROM How to retrieve several columns The column names must be separated by commas SELECT column1, column2 FROM table;
SELECT FROM How to retrieve several columns SELECT name, phone FROM Students; id 180 230 340 name Robert Jenn Robert phone 999 111 country US Scotland Ireland age 23 32 35 name Robert Jenn Robert phone 999 111 110 Kristen 444 Scotland 28 Kristen 444
SELECT FROM How to retrieve all columns Wildcard SELECT * FROM table; When an asterisk (*) is specified after the SELECT all the columns in the table are returned.
SELECT FROM ORDER BY Clause Displays the output of a query in either ascending or descending alphabetical order. ORDER BY colum [ASC DESC] Two keywords can be specified: ASC: to sort in ascending order (default). DESC: to sort in descending order.
Activity 3.1 The Distributors Database
ACTIVITY 3.1 Consider the DISTRIBUTORS database used by an imaginary group of suppliers of pieces. The database has three tables: PIECES DISTRIBUTORS SUPPLIES
ACTIVITY 3.1 The relational schema for these relations is: PIECES (#p_id, p_name, p_colour, p_weight, p_city) DISTRIBUTORS (#s_id, s_name, s_age, s_city) SUPPLIES (#id_s, #id_p, quatity) foreign key: id_s DISTRIBUTORS foreign key: id_p PIECES
ACTIVITY 3.1 Write queries to retrieve: 1. The codes of all distributors. 2. The codes of the distributors that provide a specific piece. 3. The name and the city of the distributors sorted in descending order. 4. The codes of the distributors that provide a specific piece without repeating the obtained values.
SELECT WHERE It s used to filter records. It extracts only those records that fulfill a specified condition. The rows that do not satisfy the condition are excluded. SELECT columns FROM table [WHERE condition]
SELECT WHERE Example SELECT * FROM Students WHERE age < 30; id name phone country age 180 Robert 999 US 23 230 Jenn 111 Scotland 32 340 Robert Ireland 35 110 Kristen 444 Scotland 28 id name phone country age 180 Robert 999 US 23 110 Kristen 444 Scotland 28
SELECT WHERE WHERE Clause operators = <> < <= > >= BETWEEN IN LIKE NOT IS NULL Equal Not equal Less than Less than or equal Greater than Greater than or equal Between two specified values The value matches at least one of a group. Search for a pattern Negates whatever condition that comes next. Checks for no value.
SELECT WHERE Use of quotes With SQL you must use single quotes around text values. Most DBMS also accept double quotes. Numeric values must not be enclosed in quotes.
USE OF QUOTES This is wrong: PAY ATTENTION!! This is correct: WHERE name = John; WHERE name = John ;
ACTIVITY 3.1 Write queries to retrieve: 5. The codes of distributors from Barcelona younger than 30 years old. 6. The codes of distributors and the codes of pieces for the supplies where the amount is 100.
BETWEEN Operator o To check if a value falls within a specified rang. Instead of doing: WHERE salary>=100 AND salary<=500; You can use BETWEEN as follows: WHERE salary BETWEEN 100 AND 500;
IN Operator o To check if a field matches one of several possible values. - Instead of doing:...where fname='martins' OR fname='dalzell' OR... - We can do:...where fname IN ('martins', 'dalzell',...);
IN Operator Why use the IN operator? o When working with a long list of values, the IN operator syntax is cleaner. o IN operators execute quickly than lists of OR operators. o The IN operator can contain another SELECT statement.
LIKE Operator o LIKE can be used if don t know the exact form of the string for which you re searching. o To identify partial matches, SQL uses two wildcard characters: (%) string of zero or more characters. (_) single character.
LIKE Operator Examples... WHERE name LIKE R% ;... WHERE titlle LIKE %data% ;...WHERE word LIKE t_p_ ;
IS NULL Operator o It is used to check for columns with NULL values. SELECT * FROM students WHERE phone IS NULL; id name phone country age 340 Robert Ireland 35
NOT Operator o It negates whatever condition that comes next. e.g....not IN, NOT LIKE, IS NOT NULL o NOT can also be used before a column to filter on. SELECT name FROM students WHERE NOT name= Robert ; =...name <> Robert ;
ACTIVITY 3.1 Write queries to retrieve: 7. Code and name of the pieces starting with an S. 8. Code and name of the pieces containing the letter S.
LOGICAL OPERATORS o SQL lets you specify multiple WHERE clauses by using the logical operators AND and OR....WHERE age < 30 AND country = Scotland ;...WHERE age < 30 OR country = Scotland ;
LOGICAL OPERATORS Order of evaluation I...WHERE age < 30 OR country = Scotland AND name <> Kristen ; What does this query return? =...WHERE age < 30 OR country = Scotland AND name <> Kristen ;
LOGICAL OPERATORS Order of evaluation II o SQL processes AND operators before OR operators. BUT... o Whatever condition enclosed within parentheses is evaluated first....where (age < 30 OR country = Scotland ) AND name <> Kristen ;
CALCULATED FIELDS o Data stored in the table often is not available in the exact format you need. o Data needs to be reformated before being retrieved. o You can: Concatenate fields. Use aliases. Perform mathematic calculations.
CALCULATED FIELDS Concatenate fields o You can join two more fields to report an only one. SELECT name ( country ) FROM students; name (country) Robert (US) SELECT name (country ) Jenn (Scotland) Robert (Ireland) Kristen (Scotland)
CALCULATED FIELDS Use of aliases I o In SQL you can rename the columns names so as to they have different names in the result. SELECT price AS PriceInEuros...
CALCULATED FIELDS Use of aliases II o Aliases can be complete strings but, in that case, quotes must be used. SELECT price AS Price In Euros...
CALCULATED FIELDS Mathematic calculations o Aritmetic operations can be performed in the column names, as follows: SELECT price * 1,20... o It s also possible to do it in the conditions.... WHERE (price * 1,1) > 1;
AGGREGATE FUNCTIONS o SQL provides some functions to relate to all the rows of a column. o For example, you can find the maximum value of a column, the average of his values, etc. SELECT function(column) FROM table;
AGGREGATE FUNCTIONS Exemple PRODUCTS code P34 name A price 100 sum P45 B 300 600 P98 C 50 P35 D 150 SELECT SUM(price) FROM products;
AGGREGATE FUNCTIONS List FUNCTION AVG() COUNT() MAX() MIN() SUM() RETURNS a column s average value. the number of row in a column. a column s highest value a column s lowest value. a column s highest value.
ACTIVITY 3.1 Write queries to retrieve: 9. Total amount of distributors. 10. Amount of distributors that supply at least one piece. 11. Amount of supplies for piece P2. 12. Total amount supplied for piece P2.
GROUP BY oin SQL it s possible to group rows according to the value of a column.
GROUP BY Example SELECT * FROM products GROUP BY manufacturer; code name price manufacturer 1 chair 100 35 2 3 table wardrobe 400 800 35 35 Manufacturer 35 4 5 6 sofa armchair chest 1100 320 250 47 47 89 Manufacturer 47 Manufacturer 89
GROUP BY Using aggregate functions o It s possible to apply aggregate functions to the columns that are not grouped. SELECT AVG(price), manufacturer FROM pieces GROUP BY manufacturer; AvgPrice 433,33 710 250 manufacturer 35 47 89
GROUP BY Using aggregate functions II The function applies on the column s values in the group not in all rows!!
GROUP BY How does it work? price manufacturer price manufacturer 100 35 800 35 400 35 1100 47 800 35 100 250 320 35 47 47 GROUP BY manufacturer 1100 320 47 47 400 35 250 89 AvgPrice 433,33 710 manufacturer 35 47 SELECT AVG(price), manufacturer 250 89
GROUP BY Using aggregate functions II SELECT AVG(price), manufacturer FROM pieces GROUP BY manufacturer; It must be the same!
ACTIVITY 3.1 Write the corresponding queries: 13. For each piece supplied, list the code, the maximum amount and the minimum amount supplied for this piece. 14. For each piece supplied, list the code, the maximum amount and the minimum amount supplied for this piece. Exclude all the sales of distributor D1.
GROUP BY HAVING o SQL allows us to filter groups; which groups include and which ones to exclude. o You must filter based on the complete group, not on individiual rows. o HAVING supports all the WHERE s operators and the syntax is also the same.
GROUP BY HAVING II o If we want to retrieve only the codes of manufacturers that offer products whose averege price is greater than 300. SELECT AVG(price), manufacturer FROM pieces GROUP BY manufacturer HAVING AVG(price) > 300; AvgPrice 433,33 710 manufacturer 35 47 250 89
ACTIVITY 3.1 Write the corresponding queries: 15. Write a query to retrieve the codes of pieces supplied by more than one distributor.
SELECT CLAUSES SUMMARY CLAUSE SELECT FROM [WHERE] [GROUP BY] [HAVING] [ORDER BY] DESCRIPTION Specifies columns or expressions to be returned. Specifies from which tables to take data. Filters out rows that don t satisfy the search condition. Separates rows into groups. Filter out groups that don t satisfy the search condition. Sorts the results of prior clauses to produce a final output.
SELECT CLAUSES SEQUENCE CLAUSE FROM WHERE GROUP BY HAVING SELECT ORDER BY Accesses to the tables. ACTION The rows that don t fulfil the condition are removed. Groups the rows according to the column/s specified. The groups that don t fulfill the condition are removed. Only the columns (or aggregate functions o calculated fields) specified here are included in the result. The result is ordered according to the criteria.
Queries on Multiple Tables
JOINS o With SELECT we can get data from two or more tables: SELECT column1, column2 FROM table; The column names must be separated by commas
BASIC JOIN The simplest JOIN is a two-table SELECT that has no WHERE clauses. SELECT column1, column2 FROM table; The result is the cartesian product of the two tables!!
BASIC JOIN Cartesian product PIECES CODE NAME MANUFACTURER 1 screw 35 2 nut 43 3 nail 35 MANUFACTURERS CODE NAME 35 Western Supplies 43 Grizzly Gears SELECT * pieces, manufacturers PIECES.code PIECES.name PIECES.manufacturer MANUFACTURERS.code MANUFACTURERS.name 1 screw 35 35 Western Supplies 1 screw 35 43 Grizzly Gears 2 nut 43 35 Western Supplies 2 nut 43 43 Grizzly Gears 3 nail 35 35 Western Supplies
BASIC JOIN How to keep the right rows? PIECES.code PIECES.name PIECES.manufacturer MANUFACTURERS.code 1 screw 35 35 1 screw 35 43 2 nut 43 35 2 nut 43 43 3 nail 35 35 We only want the highlighted rows How can we do it??
BASIC JOIN How to keep the right rows? SELECT * FROM pieces, manufacturers WHERE PIECES.manufacturing = MANUFACTURERS.code Matching condition
BASIC JOIN Labels and prefixes What happens if in the two tables there re two columns with the same name? PIECES CODE NAME MANUFACTURER 1 screw 35 2 nut 43 3 nail 35 MANUFACTURERS CODE NAME 35 Western Supplies 43 Grizzly Gears SELECT piece.name, manufacturers.name FROM pieces, manufacturers WHERE piece.manufacturer=manufacturer.code; PIECES.name screw nut nail MANUFACTURERS.name Westerne Supplies Grizzly Gears Westerne Supplies
BASIC JOIN Labels and prefixes II We can use labels to abbreviate the table names. PIECES CODE NAME MANUFACTURER 1 screw 35 2 nut 43 3 nail 35 MANUFACTURERS CODE NAME 35 Western Supplies 43 Grizzly Gears SELECT P.name, M.name FROM pieces P, manufacturers M WHERE P.manufacturer=M.code; PIECES.name screw nut nail MANUFACTURERS.name Westerne Supplies Grizzly Gears Westerne Supplies
ACTIVITY 3.1 Write queries to retrieve: 16. Code of pieces that have been supplied in the same or greater amount than 400. 17. Name of pieces that have been supplied in the same or greater amount than 400. 18. Name of the red pieces supplied by Clark.
INNER JOIN SELECT * FROM Pieces, Manufacturers WHERE Pieces.manufacturer= Manufacturers. Code; EQUIVALENT!! SELECT * FROM Pieces INNER JOIN Manufacturers ON Pieces.manufacturer = Manufacturers.Code;
INNER JOIN II PIECES NAME screw nut nail MANUFACTURER 35 <NULL> 35 MANUFACTURERS CODE 35 43 NAME Western Supplies Grizzly Gears SELECT * FROM pieces, manufacturers WHERE pieces.manufacturer=manufacturers.code; PIECES.code PIECES.name PIECES.manufacturer MANUFACTURERS.code MANUFACTURER.name 1 screw 35 35 Western Supplies 3 nail 35 35 Western Supplies Where is the nut??
INNER JOIN III When you re joining two tables, the first one (the one of the left) may have rows that don t have matching rows in the second table. In an INNER JOIN on those tables, all the unmatched rows are excluded from the output.
OUTER JOIN OUTER JOIN, unlike INNER JOIN, don t exclude the unmatched rows. Two types: SELECT columns FROM table1 LEFT OUTER JOIN table2 ON matching_condition; and SELECT columns FROM table1 RIGHT OUTER JOIN table2 ON matching_condition;
OUTER JOIN LEFT OUTER JOIN PIECES NAME screw nut nail MANUFACTURER 35 <NULL> 35 MANUFACTURERS CODE 35 43 NAME Western Supplies Grizzly Gears SELECT columns FROM pieces LEFT OUTER JOIN manufacturers ON pieces.manufacturer = manufacturers.code; PIECES.code PIECES.name PIECES.manufacturer MANUFACTURERS.code MANUFACTURER.name 1 screw 35 35 Western Supplies 2 nut <NULL> <NULL> <NULL> 3 nail 35 35 Western Supplies
OUTER JOIN RIGHT OUTER JOIN It s the same than a LEFT OUTER JOIN, but with the second table (the one of the right). The right outer join preserves unmatched rows from the right table but discards unmatched rows from the left table. There is also a FULL OUTER JOIN that takes all the rows from both tables.
SUBQUERIES o In a WHERE Clause, we can compare the value of column with the result of another query.
SUBQUERIES Example o Retrieve the name of the most expesive products: SELECT name FROM pieces WHERE piece = Minimum Price??
SUBQUERIES Example II o Retrieve the name of the most expensive products: SELECT name FROM pieces WHERE piece = SELECT MIN(price) FROM pieces;
ACTIVITY 3.1 Write the corresponding queries: 19. Distributors younger than the maximum age in the distributors table. 20. Distributors based in the same city as the distributor D1.
SUBQUERIES IN operator o The IN operator can be used to make up nested queries, as follows: WHERE price IN (subquery) ; The values returned by the subquery must have the same data type than the column!!
SUBQUERIES IN operator. Example o It simplifies some queries when matching tables is required. o Instead of doing... SELECT P.name FROM PIECES P, MANUFACTURER M WHERE P.manufacturer = M.code AND M.place = Bilbao ;
SUBQUERIES IN operator. Example II We can do: CODE 35 43 67 NAME Western Supplies Grizzly Gears Bilbo Tech CITY Bilbao Madrid Bilbao CODE 35 67 SELECT code FROM manufacturers WHERE city= Bilbao ; SELECT name FROM pieces WHERE manufacturer IN ( );
SUBQUERIES IN operator. Example III SELECT name FROM pieces WHERE manufacturer IN (SELECT code FROM manufacturers WHERE place = Bilbao ) ;
ACTIVITY 3.1 Write the corresponding queries: 21. Name of pieces that have been supplied in an amount same or greater than 400 (using subqueries). 22. Names of distributors that supply the piece P2. 23. Name of distributors that supply at least one red piece. 24. Codes of distributors that supply at least one of the pieces supplied by D2.
SUBQUERIES ANY, ALL and EXISTS o If the subquery is expected to produce a list of values, the following operators can also be used: ANY ALL EXISTS o ANY and ALL must be combined with one of the logical operator: =ANY, >=ANY, <ALL, <=ALL, etc.
ANY Operator o The predicate is true is the test is true for any (at least one of the elements). SELECT name FROM pieces WHERE manufacturer = ANY = IN (SELECT code FROM manufacturers WHERE place = Bilbao );
ALL Operator o Returns true if test is true for all list elements. SELECT name FROM pieces WHERE manufacturer <> ALL = NOT IN (SELECT code FROM manufacturers WHERE place = Bilbao );
EXISTS Operator o Returns true if and only if there exists at least one row in result table returned by subquery. SELECT name FROM pieces WHERE manufacturer EXISTS (SELECT * FROM manufacturers WHERE place = Bilbao ) ;
ACTIVITY 3.1 Write the corresponding queries: 25. Names of distributors that supply at least one piece. 26. Name of the distributors that supply all the pieces. 27. Codes of the distributors that supply at least one piece in an amount greater than any amount supplied by D1.
INSERT SELECT INSERT VALUES it s useful when adding single records to a table. INSERT SELECT enables to copy information from one or more tables into another table. INSERT INTO table (col1, col2...) SELECT col1, col2... FROM table WHERE search_condition
INSERT SELECT Example STUDENTS STUDENTS 2 id name phone country age id name phone country age 180 Robert 999 US 23 180 Robert 999 US 23 230 Jenn 111 Scotland 32 230 Jenn 111 Scotland 32 340 Robert Ireland 35 340 Robert Ireland 35 INSERT INTO students2 (id, name, phone, country, age) SELECT * FROM students;