forked from ricksilva/mysql_cc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_18.sql
76 lines (61 loc) · 2.69 KB
/
chapter_18.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
--
-- MySQL Crash Course
--
-- Chapter 18 – Using Views to Hide Salary Data
--
-- You can copy and paste any of these commands into your favorite MySQL tool
-- (like MySQL Workbench) and run them in your own MySQL environment.
--
-- Run this script as a user who has privs to create tables and grant permissions, like "root"
drop database if exists business;
create database business;
use business;
create table employee
(
employee_id int primary key auto_increment,
first_name varchar(100) not null,
last_name varchar(100) not null,
department varchar(100) not null,
job_title varchar(100) not null,
salary decimal(15,2) not null
);
insert into employee(first_name, last_name, department, job_title, salary)
values ('Jean',' Unger', 'Accounting', 'Bookkeeper', 81200);
insert into employee(first_name, last_name, department, job_title, salary)
values ('Brock', 'Warren', 'Accounting', 'CFO', 246000);
insert into employee(first_name, last_name, department, job_title, salary)
values ('Ruth', 'Zito', 'Marketing', 'Creative Director', 178000);
insert into employee(first_name, last_name, department, job_title, salary)
values ('Ann', 'Ellis', 'Technology', 'Programmer', 119500);
insert into employee(first_name, last_name, department, job_title, salary)
values ('Todd', 'Lynch', 'Legal', 'Compliance Manager', 157000);
create view v_employee as
select employee_id,
first_name,
last_name,
department,
job_title
from employee;
-- You can run these commands using the 'root' account that got created when you installed MySQL
drop user if exists accounting_user@localhost;
drop user if exists marketing_user@localhost;
drop user if exists techology_user@localhost;
drop user if exists legal_user@localhost;
drop user if exists hr_user@localhost;
create user accounting_user@localhost identified by 'accounting_password';
create user marketing_user@localhost identified by 'marketing_password';
create user technology_user@localhost identified by 'technology_password';
create user legal_user@localhost identified by 'legal_password';
create user hr_user@localhost identified by 'hr_password';
grant select, delete, insert, update on business.employee to hr_user@localhost;
grant select on business.v_employee to hr_user@localhost;
grant select on business.v_employee to accounting_user@localhost;
grant select on business.v_employee to marketing_user@localhost;
grant select on business.v_employee to legal_user@localhost;
grant select on business.v_employee to technology_user@localhost;
-- Exercise 18-1
create view v_employee_fn_dept as
select first_name,
department
from employee;
select * from v_employee_fn_dept;