開發者必備Mysql命令

摘要

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

數據定義語句(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
複製代碼

公衆號

mall項目全套學習教程連載中,關注公衆號第一時間獲取。git

公衆號圖片
相關文章
相關標籤/搜索