必備Mysql命令

文章來源:https://macrozheng.github.io/mall-learning/#/reference/mysqlmysql

 

開發者必備Mysql命令

開發者必備Mysql經常使用命令,涵蓋了數據定義語句、數據操縱語句及數據控制語句,基於Mysql5.7。git

數據定義語句(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
相關文章
相關標籤/搜索