开发者

How to add a new field and set all values = '1' in MySQL?

开发者 https://www.devze.com 2023-03-05 13:54 出处:网络
I want ad开发者_运维问答d a new field in my database, and set all rows values = \'1\'. How to do it correctly?

I want ad开发者_运维问答d a new field in my database, and set all rows values = '1'.

How to do it correctly?

ALTER TABLE `cxt_20110105` ADD COLUMN tbn INT(1) SET tbn = '1'


Concering the docu:

ALTER TABLE cxt_20110105 ADD COLUMN tbn INT(1) DEFAULT '1'


UPDATE TABLE cxt_20110105 SET tbn = 1;


Just be carefull that

ALTER TABLE cxt_20110105 ADD COLUMN tbn INT(1) DEFAULT '1'

will result in every row added hereafter without a value for tbn being set to '1'

if you're just using it as initial value and dont want future rows to default to a value you can do this after:

alter table cxt_20110105 change tbn tbn int(1)

If you're using MyISAM, it is a fast operation.

0

精彩评论

暂无评论...
验证码 换一张
取 消