You are on page 1of 14

COLLEGE OF COMPUTER STUDIES AND MULTIMEDIA ARTS

CCS0021L
(INFORMATION MANAGEMENT)

EXERCISE

7
DATA MANIPULATION LANGUAGE

Student Name / Group


Franz Millan P. De Vera
Name:
Name Role
Members (if Group):

Section: BSCSSE TN21


Professor: Ms. Beau Gray Habal
I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE
a. Apply knowledge through the use of current techniques and tools necessary for the IT profession. [PO: I]

II. COURSE LEARNING OUTCOME/S (CLO) ADDRESSED BY THE LABORATORY EXERCISE


 Create SQL statements that retrieve information requirements of the organization needed for reports generation.
[CLO: 4]

III. INTENDED LEARNING OUTCOME/S (ILO) OF THE LABORATORY EXERCISE


At the end of this exercise, students must be able to:
 Use SQL command to manipulate the data in the table
 Use the commit, rollback and save point as transaction control

IV. BACKGROUND INFORMATION


• A DML statement is executed when you:
 Add new rows to a table
 Modify existing rows in a table
 Remove existing rows from a table
• A transaction consists of a collection of DML statements that form a logical unit of work.
INSERT
• Add new rows to a table by using the INSERT statement:

INSERT INTO table [(column [, column...])]


VALUES (value [, value...]);

• With this syntax, only one row is inserted at a time.

• Adds one or more rows to a table


• Inserting into a table


• Inserting a record that has some null attributes requires identifying the fields that actually get data

• Inserting from another table

CCS0021L-Information Management Page 2 of


14
UPDATE
• Modify existing values in a table with the UPDATE statement:
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];

• Update more than one row at a time (if required).

SELECT
• Used for queries on single or multiple tables
• Clauses of the SELECT statement:
– SELECT
• List the columns (and expressions) to be returned from the query
– FROM
• Indicate the table(s) or view(s) from which data will be obtained
– WHERE
• Indicate the conditions under which a row will be included in the result
– GROUP BY
• Indicate categorization of results
– HAVING
• Indicate the conditions under which a category (group) will be included
– ORDER BY
• Sorts the result according to specified criteria
Syntax:

SELECT column, group_function


FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];

SELECT STATEMENT WITH AGGREGATE FUNCTIONS


Useful aggregate functions:
AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum

Syntax:
SELECT AVG(column_name) FROM table_name;
SELECT min(column_name) FROM table_name;
SELECT max(column_name) FROM table_name where [condition];

CCS0021L-Information Management Page 3 of


14
SQL Scalar functions
SQL scalar functions return a single value, based on the input value.
Useful scalar functions:
UCASE() - Converts a field to upper case
LCASE() - Converts a field to lower case
MID() - Extract characters from a text field
LEN() - Returns the length of a text field
ROUND() - Rounds a numeric field to the number of decimals specified
NOW() - Returns the current system date and time
FORMAT() - Formats how a field is to be displayed

SQL ORDER BY Syntax


SELECT column_name, column_name
FROM table_name
ORDER BY column_name  ASC|DESC, column_name ASC|DESC;
Example:
SELECT * FROM Customers
ORDER BY Country ASC;

SELECT * FROM Customers


ORDER BY Country;

The SQL BETWEEN Operator


The BETWEEN operator selects values within a range. The values can be numbers, text, or dates.

SQL BETWEEN Syntax


SELECT column_name(s)
FROM table_name
WHERE column_name  BETWEEN value1 AND value2;

Example:
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;

SELECT * FROM Products


WHERE Price NOT BETWEEN 10 AND 20;

BETWEEN Operator with IN Example

The following SQL statement selects all products with a price BETWEEN 10 and 20, but products with a CategoryID of 1,2, or 3
should not be displayed:

Example
SELECT * FROM Products
WHERE (Price BETWEEN 10 AND 20)
AND NOT CategoryID IN (1,2,3);

CCS0021L-Information Management Page 4 of


14
The SQL LIKE Operator
The LIKE operator is used to search for a specified pattern in a column.

SQL LIKE Syntax


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

Tip: The "%" sign is used to define wildcards (missing letters) both before and after the pattern. You will learn more about
wildcards in the next chapter.
The following SQL statement selects all customers with a City ending with the letter "s":
Example:
SELECT * FROM Customers
WHERE City LIKE '%s';

