MySQL常用命令大全
2020-01-01 本文已影响0人
SCOR_PIO
1、创建数据库
create database + 库名
2、删除数据库
drop database + 库名
3、显示数据库
show databases
4、创建表
create table 表名 (表列名 数据类型 属性/约束(自增、非空)索引(主键,唯一) 注释,............);
创建表添加外键: constrants 外键名 foreign key (字段) references (主表字段)
表类型-----engine:默认innoDB
5、删除表
drop table 表名,表名,表名.....
6、修改表
alter table 表名 modify 字段名 字段属性
alter table 表名 change 原字段名 新字段名 字段属性
删除列名:alter table 表名 drop 字段名
改修表名:alter table 旧表名 rename to 新表名
显示表结构:dese 表名;
显示表名称:show tables
切换数据库:use 数据库名
清空表数据:truncate 表名
7、数据库内容增删改查
增:insert into 表名 (列名)values(列值);
删:delect from 表名
改:update 表名 set 列名 = 列值 ,列名 = 列值 where 条件;
查:select * from 表名 where + 条件
条件:分组:group by ... having ...
排序:order by ... desc/asc
截取条数:limit
8、 创建索引
普通:alter table 表名 add index/key (‘列名’)
9、创建外键
alter table 从表名 add constraint 外键名 foreign key (‘从表列’)references 主表名(‘主表列’)
10、删除索引
alter table 表名 drop index 索引名
11、删除外键
alter table 表名 drop foreign key 索引名
12、显示索引
show index/constraints from table
13、创建事务
set autocommit = 0;
start construction;
sql语句
commit;
rollback;
set autocommit = 1;