SQL: Structured Query Language – Part 2
description
Transcript of SQL: Structured Query Language – Part 2
![Page 1: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/1.jpg)
FEN 2014-02-07 1
Queries: SELECT
Data Manipulation: INSERT, UPDATE, DELETE
SQL: Structured Query Language – Part 2
![Page 2: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/2.jpg)
2
VW: new database
FEN 2014-02-07
Look at this database: ..\lektion03 (SQL2)\vwDatabase.pdf
Create the database. These scripts may be helpful.
![Page 3: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/3.jpg)
3
SQL: Data Manipulation Language
SELECTUPDATEINSERTDELETE
FEN 2014-02-07
All work on tables
![Page 4: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/4.jpg)
4
Queries: SELECT
Syntax:
SELECT <attribute-list>FROM <tables>[WHERE <condition>][GROUP BY <attribute-list>][HAVING <condition>][ORDER BY <attribute-list>]
[...]: WHERE, GROUP BY, HAVING and ORDER BY may be omitted.
FEN 2014-02-07
![Page 5: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/5.jpg)
5
Examples: Company
(Q0): Row and column selection:
SELECT Bdate, AddressFROM EmployeeWHERE Fname= ’John’
AND Minit = ’B’ AND Lname = ’Smith’
All attributes:
SELECT *---
FEN 2014-02-07
![Page 6: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/6.jpg)
6
Examples: Company
(Q1): JOIN:
SELECT Fname, Lname, AddressFROM Employee, DepartmentWHERE Dname = ’Research’
AND Dno = Dnumber
• Last term in the WHERE-clause is the join-condition. If omitted the result will be the Cartesian product.
• Alternative syntax is possible.
FEN 2014-02-07
![Page 7: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/7.jpg)
7
Examples: Company
(Q2): JOIN several tables:
SELECT Pnumber, Dnum, Lname, AddressFROM Project, Employee, DepartmentWHERE Plocation = ’Stafford’
AND Dnum = DnumberAND Ssn = Mgrssn
Note: Two join-conditions in the WHERE-clause.
FEN 2014-02-07
![Page 8: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/8.jpg)
8
Examples: Company
(Q8): Ambiguous attribute names and aliases:
SELECT E.Fname, E.Lname,S.Fname, S.Lname
FROM Employee E, Employee SWHERE E.Superssn= S.Ssn
Employee is joined with itself using thealiases E and S.
’.’ (”dot”)-notation may also beused to resolve ambiguous attributeNames (remember Minibank?).
FEN 2014-02-07
![Page 9: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/9.jpg)
9
Examples: Company
SQL-tables are NOT sets(in the mathematical sense of the word set):
(Q11): SELECT Salary
FROM Employee
(Q11A): SELECT DISTINCT Salary
FROM Employee
FEN 2014-02-07
![Page 10: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/10.jpg)
10
Examples: Company
SQL-tables are NOT sets in general, but in set
operations (UNION, INTERSECT and
EXCEPT) they are:
(SELECT PNUMBER FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE LNAME = ’Smith’ AND DNUM = DNUMBER AND MGRSSN = SSN)
UNION
(SELECT PNUMBER FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE LNAME = ’Smith’ AND PNO = PNUMBER AND ESSN = SSN)
FEN 2014-02-07
![Page 11: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/11.jpg)
11
Updates i SQL:
Updates:Inserting rows: INSERTDeleting rows: DELETEUpdating row values: UPDATE
As SELECT they work on tables.
FEN 2014-02-07
![Page 12: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/12.jpg)
12
Examples: Company
Inserting a single row:INSERT INTO EMPLOYEEVALUES (’Richard’,’K’,’Marini’,’653298653’,
’30-DEC-52’,’98 Oak Forest, Katy,
’TX’,’M’,37000,’987654321’,4)
Inserting a single row, selected attributes:INSERT INTO EMPLOYEE(FNAME,LNAME,SSN)VALUES (’Richard’,’Marini’,’653298653’)
Is rejected if any of the other attributes is defined
NOT NULL and doesn’t have defined a default value.
FEN 2014-02-07
![Page 13: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/13.jpg)
13
Examples: Company
Deleting rows:
DELETE FROM EMPLOYEEWHERE LNAME =’Brown’
DELETE FROM EMPLOYEEWHERE SSN = ’123456789’
DELETE FROM EMPLOYEEWHERE DNO IN (SELECT DNUMBERFROM DEPARTMENTWHERE DNAME = ’Research’)
DELETE FROM EMPLOYEE
(Not equivalent to: ´DROP TABLE EMPLOYEE’. Why not?)
FEN 2014-02-07
![Page 14: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/14.jpg)
14
Examples: Company
Updating rows:
UPDATE PROJECTSET PLOCATION = ’Bellaire’, DNUM = 5WHERE PNUMBER = 10
UPDATE EMPLOYEESET SALARY = SALARY*1.1WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT WHERE DNAME = ’Research’)
Note, that it is only possible to affect one table in one UPDATE statement.
FEN 2014-02-07
![Page 15: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/15.jpg)
15
Exercises
1. Try out some of the SQL queries on the previous slides.
FEN 2014-02-07
![Page 16: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/16.jpg)
16
Company: Exercise
FEN 2014-02-07
Do Exercise 2, phase 4 onCompanyExercise.pdf
![Page 17: SQL: Structured Query Language – Part 2](https://reader036.fdocuments.in/reader036/viewer/2022062301/568152b4550346895dc0d660/html5/thumbnails/17.jpg)
17
Exercise: VW-database
FEN 2014-02-07
Look at this database: ..\lektion03 (SQL2)\vwDatabase.pdf
Do some of the queries in SQL