当前位置:网站首页>MySQL数据库用户管理
MySQL数据库用户管理
2022-06-30 05:49:00 【SxinY欣】
目录
一、数据表操作
1.1 克隆表
将表的数据记录生成到新的表中
#通过 LIKE 方法,复制 lyong 表结构生成 lyong_bak 表
create table lyong_bak like lyong;
#导入数据
insert into lyong_bak (select * from lyong); #备份数据内容

备份得到表里没有数据,只是克隆了表的结构
将xy中的数据克隆进备份的xy_bak表中
方法二、创建的时候同时导入内容
create table lyong_bak02 (select * from lyong);

#获取数据表的表结构、索引等信息
show create table lyong_bak\G;

1.2 清空表
删除表内的所有数据
1.2.1deldte删除
DELETE清空表后,返回的结果内有删除的记录条目;DELETE工作时是一行一行的删除记录数据的;如果表中有自增长字段,使用DELETE FROM 删除所有记录后,再次新添加的记录会从原来最大的记录 ID 后面继续自增写入记录。
#delete from 表名;
delete from lly;
insert into lly(student_name,cardid,hobby) values('xyz',123456789,'南京')
-> ;

1.2.2 truncate删除
TRUNCATE 清空表后,没有返回被删除的条目;TRUNCATE 工作时是将表结构按原样重新建立,因此在速度上 TRUNCATE 会比 DELETE 清空表快;使用 TRUNCATE TABLE 清空表内数据后,ID 会从 1 开始重新记录。
truncate table info;
insert into lly(student_name,cardid,hobby) values('llly',654321,'上海');
1.2.3创建临时表
临时表创建成功之后,使用SHOW TABLES命令是看不到创建的临时表的,临时表会在连接退出后被销毁。
如果在退出连接之前,也可以可执行增删改查等操作,比如使用 DROP TABLE 语句手动直接删除临时表。
##添加临时表lyong01
create temporary table lyong01 (
id int(4) zerofill primary key auto_increment,
name varchar(10) not null,
cardid int(18) not null unique key,
hobby varchar(50));
## 查看当前库中所有表
show tables;
##在临时表中添加数据
insert into test03 values(1,'zhangsan',123456789,'watch a film');
##查看当前表中所有数据
select * from lyong01;
##退出数据库
quit
##重新登录后进行查看
mysql -u root -p
##查看之前创建的临时表中所有数据,发现已经被自动销毁
select * from lyong01;
二、用户管理
2.1新建用户
CREATE USER ‘用户名’@‘来源地址’ [IDENTIFIED BY [PASSWORD] ‘密码’];
‘用户名’:指定将创建的用户名
‘来源地址’:指定新创建的用户可在哪些主机上登录,可使用IP地址、网段、主机名的形式,本地用户可用localhost,允许任意主机登录可用通配符%
‘密码’:
若使用明文密码,直接输入’密码’,插入到数据库时由Mysql自动加密;
若使用加密密码,需要先使用SELECT PASSWORD(‘密码’); 获取密文,再在语句中添加 PASSWORD ‘密文’;
若省略“IDENTIFIED BY”部分,则用户的密码将为空(不建议使用)
2.1.1使用明文创建用户
CREATE USER 'liy'@'localhost' IDENTIFIED BY '123456';


2.1.2使用密文创建用户
select password(‘123456’);
create user ‘zxy’@‘localhost’ identified by password ‘*E56A114692FE0DE073F9A1DD68A00EEB9703F3F1’;
查看用户信息
创建后的用户保存在mysql 数据库的user表里
use mysql
select user,authentication_string,host from user;
2、查看用户信息
use mysql;
select User,authentication_string,Host from user;
3、重命名用户
rename user 'sxy'@'localhost' to 'shixy'@'localhost';
select user,authentication_string,host from user;
4、删除用户
drop user 'shixy'@'localhost';
select user,authentication_string,host from user;

三、数据库用户授权
1、授予权限
GRANT语句:专门用来设置数据库用户的访问权限。当指定的用户名不存在时,GRANT语句将会创建新的用户;当指定的用户名存在时,GRANT 语句用于修改用户信息。
GRANT 权限列表 ON 数据库名.表名 TO '用户名'@'来源地址' [IDENTIFIED BY '密码'];
权限列表:用于列出授权使用的各种数据库操作,以逗号进行分隔,如“select,insert,update”。使用“all”表示所有权限,可授权执行任何操作。
数据库名.表名:用于指定授权操作的数据库和表的名称,其中可以使用通配符“”。例如,使用“kgc.”表示授权操作的对象为 kgc数据库中的所有表。
‘用户名@来源地址’:用于指定用户名称和允许访问的客户机地址,即谁能连接、能从哪里连接。来源地址可以是域名、IP 地址,还可以使用“%”通配符,表示某个区域或网段内的所有地址,如“%.accp.com”、“192.168.80.%”等。
IDENTIFIED BY:用于设置用户连接数据库时所使用的密码字符串。在新建用户时,若省略“IDENTIFIED BY”部分, 则用户的密码将为空。
2、数据库授权
show grants for [email protected];
#查看用户权限
指定用户可以查看哪个数据库/表,别的数据库无权访问
grant select on test.* to [email protected];
#用户yong只有test库下所有表的查询权限
边栏推荐
- STM32F103系列控制的OLED IIC 4针
- Rotating frame target detection mmrotate v0.3.1 training dota data set (II)
- [GPU] basic operation of GPU (I)
- Rotating frame target detection mmrotate v0.3.1 learning configuration
- PKCs 12:personal information exchange syntax v1.1 translation part I
- Uboot reads the DDR memory size by sending 'R' characters through the terminal
- Visualization of 3D geological model based on borehole data by map flapping software
- Do you know how to show the health code in only 2 steps
- English grammar_ Adjective / adverb Level 3 - superlative
- 抓取手机端变体组合思路设想
猜你喜欢

86. 分隔链表

2022年,谁在推动音视频产业的新拐点?

Sound net, debout dans le "sol" de l'IOT

Leetcode search insert location

English grammar_ Adjective / adverb Level 3 - superlative

How to create a CSR (certificate signing request) file?

Projet Web de déploiement du serveur Cloud

Leetcode56. consolidation interval

9. naive Bayes

剑指 Offer 18. 删除链表的节点
随机推荐
[Alibaba cloud] student growth plan answers
云服务器部署 Web 项目
C. Divan and bitwise operations
Sword finger offer 18 Delete the node of the linked list
inno setup 最简单的自定义界面效果
1380. lucky numbers in matrices
Luogup2756 pilot pairing scheme problem (maximum flow)
UML tools
1380. lucky numbers in matrices
Expansion method of unity scanning circle
Detailed explanation of issues related to SSL certificate renewal
[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point
MySQL advanced (Advanced SQL statement)
Voting vault: a new primitive for defi and Governance
电脑查看WiFi使用密码
What indicators should safety service engineers pay attention to in emergency response?
Navigate back to fragmentpageradapter - & gt; Fragment is empty - navigating back to fragmentpageradapter - & gt; fragments are empty
二十四、输入输出设备模型(串口/键盘/磁盘/打印机/总线/中断控制器/DMA和GPU)
Sword finger offer 22 The penultimate node in the linked list
Shopping list--