基本概念解析
数据表由行和列组成,专业术语中:
- 字段(field):表的纵向列结构
- 记录(record):表的横向行数据
字段添加方法详解
mysql支持三种字段添加位置,语法格式均通过alter table指令实现:
一、末尾追加字段
alter table 表名 add 新字段名 数据类型;
操作示例:
- 创建基础表结构
create table student ( id int(4), name varchar(20), sex char(1) );
- 追加年龄字段
alter table student add age int(4);
- 验证结构
| field | type | null | key | |-------|-------------|------|-----| | id | int(4) | yes | | | name | varchar(20) | yes | | | sex | char(1) | yes | | | age | int(4) | yes | |
二、首列插入字段
alter table 表名 add 新字段名 数据类型 first;
操作示例:
alter table student add stuid int(4) first;
结构验证:
| stuid | int(4) | yes | | | id | int(4) | yes | |
三、指定位置插入字段
alter table 表名 add 新字段名 数据类型 after 目标字段;
操作示例:
alter table student add stuno int(11) after name;
结构验证:
| name | varchar(20) | yes | | | stuno | int(11) | yes | |
附:mysql使用sql指定位置添加字段、删除字段
-- 在jobintenation_info_flag后面添加这三个字段 alter table t_resume add column resume_product_flag tinyint(1) not null default 0 comment '简历作品记录,0:未完成,1:完成' after jobintenation_info_flag, add self_introduce_flag tinyint(1) not null default 0 comment '自我简介记录,0:未完成,1:完成' after jobintenation_info_flag, add language_ability_flag tinyint(1) not null default 0 comment '语言能力记录,0:未完成,1:完成' after jobintenation_info_flag alter table t_resume drop column language_ability_flag; alter table t_resume drop column self_introduce_flag; alter table t_resume drop column resume_product_flag;
总结
到此这篇关于mysql数据表添加字段的三种方式总结的文章就介绍到这了,更多相关mysql数据表添加字段内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论