MySQL 列的操作

1.建表语句

 

create table 表名(
列名称 列类型 [列属性] [默认值],   ---》列声明
列名称 列类型 [列属性] [默认值],
列名称 列类型 [列属性] [默认值]
);

create table stu (name varchar(10),class varchar(10),score tinyint);

insert stu
(name,class,score)
values
("zhangsan","shuxue",50),
("zhangsan","dili",40),
("zhangsan","shuxue",90),
("lisi","yuwen",55),
("lisi","zhengzhi",45),
("wangwu","zhengzhi",30);

#把    列名称 列类型 [列属性] [默认值]    封装成    列声明
后面就用“ 列声明 ”来代替“ 列名称 列类型 [列属性] [默认值] ”

2.增加一个列

alter table 表名 add 列声明;
alter table stu add height tinyint unsigned not null default 180;
增加的列默认是在表的最后一列

可以用after 来声明新增的列在哪一些后面:
alter table 表名 add 列声明 after 列名;
alter table stu add age tinyint unsigned not null default 18 after score;

如果要放在表的最前面:
alter table 表名 add 列声明 first;
alter table stu add id int primary key auto_increment first;
primary key auto_increment(设为主键,并自动增加)

3.修改列

 

alter table 表名 change 被修改的列 新的列声明
alter table stu change height height smallint unsigned not null default 180;
alter table stu change height weight smallint unsigned not null default 180;

4.删除列

 

alter table 表名 drop 列名
alter table stu drop age;

 

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值