0% found this document useful (0 votes)
310 views

Lab 6 Data Manipulation Language (DML) & Data Control Language (DCL) PDF

This document provides an overview of using Data Control Language (DCL) and Data Manipulation Language (DML) in a database lab manual. It describes how to grant and revoke user privileges using DCL commands like GRANT and REVOKE. It also covers basic DML commands like SELECT, INSERT, UPDATE, and DELETE and how to retrieve, add, modify and remove data from database tables. The lab manual includes exercises for students to practice these skills within a set time period and provides evaluation criteria to test their understanding.

Uploaded by

Saira Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
310 views

Lab 6 Data Manipulation Language (DML) & Data Control Language (DCL) PDF

This document provides an overview of using Data Control Language (DCL) and Data Manipulation Language (DML) in a database lab manual. It describes how to grant and revoke user privileges using DCL commands like GRANT and REVOKE. It also covers basic DML commands like SELECT, INSERT, UPDATE, and DELETE and how to retrieve, add, modify and remove data from database tables. The lab manual includes exercises for students to practice these skills within a set time period and provides evaluation criteria to test their understanding.

Uploaded by

Saira Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Lab Manual for Introduction to Database Systems

Data Manipulation Language (DML) & Data Control Language (DCL)


Lab: DCL and DML

Table of Contents
1. Introduction 33

2. Activity Time boxing 33

3. Objective of the experiment 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. Homework before Lab 41


4.4. Problem Solution Modeling 41
4.5.1. Practices from home 42
4.5.2. Task-1 42
4.5.3. Task-2 42

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

7. Evaluation Task (Unseen) [Expected time = 55mins for two tasks] 45


7.1. Testing 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

DCL and DML


1. Introduction

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.

2. Activity Time boxing


Table 1: Activity Time Boxing
Task No. Activity Name Activity time Total Time
6.2 Setting-up and Setting Up 5mins 5mins
XAMPP (MySQL, Apache)
6.3 Walkthrough Tasks 30mins 60mins
7 Practice tasks 20 to 30mins for each task 50mins
8 Evaluation Task 60mins for all assigned task 40mins

3. Objective of the experiment


 To get basic understanding of DCL & DML concepts and how they are used in database.
 To understand the GRANT, and REVOKE and their purpose.
 To get an understanding of SELECT, INSERT, UPDATE, DELETE, INDEX, CREATE, ALTER, and
DROP records.

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. Grant Privileges on Table


You can grant users various privileges to tables. These permissions can be any combination of
SELECT, INSERT, UPDATE, DELETE, INDEX, CREATE, ALTER, DROP, GRANT OPTION
or ALL.

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.3. Revoke Privileges on Table


Once you have granted privileges, you may need to revoke some or all of these privileges. To do
this, you can run a revoke command. You can revoke any combination of SELECT, INSERT,
UPDATE, DELETE, REFERENCES, ALTER, or ALL.

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

4.2. INSERT INTO Syntax


We can use the INSERT INTO statement to insert a new row with all the column values, using the
following syntax:
INSERT INTO tableName VALUES (firstColumnValue, ..., lastColumnValue) -- All columns
You need to list the values in the same order in which the columns are defined in the CREATE
TABLE, separated by commas. For columns of string data type (CHAR, VARCHAR), enclosed
the value with a pair of single quotes (or double quotes). For columns of numeric data type
(INT, DECIMAL, FLOAT, DOUBLE), simply place the number.
You can also insert multiple rows in one INSERT INTO statement:
INSERT INTO tableName VALUES
(row1FirstColumnValue, ..., row1lastColumnValue),
(row2FirstColumnValue, ..., row2lastColumnValue),
...
To insert a row with values on selected columns only, use:
-- Insert single record with selected columns
INSERT INTO tableName (column1Name, ..., columnNName) VALUES (column1Value, ...,
columnNValue)
-- Alternately, use SET to set the values
INSERT INTO tableName SET column1=value1, column2=value2, ...

-- Insert multiple records


INSERT INTO tableName
(column1Name, ..., columnNName)
VALUES
(row1column1Value, ..., row2ColumnNValue),
(row2column1Value, ..., row2ColumnNValue),…
The remaining columns will receive their default value, such as AUTO_INCREMENT, default,
or NULL…

