您好,欢迎访问一九零五行业门户网

修改表结构的sql语句是什么

在mysql中,可以使用“alter table”语句修改表结构,只需要使用“alter table 表名称 add/drop/alter 字段”语句即可。“alter table”语句用于在已有的表中添加、修改或删除列。
本教程操作环境:windows7系统、mysql8版本、dell g3电脑。
alter table 语句
alter table 语句用于在已有的表中添加、修改或删除列。
sql alter table 语法
如需在表中添加列,请使用下列语法:
alter table table_nameadd column_name datatype
要删除表中的列,请使用下列语法:
alter table table_name drop column column_name
注释:某些数据库系统不允许这种在数据库表中删除列的方式 (drop column column_name)。
要改变表中列的数据类型,请使用下列语法:
alter table table_namealter column column_name datatype
实例:
create table `login_user` ( `id` int(32) not null auto_increment, `name` varchar(225) character set utf8 collate utf8_general_ci default null comment '名字', `password` varchar(26) default null comment '密码3', `type` varchar(32) default null, `state` varchar(32) default null, `create_time` datetime default null, `update_time` datetime default null, `password5` varchar(26) default null comment '密码5', primary key (`id`)) engine=innodb auto_increment=5 default charset=utf8;
1.修改字段:一般修改属性和数据类型
alter table login_user modify password varchar(25) default null comment '密码2'
2.重命名字段:alter table 表名 change 老字段 新字段 数据类型 [属性][位置];
alter table login_user change password2 password varchar(26) default null comment '密码3'
3.新增字段:alter table 表名 add [column] 字段名 数据类型 [列属性][位置]
位置:字段可以存放在表中的任意位置;
first:第一个位置;
after:在哪个字段之后;默认在最后一个字段的后面。
--添加到最后
alter table login_user add password3 varchar(26) default null comment '密码4'
--添加到指定字段后面 alter table + 表名 + add + 要添加的字段 字段类型 + after + 要跟随的字段名
alter table login_user add password6 varchar(26) default null comment '密码6' after password
4.删除字段:alter table 表名 drop 字段名;
alter table login_user drop password5
相关学习推荐:mysql教程(视频)
以上就是修改表结构的sql语句是什么的详细内容。
其它类似信息

推荐信息