0% found this document useful (0 votes)
26 views41 pages

Lecture 10: SQL - DML: Reference: Read Chapter 4 of The Textbook

This document discusses SQL data manipulation language (DML) commands. It describes the INSERT, DELETE, and UPDATE commands for adding, removing, and modifying data in database tables. Examples are provided for each command using sample tables from a COMPANY database schema. The document also discusses retrieving data using basic SELECT queries on single and joined tables with conditions.

Uploaded by

Rana Gaballah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views41 pages

Lecture 10: SQL - DML: Reference: Read Chapter 4 of The Textbook

This document discusses SQL data manipulation language (DML) commands. It describes the INSERT, DELETE, and UPDATE commands for adding, removing, and modifying data in database tables. Examples are provided for each command using sample tables from a COMPANY database schema. The document also discusses retrieving data using basic SELECT queries on single and joined tables with conditions.

Uploaded by

Rana Gaballah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

CSE 480: Database Systems

Lecture 10: SQL - DML

Reference:

Read Chapter 4 of the textbook

1
Review

SQL

DDL DML

UPDATE RETRIEVAL
SELECT
CREATE DROP ALTER FROM
WHERE
GROUP BY
HAVING
ORDER BY

INSERT DELETE UPDATE

2
SQL DML (Updates)

 There are three SQL commands to update the database


state
– INSERT
– DELETE
– UPDATE

3
COMPANY Database Schema

4
INSERT

 Add one or more tuples to a relation


– Attribute values must be listed in the same order as the attributes
specified in the CREATE TABLE command

INSERT INTO EMPLOYEE VALUES


('Richard','K','Marini','653298653','30-DEC-52',
'98 Oak Forest,Katy,TX','M',37000,'987654321',4)

5
INSERT

 An alternate form of INSERT specifies explicitly the


attribute names that correspond to values in the new tuple
– Attributes with NULL values can be left out

INSERT INTO EMPLOYEE (FNAME, LNAME, SSN)


VALUES ('Richard', 'Marini', '653298653')

6
Disable Foreign Key in MySQL

 Set foreign_key_checks = 0;

 Useful to insert a tuple for subordinate before inserting the


tuple for supervisor

INSERT INTO EMPLOYEE (FNAME, LNAME, SSN, SUPER_SSN)


VALUES ('Rob', ‘Stanley', '153298653',
'431231123')

7
INSERT

 Insertion of multiple tuples resulting from a query into a relation


– Example: Suppose we want to create a temporary table that has the name,
number of employees, and total salaries for each department.

CREATE TABLE DEPTS_INFO


(DEPT_NAME VARCHAR(10),
NO_OF_EMPS INTEGER,
TOTAL_SAL INTEGER);

INSERT INTO DEPTS_INFO


(DEPT_NAME, NO_OF_EMPS, TOTAL_SAL)
SELECT DNAME, COUNT (*), SUM (SALARY)
FROM DEPARTMENT, EMPLOYEE
WHERE DNUMBER=DNO
GROUP BY DNAME ;

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’;

DELETE FROM EMPLOYEE


WHERE SSN='123456789’;

DELETE FROM EMPLOYEE


WHERE Dno IN
(SELECT Dnumber
FROM DEPARTMENT
WHERE Dname='Research');

DELETE FROM EMPLOYEE;


9
UPDATE

 Used to modify attribute values of one or more selected


tuples

UPDATE table_name
SET set-clause
WHERE where-clause

– WHERE-clause selects the tuples to be modified


– SET-clause specifies the attributes to be modified and their new
values
– Each command modifies tuples in the same relation

10
UPDATE

 Example: Change the location and controlling department


number of project number 10 to 'Bellaire' and 5,
respectively

UPDATE PROJECT
SET PLOCATION = 'Bellaire', DNUM = 5
WHERE PNUMBER = 10

11
UPDATE

 Example: Give all employees in the 'Research'


department a 10% raise in salary

UPDATE EMPLOYEE
SET SALARY = SALARY *1.1
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research')

12
Retrieval Queries in SQL

 Basic form of the SQL retrieval queries:

SELECT <attribute list>


FROM <table list>
WHERE <condition>

– <attribute list> is a list of attribute names whose values are to be


retrieved by the query
– <table list> is a list of the relation names required to process the
query
– <condition> is a conditional (Boolean) expression that identifies
the tuples to be retrieved by the query

13
Simple SQL Queries (from 1 Table)

 Query: Retrieve the birthdate and address of the


employee 'John B. Smith‘

SELECT
FROM EMPLOYEE
WHERE

14
Simple SQL Queries (from 1 Table)

 Query: Retrieve the birthdate and address of the


employee 'John B. Smith‘

SELECT Bdate, Address


FROM EMPLOYEE
WHERE

15
Simple SQL Queries (from 1 Table)

 Query: Retrieve the birthdate and address of the


employee 'John B. Smith‘

SELECT Bdate, Address


FROM EMPLOYEE
WHERE Fname='John' AND Minit='B' AND Lname='Smith';

16
Simple SQL Queries (from 1 Table)

SELECT Bdate, Address


FROM EMPLOYEE
WHERE Fname='John' AND Minit='B' AND Lname='Smith';

Another way to interpret this:


