0% found this document useful (0 votes)
12 views19 pages

Lab 13 Introduction To View

The document is a lab manual for CS-244: Database Systems focusing on SQL Views. It covers the creation, alteration, and deletion of views in MySQL, along with various filtering methods and restrictions. The lab aims to equip students with the skills to create and manipulate views based on data from one or more tables.

Uploaded by

simply.iqra3105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views19 pages

Lab 13 Introduction To View

The document is a lab manual for CS-244: Database Systems focusing on SQL Views. It covers the creation, alteration, and deletion of views in MySQL, along with various filtering methods and restrictions. The lab aims to equip students with the skills to create and manipulate views based on data from one or more tables.

Uploaded by

simply.iqra3105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

`

GIFT School of Engineering


and Applied Sciences

Spring 2024

CS-244: Database Systems-Lab

Lab-13 Manual
Introduction to SQL Views
Lab -13 Manual 2024

Introduction to Lab
This lab introduces students to select data from views. View is a data object which does not contain
any data. Contents of the view are the resultant of a base table. They are operated just like base table
but they don’t contain any data of their own. The difference between a view and a table is that views
are definitions built on top of other tables (or views). If data is changed in the underlying table, the
same change is reflected in the view. A view can be built on top of a single or multiple tables.
The main topics of this lab include:

1. Why Views?

2. How to Create MySQL View?

3. Restrictions on View definition

4. Tools to create MySQL Views

5. Alter a MySQL view

6. DROP a MySQL view

7. MySQL CREATE VIEW with WHERE

8. MySQL CREATE VIEW with AND and OR

9. MySQL CREATE VIEW with GROUP BY

10. MySQL CREATE VIEW with ORDER BY

11. MySQL CREATE VIEW with JOIN

12. MySQL CREATE VIEW with BETWEEN and IN

13. MySQL CREATE VIEW with LIKE

14. MySQL CREATE VIEW using subqueries

Objectives of this Lab


At the end of this lab, students should be able to:

1. Create a simple view


2. Create a view by applying different data filtering methods students in previous labs.

GIFT Dept. of Computing Science CS-244: Database Systems (Lab) Page 1


Lab -13 Manual 2024

1. Why views?

 Views can be effective copies of base tables.

 Views can have column names and expressions.

 You can use any clauses in views.

 Views can be used in INSERT/UPDATE/DELETE.

 Views can contain expressions in the select list.

 Views can be views of views.

Check the privileges of the current user:

The CREATE VIEW statement requires the CREATE VIEW privilege for the view and some privilege
for each column selected by the SELECT statement. The following command shows the user
privileges.

SHOW PRIVILEGES;

2. How to Create MYSQL Views

Before creating a view we must select a database. Following command shows the list of databases.

SHOW DATABASES;

GIFT Dept. of Computing Science CS-244: Database Systems (Lab) Page 2


Lab -13 Manual 2024

Now select the database 'hr' and list the tables:

USE hr;

SHOW TABLES;

Create View:
Following statements create a view. By default, a view is associated with the default database
(currently used the database). To associate the view with a given database, specify the name as
database_name. view_name when you create it. Here is the complete syntax:

GIFT Dept. of Computing Science CS-244: Database Systems (Lab) Page 3


Lab -13 Manual 2024

CREATE VIEW view_name AS


SELECT column1, column2, ...
FROM table_name
WHERE condition;
Explanation:
The CREATE VIEW statement creates a new view.

view_name: view_name is the name of the view. A view always belongs to a database. By default, a
new view is created in the currently used database. The view name may also be used with the
database name, as database_name.view_name, but it is unnecessary if database_name is the default
database.

select_statement: The select_statement is a SELECT statement and provides the definition of the
view. select_statement can select data from base tables or other views.

Example:

CREATE VIEW my_v1 AS


SELECT
*
FROM jobs;

SELECT * FROM `my_v1`

GIFT Dept. of Computing Science CS-244: Database Systems (Lab) Page 4


Lab -13 Manual 2024

Note: A view always shows up-to-date data! The database engine recreates the view, every time a
user queries it.

GIFT Dept. of Computing Science CS-244: Database Systems (Lab) Page 5


`

3. Restriction on views definition

 The SELECT statement cannot contain a subquery in the FROM clause.


 The SELECT statement cannot refer to system or user variables.
 Within a stored program, the definition cannot refer to program parameters or local
variables.
 The SELECT statement cannot refer to prepared statement parameters.
 Any table or view referred to in the definition must exist.
 The definition cannot refer to a TEMPORARY table, and you cannot create a TEMPORARY
view.
 Any tables named in the view definition must exist at definition time.
 You cannot associate a trigger with a view.
 Aliases for column names in the SELECT statement are checked against the maximum column
length of 64 characters (not the maximum alias length of 256 characters).

4. Alter a MYSQL View


ALTER VIEW statement changes the definition of an existing view. The syntax of the statement is
similar to CREATE VIEW.

Syntax:
ALTER
VIEW view_name
AS select_statement

Example:

ALTER VIEW

my_v1 AS

SELECT

JOB_ID,

JOB_TITLE

FROM

jobs;

SELECT * FROM `my_v1`


Lab-13 Manual 2024

5. DROP a MYSQL View


DROP VIEW statement is used to remove one or more views. To drop a view, you must have DROP
privilege for each view. Here is the syntax:
Syntax:

DROP VIEW [IF EXISTS]


view_name [, view_name] ...
[RESTRICT | CASCADE]

The IF EXISTS clause prevents an error from occurring for views that don't exist.

Page | 7
Lab-13 Manual 2024

Example:

DROP VIEW my_v1;

After executing this query my_v1 view has been dropped.

6. MYSQL CREATE VIEW with WHERE


CREATE VIEW command can be used with WHERE clause.
Example:
Sample Table: Employees
CREATE VIEW emp_of_dept_60 AS SELECT
*
FROM
employees
WHERE
DEPARTMENT_ID = 60;

The above MySQL statement will create a view ‘emp_of_dept_60’ taking records (for all
columns) of employees table if those records contain the value ‘60’ for department_ID column.

7. MYSQL CREATE VIEW with AND and OR


CREATE VIEW command can be used with AND and OR operators.

Example-01:

Sample table: Employees


CREATE VIEW my_v2 AS SELECT

FROM

Page | 7
Lab-13 Manual 2024

employees

WHERE

FIRST_NAME LIKE "S%" AND SALARY > 10000;

The above MySQL statement will create a view 'my_v2' taking all records of employee table, if
(A)(i)value of the first_name column begins with ‘S’, and (ii)value of the Salary Column is more than
10,000.

Example-02:

Sample table: Employees

CREATE VIEW my_v3 AS SELECT

EMPLOYEE_ID,

FIRST_NAME,

LAST_NAME,

SALARY,

DEPARTMENT_ID

FROM

employees

WHERE

FIRST_NAME LIKE "A%" OR DEPARTMENT_ID = 60;

The above MySQL statement will create a view 'my_v2' taking all records Employee_ID, first_name,
last_name, salary and department_id column of employee table, if (A)(i)value of the first_name
column begins with ‘A’, and (ii)value of the Department_ID Column is ‘60’.

8. MYSQL CREATE VIEW with GROUP BY


CREATE VIEW command can be used with GROUP BY clause.

Example:

Sample table: Employees and Jobs

CREATE VIEW my_v4 AS SELECT

Page | 7
Lab-13 Manual 2024

j.JOB_TITLE,

COUNT(*) "No of Employee"

FROM

employees e

NATURAL JOIN jobs j

GROUP BY

j.JOB_TITLE;

The above statement will create a view 'my_v4' taking all records grouped w.r.t. job_title, from
employee_jobs and number of employees hired for each job_title (emp_jobs).

Page | 7
Lab-13 Manual 2024

9. MYSQL CREATE VIEW with ORDER BY


CREATE VIEW command can be used with ORDER BY clause.

Example:

Sample table: Employees and Jobs

CREATE VIEW my_v5 AS SELECT

j.JOB_TITLE,

COUNT(*) "No of Employee"

FROM

employees e

NATURAL JOIN jobs j

GROUP BY

j.JOB_TITLE ORDER BY COUNT(*) DESC;

Page | 7
Lab-13 Manual 2024

The above statement will create a view 'my_v4' taking all records grouped w.r.t. job_title, from
employee_jobs and number of employees hired for each job_title and sorted against the no of
employees working in each job.

10. MYSQL CREATE VIEW with JOINS


CREATE VIEW command can be used along with a JOIN statement.

Example:

Sample table: Employees and departments

CREATE VIEW my_v8 AS SELECT

e.FIRST_NAME,

d.DEPARTMENT_NAME,

Page | 7
Lab-13 Manual 2024

e.SALARY

FROM

employees e

NATURAL JOIN departments d;

The above MySQL statement will create a view 'my_v8' along with a JOIN statement.

The JOIN statement here retrieves first_name, salary from employee table and department_name
from department table if department_id of employee table and that of department table are same.

Page | 7
Lab-13 Manual 2024

11. MYSQL CREATE VIEW with BETWEEN and IN


CREATE VIEW command can be used with BETWEEN and IN operator.

Example:

Sample table: Employees

CREATE VIEW my_v6 AS SELECT

e.FIRST_NAME, e.LAST_NAME, e.SALARY, j.JOB_TITLE

FROM

employees e

NATURAL JOIN jobs j WHERE

e.SALARY BETWEEN 5000 AND 15000 AND j.JOB_TITLE IN(

"Accountant",

"President",

"Programmer",

"Stock Manager"

);

Page | 7
Lab-13 Manual 2024

The above statement will create a view 'my_v6' taking all the records of employees and jobs table, if
(A)employee’s salary is between 5000 and 15000 and (B) employee’s job title is any of the following
Accountant, President, Programmer, Stock Manager.

12. MYSQL CREATE VIEW with LIKE


CREATE VIEW command can be used with LIKE operator.

CREATE VIEW my_v7 AS SELECT

e.FIRST_NAME, e.LAST_NAME, e.SALARY

FROM

employees e

WHERE

Page | 7
Lab-13 Manual 2024

e.FIRST_NAME NOT LIKE "A%" AND e.FIRST_NAME NOT LIKE "J%";

The above MySQL statement will create a view 'my_v7' taking all the records of employees table if
(A)first name of the employee (first_name) does not start with 'A' and (B) first name of the employee
(first_name) does not start with 'J'.

13. MYSQL CREATE VIEW with Subqueries


CREATE VIEW command can be used with subqueries.

Example:

Sample table: Employees


Page | 7
Lab-13 Manual 2024

CREATE VIEW my_v9 AS SELECT

employees.FIRST_NAME,

employees.SALARY,

employees.DEPARTMENT_ID

FROM

employees

WHERE

employees.DEPARTMENT_ID =(

SELECT

DEPARTMENT_ID

FROM

departments

WHERE

DEPARTMENT_NAME = "IT"

);

The above MySQL statement will create a view 'my_v9' taking all the records of first_name, salary and
department_id columns of employees table, if department_id satisfies the condition defined within a
subquery (followed by department_id=).

Page | 7
Lab-13 Manual 2024

The subquery retrieves only department_id of “IT department” from department table.

The End

Page | 7

You might also like