Mysql 常用命令

(1)登录
mysql -h 主机名 -u 用户名 -p 密码

(2)显示所有数据库
show databases;

(3)进入指定数据库
use 库名; 例如:use mysql;

(4)查看库中所有表
show tables from 库名; 例如:show tables; show tables from mysql;

(5)查看当前库
select database();

(6)查看版本
进入mysql客户端之前:mysql -version 或 mysql -V
进入mysql客户端之后:select version();

(7)新建一个数据库
create database spark(库名);

(8)建表
create table customer(id varchar(30),age int,name varchar(30),birthday date);

(9)查看表结构
desc customer;

(10)查看表
select * from customer;

(11)查询特定列
select id ,name,sex from student;

(12)查询数据并进行过滤
select id ,name,sex from student where id =>1003;

(13)运算符查询
select id,name,sex from student where age >=18 and age <=35;
select id,name,sex from student where age between 18 and 35;

(14)查询变种
mysql > select * from student where age =18 or age =35 or age =23
mysql > select * from student where age in (18,23,35);
mysql > select * from student where name like ‘%小%‘;
mysql > select * from student where name like ‘_明%‘;
mysql > select * from student where email is null;
mysql > select * from student where email is not null;
mysql > select * from student order by age ; //升序
mysql > select * from student order by age desc ; //降序
提示:%匹配任意多个字符,_只匹配一个字符

(15)插入数据
insert into student(id,name)values(1001,‘小明‘);

(16)修改表
update student set name=‘小红‘ where id = 1001;

(17)删除表记录
delete from student where id = 1001;

(18)删除表
drop table customer;

(19)退出
一共三种方式:
mysql > exit;
mysql > quit;
ctrl+C

Mysql 常用命令

上一篇:mysql 8.0 改变数据目录和日志目录


下一篇:MySQL | 基本查询