文章来源:https://macrozheng.github.io/mall-learning/#/reference/mysql

开发者必备Mysql命令

开发者必备Mysql常用命令,涵盖了数据定义语句、数据操纵语句及数据控制语句,基于Mysql5.7。

数据定义语句(DDL)

数据库操作

  • 登录数据库:

     mysql -uroot -proot 
  • 创建数据库:
     create database test 
  • 查看所有数据库:
     show databases 

  • 选择数据库并使用:
    use test
  • 查看所有数据表:
    show tables
  • 删除数据库:
    drop database test

表操作

  • 创建表:

    create table emp(ename varchar(10),hiredate date,sal decimal(10,2),deptno int(2))  
    create table dept(deptno int(2),deptname varchar(10))

  • 查看表的定义:
    desc emp

  • 查看表定义(详细):
    show create table emp \G

  • 删除表:
    drop table emp
  • 修改表字段:
    alter table emp modify ename varchar(20)
  • 添加表字段:
    alter table emp add column age int(3)
  • 删除表字段:
    alter table emp drop column age
  • 字段改名;
    alter table emp change age age1 int(4)
  • 修改表名:
    alter table emp rename emp1

数据操纵语句(DML)

插入记录

  • 指定名称插入:

    insert into emp (ename,hiredate,sal,deptno) values ('zhangsan','2018-01-01','2000',1)
  • 不指定名称插入:
    insert into emp values ('lisi','2018-01-01','2000',1)
  • 批量插入数据:
    insert into dept values(1,'dept1'),(2,'dept2')

修改记录

update emp set sal='4000',deptno=2 where ename='zhangsan'

删除记录

delete from emp where ename='zhangsan'

查询记录

  • 查询所有记录:

    select * from emp
  • 查询不重复的记录:
    select distinct deptno from emp
  • 条件查询:
    select * from emp where deptno=1 and sal<3000
  • 排序和限制:
    select * from emp order by deptno desc limit 2
  • 分页查询(查询从第0条记录开始10条):
    select * from emp order by deptno desc limit 0,10
  • 聚合(查询部门人数大于1的部门编号):
    select deptno,count(1) from emp group by deptno having count(1) > 1
  • 连接查询:
    select * from emp e left join dept d on e.deptno=d.deptno
  • 子查询:
    select * from emp where deptno in (select deptno from dept)
  • 记录联合:
    select deptno from emp union select deptno from dept

数据控制语句(DCL)

权限相关

  • 授予操作权限(将test数据库中所有表的select和insert权限授予test用户):

    grant select,insert on test.* to 'test'@'localhost' identified by '123'
  • 查看账号权限:
    show grants for 'test'@'localhost'

  • 收回操作权限:
    revoke insert on test.* from 'test'@'localhost'

  • 授予所有数据库的所有权限:
    grant all privileges on *.* to 'test'@'localhost'
  • 授予所有数据库的所有权限(包括grant):
    grant all privileges on *.* to 'test'@'localhost' with grant option
  • 授予SUPER PROCESS FILE权限(系统权限不能指定数据库):
    grant super,process,file on *.* to 'test'@'localhost'
  • 只授予登录权限:
    grant usage on *.* to 'test'@'localhost'

帐号相关

  • 删除账号:

    drop user 'test'@'localhost'
  • 修改自己的密码:
    set password = password('123')
  • 管理员修改他人密码:
    set password for 'test'@'localhost' = password('123')

其他

字符集相关

  • 查看字符集:

    show variables like 'character%'

  • 创建数据库时指定字符集:
    create database test2 character set utf8

时区相关

  • 查看当前时区(UTC为世界统一时间,中国为UTC+8):

    show variables like "%time_zone%"

  • 修改mysql全局时区为北京时间,即我们所在的东8区:
    set global time_zone = '+8:00';
  • 修改当前会话时区:
    set time_zone = '+8:00'

  • 立即生效:
    flush privileges

最新文章

  1. SQL匹配顺序
  2. php文件删除
  3. Python小白好教程
  4. 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(6)
  5. 20169210《Linux内核原理与分析》第十二周作业
  6. jersey 过滤器
  7. HBase的基本操作
  8. 近期在调用 calendar.js 的时候出现中文乱码! 解决方式
  9. redis加入到Windows 服务
  10. ASP.NET Zero--3.菜单配置
  11. AM335X开发板学习系列——环境搭建(vbox虚拟机ubuntu14.04下minicom的安装和配置)
  12. 网时|云计算的集群技术对于传统IDC而言,又有哪些提高呢?
  13. Mysql bug: The server time zone value &#39;�й���׼ʱ��&#39; is unrecognized or represents more than one time zone.
  14. MySql my.ini 中文详细说明
  15. VMware虚拟机安装Linux后忘记root密码怎么办(三)
  16. Confluence 6 MySQL 测试你的数据库连接
  17. 2n的 位数
  18. 自学Zabbix9.1 Network Discovery 网络发现原理
  19. 将多个 docx 文件使用 POI 进行合并,生成单个文档,包含图片
  20. Fiddlercore Demo - Fiddler

热门文章

  1. X.509
  2. 性能测试基础---URL和HTTP协议
  3. 17、Learning and Transferring IDs Representation in E-commerce笔记
  4. jmeter设置代理服务器录制脚本
  5. 如何查看自己steam库里游戏是哪个区的
  6. python内置模块笔记(持续更新)
  7. pyinstaller打包多个py文件和去除cmd黑框
  8. SpringCloud微服务
  9. 11-Flutter移动电商实战-首页_屏幕适配方案和制作
  10. 持续集成学习4 jenkins常见功能