I obtain a error syntax when I exceute one query - mysql

I need to create table and I do this:
CREATE TABLE "home" (
"id" int(11) NOT NULL AUTO_INCREMENT,
"address" varchar(200) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
PRIMARY KEY ("id")
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 COLLATE=latin1_bin;
when I execute this query I obtain this error:
Errore SQL [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 '"causali" (
"id" int(11) NOT NULL AUTO_INCREMENT,
"address" va' at line 1
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 '"home" (
"id" int(11) NOT NULL AUTO_INCREMENT,
"adddress" va' at line 1
Anyone can help me?

MySQL does not like double quotes on the table / column / alias names, until we enable ANSI_QUOTES mode.
Treat " as an identifier quote character (like the ` quote character)
and not as a string quote character. You can still use backticks to quote
identifiers with this mode enabled. With ANSI_QUOTES enabled, you
cannot use double quotation marks to quote literal strings because
they are interpreted as identifiers.
This mode is by default Disabled. Now, you can either use backticks around them, or use without double quotes (except when it is a Reserved keyword, then you will have to use backticks around it).
CREATE TABLE home (
id int(11) NOT NULL AUTO_INCREMENT,
address varchar(200) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 COLLATE=latin1_bin;

Related

MySQL Error Code 1064 from workbench when create a table

I am trying to create a table using MySQL workbench, but the error msg shows this error. Check online, it shows " often due to using reserved words, missing data in the database, or mistyped/obsolete commands. " But, I did not use any reserved words or missing data. So not sure why this error happens. Thanks!
Operation failed: There was an error while applying the SQL script to the database.
Executing:
CREATE TABLE `assign`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`email` VARCHAR(128) NOT NULL,
`nickname` VARCHAR(255) NOT NULL,
`password` VARCHAR(128) NOT NULL,
`created_at` VARCHAR(45) NOT NULL DEFAULT 'CURRENT_TIMESTAMP()',
PRIMARY KEY (`id`),
UNIQUE INDEX `email_UNIQUE` (`email` ASC) VISIBLE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8' at line 8

Sql syntax issue with adding values [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 2 years ago.
I have an error in my sql on second addSql() line:
$this->addSql('CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, `key` VARCHAR(60) NOT NULL, value VARCHAR(60) NOT NULL, UNIQUE INDEX UNIQ_E545A0C58A90ABA9 (`key`), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql("INSERT INTO user (id,key,value) VALUES (1,'theKey','14')");
SQLSTATE[42000]: Syntax error or access violation: 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 'key,value) VALUES (1,'theKey','14')' at line 1
Can someone help to locate it..
The 'key` word is reserved word in MySQL. I advice to avoid using reserved as column names. Any way is it necessary use backticks to quote them:
$this->addSql('CREATE TABLE user (`id` INT AUTO_INCREMENT NOT NULL, `key` VARCHAR(60) NOT NULL, `value` VARCHAR(60) NOT NULL, UNIQUE INDEX UNIQ_E545A0C58A90ABA9 (`key`), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql("INSERT INTO user (`id`, `key`, `value`) VALUES (1,'theKey','14')");
Test it on SQLize.online
changing VALUE to VALUES should do the trick

What's wrong with this MySQL query -- db won't accept?

I have the following MySQL query:
INSERT INTO 12:12:12:12:12(timestamp,niceTime,temperature,relative_humidity,wind_speed,gust_speed,rain_mm_per_hour,nsew,str,ip) VALUES(1361707978,'2013-02-24T12:12:58+00:00',0.0,0,0.0,0.0,0.0,0,'1010101010101010','0')
The name of the table is "12:12:12:12:12".
Here is the schema:
"CREATE TABLE IF NOT EXISTS `$mac` (
`timestamp` int(11) NOT NULL,
`niceTime` varchar(20) NOT NULL,
`temperature` float NOT NULL,
`relative_humidity` int(11) NOT NULL,
`wind_speed` float NOT NULL,
`gust_speed` float NOT NULL,
`rain_mm_per_hour` float NOT NULL,
`nsew` int(11) NOT NULL,
`str` varchar(1000) NOT NULL,
`ip` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;"
No matter what I do, I cannot get the query to be accepted ;(
Query failed: 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 '12:12:12:12:12(timestamp,niceTime,temperature,relative_humidity,wind_speed,gust_' at line 1
Many thanks in advance,
you will use backticks like that to your table name
12:12:12:12:12
try this
INSERT INTO `12:12:12:12:12`(timestamp,niceTime,temperature,relative_humidity,wind_speed,gust_speed,rain_mm_per_hour,nsew,str,ip) VALUES(1361707978,'2013-02-24T12:12:58+00:00',0.0,0,0.0,0.0,0.0,0,'1010101010101010','0'
EDIT.
Rules for naming objects, including tables in MySql:
http://dev.mysql.com/doc/refman/5.1/en/identifiers.html
Identifiers may begin with a digit but
unless quoted may not consist solely
of digits.
The identifier quote character is the backtick (“`”):
Use backticks around identifiers, especially when using such unconventional table names:
INSERT INTO `12:12:12:12:12`(timestamp,niceTime,temperature,relative_humidity,wind_speed,gust_speed,rain_mm_per_hour,nsew,str,ip)
VALUES(1361707978,'2013-02-24T12:12:58+00:00',0.0,0,0.0,0.0,0.0,0,'1010101010101010','0')

Error while creating simple table

I don't see why this fails :o
CREATE TABLE "Permission" (
"id" INTEGER NOT NULL,
"name" VARCHAR(255) NOT NULL,
PRIMARY KEY ("id")
);
The message
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 '"Permission" (
"id" INT NOT NULL,
"name" VARCHAR(255) NOT NULL,
PRIMARY KEY ("id' at line 1
Thanks for any ideas!
You are using the wrong quoting character. You should use ` marks not " marks for table names and columns.
CREATE TABLE `Permission` (
`id` INTEGER NOT NULL,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
);
quickshiftin's answer is good.And you should also change:
`id` INTEGER NOT NULL,
to:
`id` INT NOT NULL,
MySQL normally uses the backtick character to enclose object references, as quickshiftin's answer says.
But it's not really "wrong" to use double quotes, it's just that the MySQL default behavior is to not permit double quotes to be used in that way.
It is possible to have MySQL allow the double quote character to be used around object references by enabling the ANSI_QUOTES sql mode for your session. I would recommend you do this only temporarily, just to get your tables defined, and then set it back.
SHOW VARIABLES LIKE 'sql_mode'
SET sql_mode = 'ANSI_QUOTES'
Before you set the sql_mode variable, you want to note the current setting, so you can set it back after you have your tables created.
SET sql_mode = ''
It's also possible to set this variable at the database level, but I've never seen that done before. I've only ever (temporarily) overridden the default at the session level, for importing SQL table definitions that use double quotes.

If I change the Charset can i use ' instead of ` for delimitating a field?

Instead of having this:
CREATE TABLE IF NOT EXISTS `address` (
`Contact_ID` smallint(6) NOT NULL DEFAULT '0' COMMENT 'Primary Key',
`Street Address` char(50) DEFAULT NULL,
`City` char(20) DEFAULT NULL,
`Zip Code` char(8) DEFAULT NULL,
`Country` char(20) DEFAULT NULL,
PRIMARY KEY (`Contact_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Moradas';
I Want to be able to do this instead:
CREATE TABLE IF NOT EXISTS 'address' (
'Contact_ID' smallint(6) NOT NULL DEFAULT ´0´ COMMENT ´Primary Key´,
'Street Address' char(50) DEFAULT NULL, (...)
I'm doing this on WAMPSERVER. This is because it's really not practical at all having to do 'shift+character' in order to produce the ` everytime I create a table and populate it.
(Upgrading to an answer)
As explained in the manual, identifiers can be unquoted if they comprise only certain characters:
Permitted characters in unquoted identifiers:
ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)
Extended: U+0080 .. U+FFFF
Also, note that the manual goes on to explain:
If the ANSI_QUOTES SQL mode is enabled, it is also permissible to quote identifiers within double quotation marks:
mysql> CREATE TABLE "test" (col INT);
ERROR 1064: You have an error in your SQL syntax...
mysql> SET sql_mode='ANSI_QUOTES';
mysql> CREATE TABLE "test" (col INT);
Query OK, 0 rows affected (0.00 sec)
The ANSI_QUOTES mode causes the server to interpret double-quoted strings as identifiers. Consequently, when this mode is enabled, string literals must be enclosed within single quotation marks. They cannot be enclosed within double quotation marks. The server SQL mode is controlled as described in Section 5.1.6, “Server SQL Modes”.