You are on page 1of 14

ASSIGNMENT 2(SUBJECT: DBMS)

NAME OF STUDENT: Harshdeep Telang


BRANCH: CS

ID: 191070024

AIM: Study of DDL and DML operations on views using SQL.


TOOL: MariaDB (If any other tool, please mention the name)
PROGRAMMING LANGUAGE: Structured Query language (SQL)

THEORY:

1. Views-VIEWS are SQL Queries.VIEWS are virtual tables that do not store any data
of their own but display data stored in other tables.. A view can contain all or a few
rows from a table. A MySQL view can show data from one table or many tables.
Operations on views(whether allowed or not):
1)a.Create view Allowed
b.Create or replace view
2) Alter view Not Allowed
3) Drop view Allowed
4) Rename Allowed

1) Insert view Allowed


2)Select view Allowed
3)Update view Allowed
4)Delete view Allowed
5) Truncate view Not Allowed

Advantages of views-

● View the data without storing the data into the object.
● Restrict the view of a table i.e. can hide some of columns in the tables.
● Hiding the data.
● It provides security, data integrity, consistency.

1
OPERATIONS EXECUTED: (MENTION THE LIST AS IT)
1. Create Database: create database database_name;
2. Use Database: use database_name;
3. Create multiple views on same tables
4. Create or replace view view_name as select column_name from table_name (To change
the definition of already existing view)
5. Describe view
6. Show create table table_name
7. Insert into view
8. Select queries (with and without * )
9. Alter view (Add Column): alter table table_name add column_name datatype;
10. Alter view (Drop Column): alter table table_name drop column_name
11. Alter view (Change Column’s data type): alter table table_name modify column_name
new_datatype;
12. Alter view (Change Column’s name): alter table table_name change
old_column_name new_column_name datatype_to_be_assigned;
13. Update view
14. Rename: rename table view1 to view2
15. Delete view
16. Truncate view: truncate table_name
17. Drop view: drop view view name
(Use of upward arrow key)

OUTPUT:

C:\Program Files\MariaDB 10.5\bin>mysql -u root -p


Enter password: **********
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.5.8-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;


+--------------------+
| Database |

2
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.001 sec)

MariaDB [(none)]> create database vjti;


Query OK, 1 row affected (0.002 sec)

MariaDB [(none)]> use vjti;


Database changed
MariaDB [vjti]> create table Student (id int,name varchar(25),marks float);
Query OK, 0 rows affected (0.038 sec)

MariaDB [vjti]> insert into Student values (1,"Harshdeep",9.7),(2,"Akash",8.9),


(3,"Gayatri",9.4),(4,"Vansh",7.9);
Query OK, 4 rows affected (0.024 sec)
Records: 4 Duplicates: 0 Warnings: 0

MariaDB [vjti]> select * from Student;


+------+-----------+-------+
| id | name | marks |
+------+-----------+-------+
| 1 | Harshdeep | 9.7 |
| 2 | Akash | 8.9 |
| 3 | Gayatri | 9.4 |
| 4 | Vansh | 7.9 |
+------+-----------+-------+
4 rows in set (0.000 sec)

MariaDB [vjti]> create view v1 as select id,name from student;


Query OK, 0 rows affected (0.018 sec)

MariaDB [vjti]> show create table v1;


+------
+-------------------------------------------------------------------------------------------------------------------
----------------------------------------------+----------------------+----------------------+
| View | Create View
| character_set_client | collation_connection |
+------
+-------------------------------------------------------------------------------------------------------------------
----------------------------------------------+----------------------+----------------------+

3
| v1 | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL
SECURITY DEFINER VIEW `v1` AS select `student`.`id` AS `id`,`student`.`name` AS
`name` from `student` | cp850 | cp850_general_ci |
+------
+-------------------------------------------------------------------------------------------------------------------
----------------------------------------------+----------------------+----------------------+
1 row in set (0.026 sec)

MariaDB [vjti]> desc v1;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(25) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.042 sec)

MariaDB [vjti]> select * from v1;


+------+-----------+
| id | name |
+------+-----------+
| 1 | Harshdeep |
| 2 | Akash |
| 3 | Gayatri |
| 4 | Vansh |
+------+-----------+
4 rows in set (0.000 sec)

MariaDB [vjti]> insert into Student values(5,"Shubham",7.7);


