[英]Adding a new type of field to an existing table
I have a table Details
It has 2 fields Name
and Country
. 我有一个表格
Details
它有2个字段Name
和Country
。 It currently has 5 rows, with the values of Country
as A,B,B,C,D. 它当前有5行,“
Country
”值为A,B,B,C,D。 I need to add a new field State
for the rows that Country
is B . 我需要为
Country
为B的行添加一个新的字段State
。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.