当前位置:网站首页> MySQL数据库基本SQL语句教程之高级操作
MySQL数据库基本SQL语句教程之高级操作
2022-06-26 14:53:00 【1024问】
前言:
一.克隆表
1.1克隆方法一(将表与内容分开克隆)
1.2克隆方法二(将表与内容一起复制)
二.清空表,删除表内的所有数据
2.1方法一
2.2方法二
2.3小小结之drop,truncate,eleted的对比
三.创建临时表
四.用户管理
4.1新建用户
4.2使用明文密码创建用户
4.3使用密文创建数据库
五.查看用户信息
六.重命名用户
七.删除用户
八.密码管理
8.1修改当前用户密码
8.2修改其他用户的密码
8.3忘记root密码
8.3.1给root设置密码
九.数据库授权
9.1关于授权
9.2授权
9.2.1授权列表
9.3数据库授权
9.4远程登陆授权(使用navicat远程登陆)
9.5撤销权限
十.总结
前言:了解了一下MySQL数据库的基本语句,这章了解一下它的高级操作,包括用户增删除与给予相对应的权限
一.克隆表1.1克隆方法一(将表与内容分开克隆)#create table 新表名 like 复制的表名; 复制格式,能将复制表的格式到新表,但是里面的内容无法复制insert into 新表名 select * from 复制的表名; 复制原表内容到新表
create table 新表名 (select * from 复制的表名)数据结构和数据能一起复制
delete from naixu1;#DELETE清空表后,返回的结果内有删除的记录条目;delete工作时是一行一行的删除记录数据的;如果表中有自增长字段,使用DELETE FROM删除所有记录后,在此添加的记录会从原来最大的记录id后面继续自增写入数据
truncate table naixu1;#TRUNCATE清空表后,没有返回被删除的条目:TRUNCATE工作时是将表结构按原样重新建立因此在速度上TRUNCATE会比DELETE清空表快使用TRUNCATE TABLE 清空表内数据后,id会从1开始重新记录
drop | truncate | delete |
属于DDL | 属于DDL | 属于DML |
不可回滚 | 不可回滚 | 可回滚 |
不可带where | 不可带where | 可带where |
表内容和结构删除 | 表内容删除 | 表结构在,表内容要看where执行的情况 |
删除速度快 | 删除速度快 | 删除速度慢,需要逐行删除 |
三.创建临时表总结:
不再需要一张表的时候用drop想删除部分数据行的时候用delete,并且带上where子句保留表而删除所有数据的时候用truncate删除速度:drop>truncate> delete安全性 delete 最好
##添加临时表niaxu3create temporary table naixu3 (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,'hehe',12345,'看美女'); ##查看当前表中所有数据select * from naixu3;##退出数据库quit ##重新登录后进行查看 mysql -u root -p##查看之前创建的临时表中所有数据,发现已经被自动销毁select * from naixu3;
CREATE USER '用户名'@'来源地址' [IDENTIFIED BY [PASSWORD] '密码'];#‘用户名':指定将创建的用户名#‘来源地址':指定新创建的用户可在哪些主机上登录,可使用IP地址、网段、主机名的形式,本地用户可用localhost,允许任意主机登录可用通配符%#‘密码':若使用明文密码,直接输入'密码',插入到数据库时由Mysql自动加密;#######若使用加密密码,需要先使用SELECT PASSWORD(‘密码'); 获取密文,再在语句中添加 PASSWORD ‘密文';#若省略“IDENTIFIED BY”部分,则用户的密码将为空(不建议使用)
4.2使用明文密码创建用户create user 'nannan'@'localhost' identified by '123455';
创建后的用户保存在 mysql 数据库的 user 表里use mysql; #使用mysql库 select User from user;
rename user 'nannan'@'localhost' to 'lnhs'@'localhost';#将用户nannan改名为lnhs
drop user 'chenchen'@'localhost';#删除用户chenchen
set password = password('123456');
set password for 'naixu'@'localhost' = password('123456');
修改配置文件,添加配置,免密登录MySQLvim /etc/my.cnfskip-grant-tables #添加,使登录mysql不适用授权表
update mysql.user set authentication_string = password('123456') where user='root';flush privileges; #刷新 登入数据库之后再次修改my.conf配置文件,注释掉之前添加的配置命令,并再次重启服务使用新密码登录
GRANT语句:专门用来设置数据库用户的访问权限。当指定的用户名不存在时,GRANT语句将 会创建新的用户;当指定的用户名存在时,GRANT 语句用于修改用户信息。
9.2授权GRANT 权限列表 ON 数据库名/表名 TO '用户名'@'来源地址' [IDENTIFIED BY '密码'];
权限列表 | 用于列出授权使用的各种数据库操作,以逗号进行分隔,如“select,insert,update”。使用“all”表示所有权限,可授权执行任何操作。 |
数据库名.表名 | 用于指定授权操作的数据库和表的名称,其中可以使用通配符 |
用户名@来源地址 | 用于指定用户名称和允许访问的客户机地址,即谁能连接、能从哪里连接。来源地址可以是域名、IP地址,还可以使用“%”通配符,表示某个区域或网段内的所有地址,如“%.accp.com”、“192.168.80.%”等。 |
IDENTIFIED BY | 用于设置用户连接数据库时所使用的密码字符串。在新建用户时,若省略“IDENTIFIED BY”部分,则用户的密码将为空。 |
权限 | 功能 |
select | 查询数据 |
insert | 插入数据 |
update | 更新数据 |
delete | 删除数据 |
create | 创建库、表 |
drop | 删除库、表 |
index | 建立索引 |
alter | 更改表属性 |
event | 事件 |
trigger on | 创建触发器 |
show grants for [email protected];#查看用户权限
指定用户可以查看哪个数据库或表,别的无法访问
grant select on hehe.* to [email protected];#用户nannan只有hehe库下所有表的查询权限
切换用户进行验证
grant all on *.* to 'nannan'@'%' identified by '123456';
revoke select on hehe.* from [email protected];
再次切换访问,就已经没有权限了
本章和拐友们讲解MySQL的高阶语句,包括了如何克隆表,如何进行用户的增删改以及用户的权限设置,总的来说就是只要记住3点增删改就行
到此这篇关于MySQL数据库基本SQL语句教程之高级操作的文章就介绍到这了,更多相关MySQL SQL语句高级操作内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- Deployment of kubernetes' controller
- Naacl2022: (code practice) good visual guidance promotes better feature extraction, multimodal named entity recognition (with source code download)
- R语言glm函数逻辑回归模型、使用epiDisplay包logistic.display函数获取模型汇总统计信息(自变量初始和调整后的优势比及置信区间,回归系数的Wald检验的p值)、结果保存到csv
- 【 Native cloud】 Éditeur ivx Programmable par tout le monde
- Attention meets geometry: geometry guided spatiotemporal attention consistency self supervised monocular depth estimation
- Kubernetes的pod调度
- C语言刷题随记 —— 乒乓球比赛
- Redis transaction and watch instruction
- Is it safe to open an account by digging money? Is there any risk?
- The DOTPLOT function in the epidisplay package of R language visualizes the frequency of data points in different intervals in the form of point graphs, specifies the grouping parameters with the by p
猜你喜欢
功能:crypto-js加密解密
Bank of Beijing x Huawei: network intelligent operation and maintenance tamps the base of digital transformation service
设计人员拿到的工程坐标系等高线CAD图如何加载进图新地球
Halcon C # sets the form font and adaptively displays pictures
RestCloud ETL解决shell脚本参数化
Restcloud ETL extraction de données de table de base de données dynamique
Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法
Use abp Zero builds a third-party login module (II): server development
15 bs对象.节点名称.节点名称.string 获取嵌套节点内容
View touch analysis
随机推荐
设计人员拿到的工程坐标系等高线CAD图如何加载进图新地球
使用RestCloud ETL Shell组件实现定时调度DataX离线任务
【云原生】 ”人人皆可“ 编程的无代码 iVX 编辑器
整理了一批脚本标准的函数模块(2021版)
集群中命令的执行过程
TCP 复位攻击原理
TS common data types summary
Restcloud ETL extraction de données de table de base de données dynamique
房东拿租金去还房贷是天经地义的嘛
The tablestack function of the epidisplay package of R language makes a statistical summary table (descriptive statistics of groups, hypothesis test, etc.), does not set the by parameter to calculate
Redis cluster re fragmentation and ask command
Numpy基本使用
Document 1
Redis事务与watch指令
Attention meets geometry: geometry guided spatiotemporal attention consistency self supervised monocular depth estimation
This is the graceful file system mounting method, which is effective through personal testing
Bank of Beijing x Huawei: network intelligent operation and maintenance tamps the base of digital transformation service
vue中缓存页面 keepAlive使用
Login authentication service
信息学奥赛一本通 1405:质数的和与积 (思维题)