mysql命令大全(速成版)

—————————–操作数据库————————————
进入数据库
mysql -uroot -p

推出数据库
quit

查看数据库
show databases;

创建数据库
create database db_name;

删除数据库
drop datadase db_name;

进入数据库
use database;

查看当前所在数据库
select database();

—————————操作表结构—————————————-
查看数据库下有哪些表
show tables;

创建表
create table 表名(字段 类型(长度),。。。。。)

删除表
drop table 表名

查看表结构
desc 表名

查看建表语句
show create table 表名

修改表字段值
alter table 表名 modify 字段(长度)

修改表字段
alter table 表名 change 原字段 新字段 类型 (长度)

删除表字段
alter table 表名 drop 字段名

插入字段
alter table 表名 add 添加字段

在**之后插入
alter table 表名 add 新字段 类型(长度) after 在那个字段之后

插入最前面
alter table 表名 add 新字段 类型(长度) first

修改表名字
alter table 旧表名 rename 新表名

修改字段顺序
alter table 表名 modify 原来的字段 first 或者 after 在那个字段之后

————————–索引—————————-
主键索引
alter table 表名 add primary key(字段)

唯一索引
alter table 表名 add unique(字段)

普通索引
alter table 表名 add index(字段)

全文索引
alter table 表名 addfulltext(字段)

————————-插入数据—————————–
单个数据
insert into 表名 (字段1,字段2.。。)values (值1,值2,。。。。。)

多个数据插入
insert into 表名 (字段1,字段2.。。)values (值1,值2,。。。。。),(值1,值2,。。。。。)

————————-删除数据—————————–
delete from 表名 where 条件

————————-修改数据—————————–

updata set 表名 字段 = 新值 where 条件

————————查询———————————-
1.特定字段查询
select 字段1,字段2 from 表名

2.去重查询
select distinct(字段) from 表名

3.where 条件查询
select 字段1,字段2 where 条件

4.区间查询
select 字段 from 表名 where 字段 between 21 and 30

5.or查询
select 字段1 , 字段2.。。 from 表名 where 条件1>500 or 条件2 > 30;

6.反查询
select 字段 from 表名 where != 20

7.in查询
select 字段 from 表名 where 字段 in(20,40)

8.模糊查询
select *from 表名 where 字段 like ‘%’

9.and查询
select *from 表名 where 字段 条件 and 条件

10.排序,升序
select *from 表名 order by 字段asc

11.降序
select *from 表名 order by(字段) desc

12.分页
select 字段,字段 from 表名 limit 起始位置,个数

你可能感兴趣的:(数据库)