MySQL view + user management

view

** The view is a virtual table whose content is defined by the query. ** Like a real table, the view contains a series of named columns and rows of data. The data changes
of the view will affect the base table, and the data changes of the base table will also affect the view.

Basic use

Create a view
create view view name as select statement; // When querying ~~, create a view

select * from v_ename_dname;
Insert picture description here

Modified the view, which affects the base table data.
Modified the base table, which affects the view.

 update v_ename_dname set dname='sales'  where ename='CLARK';
select * from EMP  where ename='CLARK';

Insert picture description here

Delete view

drop view view name;

View rules and restrictions

As with tables, they must be uniquely named (views or table names with the same name cannot appear) The number of views created is unlimited, but the performance impact after creating a complex query as a view must be considered

The view cannot add an index , nor can it have associated triggers or the default value view can improve security, and must have sufficient access rights
** order by can be used in the view, ** but if you retrieve data from the view, select also contains order by, then the order by in this view will be overwritten. The view can be used with tables

User Management

If we can only use the root user, there is a security risk. At this time, you need to use MySQL user management.

Insert picture description here

user

1 User information

Users in MySQL are stored in the user table of the system database mysql

select host,user,authentication_string from user;

Field explanation:
host: indicates from which host this user can log in, if it is localhost, it can only log in from this machine user: user name
authentication_string: user password encrypted by password function
* _priv: user-owned permissions

Create user syntax:
create user 'user name' @ 'login host / ip' identified by 'password';

create user 'litao'@'localhost' identified by '123456';
select host,user, password from user where user='litao';

3 Delete user

Syntax:
drop user 'user name' @ 'hostname' Example:

Modify user password syntax:

1 Change your own password
set password = password ('new password');

** 2 root users modify the password of the specified user **
set password for 'user name' @ 'host name' = password ('new password');
set password for 'litao' @ 'localhost' = password ('abcdefg' );

Database permissions

List of permissions provided by MySQL database:
Insert picture description here
**

1 Authorize users

The user just created does not have any permissions. Need to authorize the user. **
Syntax:
grant permission list on library. Object name to 'user name' @ 'login location' [identified by 'password']
Description: permission list, multiple permissions are separated by commas

Permission list, multiple permissions are separated by commas

grant select on…
grant select, delete, create on…
grant all [privileges] on… – means to grant the user all the permissions on the object

On behalf of the system. "" All in all objects in the database (tables, views, stored procedures, etc.)
library : indicates that a database of all * data objects (tables, views, stored procedures, etc.)
IDENTIFIED by optional. If the user exists, change the password while granting the permission, if the user does not exist, it is to create a user

-- root 给 litao 赋予账户查询mytest库的DEPT表的权限
mysql> grant select on mytest.DEPT to 'litao'@'localhost';
show databases;
-- litao账户可以看到mytest库 

Insert picture description here
Insert picture description here
Note: If it is found that the authorization is not effective, execute the following instructions:

   flush privileges;

2 Recycling authority

Syntax:
revoke permission list on library. Object name from 'user name' @ 'login location'; Example:

-- 回收litao对数据库的所有权限
revoke all on mytest.DEPT from 'litao'@'localhost';
 
Published 119 original articles · praised 47 · 30,000+ views

Guess you like

Origin blog.csdn.net/weixin_44030580/article/details/105484705