DELETE
Removes rows from a table
Delete certain rows
DELETE FROM CUSTOMER_T WHERE CUSTOMERSTATE = ‘HI’;
Delete all rows
DELETE FROM CUSTOMER_T;

TRUNCATE Statement
 Removes all rows from a table, leaving the table empty and the table structure intact.
 It is a data definition language (DDL) statement rather than a DML statement; cannot easily be undone.

A database transaction consists of one of the following:


 DML Statements that constitute one consistent change to the data.
 One DDL statement.
 One data control language (DCL) statement.

SAVEPOINT
 Create a marker in the current transaction by using the SAVEPOINT statement.

ROLLBACK
 Discard all pending changes by using the ROLLBACK statement.
 Roll back to the marker by using the ROLLBACK TO SAVEPOINT statement.

CCS0021L-Information Management Page 5 of


14
COMMIT
 Save the changes made permanently in the database by using COMMIT.

CCS0021L-Information Management Page 6 of


14
V. GRADING SYSTEM / RUBRIC

Criteria Descriptions Points


Creation of Tables (1-10 All the tables in the ERD are properly made 50
and complete
Changes made on the Tables The use of correct Data Definition 50
Language to change the characteristics of
the table are correct
Total 100%

CCS0021L-Information Management Page 7 of


14
VI. LABORATORY ACTIVITY

Instructions: Download the two files before starting this exercise, namely: hrcreate.txt and hrpopulate.txt. Both files
are found in Canvas. Copy the contents of hrcreate.txt first to the worksheet in SQL developer and click run script.
Then do the same with hrpopulate.txt file. One you see the tables and contents inside the table, you can start
answering the following:
Write the DML code the show the following. Then for each number, copy and paste the code including the
screenshot of the output.

Queries:
1. Show the employee id, first name, and last names of employees who are managed by manager id
114.

2. Show the employee id and the salary of the employees who has a salary higher than 11,999.

CCS0021L-Information Management Page 8 of


14
3. Show the job title of the job id AC_MGR and IT_PROG.

4. Show all the columns of the departments table that has no manager id.

5. Show the country id and country names of countries that are either Italy, Singapore, United States
of America or countries found in region 1.

CCS0021L-Information Management Page 9 of


14
6. Using the job history table, sow the employee id, start dates, and end dates of employees has an id
of 102, 101 or 176 or their end dates are Dec 31, 1998.

7. Show all the columns of the employees that has a job id of AD_PRES.

8. Show the job id and the number of employees that has a job id of SA_REP or ST_CLERK. Group
it by job id and arrange it by its job id.

CCS0021L-Information Management Page 10


of 14
9. Show the job ids and average salaries per job id of the employees table, group it based on job id.
Show only the job ids that has average greater than 10,000, and arrange it based on job id.

10. Show the salary, and the count of employees who has the same salary. Arrange it by salary in
ascending order. Rename the count of employees to ‘Number_of_Persons’.

CCS0021L-Information Management Page 11


of 14
Please do the following in sequential order:
11. Add a new job record in the jobs table. The new record has the following details:
Job id – MK_ASST
Job title – Marketing Assistant
Minimum salary 3000
Maximim salary is 13000

12. Add a new employee using the new job id that was created in number 11. The following details are
below:
Employee id is 500

CCS0021L-Information Management Page 12


of 14
First name is Alicia
Last name is Santos
Phone number is 650.124.0000
Job id is MK_ASST
Salary is 10000
Department id is 20

13. Change the last name Delos Santos and salary to 12,500 of the employee with an employee id of
500.

14. Create a save point name SPEmp500.


delete from employees where employee_id = 500;

15. Delete the record of the employee id 500.

CCS0021L-Information Management Page 13


of 14
delete from employees where employee_id = 500;

16. Restore the deleted record (employee id 500) back to the employees table.
Rollback SPEmp500;

17. Create a view that has all the details of the employees table.

VII. QUESTION AND ANSWER

1. What is the advantage of using a view?


Using a view will simplify the data access. A view can be used to perform a calculation and display its
result. For example, a view definition that invokes aggregate functions can be used to display a summary.

VIII. REFERENCES

 Hoffer, J.A., Prescott, M.B., McFadden, F.R. (2007). Modern Database Management 8th
Edition. New Jersey: Pearson Prentice Hall.

CCS0021L-Information Management Page 14


of 14

You might also like