cj333
cj333

Reputation: 2609

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

I want add 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'

Upvotes: 2

Views: 8094

Answers (3)

Fernando Fabreti
Fernando Fabreti

Reputation: 4366

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.

Upvotes: 5

strauberry
strauberry

Reputation: 4199

Concering the docu:

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

Upvotes: 6

HaloWebMaster
HaloWebMaster

Reputation: 928

UPDATE TABLE cxt_20110105 SET tbn = 1;

Upvotes: 4

Related Questions