Search code examples
mysqlinsertauto-incrementinsertion

Insert query in mysql with an auto_increment field


Ok,

I am sure I am doing something wrong here but I can't for the life of me figure it out.

Here is my table

CREATE TABLE `email_queue` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `from` varchar(256) DEFAULT NULL,
  `to` varchar(4182) DEFAULT NULL,
  `cc` varchar(4182) DEFAULT NULL,
  `subject` varchar(4182) DEFAULT NULL,
  `body` varchar(4182) DEFAULT NULL,
  `status` varchar(64) DEFAULT NULL,
  `attempts` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) 

When I do a

insert into email_queue values (1,'','','','','','',0);

it works fine and inserts the blank values

but when I try to insert partial values using

insert into email_queue(to) values('sample_to_name');

it errors out saying ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to) v alues('sample_to_name')' at line 1

What am I doing wrong?


Solution

  • to is mysql reserved word should be in backticks.

    Either avoid the creating column names with Reserved words or enclose them with backtick ``

    insert into email_queue(`to`) values('sample_to_name');