You are on page 1of 19

ISYS6508

Database System

Week 6
SQL: Data Manipulation

Database System A Practical Approach to Design, Implemetation, and Management


Connolly, T., & Begg, C.
Chapter 6
Learning Objective
At the end of this semester, the student should
be able to:
• Construct SQL that suit the problem
Sub Topics
• Data Manipulation (simple queries)
• Views
DATA MANIPULATION (SIMPLE
QUERIES)
SQL DML statements
• SELECT – to query data in the database
• INSERT – to insert data into a table
• UPDATE – to update data in a table
• DELETE – to delete data from a table
Simple Queries (SELECT)
• SELECT [DISTINCT | ALL] {* |
[columnExpression [AS newName]] [, . . .]}
FROM TableName [alias] [, . . .]
[WHERE condition]
[GROUP BY columnList] [HAVING condition]
[ORDER BY columnList]
sequence of processing in
a SELECT
• FROM specifies the table or tables to be used
• WHERE filters the rows subject to some condition
• GROUP BY forms groups of rows with the same
column value
• HAVING filters the groups subject to some condition
• SELECT specifies which columns are to appear in the
output
• ORDER BY specifies the order of the output.
SELECT*
SELECT *
FROM Staff;
SELECT specific Column
SELECT staffNo, fName, IName, salary
FROM Staff;
Row Selection
SELECT staffNo, fName, IName, position, salary
FROM Staff
WHERE salary > 10000
Condition Like/Not Like
SELECT ownerNo, fName, IName, address, telNo
FROM PrivateOwner
WHERE address LIKE ‘%Glasgow%’;
Sorting Result
SELECT staffNo, fName, IName, salary
FROM Staff
ORDER BY salary DESC;
Agregate Function
SELECT COUNT(staffNo) AS myCount, SUM(salary) AS mySum
FROM Staff
WHERE position = ‘Manager’;
Grouping Results
SELECT branchNo, COUNT(staffNo) AS myCount, SUM(salary)
AS mySum
FROM Staff
GROUP BY branchNo
ORDER BY branchNo;
INSERT
• INSERT INTO TableName [(columnList)]
VALUES (dataValueList)

• Example
INSERT INTO Staff
VALUES (‘SG16’, ‘Alan’, ‘Brown’, ‘Assistant’, ‘M’,
DATE ‘1957-05-25’, 8300, ‘B003’);
UPDATE
• UPDATE TableName
SET columnName1 = dataValue1 [,
columnName2 = dataValue2 . . .]
[WHERE searchCondition]
• Example
UPDATE Staff
SET salary = salary*1.05
WHERE position = ‘Manager’;
DELETE
• DELETE FROM TableName
[WHERE searchCondition]
• Example
DELETE FROM Viewing
WHERE propertyNo = ‘PG4’;
Reference
Connolly, T., & Begg, C. (2015). Database
System A Practical Approach to Design,
Implemetation, and Management 6th Edition.
Pearson
Thank You

You might also like