MySQL常用命令(Linux环境)

一、数据定义语句(DDL)

数据库操作

●登录数据库:

bash 复制代码
mysql -uroot -proot

●创建数据库:

bash 复制代码
create database test

●查看所有数据库:

bash 复制代码
show databases

●选择数据库并使用:

bash 复制代码
use test

●查看所有数据表:

bash 复制代码
show tables

●删除数据库:

bash 复制代码
drop database test

表操作

●创建表:

bash 复制代码
create table emp(ename varchar(10),hiredate date,sal decimal(10,2),deptno int(2))

create table dept(deptno int(2),deptname varchar(10))

●查看表的定义:

bash 复制代码
desc emp

●查看表定义(详细):

bash 复制代码
show create table emp \G

●删除表:

bash 复制代码
drop table emp

●修改表字段:

bash 复制代码
alter table emp modify ename varchar(20)

●添加表字段:

bash 复制代码
alter table emp add column age int(3)

●删除表字段:

bash 复制代码
alter table emp drop column age

●字段改名;

bash 复制代码
alter table emp change age age1 int(4)

●修改表名:

bash 复制代码
alter table emp rename emp1

二、数据操纵语句(DML)

插入记录

●指定名称插入:

bash 复制代码
insert into emp (ename,hiredate,sal,deptno) values ('zhangsan','2018-01-01','2000',1)

●不指定名称插入:

bash 复制代码
insert into emp values ('lisi','2018-01-01','2000',1)

●批量插入数据:

bash 复制代码
insert into dept values(1,'dept1'),(2,'dept2')

修改记录

bash 复制代码
update emp set sal='4000',deptno=2 where ename='zhangsan'

删除记录

bash 复制代码
delete from emp where ename='zhangsan'

查询记录

●查询所有记录:

bash 复制代码
select * from emp

●查询不重复的记录:

bash 复制代码
select distinct deptno from emp

●条件查询:

bash 复制代码
select * from emp where deptno=1 and sal<3000

●排序和限制:

bash 复制代码
select * from emp order by deptno desc limit 2

●分页查询(查询从第0条记录开始10条):

bash 复制代码
select * from emp order by deptno desc limit 0,10

●聚合(查询部门人数大于等于1的部门编号):

bash 复制代码
select deptno,count(deptno) from emp group by deptno having count(deptno) >= 1

●连接查询:

bash 复制代码
select * from emp e left join dept d on e.deptno=d.deptno

●子查询:

bash 复制代码
select * from emp where deptno in (select deptno from dept)

●记录联合:

bash 复制代码
select deptno from emp union select deptno from dept

数据控制语句(DCL)

权限相关

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

bash 复制代码
grant select,insert on test.* to 'test'@'localhost' identified by '123'

●查看账号权限:

bash 复制代码
show grants for 'test'@'localhost'

●收回操作权限:

bash 复制代码
revoke insert on test.* from 'test'@'localhost'

●授予所有数据库的所有权限:

bash 复制代码
grant all privileges on *.* to 'test'@'localhost'

●授予所有数据库的所有权限(包括grant):

bash 复制代码
grant all privileges on *.* to 'test'@'localhost' with grant option

●授予SUPER、PROCESS、FILE权限(系统权限不能指定数据库):

bash 复制代码
grant super,process,file on *.* to 'test'@'localhost'

●只授予登录权限:

bash 复制代码
grant usage on *.* to 'test'@'localhost'

●刷新权限并立即生效:

bash 复制代码
flush privileges

帐号相关

●删除账号:

bash 复制代码
drop user 'test'@'localhost'

●修改自己的密码:

bash 复制代码
set password = password('123')

●管理员修改他人密码:

bash 复制代码
set password for 'test'@'localhost' = password('123')

其他

字符集相关

●查看字符集:

bash 复制代码
show variables like 'character%'

●创建数据库时指定字符集:

bash 复制代码
create database test2 character set utf8

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

bash 复制代码
show variables like "%time_zone%"

●修改mysql全局时区为北京时间,即我们所在的东8区:

bash 复制代码
set global time_zone = '+8:00';

●修改当前会话时区:

bash 复制代码
set time_zone = '+8:00'
相关推荐
AAA修煤气灶刘哥1 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
程序新视界1 天前
学习MySQL绕不开的两个基础概念:聚集索引与非聚集索引
mysql
RestCloud2 天前
跨境数据传输:ETL如何处理时区与日期格式差异
mysql·api
RestCloud2 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
少妇的美梦2 天前
logstash教程
运维
chen9452 天前
k8s集群部署vector日志采集器
运维
chen9452 天前
aws ec2部署harbor,使用s3存储
运维
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
xiaok2 天前
mysql中怎么创建一个可控权限数据库账号密码给到开发者
mysql
轻松Ai享生活2 天前
5 节课深入学习Linux Cgroups
linux