mysql操作
一、连接数据库
mysql -u user -p 例:mysql -u root -p
常见错误如下:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2), it means that the MySQL server daemon (Unix) or service (Windows) is not running.
退出连接:
QUIT 或者 Ctrl+D
2、 查看数据库,创建数据库,使用数据库查看数据库: show databases;
![33e4d3079e78bc84fd76b4afc324636b.png](https://img-blog.csdnimg.cn/img_convert/33e4d3079e78bc84fd76b4afc324636b.png)
显示当前使用的数据库中所有表:SHOW TABLES;
![9d2faa4edd8ba4631c6911464089bc29.png](https://img-blog.csdnimg.cn/img_convert/9d2faa4edd8ba4631c6911464089bc29.png)
3、 用户管理
注:用户权限相关数据保存在mysql数据库的user表中,所以也可以直接对其进行操作(不建议)
4、 权限管理
mysql对于权限这块有以下限制:
![0787e331c531058aa8ee509bc0c5e335.png](https://img-blog.csdnimg.cn/img_convert/0787e331c531058aa8ee509bc0c5e335.png)
对于数据库及内部其他权限如下:
![ff94687456715b21ffbb8c1f624b68ec.png](https://img-blog.csdnimg.cn/img_convert/ff94687456715b21ffbb8c1f624b68ec.png)
对于用户和IP的权限如下:
![f299c4dcc411aefc9bd4648f2e2c2865.png](https://img-blog.csdnimg.cn/img_convert/f299c4dcc411aefc9bd4648f2e2c2865.png)
1、查看权限:
show grants for '用户'@'IP地址'
2、授权
grant 权限 on 数据库.表 to '用户'@'IP地址'
3、取消授权
revoke 权限 on 数据库.表 from '用户'@'IP地址'
授权实例如下:
![51c8e3ae6a805950ed9ae1b6a810b813.png](https://img-blog.csdnimg.cn/img_convert/51c8e3ae6a805950ed9ae1b6a810b813.png)
五、mysql表操作
1、 查看表
![596390de3781e6f5c61c8c2cba79ca07.png](https://img-blog.csdnimg.cn/img_convert/596390de3781e6f5c61c8c2cba79ca07.png)
2、 创建表
![cdde5236607b361da02b88023e9ede1b.png](https://img-blog.csdnimg.cn/img_convert/cdde5236607b361da02b88023e9ede1b.png)
3、删除表
drop table 表名
4、清空表内容
delete from 表名
truncate table 表名
![555cf8cbeb8d2ed96b58f0b6f551a650.png](https://img-blog.csdnimg.cn/img_convert/555cf8cbeb8d2ed96b58f0b6f551a650.png)
5、 基本数据类型
MySQL的数据类型大致分为:数值、时间和字符串
![ff08902675490e54af57f97bae62783a.png](https://img-blog.csdnimg.cn/img_convert/ff08902675490e54af57f97bae62783a.png)
mysql表内容操作
表内容操作无非就是增删改查,当然用的最多的还是查,而且查这一块东西最多,用起来最难,当然对于大神来说那就是so easy了,对于我这种小白还是非常难以灵活运用的,下面咱来一一操作一下
1、 增
![3fb14b10318c036c31c962ad6db03d38.png](https://img-blog.csdnimg.cn/img_convert/3fb14b10318c036c31c962ad6db03d38.png)
2、 删
![758d7870400577ee5e9098ae05c0da5b.png](https://img-blog.csdnimg.cn/img_convert/758d7870400577ee5e9098ae05c0da5b.png)
3、 改
![13af00d8a2c8373ecd60314a57df2793.png](https://img-blog.csdnimg.cn/img_convert/13af00d8a2c8373ecd60314a57df2793.png)
4、 查
![44c4ceda2986d4ae274d5e2faf495fff.png](https://img-blog.csdnimg.cn/img_convert/44c4ceda2986d4ae274d5e2faf495fff.png)
查这块的条件太多太多我给列举出来至于组合还得看大家的理解程度哈
a、 条件判断where
![97796f0c2cd97aebdff718368e8e2bcb.png](https://img-blog.csdnimg.cn/img_convert/97796f0c2cd97aebdff718368e8e2bcb.png)
b、通配符like
![8fafafe19774d81b882934c844f184c6.png](https://img-blog.csdnimg.cn/img_convert/8fafafe19774d81b882934c844f184c6.png)
c、限制limit
![5a8fe49890a5375f5599acf74de7b72d.png](https://img-blog.csdnimg.cn/img_convert/5a8fe49890a5375f5599acf74de7b72d.png)
d、排序asc,desc
![8e5b28e554eaa63184c7c312d7edd005.png](https://img-blog.csdnimg.cn/img_convert/8e5b28e554eaa63184c7c312d7edd005.png)
e、分组group by
![ab55b5f8c96233ce7a15c59e5b1e4d15.png](https://img-blog.csdnimg.cn/img_convert/ab55b5f8c96233ce7a15c59e5b1e4d15.png)
来源:https://www.toutiao.com/a6316302690062909698/