无法使用shell将值插入mysql表

时间:2019-12-26 05:12:10

标签: mysql

我正在研究shell中的mysql,并尝试使用INSERT INTO向表中插入值。...但出现此错误。

ERROR 1136 (21S01): Column count doesn't match value count at row 1

我的桌子:

enter image description here

这就是我要插入值的方式:

INSERT INTO clients VALUES 
('Maria Dolores Gomez','Maria Dolores.95983222J@random.names','1971-06-06','F',1,'2018-04-09 16:51:30'),
('Adrian Fernandez','Adrian.55818851J@random.names','1970-04-09','M',1,'2018-04-09 16:51:30'),
('Maria Luisa Marin','Maria Luisa.83726282A@random.names','1957-07-30','F',1,'2018-04-09 16:51:30'),
('Pedro Sanchez','Pedro.78522059J@random.names','1992-01-31','M',1,'2018-04-09 16:51:30');

4 个答案:

答案 0 :(得分:3)

您为VALUES子句数据集中的每个记录指定6个值,而表结构包含8个字段。

您必须指定6个字段,这些字段是您在查询中为以下字段指定的值:

INSERT INTO clients (name, email, birthdate, gender, active, created_at)
VALUES 
('Maria Dolores Gomez','Maria Dolores.95983222J@random.names','1971-06-06','F',1,'2018-04-09 16:51:30'),
('Adrian Fernandez','Adrian.55818851J@random.names','1970-04-09','M',1,'2018-04-09 16:51:30'),
('Maria Luisa Marin','Maria Luisa.83726282A@random.names','1957-07-30','F',1,'2018-04-09 16:51:30'),
('Pedro Sanchez','Pedro.78522059J@random.names','1992-01-31','M',1,'2018-04-09 16:51:30');

或者,您可以为VALUES子句中未列出的字段指定“占位符”值。 NULL或DEFAULT表示自动递增,而DEFAULT表示updated_at:

INSERT INTO clients 
VALUES 
(NULL,'Maria Dolores Gomez','Maria Dolores.95983222J@random.names','1971-06-06','F',1,'2018-04-09 16:51:30',DEFAULT),
(NULL,'Adrian Fernandez','Adrian.55818851J@random.names','1970-04-09','M',1,'2018-04-09 16:51:30',DEFAULT),
(NULL,'Maria Luisa Marin','Maria Luisa.83726282A@random.names','1957-07-30','F',1,'2018-04-09 16:51:30',DEFAULT),
(NULL,'Pedro Sanchez','Pedro.78522059J@random.names','1992-01-31','M',1,'2018-04-09 16:51:30',DEFAULT);

PS。我更喜欢前一种方法。后者受任何表结构更改的影响,而如果您要更改表结构中的字段顺序或向其中添加可空字段,则前者可能保持不变。

答案 1 :(得分:3)

添加字段。

INSERT INTO clients (name, email, birthdate, gender, active, created_at) VALUES 
('Maria Dolores Gomez','Maria Dolores.95983222J@random.names','1971-06-06','F',1,'2018-04-09 16:51:30'),
('Adrian Fernandez','Adrian.55818851J@random.names','1970-04-09','M',1,'2018-04-09 16:51:30'),
('Maria Luisa Marin','Maria Luisa.83726282A@random.names','1957-07-30','F',1,'2018-04-09 16:51:30'),
('Pedro Sanchez','Pedro.78522059J@random.names','1992-01-31','M',1,'2018-04-09 16:51:30');

答案 2 :(得分:2)

您已将update_at列指定为默认值current_timestamp,并在default脚本上为自动递增字段和insert列添加了update_at。 / p>

insert into clients values
(default, 'Maria Dolores Gomez','Maria Dolores.95983222J@random.names','1971-06-06','F',1,'2018-04-09 16:51:30', default),
(default, 'Adrian Fernandez','Adrian.55818851J@random.names','1970-04-09','M',1,'2018-04-09 16:51:30', default),
(default, 'Maria Luisa Marin','Maria Luisa.83726282A@random.names','1957-07-30','F',1,'2018-04-09 16:51:30', default),
(default, 'Pedro Sanchez','Pedro.78522059J@random.names','1992-01-31','M',1,'2018-04-09 16:51:30', default);

请参见dbfiddle进行测试。

答案 3 :(得分:0)

我希望这对您有帮助:-

insert into clients(`name`,`email`,`birthdate`,`gender`,`active`,`created_at`,`updated_at`) VALUES
('Maria Dolores Gomez','Maria5983222J@random.names','1971-06-06','F',1,'2018-04-09 16:51:30','2018-04-09 16:51:30');