Mysql view and management

MySQL


view

A view is a virtual table whose content is defined by a query. Like a real table, a view contains columns whose data comes from the corresponding real table (base table).

  1. create view 视图名 as select语句
  2. alter view 视图名 as select语句 --更新成新的视图
  3. SHOW CREATE VIEW 视图名
  4. drop view 视图名1,视图名2
-- 创建一个视图emp_view01,只能查询emp表的(empno、ename, job 和 deptno ) 信息

-- 创建视图
CREATE VIEW emp_view01
	AS
	SELECT empno, ename, job, deptno FROM emp; 

-- 查看视图
DESC emp_view01

SELECT * FROM emp_view01;
SELECT empno, job  FROM emp_view01;

-- 查看创建视图的指令
SHOW CREATE VIEW emp_view01
-- 删除视图
DROP VIEW emp_view01;


-- 视图的细节

-- 1. 创建视图后,到数据库去看,对应视图只有一个视图结构文件(形式: 视图名.frm) 
-- 2. 视图的数据变化会影响到基表,基表的数据变化也会影响到视图[insert update delete ]

-- 修改视图 会影响到基表

UPDATE emp_view01 
	SET job = 'MANAGER' 
	WHERE empno = 7369
	
SELECT * FROM emp; -- 查询基表


SELECT * FROM emp_view01

-- 修改基本表, 会影响到视图

UPDATE emp 
	SET job = 'SALESMAN' 
	WHERE empno = 7369

-- 3. 视图中可以再使用视图 , 比如从emp_view01 视图中,选出empno,和ename做出新视图
DESC emp_view01

CREATE VIEW emp_view02
	AS
	SELECT empno, ename FROM emp_view01
	
SELECT * FROM emp_view02

Notice

  1. After creating the view, go to the database to see that there is only one view structure file for the corresponding view (form: view name.frm)
  2. Data changes in the view will affect the base table, and data changes in the base table will also affect the view [insert update delete]
  3. Views can be reused within views, and the data still comes from the base table
  • Safety. Some data tables contain important information, and some fields are confidential and cannot be directly viewed by users. At this time, you can create a view in which only some fields are retained. In this way, users can query the fields they need, but cannot view confidential fields.
  • performance. Data in relational databases are often stored in tables, and foreign keys are used to establish relationships between these tables. At this time, database queries usually use connections ( JOIN ). This is not only troublesome, but also relatively inefficient. If you create a view that combines related tables and fields, you can avoid using JOIN to query data.
  • flexible. If there is an old table in the system, this table will be abandoned due to design problems. However, many applications are based on this table, which is not easy to modify. At this time, a view can be created, and the data in the view is directly mapped to the newly created table. In this way, a lot less changes can be made, and the purpose of upgrading the data table is achieved.





Mysql management

MysqlThe users in are stored in mysqltablesuser in the system database

userImportant field descriptions of the table:

  1. host: The "location" that allows login
    localhostmeans that the user is only allowed to log in locally, and the IP address can also be specified, such as:192.168.1.100
  2. user:username;
  3. authentication string:Password is the password encrypted by the function mysql.password()

Create user

create user '用户名'@'允许登录位置' identified by '密码'

Create a user and specify a password


delete users

drop user '用户名'@'允许登录位置';

User changes password

Change your password

set password = password('密码');

Modify other people's passwords (requires permission to modify user passwords)

set password for '用户名'@'登录位置'=password('密码');

Authorize users

grant 权限列表 on.对象名 to '用户名'@'登录位置'【identified  by '密码'
  1. Permission list, multiple permissions separated by commas
   grant select on .....

   grant select,delete,create on ......

   grant all [privileges] on .... //表示赋予该用户在该对象上的所有权限
  1. Special note
    *.*: Represents all objects (tables, views, stored procedures) of all databases in this system

    库.*: Represents all data objects (tables, views, stored procedures, etc.) in a database

  2. identified byIt can be omitted or written out.

    (1) If the user exists, the user's password is modified at the same time.

    (2) If the user does not exist, create the user!


Recycle user authorization

revoke 权限列表 on.对象名 from '用户名'@'登录位置';

Permission Validation Instructions

If the permissions do not take effect, you can execute the following command to refresh

FLUSH PRIVILEGES;




Notice

  1. When creating a user, if you do not specify a Host, it %means %that all IPs have connection permissions.

    create user XX;
    
  2. You can also specify in this way
    create user 'xxx'@'192.168.1.%' that the IP of user xx 192.168.1.*can log in to mysql.

  3. When deleting a user, if host is not %, you need to explicitly specify 'user'@'host value'

-- 说明 用户管理的细节
-- 在创建用户的时候,如果不指定Host, 则为% , %表示表示所有IP都有连接权限 
-- create user  xxx;

CREATE USER jack

SELECT `host`, `user` FROM mysql.user

-- 你也可以这样指定 
-- create user  'xxx'@'192.168.1.%'  表示 xxx用户在 192.168.1.*的ip可以登录mysql

CREATE USER 'smith'@'192.168.1.%'

-- 在删除用户的时候,如果 host 不是 %, 需要明确指定  '用户'@'host值'

DROP USER jack -- 默认就是 DROP USER 'jack'@'%'

DROP USER 'smith'@'192.168.1.%'

Supongo que te gusta

Origin blog.csdn.net/Raccon_/article/details/132426807
Recomendado
Clasificación