开发者

Adding a new type of field to an existing table

开发者 https://www.devze.com 2023-04-01 13:19 出处:网络
I have a table Details It has 2 fields Name and Country. It currently has 5 rows, with the values of Country as A,B,B,C,D. I need to add a new field Stat开发者_StackOverflowe for the rows that Country

I have a table Details It has 2 fields Name and Country. It currently has 5 rows, with the values of Country as A,B,B,C,D. I need to add a new field Stat开发者_StackOverflowe for the rows that Country is B . Is this possible?

Thanks


You cannot add one column for a single row, that's not how relational databases work. However, you can either add a column for the whole table and set to null the rows where you don't have any data, or create a separate table to store your states.

For instance:

CREATE TABLE state (detail_id integer, state_name varchar(50));
INSERT INTO state (detail_id, state_name) VALUES ((SELECT detail_id
                                                     FROM details
                                                    WHERE Country = 'B'
                                                    LIMIT 1),
                                                  'Some State');


A table's design is fixed, regardless of the data. If you want to add a column, it will apply to all rows.

ALTER TABLE `Details` ADD COLUMN `State` VARCHAR(12) NULL; -- 12 was just a guess
0

精彩评论

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