表的增删改查
一、表介绍
表相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段。
二、创建表
语法:
create tableb 表名(字段名1 类型[(宽度)约束条件],字段名2 类型[(宽度)约束条件],字段名3 类型[(宽度)约束条件],......);
注意:
1、在同一张表中,字段名是不能相同
2、宽度和约束条件可选
3、字段名和类型是必须的
三、查看表
语法:
show create table db1.t1;或show create table db1.t1\G #(G代表用行显示不用表格显示)
desc db1.t1;
四、修改表结构
语法:
1.修改表名:
alter table 表名 rename 新表名
2.增加字段
alter table 表名
add 字段名 数据类型 [完整性约束条件......],
add 字段名 数据类型 [完整性约束条件......];
alter table 表名
add 字段名 数据类型 [完整性约束条件......] first;
alter table 表名
add 字段名 数据类型 [完整性约束条件......] after 字段名;
3.删除字段
alter table 表名
drop 字段名;
4.修改字段
alter table 表名
modify 字段名 数据类型[完整性约束条件.......];
alter table 表名
change 旧字段名 新字段名 旧数据类型[完整性约束条件.......];
alter table 表名
change 旧字段名 新字段名 新数据类型[完整性约束条件.......];
五、复制表
复制表结构+记录(key不会复制:主键、外键和索引)
create table new_service select * from service;
只复制表结构
select * from service where 1=2; //条件为假,查不到任何记录
Empty set (0.00 sec)
create table new1_service select * from service where 1=2;
Records:0 Duplicates:0 Warnings:0
create table t4 like employees;
六、删除表
drop table 表名;
浙公网安备 33010602011771号