简体   繁体   English

插入表中,而不必为每一行指定每个列值

[英]Insert into table without having to specify every column value for each row

How do I insert into a table where the number of column value supplied is different for each row. 如何插入表格,其中每行提供的列值的数量不同。 If there is no value for a column. 如果一列没有值。

CREATE TABLE myTable (column1 VARCHAR(5), column2 VARCHAR(5), column3 VARCHAR(5),
column4 VARCHAR(5), column5 VARCHAR(5), column6 VARCHAR(5))
INSERT INTO myTable ('A','B','C','D'),('A','B','C'),('A','B'),('A'),
('B','C','D'),('C','D','A')

I am fully aware of having the each row inserted by using NULL for the column that is empty. 我完全知道通过对空列使用NULL来插入每一行。

CREATE TABLE myTable (column1, column2, column3, column4, column5, column6)
INSERT INTO myTable ('A','B','C','D',NULL,NULL),('A','B','C',NULL,NULL,NULL),
('A','B',NULL,NULL,NULL,NULL),('A',NULL,NULL,NULL,NULL,NULL),
('B','C','D',NULL,NULL,NULL),('C','D','A',NULL,NULL,NULL)

Is there a way to insert each row without having to specifying the column as NULL if there column value is not supplied? 如果没有提供列值,是否有一种方法可以插入每一行而不必将列指定为NULL?

You can insert them separately: 您可以将它们分别插入:

insert into mytable (col1, col2, col3, col4) values ('A', 'B', 'C', 'D');
insert into mytable (col1, col2, col3) values ('A', 'B', 'C'), ('D', 'E', 'F');
insert into mytable (col3, col4) values ('C', 'D');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM