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

DBMS lab Manual exp 9, 10 & 11

The document provides an overview of SQL views and triggers, detailing how to create, delete, and update views, as well as the differences between views and tables. It also explains triggers, their types, and how to create and manage user permissions in MySQL. Additionally, it includes examples and answers to common questions regarding views, triggers, and user roles.

Uploaded by

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

DBMS lab Manual exp 9, 10 & 11

The document provides an overview of SQL views and triggers, detailing how to create, delete, and update views, as well as the differences between views and tables. It also explains triggers, their types, and how to create and manage user permissions in MySQL. Additionally, it includes examples and answers to common questions regarding views, triggers, and user roles.

Uploaded by

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

Global Institute of Technology

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Experiment -9

Aim-. Write the query to create the views.

VIEW : Views are small part of sql . In SQL views are kind of virtual tables. A view also
has rows and columns as they are in a real table in the database. We can create a view by
selecting fields from one or more tables present in the database.

syntax : CREATE WEW view name AS SELECT column1, column2, ... FROM table name
WHERE condition;
Example :

CREATE VIEW stu View AS SELECT NAME, ADDRESS FROM college WHERE
S_ID < 5;

To see the result :

T * FROM stu_view;

Output :

name address

ani goa

nia chennai

Creating view from multiple table :

CREATE VIEW stu View AS


SELECT college.NAME, college.ADDRESS, bank.MARKS
FROM college, bank WHERE college.NAME = bank.NAME;

DR SANGEETA SONI 42
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

To see result :

T * From stu_view;

Output :

Name Address Marks


Ani Goa 99
Nia Chennai 98
Kia Delhi 89
Ishi Bhutan 70

DELETING VIEWS:

syntax : DROP VIEW view name,

Example :

Drop VIEW stu_view;

UPDATING VIEWS :

• Insertion :

syntax :
INSERT INTO view name(column1, column2 , column3,..)
VALUES(value1, value2, value3..);

• Deleting a row from a View:

syntax : DELETE FROM view name WHERE condition,

VIVA Questions:-
1. What is a View in SQL Server?

A view can be considered as a virtual table. As a view represents a virtual table it does not
physically store any data by default.

DR SANGEETA SONI 43
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

2. What are the differences between a table and a view?


 The table is physical and the view is logical.
 A table is an independent object whereas view is a dependent object that is a view depends
on a table or tables from which it is loading the data.

3. How many types of views are there in SQL Server?


We can create the view in two ways those are
1. Simple view and Updatable views:
 The view which is created basing on the columns of a single table is known as
the simple view.
 We can perform all DML operations on a simple view so that a simple view is
also called an updatable view or dynamic view.

2. Complex view and non-updatable views.

 When we create a view on more than 1 table then it is known as the complex
view.
 On a complex view, we cannot perform DML operations so that a complex
view is also called a non-updatable or static view.

4. Can we create a view based on other views?


Yes, usually, we create views based on tables, but it is also possible to create views based on
views.

5. Why do we need Views in SQL Server?


To protect the data. If we have a table containing sensitive data in certain columns, we might
wish to hide those columns from certain groups of users.

DR SANGEETA SONI 44
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Experiment-10
Aim- Perform the queries for triggers.

TRIGGERS : A SQL trigger is a database object just like a stored procedure, or we can say
it is a special kind of stored procedure which fires when an event occurs in a database. We
can execute a SQL query that will "do something" in a database when an event is fired.

Types of Triggers

1. DDL Trigger
2. DML Trigger

RIGGERS PROCEDURES

They are automatically executed on occurrence of They can be executed whenever


specified event. required.

But, you can call a procedure inside


Triggers can't be called inside a procedure.
a trigger.

We can pass parameters to


We can not pass parameters to triggers.
procedures.

Procedure may return values on


Trigger never return value on execution.
execution.

Creating a trigger function :

CREATE FUNCTION trigger_function()

RETURNS trigger AS

SQL CREATE TRIGGER statement :

