Lab-5 fp304 Jun-2013

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

DECEMBER

F3038DATABASE SYSTEM
2010

LAB 5: STRUCTURED QUERY LANGUAGE (SQL) PART 2

LEARNING OUTCOMES
This Lab sheet encompasses activity 4B & 4C.

By the end of this laboratory session, you should be able to:


 Use SQL commands to manipulate data
 Develop query statement with set comparisons
 Develop nested queries statement

Hardware / Software :Oracle 10g / MySQL Server 5.0

ACTIVITY 4B

Activity Outcome: Able to apply SQL commands to manipulate data.

EXAMPLES OF DATA MANIPULATION LANGUAGE (DML) COMMANDS

DEPARTMENT
DEPT_NO DEPT_NAME DEPT_LOCATION
D01 JTMK CAMPUS A
D02 JKE CAMPUS A
D03 JPA CAMPUS B
D04 JP CAMPUS B
D05 JKM CAMPUS A
D06 JKA CAMPUS A
D07 JKP CAMPUS A

EMPLOYEE
EMP_ EMP_ EMP_ EMP_ EMP_ SALARY DEPT_ EMP_
NO FIRSTNAME LASTNAME PHONE HIREDATE NO ADDRESS
EMP10 AHMAD HAKIMI 01267470 01/01/20 20000 D01 TAMAN
00001 87 11 0.00 SRI
PUTRA,
BANTING
EMP10 JULIA JOHAN 01254323 01/04/20 50000 D01 TAMAN
00002 13 11 .00 KEMUNING
,IPOH
EMP10 SETH COHAN 01987678 09/02/20 20000 D03 TAMAN
00003 98 12 .00 BUDIMAN,
KANGAR
EMP10 RAYMOND LIM 01478394 03/06/20 40000 D05 TAMAN
00004 77 12 .00 ANGGERIK
,CHERAS
EMP10 HANITH EMELIO 01234576 03/06/20 20000 D05 TAMAN
00005 89 12 0.00 HILLCRES
T,KL
EMP10 SATHINI RAJU 01879928 03/06/20 70000 D02 TAMAN
00006 73 12 0.00 JAYA,
IPOH
DECEMBER
F3038DATABASE SYSTEM
2010

a) INSERT INTO Statement

The first form doesn't specify the column names where the data will be inserted,
only their values:

INSERT INTO table_name


VALUES (value1, value2, value3,...)

Example (REFER ACTIVITY 4A LAB 4):

INSERT INTO DEPARTMENT


VALUES ('D01', 'JTMK', 'CAMPUS A');

The second form specifies both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3,...)


VALUES (value1, value2, value3,...)

Example (REFER ACTIVITY 4A LAB 4):

INSERT INTO EMPLOYEE


(EMP_NO,EMP_FIRSTNAME,EMP_LASTNAME,EMP_PHONE,EMP_HIREDATE,SALA
RY,DEPT_NO)VALUES ('EMP1000001', 'AHMAD', 'HAKIMI',0126747087,
'2011/01/01',200000.00, 'D01');

Insert all the data below by using the pervious exercise on Lab 4

STUDENT
StudID StudName StudAddress StudBirthDate CourseID
IT101 Aminah Perak 19/10/1990 C1000
IT102 Joe Selangor 13/08/1990 C1000
IT103 Mona Perak 23/07/1991 C1000
IT104 Husna Melaka 09/05/1990 C1002
IT105 Lingling Melaka 07/07/1989 C1004
IT106 Maniam Penang 01/12/1989 C1003

COURSE
CourseID CourseName LectID
C1000 Software Development L1001
C1001 Artificial Intelegence L1003
C1002 Database L1002
C1003 Interactive Multimedia L1010
C1004 Networking L1005

DEPARTMENT
DepartID DepartName
D1000 Software Engineering
D1001 Industrial Computing
D1002 Comp System & Comm
D1003 Interactive Multimedia
D1004 Management
DECEMBER
F3038DATABASE SYSTEM
2010

LECTURER
LectID LectName LectTelNo DepartID
L1001 Mokhsin 0178908767 D1000
L1002 Sheima 0192299090 D1000
L1003 Raju 0101989909 D1001
L1004 Zulkifli 0123984902 D1000
L1005 Rahmad 0145667389 D1002
L1006 Naina 0143662738 D1001
L1007 Junaida 0135557728
L1008 Hamid D1004
L1009 Chong
L1010 Sam D1003