For each row in Employee table
If row.Fname='John' AND row.Minit='B' AND
row.Lname='Smith' then
print row.Bdate, row.Address

17
Simple SQL Queries (from 1 Table)

 Query: Retrieve the name and address of employees who


work for department number 5

SELECT Fname, Lname, Address


FROM Employee
WHERE Dno = 5;

18
Simple SQL Queries (from 1 Table)

 Query: Retrieve all the rows and columns in the


Employee table

SELECT *
FROM Employee;

Wildcard (*) in the SELECT clause means retrieve all columns

No WHERE clause means all the rows will be retrieved

19
Simple SQL Queries (from 2 Tables)

 Query: Retrieve the first name, last name and address of


all employees who work for the 'Research' department

20
Join Operation

 SQL uses JOIN operation to combine information from


two or more tables
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

R.ID S.ID R.Name S.Degree


1 1 John BS
2 2 Mary BS
2 2 Mary MS
3 3 Bob MS 21
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

R.ID S.ID R.Name S.Degree


1 1 John BS SELECT *
2 2 Mary BS FROM R, S
2 2 Mary MS
WHERE R.ID = S.ID
3 3 Bob MS

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)

 Query: Retrieve the first name, last name and address of


all employees who work for the 'Research' department

SELECT
FROM Employee, Department
WHERE

23
Simple SQL Queries (from 2 Tables)

 Query: Retrieve the first name, last name and address of


all employees who work for the 'Research' department

SELECT Fname, Lname, Address


FROM Employee, Department
WHERE

24
Simple SQL Queries (from 2 Tables)

 Query: Retrieve the first name, last name and address of


all employees who work for the 'Research' department

SELECT Fname, Lname, Address


FROM Employee, Department
WHERE Dname='Research'

25
Simple SQL Queries (from 2 Tables)

 Query: Retrieve the first name, last name and address of


all employees who work for the 'Research' department

SELECT Fname, Lname, Address


FROM Employee, Department
WHERE Dname='Research' AND Dnumber=Dno;

26
Simple SQL Queries (from 2 Tables)

 Query: Retrieve the name of each project and the name


of the department that controls it

SELECT
FROM Department, Project
WHERE

27
Simple SQL Queries (from 2 Tables)

 Query: Retrieve the name of each project and the name


of the department that controls it

SELECT Pname, Dname


FROM Department, Project
WHERE

28
Simple SQL Queries (from 2 Tables)

 Query: Retrieve the name of each project and the name


of the department that controls it

SELECT Pname, Dname


FROM Department, Project
WHERE Dnum=Dnumber;

29
Exercise

List the names of all


employees and their
corresponding
department names

30
Exercise

List the names of


managers for each
department

31
Simple SQL Queries (from 3 Tables)

 Query: For every project located in 'Stafford', list the


project number, controlling department number, and the
department manager's last name, address, and birthdate

SELECT
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE

32
Simple SQL Queries (from 3 Tables)

 Query: For every project located in 'Stafford', list the


project number, controlling department number, and the
department manager's last name, address, and birthdate

SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS


FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE

33
Simple SQL Queries (from 3 Tables)

 Query: For every project located in 'Stafford', list the


project number, controlling department number, and the
department manager's last name, address, and birthdate

SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS


FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE PLOCATION='Stafford'

34
Simple SQL Queries (from 3 Tables)

 Query: For every project located in 'Stafford', list the


project number, controlling department number, and the
department manager's last name, address, and birthdate

SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS


FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE PLOCATION='Stafford‘ AND
DNUM=DNUMBER AND MGRSSN=SSN;
35
Aliases
 If a query refers to two or more attributes with the same name
but in different relations, we must qualify the attribute name
with the relation name by prefixing the relation name to the
attribute name
R ID Name ID Degree S
1 John 1 BS
2 Mary 2 BS SELECT *
3 Bob 2 MS
FROM R, S
3 MS
WHERE R.ID = S.ID
R.ID S.ID R.Name S.Degree
1 1 John BS What if we want to
2 2 Mary BS join the same table?
2 2 Mary MS
Use tuple variables
3 3 Bob MS
36
Tuple Variables

 Query: For each employee, retrieve the employee's name


and the name of his or her immediate supervisor

SELECT
FROM EMPLOYEE E, EMPLOYEE S
WHERE

 E and S are tuple variables

37
Tuple Variables

 Query: For each employee, retrieve the employee's name


and the name of his or her immediate supervisor

SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME


FROM EMPLOYEE E, EMPLOYEE S
WHERE

 E and S are tuple variables

38
Tuple Variables

 Query: For each employee, retrieve the employee's name


and the name of his or her immediate supervisor

SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME


FROM EMPLOYEE E, EMPLOYEE S
WHERE E.SuperSSN = S.SSN

 E and S are tuple variables

39
Exercise

 Query: Find the names of employees who earn more than


their supervisors

SELECT E.FNAME, E.LNAME


FROM EMPLOYEE E, Employee S
WHERE E.SUPERSSN=S.SSN AND E.SALARY >
S.SALARY;

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

SELECT FNAME, LNAME


FROM EMPLOYEE
WHERE (SELECT COUNT(*)
FROM PROJECT
WHERE DNO=DNUM) >= 2
A MORE GENERAL METHOD LATER

41

You might also like