-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9_learning_sql_Function_DCL_Grant_Revoke_Indexing_Triggers_Subquery_Views.sql
191 lines (106 loc) 路 5.2 KB
/
day9_learning_sql_Function_DCL_Grant_Revoke_Indexing_Triggers_Subquery_Views.sql
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
select * from employees;
alter table employees add column status varchar(20) ;
update employees set status = 'Active';
ALTER TABLE employees
ALTER COLUMN status SET NOT NULL
-- creating function for update date automatically when employee not active
create function update_on_status_change()
returns trigger as $$
begin
new.last_updated =now(); -- it wil display data is updated
return new;
end;
$$ language plpgsql;
create trigger for_update_status
before update on employees
for each row
execute function update_on_status_change()
select * from employees;
create view data_update as select * from employees;
--
update employees set status ='InActive' where id =4;
-- "2024-04-18 21:56:07.343581" 48000 "2024-04-25 16:10:32.144669" date time automatically update in last_updated
-- indexing in post gress
create table emp_table(id serial primary key, emp_name varchar, salary numeric);
insert into emp_table(emp_name, salary)
values('Ravi',50000),('John',90000),('Shilpa',80000),('Priya',55000),('Mohan',75000),('Akhil',75000),('Manoj',95000),
('Shekar',60000),('Kumar',65000),('Komal',95000);
select * from emp_table;
select * from emp_table where salary=55000; -- Total rows: 1 of 1 Query complete 00:00:00.153
create index salary_index on emp_table(salary);
select * from emp_table where salary=55000; --Total rows: 1 of 1Query complete 00:00:00.092
-- this saves times and reduce load to system and also help to retrive data quickly
explain
select * from emp_table where salary=55000;
-- DCL COMMANDS
-- 0---------------------------------------------
CREATE USER obitorin password '123456';
-- after that go to in postgres/postgres@Postgressql16
-- then click for new connection and choose there new user for this database
select * from employees;
-- ERROR: permission denied for table employees the use dont have access yet
-- user dont havae permissions so now switch to root first
grant select on employees to obitorin;
-- now again switch to user
select * from employees;
-- now it will work because it already have permission to access table
-- now user trying to update table it will definatly say an permission denied
update employees set status='InActive' where id = 6;
-- ERROR: permission denied for table employees
-- switch to root user
-- lets give the access to user
grant update on employees to obitorin;
-- switch to user now again
update employees set status='InActive' where id = 6;
-- UPDATE 1
--Query returned successfully in 119 msec.
select * from employees;
delete from employees where status ='InActive';
-- same issue again it decline
-- do the same thing again give access
grant insert,delete on employees to obitorin;
-- now switch
delete from employees where status ='InActive';
select * from employees;
-- it worked
-- granting revoking at once all
grant all on employees to obitorin;
revoke all on employees from obitorin;
drop user obitorin;
-- ----------------------------------------------------------------mysql commands
mysql> use day9;
Database changed
mysql> create table emp_table(id int auto_increment primary key,emp_name varchar(20),salary int);
Query OK, 0 rows affected (0.06 sec)
mysql> select * from emp_table where salary =55000;
+----+----------+--------+
| id | emp_name | salary |
+----+----------+--------+
| 4 | Priya | 55000 |
+----+----------+--------+
1 row in set (0.00 sec)
mysql> explain select * from emp_table where salary =55000;
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | emp_table | NULL | ALL | NULL | NULL | NULL | NULL | 10 | 10.00 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
-- about index--------------------------------------------------------------------
mysql> create index salary_index on emp_table (salary);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from emp_table where salary =55000;
+----+----------+--------+
| id | emp_name | salary |
+----+----------+--------+
| 4 | Priya | 55000 |
+----+----------+--------+
1 row in set (0.00 sec)
mysql> explain select * from emp_table where salary =55000;
+----+-------------+-----------+------------+------+---------------+--------------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+---------------+--------------+---------+-------+------+----------+-------+
| 1 | SIMPLE | emp_table | NULL | ref | salary_index | salary_index | 5 | const | 1 | 100.00 | NULL |
+----+-------------+-----------+------------+------+---------------+--------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)