您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

mysql怎么增加列(字段)

2024/3/11 8:07:24发布24次查看
在mysql中,可以使用“alter table”语句和add关键字来增加列(字段),语法为“alter table 表名 add 新字段名 数据类型[约束条件];”;这种语法格式会在数据表的末尾添加列(字段)。
本教程操作环境:windows7系统、mysql8版本、dell g3电脑。
mysql 数据表是由行和列构成的,通常把表的“列”称为字段(field),把表的“行”称为记录(record)。随着业务的变化,可能需要在已有的表中添加新的字段。
在mysql中,可以使用“alter table”语句和add关键字来增加列(字段)。
语法:
alter table 表名 add 新字段名 数据类型[约束条件];
对语法格式的说明如下:
表名 :为数据表的名字;
新字段名 :为所要添加的字段的名字;
数据类型 :为所要添加的字段能存储数据的数据类型;
[约束条件] :是可选的,用来对添加的字段进行约束。
这种语法格式默认在表的最后位置(最后一列的后面)添加新字段。
示例:
我们有一个student 数据表,使用 desc 查看 student 表结构
mysql> desc student;+-------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+-------+-------------+------+-----+---------+-------+| id | int(4) | yes | | null | || name | varchar(20) | yes | | null | || sex | char(1) | yes | | null | |+-------+-------------+------+-----+---------+-------+3 rows in set (0.01 sec)
使用 alter table 语句添加一个 int 类型的字段 age,在看看 student 表结构
mysql> alter table student add age int(4);query ok, 0 rows affected (0.16 sec)records: 0 duplicates: 0 warnings: 0mysql> desc student;+-------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+-------+-------------+------+-----+---------+-------+| id | int(4) | yes | | null | || name | varchar(20) | yes | | null | || sex | char(1) | yes | | null | || age | int(4) | yes | | null | |+-------+-------------+------+-----+---------+-------+4 rows in set (0.00 sec)
可以看出student 表已经添加了 age 字段,且该字段在表的最后一个位置,添加字段成功。
那么如果想要在开头或者中间添加字段,需要怎么做?
如果希望在开头位置(第一列的前面)添加新字段,那么需要借助 first 关键字
如果希望在中间位置添加新字段,那么需要借助 after 关键字
语法格式如下:
alter table 表名 add 新字段名 数据类型 [约束条件] first;alter table 表名 add 新字段名 数据类型 [约束条件] after <已经存在的字段名>;
示例:
mysql> alter table student add stuid int(4) first;query ok, 0 rows affected (0.14 sec)records: 0 duplicates: 0 warnings: 0mysql> desc student;+-------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+-------+-------------+------+-----+---------+-------+| stuid | int(4) | yes | | null | || id | int(4) | yes | | null | || name | varchar(20) | yes | | null | || sex | char(1) | yes | | null | || age | int(4) | yes | | null | |+-------+-------------+------+-----+---------+-------+5 rows in set (0.00 sec)
student 表中添加了一个 stuid 字段,且该字段在表中的第一个位置
mysql> alter table student add stuno int(11) after name;query ok, 0 rows affected (0.13 sec)records: 0 duplicates: 0 warnings: 0 mysql> desc student;+-------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+-------+-------------+------+-----+---------+-------+| stuid | int(4) | yes | | null | || id | int(4) | yes | | null | || name | varchar(20) | yes | | null | || stuno | int(11) | yes | | null | || sex | char(1) | yes | | null | || age | int(4) | yes | | null | |+-------+-------------+------+-----+---------+-------+6 rows in set (0.00 sec)
student 表中添加了一个 stuid 字段,且该字段在 name 字段后面的位置
【相关推荐:mysql视频教程】
以上就是mysql怎么增加列(字段)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product