Lecture 10: SQL - DML: Reference: Read Chapter 4 of The Textbook
Lecture 10: SQL - DML: Reference: Read Chapter 4 of The Textbook
Reference:
1
Review
SQL
DDL DML
UPDATE RETRIEVAL
SELECT
CREATE DROP ALTER FROM
WHERE
GROUP BY
HAVING
ORDER BY
2
SQL DML (Updates)
3
COMPANY Database Schema
4
INSERT
5
INSERT
6
Disable Foreign Key in MySQL
Set foreign_key_checks = 0;
7
INSERT
8
DELETE
Removes tuples from a relation
– Includes a WHERE-clause to select the tuples to be deleted
Examples:
DELETE FROM EMPLOYEE
WHERE Lname='Brown’;
UPDATE table_name
SET set-clause
WHERE where-clause
10
UPDATE
UPDATE PROJECT
SET PLOCATION = 'Bellaire', DNUM = 5
WHERE PNUMBER = 10
11
UPDATE
UPDATE EMPLOYEE
SET SALARY = SALARY *1.1
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research')
12
Retrieval Queries in SQL
13
Simple SQL Queries (from 1 Table)
SELECT
FROM EMPLOYEE
WHERE
14
Simple SQL Queries (from 1 Table)
15
Simple SQL Queries (from 1 Table)
16
Simple SQL Queries (from 1 Table)
17
Simple SQL Queries (from 1 Table)
18
Simple SQL Queries (from 1 Table)
SELECT *
FROM Employee;
19
Simple SQL Queries (from 2 Tables)
20
Join Operation
R ID Name ID Degree S
1 John 1 BS
2 Mary 2 BS
3 Bob 2 MS
3 MS
Join on R. ID = S.ID
In this case, a row in R is “merged” with a row in S if their IDs are the same
22
Simple SQL Queries (from 2 Tables)
SELECT
FROM Employee, Department
WHERE
23
Simple SQL Queries (from 2 Tables)
24
Simple SQL Queries (from 2 Tables)
25
Simple SQL Queries (from 2 Tables)
26
Simple SQL Queries (from 2 Tables)
SELECT
FROM Department, Project
WHERE
27
Simple SQL Queries (from 2 Tables)
28
Simple SQL Queries (from 2 Tables)
29
Exercise
30
Exercise
31
Simple SQL Queries (from 3 Tables)
SELECT
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE
32
Simple SQL Queries (from 3 Tables)
33
Simple SQL Queries (from 3 Tables)
34
Simple SQL Queries (from 3 Tables)
SELECT
FROM EMPLOYEE E, EMPLOYEE S
WHERE
37
Tuple Variables
38
Tuple Variables
39
Exercise
40
Find the names of the department where John Smith is currently working.
SELECT DNAME
FROM EMPLOYEE, DEPARTMENT
WHERE DNO=DNUMBER and FNAME=‘John’ and LNAME=‘Smith’
Find names of the managers earning more than 100K
SELECT FNAME, LNAME
FROM EMPLOYEE, DEPARTMENT
WHERE DNO=DNUMBER and MGRSSN=SSN and SALARY> 10000
Find names of employees worked on multiple (at least 2) projects.
SELECT name
FROM EMPLOYEE A, EMPLOEE B, DEPARTMENT C, DEPARTMENT D
WHERE A.SSN = B.SSN AND C.DNUM not= D.DNUM
OR
41