-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews_in_sql.sql
More file actions
71 lines (48 loc) · 1.88 KB
/
views_in_sql.sql
File metadata and controls
71 lines (48 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
views are required when we are fetching data again and again from set of tables using joins
or doing large operation again and again and to minimize the time we can use
using views we can overcome need for creating more tables even though the req column and data
are available in the exisiting table.
- its basically make a kind of replica of required table
- views are nothing but virtual tables.
- hide the database implementation of the actual table and show the desired virtual views to the users
*/
use temp_db;
show tables; -- i have dropped all the tables to start from the fresh
-- emp1 id, name, exp
create table emp1(id int, name varchar(15), exp int);
insert into emp1 values(1, 'Sanoj', 1);
insert into emp1 values(2, 'Kumar', 3);
insert into emp1 values(3, 'Deepak', 10);
insert into emp1 values(4, 'Vivek', 15);
select * from emp1;
-- creating view of emp1 having id and name
create view emp1view
as
select id, name from emp1; -- now the emp1view only has id, name
select * from emp1view; -- normally work as the normal table work
create view emp1exp
as
select * from emp1 where exp > 2;
select * from emp1exp;
-- we can do this using two tables also
create table emp2(id int, country varchar(15));
insert into emp2 values(1, 'IND');
insert into emp2 values(2, 'USA');
insert into emp2 values(3, 'UK');
select * from emp2;
-- creating view to show all the emp details
create view empDetails
as
select o.id, o.name, o.exp, t.country
from emp1 o JOIN emp2 t ON o.id = t.id;
select * from empDetails;
create view viewemp1
as
select * from emp1; -- replica of the emp1
select * from viewemp1; -- if we do crud operation on the view then or in the original table then what happen to view
insert into emp1 values(1, 'Deepak kumar', 109);
-- now in the view
select * from viewemp1; -- gets updated accordingly
delete from viewemp1 where id = 4;
select * from emp1;