DB2数据库对现有表格字段修改

数据库表:Websites

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 菜鸟教程      | http://www.runoob.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
+----+--------------+---------------------------+-------+---------+

1.添加字段

对已经存在的表格添加字段:

alter table [table_name] add [column_name] [column_type]

例如:

alter table Websites add country1 varchar(30) ;
reorg table Websites;

2.更改字段类型

alter table  [table_name] alter column [column_name] set data type  [column_type] 

例如:

alter table Websites alter column country set data type varchar(60);
reorg table Websites;

注意: 更改字段类型是有限制的,如将字段改为比之前类型长度大的可以,如果要改小或者修改小数点长度,必须先drop掉原来的column,然后再重新添加.

例如我要将一个varchar(10)的column改为varchar(6) 或者将一个DECIMAL(16, 2)的column改为DECIMAL(16, 4)等,均不能使用上述语句修改,另外改为不同的类型,也需要先drop掉column。

3.去掉字段

alter table [table_name] drop column [column_name]

注意: drop掉字段之后,可能会导致表查询/插入操作不能执行,需要对表进行reorg
例如:

alter table Websites drop column country;
reorg table Websites;

4.为字段添加默认值

alter table [table_name] alter column [column_name] set default [value]

例如:

alter table Websites alter column country set default "CN";
reorg table Websites;

5. 添加带默认值的字段

alter table [table_name] add column [column_name] [column_type] not null with default [value]

例如:

alter table Websites add column country varchar(60) not null with default "CN";
reorg table Websites;

6. 设置字段默认时间为当前时间

alter table [table_name] alter column [column_name] set default  current date; 

例如:

alter table Websites alter column country  set default  current date;
reorg table Websites;
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值