使用 INSERT INTO 插入多行 VALUES 表示不同的 table headers
Insert multiple rows with VALUES meant for different table headers using INSERT INTO
我不知道问这个问题会不会很奇怪,但我遇到了这个我试图弄清楚的问题。我试图在网上浏览任何示例(没有找到任何示例),因此导致我在这个论坛中提问。
问题来了:
我有一个 SQL table 看起来像这样:
CREATE TABLE product
(
productName VARCHAR(100) PRIMARY KEY NOT NULL,
productDescription VARCHAR(1000) NOT NULL,
"weight" INTEGER NULL,
volume INTEGER NULL,
sizeInformation VARCHAR(1000) NULL,
brandName VARCHAR(100) NOT NULL,
FOREIGN KEY (brandName) REFERENCES brand(brandName)
);
我有一个 INSERT
语句,如下所示:
BEGIN TRANSACTION;
INSERT INTO product
VALUES ("Power Gel Laundry Detergent - Anti-Bacterial", "Anti-Bacterial", 10, "Dynamo"),
("Power Gel Laundry Detergent - Anti-Bacterial", "Anti-Bacterial", 15, "CloroxLOL");
COMMIT;
所以对于第一个 INSERT VALUES
语句(编号为 10),我想将该编号放在 10进入 product(weight)
,对于第二个 INSERT VALUES
语句(编号为 15),我想将该编号放入 15 到 product(volume)
.
有什么办法吗?
我想你想要:
insert into product (productName, productDescription, weight, volume, brandName)
values
('Power Gel Laundry Detergent - Anti-Bacterial', 'Anti-Bacterial', 10, null, 'Dynamo'),
('Power Gel Laundry Detergent - Anti-Bacterial', 'Anti-Bacterial', null, 15, 'CloroxLOL');
想法是将两列都包含在目标列表中,并使用 null
值调整值列表。
您还可以有两个单独的 inserts
语句,具有不同的目标列列表(尽管输入的时间更长):
insert into product (productName, productDescription, weight, brandName)
values ('Power Gel Laundry Detergent - Anti-Bacterial', 'Anti-Bacterial', 10, null, 'Dynamo');
insert into product (productName, productDescription, volume, brandName)
values ('Power Gel Laundry Detergent - Anti-Bacterial', 'Anti-Bacterial', null, 15, 'CloroxLOL');
备注:
始终在 insert
语句中枚举目标列的列表 - 如果没有目标列表,数据库希望您提供值 所有列 ;您的原始查询已经无效(有 4 个值,而您的 table 有 6 列)
对文字字符串使用单引号而不是双引号(虽然有些数据库支持双字符串)——这是 SQL 标准,所有数据库都支持
在定义数据库对象(tables,列)时避免使用双引号:它们使事情变得更复杂,通常没有任何好处
我不知道问这个问题会不会很奇怪,但我遇到了这个我试图弄清楚的问题。我试图在网上浏览任何示例(没有找到任何示例),因此导致我在这个论坛中提问。
问题来了:
我有一个 SQL table 看起来像这样:
CREATE TABLE product
(
productName VARCHAR(100) PRIMARY KEY NOT NULL,
productDescription VARCHAR(1000) NOT NULL,
"weight" INTEGER NULL,
volume INTEGER NULL,
sizeInformation VARCHAR(1000) NULL,
brandName VARCHAR(100) NOT NULL,
FOREIGN KEY (brandName) REFERENCES brand(brandName)
);
我有一个 INSERT
语句,如下所示:
BEGIN TRANSACTION;
INSERT INTO product
VALUES ("Power Gel Laundry Detergent - Anti-Bacterial", "Anti-Bacterial", 10, "Dynamo"),
("Power Gel Laundry Detergent - Anti-Bacterial", "Anti-Bacterial", 15, "CloroxLOL");
COMMIT;
所以对于第一个 INSERT VALUES
语句(编号为 10),我想将该编号放在 10进入 product(weight)
,对于第二个 INSERT VALUES
语句(编号为 15),我想将该编号放入 15 到 product(volume)
.
有什么办法吗?
我想你想要:
insert into product (productName, productDescription, weight, volume, brandName)
values
('Power Gel Laundry Detergent - Anti-Bacterial', 'Anti-Bacterial', 10, null, 'Dynamo'),
('Power Gel Laundry Detergent - Anti-Bacterial', 'Anti-Bacterial', null, 15, 'CloroxLOL');
想法是将两列都包含在目标列表中,并使用 null
值调整值列表。
您还可以有两个单独的 inserts
语句,具有不同的目标列列表(尽管输入的时间更长):
insert into product (productName, productDescription, weight, brandName)
values ('Power Gel Laundry Detergent - Anti-Bacterial', 'Anti-Bacterial', 10, null, 'Dynamo');
insert into product (productName, productDescription, volume, brandName)
values ('Power Gel Laundry Detergent - Anti-Bacterial', 'Anti-Bacterial', null, 15, 'CloroxLOL');
备注:
始终在
insert
语句中枚举目标列的列表 - 如果没有目标列表,数据库希望您提供值 所有列 ;您的原始查询已经无效(有 4 个值,而您的 table 有 6 列)对文字字符串使用单引号而不是双引号(虽然有些数据库支持双字符串)——这是 SQL 标准,所有数据库都支持
在定义数据库对象(tables,列)时避免使用双引号:它们使事情变得更复杂,通常没有任何好处