MySQL 常用命令

启动 MySQL 服务

1
systemctl start mysqld

查看 MySQL 服务运行状态

1
systemctl status mysqld

关闭 MySQL 服务

1
systemctl stop mysqld

登录 MySQL 服务器

1
mysql -u root -p

退出 mysql> 命令提示窗口的三种方式

1
2
3
mysql> exit;
mysql> quit;
Ctrl + C

显示所有数据库

1
show databases;

进入指定数据库

1
2
3
4
use 库名; 

例如:
use mysql;

查看库中所有表

1
2
3
4
5
show tables from 库名; 

例如:
show tables;
show tables from mysql;

查看当前库

1
select database();

新建数据库

1
2
3
4
create database 数据库名;

例如:
create database test;

删除数据库

1
2
3
4
drop database 数据库名;

例如:
drop database test;

建表

1
create table student(id varchar(30), name varchar(30), age int, birthday date);

查看表结构

1
desc student;

插入数据

1
2
3
insert into student(id, name, age, birthday) values('1001','小明', 20, '2020-01-01');
insert into student(id, name, age, birthday) values('1002','小红', 21, '2020-02-02');
insert into student(id, name, age, birthday) values('1003','小刚', 22, '2020-03-03');

查看表

1
select * from student;

修改表

1
update student set name = '小李' where id = '1001';

删除表记录

1
delete from student where id = '1001';

表新增字段

1
alter table student add age2 int;

表删除字段

1
alter table student drop age2;

表修改字段类型

1
alter table student modify age2 varchar(30);

表修改字段名字

1
alter table student change age2 age3 int;

删除表

1
drop table student;

查询特定列

1
select id, name from student;

查询数据并进行过滤

1
select id, name from student where id = '1003';

运算符查询

1
2
select id, name from student where age >=18 and age <=35;
select id, name from student where age between 18 and 35;

本文参考:https://www.cnblogs.com/szx666/p/13736659.html