Detailed MySQL database advanced commands

1. Advanced operation of data table

(1) Clone the table and generate the data records of the data table into a new table
方法一:
create table 新表名 like 旧表名;				#通过 LIKE 方法,复制旧表结构生成新表
insert into 新表名 select * from 旧表名;
方法二:
CREATE TABLE 新表名 (SELECT * from 旧表名);
show create table 新表名\G					#获取数据表的表结构、索引等信息
SELECT * from 新表名;
(2) Empty the table, delete all data in the table
方法一:
delete from 表名;
#DELETE清空表后,返回的结果内有删除的记录条目;DELETE工作时是一行一行的删除记录数据的;
如果表中有自增长字段,使用DELETE FROM 删除所有记录后,再次新添加的记录会从原来最大的记录 ID 后面继续自增写入记录。
方法二:
truncate table 表名;
#TRUNCATE 清空表后,没有返回被删除的条目;TRUNCATE 工作时是将表结构按原样重新建立
因此在速度上 TRUNCATE 会比 DELETE 清空表快;使用 TRUNCATE TABLE 清空表内数据后,ID 会从 1 开始重新记录。
(3) Create a temporary table

After the temporary table is created successfully, the temporary table created cannot be seen using the SHOW TABLES command. The temporary table will be destroyed after the connection exits.
Before exiting the connection, you can also perform operations such as adding, deleting, modifying, and checking, such as using the DROP TABLE statement to manually directly delete the temporary table.

CREATE TEMPORARY TABLE 表名 (字段1 数据类型,字段2 数据类型[,...][,PRIMARY KEY (主键名)]);

Example:

##创建表
create temporary table test01 (
id int(4) zerofill primary key auto_increment,
name varchar(10) not null,
cardid int(18) not null unique key,
hobby varchar(50));

##往表里插入信息
insert into test03 values(1,'zhangsan',123456,'running');

##查看表
select * from test03;
show tables;

(4) Create foreign key constraints

Note: The field of the child table associated with the foreign key must be set as the primary key. The foreign key fields of the main table and the fields of the sub-table are required to have the same data type, character length, and constraints.

#创建子表test01
create table test01 (sid int(4),hobname varchar(20));

#为子表添加一个主键约束。主键名建议以“PK_”开头。
alter table test01 add constraint PK_sid primary key test01 (sid);

#为 test02 表添加外键,并将 test02 表的 hobby 字段和 test01 表的 sid 字段建立外键关联。
#外键名建议以“FK_”开头。
alter table test02 add constraint FK_hob foreign key (hobby) references test01 (sid);

desc test02;
#查看和删除外键约束
show create table test02;
alter table test02 drop foreign key FK_hob;
alter table test02 drop key FK_hob;
desc test02;

6 common constraints in MySQL:

  • Primary key constraint (primary key)
  • Foreign key constraints (foreign key)
  • Not null constraint (not null)
  • Uniqueness constraint (unique [key|index])
  • Default value constraint (default)
  • Self-increment constraint (auto_increment)

===============================================================================================

Two, database user management

(1) New user
CREATE USER '用户名'@'来源地址' [IDENTIFIED BY [PASSWORD] '密码'];
'用户名':指定将创建的用户名

'来源地址':指定新创建的用户可在哪些主机上登录
可使用IP地址、网段、主机名的形式,本地用户可用localhost,允许任意主机登录可用通配符%

'密码':若使用明文密码,直接输入'密码',插入到数据库时由Mysql自动加密;
若使用加密密码,需要先使用SELECT PASSWORD('密码'); 获取密文,再在语句中添加 PASSWORD '密文';
若省略“IDENTIFIED BY”部分,则用户的密码将为空(不建议使用)

Example:

#明文加密
CREATE USER 'user1'@'localhost' IDENTIFIED BY '123456';  

#密文加密
SELECT PASSWORD('abc123');                              
CREATE USER 'user2'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9';
(2) View user information
#创建后的用户保存在 mysql 数据库的 user 表里
USE mysql;
SELECT User,authentication_string,Host from user;
(3) Rename user
RENAME USER '用户名'@'来源地址' TO '更改用户名'@'来源地址';
(4) Delete user
DROP USER '用户名'@'来源地址';
(5) Modify the password of the currently logged in user
SET PASSWORD = PASSWORD('abc123');
(6) Modify other user passwords
SET PASSWORD FOR '用户名'@'来源地址' = PASSWORD('密码');
(7) The solution to forget the root password

method one:

修改 /etc/my.cnf 配置文件,不使用密码直接登录到 mysql

vim /etc/my.cnf
[mysqld]
skip-grant-tables					#添加,使登录mysql不使用授权表

systemctl restart mysqld

mysql								#直接登录

Method Two:

使用 update 修改 root 密码,刷新数据库
UPDATE mysql.user SET AUTHENTICATION_STRING = PASSWORD('密码') where user='root';

FLUSH PRIVILEGES;
quit

mysql -u root -p密码

注意:最后再把 /etc/my.cnf 配置文件里的 skip-grant-tables 删除,并重启 mysql 服务。

===============================================================================================

Three, database user authorization

(1) Grant permissions

GRANT statement: specifically used to set access permissions for database users. When the specified user name does not exist, the GRANT statement will create a new user; when the specified user name exists, the GRANT statement is used to modify user information.

GRANT 权限列表 ON 数据库名.表名 TO '用户名'@'来源地址' [IDENTIFIED BY '密码'];
#权限列表:用于列出授权使用的各种数据库操作,以逗号进行分隔,如“select,insert,update”。
使用“all”表示所有权限,可授权执行任何操作。

#数据库名.表名:用于指定授权操作的数据库和表的名称,其中可以使用通配符“*”。
例如,使用“kgc.*”表示授权操作的对象为 kgc数据库中的所有表。

#'用户名@来源地址':用于指定用户名称和允许访问的客户机地址,即谁能连接、能从哪里连接。
来源地址可以是域名、IP 地址,还可以使用“%”通配符,表示某个区域或网段内的所有地址,如“%.kgc.com”、“192.168.80.%”等。

#IDENTIFIED BY:用于设置用户连接数据库时所使用的密码字符串。
在新建用户时,若省略“IDENTIFIED BY”部分, 则用户的密码将为空。

#允许用户 XXX 在本地查询 XXX 数据库中 所有表的数据记录,但禁止查询其他数据库中的表的记录。
GRANT select ON kgc.* TO '用户名'@'来源地址' IDENTIFIED BY '密码';

#允许用户 XXX 在所有终端远程连接 mysql,并拥有所有权限。
GRANT ALL [PRIVILEGES] ON *.* TO '用户名'@'%' IDENTIFIED BY '密码';
(2) View authority
SHOW GRANTS FOR 用户名@来源地址;
(3) Revocation of authority
REVOKE 权限列表 ON 数据库名.表名 FROM 用户名@来源地址;

SHOW GRANTS FOR'lisi'@'%';
#USAGE permission can only be used for database login and cannot perform any operations; USAGE permission cannot be recycled, that is, REVOKE cannot delete users.

(4) Remember to refresh after authorization
flush privileges;

===============================================================================================

Guess you like

Origin blog.csdn.net/weixin_51468875/article/details/113253546