MySQL_数据表命令

数据表操作

关于Mysql的数据类型,点击一下

1.创建数据表:

create [temporapy] table [if not exists] <表名>
[(<字段名> <数据类型> [完整性约束条件] [,...])]
[表的选项];

temporapy表示新创建的表为临时表
表的选项用于描述如何春促引擎、字符集等选项
engine=存储引擎类型
default charset=字符集类型

2. 修改表结构:

1. 增加一个字段 :

//first 代表第一个位置
//after 代表在字段名2之后插入字段名1
alter table 数据表名
add (<新字段><数据类型>[<完整约束条件>][first|after已存在的字段名][,...])

2. 修改字段数据类型

alter table 数据表名
modify column(<字段名><新数据类型>[<完整性约束条件>])

3. 修改字段名

alter table<表名>
change <旧列名><新列名><新数据类型>

4. 修改字段位置

alter table 数据表名
modify 字段名1 数据类型 first;
·······································
alter table 数据表名
modify 字段名1 数据类型 after 字段名;
first 代表第一个位置
after 代表在字段名2之后插入字段名1

5. 删除字段

alter table 数据表名
drop {column <字段名>|<完整性约束>}[,...]
alter table 数据表名
drop column 字段名;

6. 修改数据表名

alter table 数据表名
rename [as] <新数据表名>
alter table 数据表名
rename [to] <新数据表名>

7. 为指定的字段添加索引

alter table 数据表名
add index [索引名] (索引字段,...)

8. 删除索引

alter table 数据表名
drop  index <索引名>;

数据表查看

1.查看数据库中所有表的信息

show tables;
show tables from 数据库名;

2.查看表结构

{describe | desc} <表名> [字段名];
例如:desc abc student;查看abc数据库中student数据表的表结构
或者desc student;查看当前数据库中student数据表的表结构

show columns from 表名;查看表结构

3.查看创建表时所输入的命令

show create table 表名;

4.删除数据表

drop table 表1,表2;

drop table if exists 表1,表2

猜你喜欢

转载自www.cnblogs.com/ninebook/p/12001967.html