0

I have a query like

INSERT INTO support_users VALUES("1",,"2","1","4",,,"2017-05-06 20:24:36");

but new MySQL not inserting given error Error Code: 1064

but I changed it to

INSERT INTO support_users VALUES("1","","2","1","4",,,"2017-05-06 20:24:36");

working

but previous MySQL not having such an issue.how to solve that. without changing query

table definition

CREATE TABLE support_users (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `client` int(11) DEFAULT NULL,
  `user` int(11) DEFAULT NULL,
  `ticket` int(11) DEFAULT NULL,
  `scope` int(11) DEFAULT NULL,
  `notify` tinyint(1) DEFAULT NULL,
  `email` tinyint(1) DEFAULT NULL,
  `added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=46 DEFAULT CHARSET=utf8;
Kulshreshth K
  • 1,096
  • 1
  • 12
  • 28

2 Answers2

3

Firstly, respect the Datatype. If its Integer don't use quotes.

The right way to approach your problem will be use of NULL. So this works across any version of MySQL.

INSERT INTO support_users VALUES(1,NULL,2,1,4,NULL,NULL,'2017-05-06 20:24:36');
Martin
  • 22,212
  • 11
  • 70
  • 132
Ganesh Chandrasekaran
  • 1,578
  • 12
  • 17
1

1)

Integer column values should not be quoted.

So "1" becomes 1

2)

Auto Increment values should not be manally given to the SQL -- let it work it out itself.

3)

Inserting NULL using the NULL keyword.

4)

Your date-time column is set in its definition to show the current date-time of the row being inserted when it is inserted. As you are inserting (rather than updating, etc.) you therefore do not need to include this data.

5)

It is easier for your sanity to also list which columns are being added to with the INSERT instruction.

6)

Use single quotes ' around string data not double quotes ("), when working directly with MySQL.

Result:

INSERT INTO support_users( `id`, `ticket`, `scope` , `notify`)     
       VALUES (1, 2,1,4);

This will cause errors if you try to insert twice, because you are forcing the Primary Key (id) to be 1. To avoid this, skip the Auto Increment (id) column value.


Or for clarity only; the full SQL:

INSERT INTO support_users VALUES (1, NULL, 2, 1, 4, NULL, NULL, '2017-05-06 20:24:36');

Skipping the Auto Increment (id) column value (as referenced above):

INSERT INTO support_users VALUES (NULL, NULL, 2, 1, 4, NULL, NULL, '2017-05-06 20:24:36');
Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132