Date post: | 06-Apr-2018 |
Category: |
Documents |
Upload: | jahanzeb-bashir-bhutta |
View: | 222 times |
Download: | 0 times |
of 19
8/3/2019 Lecture 20111115-1130 Tuesday
1/19
Structured Query Language (SQL)
1. Computing a Join
2. Outer Join3. Exist and Not exist
4. Combining the result tables: Union, Intersect Except
5. Adding data to the database
6. Updating data in the database7. Deleting data in the database
8/3/2019 Lecture 20111115-1130 Tuesday
2/19
Computing a Join
A join is a subset ofcartesian product
The Cartesian product oftwo tables is another table consisting ofall
possible pairs ofrows from the two tables
The columns ofthe product table are all the columns ofthe first table
followed by all the columns ofthe second table.
Ifwe specify a two-table query without a WHERE clause, SQL produces the
Cartesian product ofthe two tables as the query result
8/3/2019 Lecture 20111115-1130 Tuesday
3/19
Outer Join
The join operation combines data from two tables by forming pairs of
related rows where the matching columns in each table have the same
value. Ifone row ofa table is unmatched, the row is omitted from the
result table.
The Outer join retains rows that do not satisfy the join condition
Ifwe want to include the unmatched rows in the result table, we can use
an Outer join.
There are three types ofOuter join:
Left Outer Join
Right Outer Join
Full Outer Join
8/3/2019 Lecture 20111115-1130 Tuesday
4/19
Left Outer Join
Example List all branch offices and any properties that are in the
same city.
Query SELECT b.*, p.*
FROM Branch1 b LEFT JOIN PropertyForRent1 p
ON b.bCity = p.pCity;
Outcome
SELECT b.*, p.*
FROM Branch1 b, PropertyForRent1 p
WHERE b.bCity = p.pCity;
8/3/2019 Lecture 20111115-1130 Tuesday
5/19
Right Outer Join
Example List all properties and any branch offices that are in the
same city.
Query SELECT b.*, p.*
FROM Branch1 b RIGHT JOIN PropertyForRent1 p
ON b.bCity = p.pCity;Outcome
8/3/2019 Lecture 20111115-1130 Tuesday
6/19
Full Outer join
Example List the branch offices and properties that are in the same
city along with any unmatched branches or properties.
Query SELECT b.*, p.*
FROM Branch1 b FULL JOIN PropertyForRent1 p
ON b.bCity = p.pCity;
Outcome
8/3/2019 Lecture 20111115-1130 Tuesday
7/19
EXISTS and NOT EXISTS
The keywords EXISTS and NOT EXISTS are designed for use only with
subqueries
They produce a simple true/false result
EXISTS is true ifand only ifthere exists at least one row in the result table
returned by the subquery
It is false ifthe subquery returns an empty result table
NOT EXISTS is the opposite ofEXISTS. Since EXISTS and NOT EXISTS check
only for the existence or non-existence ofrows in the subquery result
table the subquery can contain any number ofcolumns
8/3/2019 Lecture 20111115-1130 Tuesday
8/19
Example Find all staff who work in a London branch office
Query SELECT staffNo, fName, lName, position
FROM Staff s
WHERE EXISTS (SELECT *
FROM Branch b
WHERE s.branchNo = b.branchNo
AND city = London
);
Outcome
This query could be rephrased as Find all staffsuch that there exists aBranch row containing his/her branch number, branchNo, and the branchcity equal to London. The test for inclusion is the existence ofsuch a row.Ifit exists, the subquery evaluates to true
8/3/2019 Lecture 20111115-1130 Tuesday
9/19
Combining Result Tables (UNION, INTERSECT, EXCEPT)
In SQL, we can use the normal set operations ofUnion, Intersection, and
Difference to combine the results oftwoor more queries into a singleresult table:
The Union oftwo tables, A and B, is a table containing all rows that are in
either the first table A or the second table B or both.
The Intersection oftwo tables, A and B, is a table containing all rows that are
common to both tables A and B. The Difference oftwo tables, A and B, is a table containing all rows that are in
table A but are not in table B.
8/3/2019 Lecture 20111115-1130 Tuesday
10/19
Use ofUNION
Example Construct a list of all cities where there is either a branch
office or a property. Query (SELECT city
FROM Branch
WHERE city IS NOT NULL )
UNION
(SELECT cityFROM PropertyForRent
WHERE city IS NOT NULL)
Result
8/3/2019 Lecture 20111115-1130 Tuesday
11/19
Use ofINTERSECT
Example Construct a list of all cities where there is both a branch
office and a property. Query (SELECT city
FROM Branch
WHERE city IS NOT NULL )
INTERSECT
(SELECT cityFROM PropertyForRent
WHERE city IS NOT NULL)
Result
8/3/2019 Lecture 20111115-1130 Tuesday
12/19
Use ofEXCEPT
Example Construct a list of all cities where there is a branch office
but no properties. Query (SELECT city
FROM Branch
WHERE city IS NOT NULL )
EXCEPT
(SELECT cityFROM PropertyForRent
WHERE city IS NOT NULL)
Result
8/3/2019 Lecture 20111115-1130 Tuesday
13/19
Database Updates
SQL is a complete data manipulation language that can be used for
modifying the data in the database as well as querying the database.
The commands for modifying the database are not as complex as the
SELECT statement.
SQL statements that modify the contents ofthe tables in the database:
INSERT adds new rows ofdata to a table;
UPDATE modifies existing data in a table;
DELETE removes rows ofdata from a table.
8/3/2019 Lecture 20111115-1130 Tuesday
14/19
Adding data to the database (INSERT)
There are twoforms ofthe INSERT statement.
1-Single row 2-Multiple rows
Single row format: INSERT INTO TableName [(columnList)]
VALUES (dataValueList)
Example INSERT INTO Staff
VALUES (SG16, Alan, Brown, Assistant, M, DATE 1957-05-25, 8300, B003);
TableName may be either a base table or an updatable view (see Section 6.4), and
columnList represents a list ofone or more column names separated by commas.
The columnList is optional; ifomitted, SQL assumes a list ofall columns in their originalCREATE TABLE order.
Ifspecified, then any columns that are omitted from the list must have been declared
as NULL columns when the table was created, unless the DEFAULT option was used
when creating the column
The dataValueList must match the columnListas follows: the number ofitems in each list must be the same;
there must be a direct correspondence in the position ofitems in the two lists, so that the firstitem in dataValueList applies to the first item in columnList, the second item in dataValueListapplies to the second item in columnList, and so on
the data type ofeach item in dataValueList must be compatible with the data type of thecorresponding column.
8/3/2019 Lecture 20111115-1130 Tuesday
15/19
INSERT . . . VALUES
INSERT INTO Staff
VALUES (SG16, Alan, Brown, Assistant, M, DATE 1957-05-25,
8300,B003);
8/3/2019 Lecture 20111115-1130 Tuesday
16/19
INSERT using defaults
INSERT INTO Staff (staffNo, fName, lName, position, salary, branchNo)
VALUES (SG44, Anne, Jones, Assistant, 8100, B003);
INSERT INTO Staff
VALUES (SG44, Anne, Jones, Assistant, NULL, NULL, 8100, B003);
8/3/2019 Lecture 20111115-1130 Tuesday
17/19
INSERT . . . SELECT
INSERT INTO StaffPropCount
(SELECT s.staffNo, fName, lName, COUNT(*)
FROM Staff s, PropertyForRent p
WHERE s.staffNo = p.staffNo
GROUP BY s.staffNo, fName, lName)
UNION(SELECT staffNo, fName, lName, 0
FROM Staff s
WHERE NOT EXISTS (SELECT *
FROM PropertyForRent p
WHERE p.staffNo = s.staffNo));
8/3/2019 Lecture 20111115-1130 Tuesday
18/19
Modifying data in the database (UPDATE)
The UPDATE statement allows the contents ofexisting rows in a named
table to be changed.
The format ofthe command is:
UPDATE TableName
SET columnName1 = dataValue1 [, columnName2 = dataValue2 . . . ]
[WHERE searchCondition]
TableName can be the name of a base table or an updatable view
The SET clause specifies the names ofone or more columns that are to be
updated.
The WHERE clause is optional; ifomitted, the named columns are
updated for all rows in the table.
Ifa WHERE clause is specified, only those rows that satisfy the
searchCondition are updated.
The new dataValue(s) must be compatible with the data type(s) for the
corresponding column(s).
8/3/2019 Lecture 20111115-1130 Tuesday
19/19
UPDATE all rows UPDATE Staff
SET salary = salary*1.03;
UPDATE specific rows UPDATE Staff
SET salary = salary*1.05
WHERE position = Manager;
UPDATE multiple columns UPDATE Staff
SET position = Manager, salary = 18000
WHERE staffNo = SG14; ERE position = Manager;