Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INSERT INTO table VALUES.. vs INSERT INTO table SET Error

I'm trying to run the following query:

"insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234567890';"

The query looks good to me and it prints a warning:

1 row(s) affected, 2 warning(s): 1364 Field 'country' doesn't have a default value 1292 Truncated incorrect DOUBLE value: 'http://google.com'

And the info isn't stored as expected:

id , ref , bnid, source, country
'5', NULL, NULL, '0'   , ''

If I run the normal syntax like:

insert into visits (source,country,ref) values('http://google.com','en','1234567890');

I get the expected result:

'6', '1234567890', NULL, 'http://google.com', 'en'

The first syntax (insert set) was working in previous server. Now I changed to one with cpanel and it doesn't. It's the second time this week I get this problem in two different VPS with cpanel so I'm guessing it should be the version number or mysql config.

Mysql Version: 5.1.63-cll

Table:

CREATE TABLE `visits` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ref` varchar(64) DEFAULT NULL,
  `bnid` int(11) DEFAULT NULL,
  `source` varchar(256) DEFAULT NULL,
  `country` varchar(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

Any help?

like image 696
jribeiro Avatar asked Jul 05 '12 17:07

jribeiro


People also ask

What is the difference between insert and insert into in SQL?

If you are using Insert or Insert into both will insert the data in Table. However Insert into is basically used to fatch the data from another table using select command and insert into table where you want to insert the data.

Can we use set with insert query in SQL?

MySQL INSERT used to insert a specific value To insert values into the columns, we can use the SET clause instead of the VALUES clause. INSERT INTO Customers SET ID=2, FirstName='User2'; In the output, the statement inserts values into the columns based on the explicitly specified values in the SET clause.

Which is faster insert into or select into?

INTO' creates the destination table, it exclusively owns that table and is quicker compared to the 'INSERT … SELECT'. Because the 'INSERT … SELECT' inserts data into an existing table, it is slower and requires more resources due to the higher number of logical reads and greater transaction log usage.

What is the SQL clause that inserts values into a table?

The INSERT INTO statement is used to insert new records in a table.


1 Answers

Remove 'and' from the insert query

insert into visits set source = 'http://google.com' , country = 'en' , ref = '1234567890'
like image 122
Sabeen Malik Avatar answered Sep 21 '22 03:09

Sabeen Malik