Lab 6 Data Manipulation Language (DML) & Data Control Language (DCL) PDF
Lab 6 Data Manipulation Language (DML) & Data Control Language (DCL) PDF
Table of Contents
1. Introduction 33
4. Concept Map 33
4.1. Grant/Revoke Privileges 33
4.1.1. Description 34
4.1.2. Grant Privileges on Table 34
4.1.3. Revoke Privileges on Table 35
4.1.4. Syntax 35
4.1.5. Example 36
4.2. INSERT INTO Syntax 36
4.2.1. Example 37
4.3. SELECT 37
4.3.1. Selecting from a table: 38
4.3.2. Specifying a criteria with WHERE 38
4.3.3. Comparison Operators 39
4.3.3.1. Arithmetic Operators 39
4.3.3.2. String Pattern Matching - LIKE and NOT LIKE 39
4.3.3.3. IN, NOT IN 40
4.3.3.4. IS NULL, IS NOT NULL 40
4.3.4. Logical Operators - AND, OR, NOT, XOR 40
4.3.4.1. BETWEEN, NOT BETWEEN 41
5. Procedure& Tools 42
5.1. Tools 42
5.2. Setting-up and Setting Up XAMPP (MySQL, Apache) [Expected time =
5mins]42
5.3. Walkthrough Task [Expected time = 30mins] 42
6. Practice Tasks 45
6.1. Practice Task 1 [Expected time = 50mins] 45
6.2. Out comes 45
8. Evaluation criteria 45
9. Further Reading 46
9.1. Books 46
`
Page 31
Lab: DCL and DML
9.2. Slides 46
10. REFERENCES: 46
10.1. SQL-99 Complete, Really, by Peter Gulutzan & Trudy Pelzer. 46
`
Page 32
Lab: DCL and DML
You will learn DCL and DML in this lab. DML is abbreviation of Data Manipulation
Language. It is used to retrieve, store, modify, delete, insert and update data in database. DCL is
abbreviation of Data Control Language. It is used to create roles, permissions, and referential
integrity as well it is used to control access to database by securing it. You can GRANT and
REVOKE privileges on various database objects in MySQL. You can then view the privileges
assigned to a user using the SHOW GRANTS command. We'll look at how to grant and revoke
privileges on tables, function, and procedures in MySQL.
4. Concept Map
4.1. Grant/Revoke Privileges
This MySQL tutorial explains how to grant and revoke privileges in MySQL with syntax and
examples.
`
Page 33
Lab: DCL and DML
4.1.1. Description
You can GRANT and REVOKE privileges on various database objects in MySQL. You can then
view the privileges assigned to a user using the SHOW GRANTS command. We'll look at how to
grant and revoke privileges on tables, function, and procedures in MySQL.
4.1.2.1. Syntax
The syntax for granting privileges on a table in MySQL is:
GRANT privileges ON object TO user;
Privileges:
It can be any of the following values:
Privilege Description
SELECT Ability to perform SELECT statements on the table.
INSERT Ability to perform INSERT statements on the table.
UPDATE Ability to perform UPDATE statements on the table.
DELETE Ability to perform DELETE statements on the table.
INDEX Ability to create an index on an existing table.
CREATE Ability to perform CREATE TABLE statements.
Ability to perform ALTER TABLE statements to change the table
ALTER
definition.
DROP Ability to perform DROP TABLE statements.
GRANT
Allows you to grant the privileges that you possess to other users.
OPTION
ALL Grants all permissions except GRANT OPTION.
Object:
The name of the database object that you are granting permissions for. In the case of
granting privileges on a table, this would be the table name.
User:
The name of the user that will be granted these privileges.
`
Page 34
Lab: DCL and DML
4.1.2.2. Example
Let's look at some examples of how to grant privileges on tables in MySQL.
For example, if you wanted to grant SELECT, INSERT, UPDATE, and DELETE privileges on a
table called contactsto a user name smithj, you would run the following GRANT statement:
GRANT SELECT, INSERT, UPDATE, DELETE ON contacts TO 'smithj'@'localhost';
You can also use the ALL keyword to indicate that you wish to grant all permissions except
GRANT OPTION to a user named smithj. For example:
GRANT ALL ON contacts TO 'smithj'@'localhost';
If you wanted to grant only SELECT access on the contacts table to all users, you could grant the
privileges to *. For example:
GRANT SELECT ON contacts TO '*'@'localhost';
4.1.4. Syntax
The syntax for revoking privileges on a table in MySQL is:
REVOKE privileges ON object FROM user;
Privileges:
It can be any of the following values:
Privilege Description
SELECT Ability to perform SELECT statements on the table.
INSERT Ability to perform INSERT statements on the table.
UPDATE Ability to perform UPDATE statements on the table.
DELETE Ability to perform DELETE statements on the table.
CREATE Ability to perform CREATE TABLE statements.
ALTER Ability to perform ALTER TABLE statements to change the table definition.
DROP Ability to perform DROP TABLE statements.
ALL Grants all permissions except GRANT OPTION.
`
Page 35
Lab: DCL and DML
4.1.5. Example
Let's look at some examples of how to revoke privileges on tables in MySQL.
For example, if you wanted to revoke DELETE and UPDATE privileges on a table
called contacts from a user named smithj, you would run the following REVOKE statement:
REVOKE DELETE, UPDATE ON contacts FROM 'smithj'@'localhost';
If you wanted to revoke all permissions (except GRANT OPTION) on a table for a user
named smithj, you could use the ALL keyword as follows:
REVOKE ALL ON contacts FROM 'smithj'@'localhost';
If you had granted SELECT privileges to * (ie: all users) on the contacts table and you wanted to
revoke these privileges, you could run the following REVOKE statement:
REVOKE SELECT ON contacts FROM '*'@'localhost';
`
Page 36
Lab: DCL and DML
4.2.1. Example
CREATE TABLE `products` (
`productID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`productCode` char(3) NOT NULL DEFAULT '',
`name` varchar(30) NOT NULL DEFAULT '',
`quantity` int(10) unsigned NOT NULL DEFAULT '0',
`price` decimal(7,2) NOT NULL DEFAULT '99999.99',
PRIMARY KEY (`productID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Let's fill up our "products" table with rows. We set the productID of the first record to 1001, and
use AUTO_INCREMENT for the rest of records by inserting a NULL, or with a missing column
value. Take note that strings must be enclosed with a pair of single quotes (or double quotes).
-- Insert a row with all the column values
mysql> INSERT INTO products VALUES (1001, 'PEN', 'Pen Red', 5000, 1.23);
Query OK, 1 row affected (0.04 sec)
4.3. SELECT
We can SELECT an expression or evaluate a built-in function.
mysql> SELECT 1+1;
+-----+
| 1+1 |
+-----+
|2|
+-----+
`
Page 37
Lab: DCL and DML
// Multiple columns
mysql> SELECT 1+1, NOW();
+-----+---------------------+
| 1+1 | NOW() |
+-----+---------------------+
| 2 | 2016-10-24 22:16:34 |
+-----+---------------------+
1 row in set (0.00 sec)
-- List all the rows of ALL columns, * is a wildcard denoting all columns
SELECT * FROM tableName
For examples,
-- List all rows for the specified columns
mysql> SELECT name, price FROM products;
+-----------+-------+
| name | price |
+-----------+-------+
| Pen Red | 1.23 |
| Pen Blue | 1.25 |
| Pen Black | 1.25 |
| Pencil 2B | 0.48 |
| Pencil 2H | 0.49 |
+-----------+-------+
5 rows in set (0.00 sec)
-- List all rows of ALL the columns. The wildcard * denotes ALL columns
`
Page 38
Lab: DCL and DML
`
Page 39
Lab: DCL and DML
`
Page 40
Lab: DCL and DML
+-----------+-------------+----------+----------+-------+
| productID | productCode | name | quantity | price |
+-----------+-------------+----------+----------+-------+
| 1001 | PEN | Pen Red | 5000 | 1.23 |
| 1002 | PEN | Pen Blue | 8000 | 1.25 |
+-----------+-------------+----------+----------+-------+
mysql> SELECT * FROM products WHERE quantity >= 5000 AND price < 1.24 AND name LIKE
'Pen %';
+-----------+-------------+---------+----------+-------+
| productID | productCode | name | quantity | price |
+-----------+-------------+---------+----------+-------+
| 1001 | PEN | Pen Red | 5000 | 1.23 |
+-----------+-------------+---------+----------+-------+
mysql> SELECT * FROM products WHERE NOT (quantity >= 5000 AND name LIKE 'Pen %');
+-----------+-------------+-----------+----------+-------+
| productID | productCode | name | quantity | price |
+-----------+-------------+-----------+----------+-------+
| 1003 | PEN | Pen Black | 2000 | 1.25 |
| 1004 | PEC | Pencil 2B | 10000 | 0.48 |
| 1005 | PEC | Pencil 2H | 8000 | 0.49 |
+-----------+-------------+-----------+----------+-------+
You must solve the following problems at home before the lab.
`
Page 41
Lab: DCL and DML
4.5.2. Task-1
Create privileges which is given in above Problem description.
4.5.3. Task-2
Run at least 10 record in customer and employee relation.
5. Procedure& Tools
5.1. Tools
5.2. Setting-up and Setting Up XAMPP (MySQL, Apache) [Expected time = 5mins]
`
Page 42
Lab: DCL and DML
`
Page 43
Lab: DCL and DML
`
Page 44
Lab: DCL and DML
6. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time.
After completing this lab, student will be able to understand the use of DML and DCL.
The lab instructor will give you unseen task depending upon the progress of the class.
7.1. Testing
8. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
`
Page 45
Lab: DCL and DML
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
9. Further Reading
9.1. Books
Text Book:
10. REFERENCES:
`
Page 46