Insert query in mysql with an auto_increment field - mysql

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?

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');

You need back-ticks around to
insert into email_queue(`to`) values('sample_to_name');

problem is because
to is mysql reserved word

Related

MySQL ERROR 1064 in line, but don't know what is wrong?

I've got the following line I want to execute in MySQL:
CREATE TABLE 'virtual_domains' (
'id' int(11) NOT NULL auto_increment,
'name' varchar(50) NOT NULL,
PRIMARY KEY ('id'))
ENGINE=InnoDB DEFAULT CHARSET=utf8;
However, it gave me this error:
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 ''virtual_domains' ('id' int(11) NOT NULL
auto_increment, 'name' varchar(50) NOT ' at line 1
What am I missing here??
Thanks for the help!
Rob
remove the single quotes around the table and column names. use backticks instead.
CREATE TABLE `virtual_domains` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`))
ENGINE=InnoDB DEFAULT CHARSET=utf8;
In addition to use of backticks (`symbol`), since none of the identifiers you have used require escaping, you can simply remove the escaping altogether:
CREATE TABLE virtual_domains (
id int(11) NOT NULL auto_increment,
name varchar(50) NOT NULL,
PRIMARY KEY (id))
ENGINE=InnoDB DEFAULT CHARSET=utf8;
Alternatively when you do need to escape symbols, instead of using backticks, consider using ANSI compliant quotes ("symbol"). You will need to set SQL_MODE=ANSI_QUOTES:
SET SQL_MODE=ANSI_QUOTES;
CREATE TABLE "virtual_domains" (
"id" int(11) NOT NULL auto_increment,
"name" varchar(50) NOT NULL,
PRIMARY KEY ("id"))
ENGINE=InnoDB DEFAULT CHARSET=utf8;
The benefit of this is improved portability between the various RDBMS.
SqlFiddle here

Mysql throws an error when one table name is not surrounded by single quotes

I'm building a simple app which lists teams and matches. The Team and Match databases were built with the following scripts (I'm using PhpMyadmin):
CREATE TABLE IF NOT EXISTS `Team` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(120) NOT NULL,
`screen_name` varchar(100) NOT NULL,
`sport_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE IF NOT EXISTS `Match` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sport_id` int(11) NOT NULL,
`team_one_id` int(11) NOT NULL,
`team_two_id` int(11) NOT NULL,
`venue` varchar(80) NOT NULL,
`kick_off` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
If i do:
SELECT * FROM Team
The script runs and I get an empty result. But, incredibly, if I do
SELECT * FROM Match
I get the following error:
1064 - 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 'Match' at line 1
Instead, I have to do:
SELECT * FROM `Match`
And it works. I have other tables in the database but this is the only behaving like this. Any ideas why?
match is a reserved word in SQL
Read more here:
https://drupal.org/node/141051
Match is a Function in MySQL therefore you must put the quotes around it.
You need encapsulate it in quotes because Match is a keyword.
See key word list

Why this query throwing error while inserting record

i have created one table using following query
CREATE TABLE `events` (
`event_id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`event_name` varchar(100) NOT NULL,
`description` text,
`event_date` datetime NOT NULL,
`repeat` tinyint(4) NOT NULL,
`share` varchar(100) DEFAULT NULL,
`share_type` varchar(50) NOT NULL,
PRIMARY KEY (`event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
and now i am trying to insert record using this query
INSERT INTO events (username,event_name,description,event_date,repeat,share,share_type) VALUES ('bhavik','Will go home','','2012-11-11 18:10','0','','public');
error i am getting
#1064 - 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 'username,event_name,description,event_date,repeat,share,share_type) VALUES ('bha' at line 1
repeat is a reserved keyword
use `repeat` in the insert statement
INSERT INTO events (username,event_name,description,event_date,`repeat`,share,share_type) VALUES ('bhavik','Will go home','','2012-11-11 18:10','0','','public');

Error 1064 <42000> on mysql when i'm trying to create table

CREATE TABLE `photos` (
`title` varchar(255) not null,
`id` int(11) not null,
`ph_path` varchar(255) not null,
`description` varchar(255) not null,
`privilange` varchar(20) not null,
`owner` varchar(60) not null,
`provoles` int(11),
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=greek;
I'm getting error 1064 <4200> and I'm not sure what is wrong.
You have a trailing comma in the primary key:
PRIMARY KEY (`id`), <--- remove that
The full error would read something like:
check the manual that corresponds to your MySQL server version for the right syntax to use near ') ENGINE=InnoDB
In MySQL, the position pointed to by the error message (near ')ENGINE) will show you the character immediately after where the error occurred. Look to the previous thing in your statement and you'll find your syntax error.
You will have to remove the comma after PRIMARY KEY (`id`).

Why is my Update statement not working?

Using the following query, I can't figure out why my update is not working. I'm sure its something stupid, but any help is greatly appreciated:
UPDATE Mail
SET From="Spouse"
WHERE ItemNum=9;
The Error is:
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 'From="Spouse" WHERE ItemNum=9' at line 1
The schema of Mail is:
CREATE TABLE `Mail` (
`ItemNum` int(11) NOT NULL auto_increment,
`Qtr` int(11) NOT NULL default '0',
`MsgDate` date default NULL,
`From` varchar(64) default NULL,
`Message` varchar(255) default NULL,
PRIMARY KEY (`ItemNum`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=latin1
From is a MySQL reserved keyword. Surround it with backticks:
UPDATE Mail
SET `From`="Spouse"
WHERE ItemNum=9;