SUBJECT
SubID SubName
SC100 Programming Technique
SC101 System Development
SC102 Database
SC103 Object Oriented
SC104 Multimedia System
SC105 Logic Programming

STUD_SUB
StudSubID StudID SubID Mark Grade
SS100 IT101 SC100 60 B
SS101 IT102 SC100 85 A
SS102 IT103 SC100 90 A
SS103 IT104 SC103 76 B
SS104 IT105 SC103 56 C
SS105 IT106 SC104 65 B
SS106 IT101 SC104 45 D
SS107 IT102 SC104 98 A
SS108 IT103 SC101 67 B
SS109 IT104 SC101 54 C
SS110 IT105 SC101 87 A
SS111 IT106 SC101 83 A

b) UPDATE Statement

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

Example (REFER ACTIVITY 4A LAB 4):

UPDATE EMPLOYEE
SET EMP_ADDRESS='TAMAN SRI PUTRA,BANTING'
WHERE EMP_NO='EMP1000001';
DECEMBER
F3038DATABASE SYSTEM
2010

c) DELETE Statement

DELETE FROM table_name


WHERE some_column=some_value

Example (REFER ACTIVITY 4A LAB 4):

DELETE FROM EMPLOYEE


WHERE EMP_NO='EMP1000001';

d) SELECTING DATA Statement

Select specific column:

SELECT column_name(s)
FROM table_name

Example (REFER ACTIVITY 4A LAB 4):

SELECT DEPT_NO,DEPT_NAME
FROM DEPARTMENT;

Select all columns:

SELECT * FROM table_name

Example (REFER ACTIVITY 4A LAB 4):

SELECT *
FROM DEPARTMENT;

e) SELECT-FROM-WHERE Statement

SELECT column_name(s)
FROM table_name
WHERE column_name operator value

Example (REFER ACTIVITY 4A LAB 4):

SELECT DEPT_NO,DEPT_NAME
FROM DEPARTMENT
WHERE DEPT_NO = 'D01';
DECEMBER
F3038DATABASE SYSTEM
2010

Comparison Operators:

Operator Purpose Example


= Equality test. SELECT * FROM EMPLOYEE WHERE
EMP_NO ='EMP1000001';
<> Not Equal To SELECT * FROM EMPLOYEE WHERE
DEPT_NO <> 'D01';
!= Not Equal To (not ISO standard)
!> Not Greater Than (not ISO
!< standard)
Not Less Than (not ISO
standard)
> Greater than SELECT EMP_NAME,DEPT_NO FROM
EMPLOYEE WHERE EMP_SALARY >
1000000.00
< Less than SELECT EMP_NAME,DEPT_NO FROM
EMPLOYEE WHERE EMP_SALARY <
1000000.00
>= Greater Than or equal to SELECT EMP_NAME,DEPT_NO FROM
EMPLOYEE WHERE EMP_SALARY >=
1000000.00
<= Less than or equal to SELECT EMP_NAME,DEPT_NO FROM
EMPLOYEE WHERE EMP_SALARY <=
1000000.00
DECEMBER
F3038DATABASE SYSTEM
2010

f) LIKE
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern

Example (REFER ACTIVITY 4A LAB 4):

SELECT DEPT_NO,DEPT_NAME
FROM DEPARTMENT
WHERE DEPT_NAME LIKE 'J%';

This SQL statement will match any department name that start with 'J'. Strings must be in single
quotes. Or you can specify,

SELECT DEPT_NO,DEPT_NAME
FROM DEPARTMENT
WHERE DEPT_NAME LIKE '%K';

This statement will match any last names that end in a 'K'.

vii) ORDER BY

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC

Example (REFER ACTIVITY 4A LAB 4):

SELECT DEPT_NO,DEPT_NAME
FROM DEPARTMENT
ORDER BY DEPT_NO ASC

(OR)

SELECT DEPT_NO,DEPT_NAME
FROM DEPARTMENT
ORDER BY DEPT_NO DESC

viii) IN

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)

Example (REFER ACTIVITY 4A LAB 4):

SELECT DEPT_NO,DEPT_NAME
FROM DEPARTMENT
WHERE DEPT_NAME IN ('JTMK');
DECEMBER
F3038DATABASE SYSTEM
2010