`
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)

-- Insert multiple rows in one command


-- Inserting NULL to the auto_increment column results in max_value + 1
mysql> INSERT INTO products VALUES
(NULL, 'PEN', 'Pen Blue', 8000, 1.25),
(NULL, 'PEN', 'Pen Black', 2000, 1.25);
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0

-- 2nd column (productCode) is defined to be NOT NULL


mysql> INSERT INTO products values (NULL, NULL, NULL, NULL, NULL);
ERROR 1048 (23000): Column 'productCode' cannot be null

-- Query the table


mysql> SELECT * FROM products;
+-----------+-------------+-----------+----------+------------+
| productID | productCode | name | quantity | price |
+-----------+-------------+-----------+----------+------------+
| 1001 | PEN | Pen Red | 5000 | 1.23 |
| 1002 | PEN | Pen Blue | 8000 | 1.25 |
| 1003 | PEN | Pen Black | 2000 | 1.25 |
| 1004 | PEC | Pencil 2B | 10000 | 0.48 |
| 1005 | PEC | Pencil 2H | 8000 | 0.49 |
| 1006 | PEC | Pencil HB | 0 | 9999999.99 |
+-----------+-------------+-----------+----------+------------+
6 rows in set (0.02 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

1 row in set (0.00 sec)

mysql> SELECT NOW();


+---------------------+
| NOW() |
+---------------------+
| 2016-10-24 22:13:29 |
+---------------------+
1 row in set (0.00 sec)

// Multiple columns
mysql> SELECT 1+1, NOW();
+-----+---------------------+
| 1+1 | NOW() |
+-----+---------------------+
| 2 | 2016-10-24 22:16:34 |
+-----+---------------------+
1 row in set (0.00 sec)

4.3.1. Selecting from a table:


The most common, important and complex task is to query a database for a subset of data that
meets your needs - with the SELECT command. The SELECT command has the following syntax:
-- List all the rows of the specified columns
SELECT column1Name, column2Name, ... FROM tableName

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

4.3.2. Specifying a criteria with WHERE

-- List rows that meet the specified criteria in WHERE clause


SELECT column1Name, column2Name,... FROM tableName WHERE criteria
SELECT * FROM tableName WHERE criteria

`
Page 38
Lab: DCL and DML

4.3.3. Comparison Operators


For numbers (INT, DECIMAL, FLOAT), you could use comparison operators: '=' (equal
to), '<>' or '!=' (not equal to), '>' (greater than), '<' (less than), '>=' (greater than or equal
to), '<='(less than or equal to), to compare two numbers. For example, price > 1.0, quantity <= 500.
mysql> SELECT name, price FROM products WHERE price < 1.0;
+-----------+-------+
| name | price |
+-----------+-------+
| Pencil 2B | 0.48 |
| Pencil 2H | 0.49 |
+-----------+-------+
2 rows in set (0.00 sec)
CAUTION: Do not compare FLOATs (real numbers) for equality ('=' or '<>'), as they are not
precise. On the other hand, DECIMAL are precise.
For strings, you could also use '=', '<>', '>', '<', '>=', '<=' to compare two strings (e.g., productCode
= 'PEC'). The ordering of string depends on the so-called collation chosen. For example,
mysql> SELECT name, price FROM products WHERE productCode = 'PEN';
-- String values are quoted
+-----------+-------+
| name | price |
+-----------+-------+
| Pen Red | 1.23 |
| Pen Blue | 1.25 |
| Pen Black | 1.25 |
+-----------+-------+
3 rows in set (0.00 sec)

4.3.3.1. Arithmetic Operators


You can perform arithmetic operations on numeric fields using arithmetic operators, as tabulated
below:
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
DIV Integer Division
% Modulus (Remainder)

4.3.3.2. String Pattern Matching - LIKE and NOT LIKE


For strings, in addition to full matching using operators like '=' and '<>', we can perform pattern
matching using operator LIKE (or NOT LIKE) with wildcard characters. The wildcard '_'matches
any single character; '%' matches any number of characters (including zero). For example,
 'abc%' matches strings beginning with 'abc';
 '%xyz' matches strings ending with 'xyz';
 '%aaa%' matches strings containing 'aaa';

`
Page 39
Lab: DCL and DML

 ' ' matches strings containing exactly three characters; and


 'a_b%' matches strings beginning with 'a', followed by any single character, followed by 'b',
followed by zero or more characters.
-- "name" begins with 'PENCIL'
mysql> SELECT name, price FROM products WHERE name LIKE 'PENCIL%';
+-----------+-------+
| name | price |
+-----------+-------+
| Pencil 2B | 0.48 |
| Pencil 2H | 0.49 |
+-----------+-------+

-- "name" begins with 'P', followed by any two characters,


-- followed by space, followed by zero or more characters
mysql> SELECT name, price FROM products WHERE name LIKE 'P %';
+-----------+-------+
| name | price |
+-----------+-------+
| Pen Red | 1.23 |
| Pen Blue | 1.25 |
| Pen Black | 1.25 |
+-----------+-------+
MySQL also support regular expression matching via the REGEXE operator.

4.3.3.3. IN, NOT IN


You can select from members of a set with IN (or NOT IN) operator. This is easier and clearer than the
equivalent AND-OR expression.
mysql> SELECT * FROM products WHERE name IN ('Pen Red', 'Pen Black');
+-----------+-------------+-----------+----------+-------+
| productID | productCode | name | quantity | price |
+-----------+-------------+-----------+----------+-------+
| 1001 | PEN | Pen Red | 5000 | 1.23 |
| 1003 | PEN | Pen Black | 2000 | 1.25 |
+-----------+-------------+-----------+----------+-------+

4.3.3.4. IS NULL, IS NOT NULL


NULL is a special value, which represent "no value", "missing value" or "unknown value". You
can checking if a column contains NULL by IS NULL or IS NOT NULL. For example,
mysql> SELECT * FROM products WHERE productCode IS NULL;
Empty set (0.00 sec)
Using comparison operator (such as = or <>) to check for NULL is a mistake - a very common
mistake. For example,
SELECT * FROM products WHERE productCode = NULL;
-- This is a common mistake. NULL cannot be compared.

4.3.4. Logical Operators - AND, OR, NOT, XOR


You can combine multiple conditions with boolean operators AND, OR, XOR. You can also invert
a condition using operator NOT. For examples,
mysql> SELECT * FROM products WHERE quantity >= 5000 AND name LIKE 'Pen %';

`
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 |
+-----------+-------------+-----------+----------+-------+

