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
Related
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
Trying to create the following table:
CREATE TABLE login (
IdUser int(11) NOT NULL AUTO_INCREMENT,
username varchar(45) CHARACTER SET latin1 NOT NULL,
pass varchar(45) CHARACTER SET latin1 NOT NULL,
PRIMARY KEY (IdUser),
ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8$$);
Doesn't seem to work properly. The error I'm getting in MYSQL is:
#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 '=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8$$' at line 6
Bracket in the wrong spot:
PRIMARY KEY (IdUser),
ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8$$);
^----
should be
PRIMARY KEY (IdUser) <--note removed comma
) ENGINE=MyIsam etc...
^---
You're treating those table options as they were fields, by placing them WITHIN the () field definition block.
In my case some of the sql is working but mostly not working after changing Type=ENGINE.
CREATE TABLE `p4_acl_page` (
`id` int(2) NOT NULL auto_increment,
`label` varchar(80) default NULL,
`lastupdate` timestamp(14) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyIsam;
So I'm a complete novice with MySQL and I'm trying to follow a tutorial that will allow me to sort out an Ajax + JQuery page for my database.
However, running this code in PHPMyAdmin:
CREATE TABLE IF NOT EXISTS 'add_delete_record' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'content' text NOT NULL,
PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
produces the error:
SQL query:
CREATE TABLE IF NOT EXISTS 'add_delete_record'(
'id'INT( 11 ) NOT NULL AUTO_INCREMENT , 'content'TEXT NOT NULL ,
PRIMARY KEY ( 'id' ) ) ENGINE = INNODB DEFAULT CHARSET = latin1
AUTO_INCREMENT =1 MySQL said:
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 ''add_delete_record' ( 'id' int(11) NOT NULL AUTO_INCREMENT,
'content' text' at line 1
Being a novice I have absolutely no clue what is wrong, apart from the fact that there is a problem with the syntax somewhere? Thanks to anyone who can help!
Use backticks instead. Single quotes represent string literals.
CREATE TABLE IF NOT EXISTS `add_delete_record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
use backticks
CREATE TABLE IF NOT EXISTS `add_delete_record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
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
I received a MySQL data dump and am trying to insert the data into a set of temporary tables. The creation statement for the first table is shown below. When I run this I receive the error: "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 ''temp_books'( 'ID'int( 11 ) NOT NULL AUTO_INCREMENT, 'start'varchar( 20 ) ' at line 1". I've checked the documentation for MySQL syntax, and I don't see that the problem is.
CREATE TABLE 'temp_books' (
'ID' int(11) NOT NULL AUTO_INCREMENT,
'start' varchar(20) NOT NULL,
'customer_id' int(11) NOT NULL DEFAULT '0',
'total_num' int(11) NOT NULL,
'amount' double(5,2) NOT NULL DEFAULT '0.00',
'changed' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('ID'),
UNIQUE KEY 'start' ('start')
) ENGINE=MyISAM AUTO_INCREMENT=4853 DEFAULT CHARSET=latin1;
You shouldn't put single-quotes on your identifiers. If you're going to quote them use the "back tick" character (“`”). You can also use double-quotes but you have to specify that mode:
SET sql_mode='ANSI_QUOTES';
http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
I've ALWAYS had issues with CREATE TABLE. Not sure why. Takes some trial-and-error.
Try this:
CREATE TABLE temp_books (
ID int(11) NOT NULL AUTO_INCREMENT,
start varchar(20) NOT NULL,
customer_id int(11) NOT NULL DEFAULT '0',
total_num int(11) NOT NULL,
amount double(5,2) NOT NULL DEFAULT '0.00',
changed timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (ID),
UNIQUE KEY start (start)
) ENGINE=MyISAM AUTO_INCREMENT=4853 DEFAULT CHARSET=latin1;
I had to delete the quote marks, as well as the default for the changed field, as well as the default charset. Hopefully that won't affect the data.
Here's another way of writing it that might work for some: (left away most of the columns for brevity)
create table temp_books
(
id int not null,
start varchar(255) null,
constraint six_cb_datasource_pk
primary key (id)
);