ix) DISTINCT

SELECT DISTINCT column_name(s)


FROM table_name

Example (REFER ACTIVITY 4A LAB 4):

SELECT DISTINCT DEPT_NO


FROM DEPARTMENT

x) AND Operator

The AND operator displays a record if both the first condition and the second condition are true.

Example (REFER ACTIVITY 4A LAB 4):

SELECT * FROM EMPLOYEE


WHERE EMP_ FIRSTNAME ='AHMAD'
AND EMP_LASTNAME='HAKIMI'

xi) OR Operator

The OR operator displays a record if either the first condition or the second condition is true.

Example (REFER ACTIVITY 4A LAB 4):

SELECT * FROM EMPLOYEE


WHERE EMP_ FIRSTNAME ='AHMAD'
OR EMP_LASTNAME='HAKIMI'

xii) JOIN
 JOIN: Return rows when there is at least one match in both tables

SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name

PS: INNER JOIN is the same as JOIN.

Example (REFER ACTIVITY 4A LAB 4):

SELECT EMPLOYEE.EMP_FIRSTNAME, EMPLOYEE.EMP_LASTNAME,


DEPARTMENT.DEPT_NAME
FROM EMPLOYEE
INNER JOIN DEPARTMENT
ON EMPLOYEE.DEPT_NO=DEPARTMENT.DEPT_NO;
DECEMBER
F3038DATABASE SYSTEM
2010

 LEFT JOIN: Return all rows from the left table, even if there are no matches in the
right table

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

Example (REFER ACTIVITY 4A LAB 4):

SELECT EMPLOYEE.EMP_FIRSTNAME, EMPLOYEE.EMP_LASTNAME,


DEPARTMENT.DEPT_NO
FROM EMPLOYEE
LEFT JOIN DEPARTMENT
ON EMPLOYEE.DEPT_NO=DEPARTMENT.DEPT_NO;

 RIGHT JOIN: Return all rows from the right table, even if there are no matches in the
left table

SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

Example (REFER ACTIVITY 4A LAB 4):

SELECT EMPLOYEE.EMP_FIRSTNAME, EMPLOYEE.EMP_LASTNAME,


DEPARTMENT.DEPT_NO
FROM EMPLOYEE
RIGHT JOIN DEPARTMENT
ON EMPLOYEE.DEPT_NO=DEPARTMENT.DEPT_NO;

 FULL JOIN: Return rows when there is a match in one of the tables

SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name

Example (REFER ACTIVITY 4A LAB 4):

SELECT EMPLOYEE.EMP_FIRSTNAME, EMPLOYEE.EMP_LASTNAME,


DEPARTMENT.DEPT_NO
FROM EMPLOYEE
FULL JOIN DEPARTMENT
ON EMPLOYEE.DEPT_NO=DEPARTMENT.DEPT_NO;
DECEMBER
F3038DATABASE SYSTEM
2010

 NESTED QUERIES: Return rows when there is a match in one of the tables

SELECT EMP_FIRSTNAME, EMP_LASTNAME


FROM EMPLOYEE
WHERE DEPT_NO IN (SELECT DEPT_NO FROM DEPARTMENT)

EXERCISES (CLO3)

1. Refer to the tables on page 26 , answer all the below questions :-

i. Update the data in Course table from Database to Database Management


where CourseId is C1002. (2m)

ii. Delete "Logic Programming" from Subject table . (2m)

iii. Select only the Student from “Perak”. . (2m)

iv. Select the Lecturer who phone num start with “014”. (2m)

v. Show the OUTPUT for SQL syntax below: (3m)

Select S.StudName,C.CourseName
From Student S, Course C
Where S.CourseID=C.CourseID;

vi. Show the OUTPUT for SQL syntax below: (3m)

Select S.StudName,C.CourseName,L.LectName
From Student S, Course C ,Lecturer
Where S.CourseID=C.CourseID and
C.LectID=L.LectID

vii. Select student name, subject name, gred and mark ,which mark >=55
(5m)

viii. Write SQL syntax to show a sum of student mark for each subject . (3m)

ix. Write SQL syntax to show lecturer name, department name where the lecturer
are from Software Engineering department. (3m)
DECEMBER
F3038DATABASE SYSTEM
2010

NOTES
DECEMBER
F3038DATABASE SYSTEM
2010

1.

You might also like