4.3.4.1. BETWEEN, NOT BETWEEN


To check if the value is within a range, you could use BETWEEN ... AND ... operator. Again, this
is easier and clearer than the equivalent AND-OR expression.
mysql> SELECT * FROM products
WHERE (price BETWEEN 1.0 AND 2.0) AND (quantity BETWEEN 1000 AND 2000);
+-----------+-------------+-----------+----------+-------+
| productID | productCode | name | quantity | price |
+-----------+-------------+-----------+----------+-------+
| 1003 | PEN | Pen Black | 2000 | 1.25 |
+-----------+-------------+-----------+----------+-------+

5. Homework before Lab

You must solve the following problems at home before the lab.

4.4. Problem Solution Modeling


After reading the reference material mentioned in the introduction, now you are ready to perform
homework assigned to you

4.5. Problem description:

This task is the next step of the Lab-01 Task 01.


Create two roles like Customer and Employees. Customer can access the outlet and generate an
order, according to the order details pay the amount to the employee. Customer can perform select
operation on productlines and products relation. Customer can INSERT, UPDATE, and Delete

`
Page 41
Lab: DCL and DML

operation on these tables like orderdetails, orders, and payments.

4.5.1. Practices from home

Solve the following subtasks.

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

In this section tools installation and setup is defined.

5.2. Setting-up and Setting Up XAMPP (MySQL, Apache) [Expected time = 5mins]

Refer to Lab 1 sec 6.2.

5.3. Walkthrough Task [Expected time = 30mins]

This task is designed to guide you towards creating your


5.3.1. Create table products using.

Figure 1: Create table

5.3.2. Insert data into table

`
Page 42
Lab: DCL and DML

Figure 2: Insert data into table

5.3.3. Show all data of table using select command

Figure 3: Select all data of table

5.3.4. Select command for specific column.

Figure 4: Select specific column from relation

5.3.5. Specifying a criteria with WHERE and conditional operator

`
Page 43
Lab: DCL and DML

Figure 5: Select using conditional statement

5.3.6. String Pattern Matching - LIKE

Figure 6: String Pattern Matching - LIKE

5.3.7. IN, NOT IN

Figure 7: IN, NOT IN

5.3.8. Logical Operators

Figure 8: Logical Operators

5.3.9. BETWEEN, NOT BETWEEN

Figure 9: BETWEEN, NOT BETWEEN

`
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.

6.1. Practice Task 1 [Expected time = 50mins]


Consider the schema given in Lab01 (7.1 Practice Task), write down following SQL queries. You
can use data types of your own choice.

1. Create table “CUSTOMERS” with all required constraints.


2. Alter table “CUSTOMERS” to apply primary key constraint.
3. Alter table “PAYMENTS” to apply the foreign key constraint.
4. Add AUTO-INCREMENT constraint to CUSTOMERS table.
5. INSERT complete record.
6. Select all customers where customerName contains a word “MR.” at start.
7. Select all customers where customerName contains a word “MINI”.
8. Select all payments of the year “2015”.
9. Show quantity available in stock of all products.
10. Show customer number, order number, status of all orders.
11. Show all customers who were not serviced by a Sales Representative.
12. Show all customers serviced by Sales Representative whose employeenumber is 115.
13. Show all orders those are not shipped yet (Hint: where shipped date is NULL)
14. Show all customers who have not provided addressLine2.
15. Show customer number, order number, status of all orders made by customer whose
customernumber is 103.

6.2. Out comes

After completing this lab, student will be able to understand the use of DML and DCL.

7. Evaluation Task (Unseen) [Expected time = 55mins for two tasks]

The lab instructor will give you unseen task depending upon the progress of the class.

7.1. Testing

7.1.1. Test Cases for Practice Task-1

Test cases will be provided by the instructor.

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

This section provides the references to further polish your skills.

9.1. Books

Text Book:

 Database Systems, A practical approach to design, implementation and management by


Thomas Connolly, Carolyn Begg, Addison Wesley , Fifth Edition,

10. REFERENCES:

10.1. SQL-99 Complete, Really, by Peter Gulutzan & Trudy Pelzer.

 More examples for the SELECT command:


http://dev.mysql.com/doc/mysql/en/select.html
 MySQL operators:
http://dev.mysql.com/doc/mysql/en/non-typed_operators.html
 Built-in functions:
http://dev.mysql.com/doc/mysql/en/functions.html
 Joining tables:
http://www.melonfire.com/community/columns/trog/article.php?id=148
 Using subqeries:
http://www.melonfire.com/community/columns/trog/article.php?id=204

`
Page 46

You might also like