DR SANGEETA SONI 45
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

CREATE TRIGGER trigger_name

{BEFORE | AFTER | INSTEAD OF} (event [OR ...]}

ON table_name

[FOR [EACH] {ROW | STATEMENT}]

EXECUTE PROCEDURE trigger_function

Example :

CREATE TABLE employees(

id SERIAL PRIMARY

KEY,

first_name VARCHAR(40) NOT NULL,

last_name VARCHAR(40) NOT NULL);

INSERT INTO employees (first_name, last_name)

VALUES ('John', ’Doe’);

INSERT INTO employees (first_name, last_name)

VALUES ('Lily', ’Bush’);

T * FROM employees;

Output :

_name ast_name

1 Doe
John

DR SANGEETA SONI 46
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

2 Lily Blush

UPDATE QUERY :

UPDATE employees

SET last_name = 'Brown'

WHERE ID = 2;

T * FROM employees;

Output :

_name ast_name

1 John Doe

2 Lily Brown

Viva Questions:-

1. Advantages of Triggers :

• Trigger generates some derived column values automatically


• Enforces referential integrity
• Event logging and storing information on table access
• Auditing
• Synchronous replication of tables
• Imposing security authorizations
• Preventing invalid transactions

DR SANGEETA SONI 47
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

2. What is a DML Trigger in SQL Server?

These DML Triggers execute when the user tries to modify or change data through data
manipulation language events such as INSERT, UPDATE and DELETE statements on the table
or view.

3. What are DDL triggers in SQL Server?

The DDL triggers fires in response to a variety of data definition language events such as Create,
Alter, Drop, Grant, Denay and Revoke.

4. What is Nested Trigger?

A trigger can also contain INSERT, UPDATE and DELETE logic within itself. A trigger that
contains data modification logic within itself which causes another trigger to be fired is called the
nested trigger.

5. What are the special tables used by Triggers in SQL Server?


Triggers make use of two special tables called inserted and deleted.
The inserted table contains the data referenced in an INSERT before it is committed to the
database.
The deleted table contains the data in the underlying table referenced in a DELETE before it is
removed from the database.

DR SANGEETA SONI 48
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Experiment-11

Aim- Write the query for creating the users and their roles.

Create a New User :

mysql > CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';


At this point new user has no permissions to do anything with the databases. In fact, even
if new user tries to login (with the password, password), they will not be able to reach the
MySQL shell.

provide the user with access to the information :

mysql > GRANT ALL PRIVILEGES ON * . * TO ’newuser’@'localhost’;


asterisks in this command refer to the database and table that they can access .Once you have
finalized the permissions that you want to set up for your new users, always be sure to reload
all the privileges.Your changes will now be in effect.

mysql > FLUSH PRIVILEGES;


How To Grant Different User Permissions

• ALL PRIVILEGES - as we saw previously, this would allow a MySQL user full
access to a designated database (or if no database is selected, global access across the
system)

• CREATE - allows them to create new tables or databases

• DROP - allows them to them to delete tables or databases

• DELETE - allows them to delete rows from tables

• INSERT - allows them to insert rows into tables

• SELECT - allows them to use the SELECT command to read through databases

• UPDATE - allow them to update table rows

• GRANT OPTION - allows them to grant or remove other users' privileges

DR SANGEETA SONI 49
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

To provide a specific user with a permission, you can use this framework:

mysql > GRANT type of permission ON database name.table name TO


’username @'localhost’;

If you need to revoke a permission, the structure is almost identical to granting it:
mysql > REVOKE type of _permission ON database_ name.table name FROM
’username @’localhost’;
You can review a user's current permissions by running the following:

mysql>SHOW GRANTS username;

Just as you can delete databases with DROP, you can use DROP to delete a user
altogether:

mysql > DROP USER ’username’ @’localhost’;

To test out your new user, log out by typing:

mysql>quit

log back in with this command in terminal:

mysql >mysql -u [username] -p

DR SANGEETA SONI 50

You might also like