Query OK, 1 row affected (0.008 sec)

MariaDB [vjti]> select * from v1;


+------+-----------+
| id | name |
+------+-----------+
| 1 | Harshdeep |
| 2 | Akash |
| 3 | Gayatri |
| 4 | Vansh |
| 5 | Shubham |
+------+-----------+
5 rows in set (0.001 sec)

MariaDB [vjti]> insert into v1 values (6,"Shreeja",8.2);


ERROR 1136 (21S01): Column count doesn't match value count at row 1
MariaDB [vjti]> insert into v1 values (6,"Shreeja");

4
Query OK, 1 row affected (0.008 sec)

MariaDB [vjti]> create view v2 as select id,marks from Student;


Query OK, 0 rows affected (0.012 sec)

MariaDB [vjti]> select * from v2;


+------+-------+
| id | marks |
+------+-------+
| 1 | 9.7 |
| 2 | 8.9 |
| 3 | 9.4 |
| 4 | 7.9 |
| 5 | 7.7 |
| 6 | NULL |
+------+-------+
6 rows in set (0.017 sec)

MariaDB [vjti]> create or replace view v1 as select name,marks from student;


Query OK, 0 rows affected (0.014 sec)

MariaDB [vjti]> select * from v1;


+-----------+-------+
| name | marks |
+-----------+-------+
| Harshdeep | 9.7 |
| Akash | 8.9 |
| Gayatri | 9.4 |
| Vansh | 7.9 |
| Shubham | 7.7 |
| Shreeja | NULL |
+-----------+-------+
6 rows in set (0.011 sec)

MariaDB [vjti]> alter table v1 add DOB int;


ERROR 1347 (HY000): 'vjti.v1' is not of type 'BASE TABLE'
MariaDB [vjti]> update v1 set marks = 9.5 where name = "Shreeja";
Query OK, 1 row affected (0.013 sec)
Rows matched: 1 Changed: 1 Warnings: 0

MariaDB [vjti]> select * from v1;


+-----------+-------+
| name | marks |
+-----------+-------+
| Harshdeep | 9.7 |
| Akash | 8.9 |

5
| Gayatri | 9.4 |
| Vansh | 7.9 |
| Shubham | 7.7 |
| Shreeja | 9.5 |
+-----------+-------+
6 rows in set (0.001 sec)

MariaDB [vjti]> select * from Student;


+------+-----------+-------+
| id | name | marks |
+------+-----------+-------+
| 1 | Harshdeep | 9.7 |
| 2 | Akash | 8.9 |
| 3 | Gayatri | 9.4 |
| 4 | Vansh | 7.9 |
| 5 | Shubham | 7.7 |
| 6 | Shreeja | 9.5 |
+------+-----------+-------+
6 rows in set (0.000 sec)

MariaDB [vjti]> delete from v1 where name = "Shubham";


Query OK, 1 row affected (0.006 sec)

MariaDB [vjti]> select * from v1;


+-----------+-------+
| name | marks |
+-----------+-------+
| Harshdeep | 9.7 |
| Akash | 8.9 |
| Gayatri | 9.4 |
| Vansh | 7.9 |
| Shreeja | 9.5 |
+-----------+-------+
5 rows in set (0.001 sec)

MariaDB [vjti]> rename table v1 to v3;


Query OK, 0 rows affected (0.014 sec)

MariaDB [vjti]> select * from v3;


+-----------+-------+
| name | marks |
+-----------+-------+
| Harshdeep | 9.7 |
| Akash | 8.9 |
| Gayatri | 9.4 |
| Vansh | 7.9 |

6
| Shreeja | 9.5 |
+-----------+-------+
5 rows in set (0.013 sec)

MariaDB [vjti]> drop view v3;


Query OK, 0 rows affected (0.001 sec)

SNAPSHOTS OF YOUR CODE:


1.Create Database

2.Use database

3.Create Multiple views on the same table

7
4.Create or replace view

5.Describe view

8
6.Show create table

7.Insert into view

8.Select Queries

9
10
9.Alter view-Add column

10.Alter view-Drop column

11.Alter view-Change column’s data type


11
12.Alter view- Change Column name
This command is not allowed on views.

13.Update view

12
14.Rename

15.Delete view

16.Truncate view

13
17.Drop view

*******End of the Assignment*******

14

You might also like