CREATE DATABASE - Error 1604 [duplicate] - mysql

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
I tried to create Database with:
CREATE DATABASE 'mynewdb';
and got:
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 ''mynewdb'' at line 1
Why? What did I wrong?

Don't use quotes:
CREATE DATABASE mynewdb;

You have to remove your quotes or use backticks such:
CREATE DATABASE mynewdb;
or
CREATE DATABASE `mynewdb`;
Backticks are to be used for database, table and column identifiers, but are only necessary when the identifier is a MySQL reserved keyword, or when the identifier contains whitespace characters or characters beyond a limited set (see below) It is often recommended to avoid using reserved keywords as column or table identifiers when possible, avoiding the quoting issue.
Here a very good answer When to use single quotes, double quotes, and backticks in MySQL.

Related

check the manual that corresponds to your mysql server version for the right syntax to use near '( - [duplicate]

This question already has answers here:
Hyphens in column names in MySQL DB
(6 answers)
Closed 3 years ago.
I'm just getting into MySQL and I made this column named Name-Player, and it gives me this error. "check the manual that corresponds to your mysql server version for the right syntax to use near '( -"
I've tried changing the name of it in MySQL workbench but it doesn't update, also tried in MySQL Shell, but it just gives me the same error.
No Code.
Expecting to change the name of it, hoping it will resolve the problem.
That column name contains a dash, which is a special character in MySQL.
If you want to keep it like that, you would need to quote this identifier by surrounding it with backticks, each and every time you use it.
A probably better option would be to rename the column to something that does not contain special characters:
ALTER TABLE my_table CHANGE `Name-Player` `my_new_name` <my_datatype>;
... where <my_datatype> stands for the datatype (and lenght) of your column.

Escaping database name in prepared statement with Node

I am trying to use mysql library to request my database.
I need to use the syntax with query placeholders, so I tried a simple request:
connection.query('DROP DATABASE IF EXISTS ?;', ['mydb']);
But this leads to :
'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 \'\'mydb\'\' at line 1'
The query is indeed:
'DROP DATABASE IF EXISTS \'mydb\';'
So how is this supposed to work actually ?
The MySQL notation for this is:
DROP DATABASE IF EXISTS `mydb`
Since you're escaping it as a string, that's not a database reference it can drop.
Some drivers support alternate placeholders for this very reason:
DROP DATABASE IF EXISTS ??
Normally you can't use placeholder values for things like databases, columns or tables as these are treated differently. This is a limitation of the driver.
You just need to be careful on user-supplied values and names with irregular characters in them. These need to be escaped according to MySQL schema identifier rules.

SQL "use" command not working in MySQL 5.7

I am having problems getting the SQL "use" command to work in MySQL 5.7.
I have a database 'mydb' with a 'character' table. (At the moment, the table is empty, but I don't think that should matter here).
Explicitly using "use" gives me an error:
use mydb;
select count(*) from character;
Error Code: 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 'character' at line 1 0.000 sec
However, the following line, which does not use "use", works okay:
select count(*) from mydb.character;
Does anyone know what I am doing wrong? Admittedly, I am quite new to SQL. The full output is shown below:
character is a reserved keyword in MySQL. Hence you get the error when you select from that table. Escape with `` to avoid this. Or use names other than reserved keywords for your tablenames.
Reserved Keywords in MySQL

Create database with dasherized name from mysql CLI

When I use the MySQL client to create database, if I type below command:
create database nice-day;
Then it tells me that:
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 '-day' at line 1
But when I use phpAdmin tool, I can create the database named nice-day. What is the problem?
The - character is not considered to be part of an identifier in SQL, so the database name must be quoted:
CREATE DATABASE `nice-day`
In general, though, it is advisable to use underscores (_) instead of dashes in database names to avoid this issue.
Please use backticks "`" (the key before the 1 on a standard US 101 keyboard).
as per http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
The identifier quote character is the backtick (“`”):

What is the difference between the backtick and the square bracket in SQL statements?

I thought there would be another question about this but I was unable to find one. In MySQL with PHP I usually encapsulate my field names with backticks to mask any reserved names or characters. However, one of my colleagues has pointed out that this can also be achieved using square brackets. Excluding the fact that the backticks are not compatible with SQL server (apparently), what is the difference? Which should I use?
SELECT `username` FROM `users`
SELECT [username] FROM [users]
SQL Server/T-SQL uses square brackets (as well as MS Access), while MySQL uses backticks.
As far as I know, can turn up in documentation, or use in testing, square brackets are not valid for MySQL. So if you need to enclose a keyword as a table name in SQL Server, use [], and in MySQL use backticks, or double-quotes when ANSI_QUOTES is enabled.
From the documentation:
The identifier quote character is the backtick (“`”):
mysql> SELECT * FROM `select` WHERE `select`.id > 100;
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)
Both are non-standard ways to quote object names that should either be case-sensitive, are a reserved word or contain special characters that are not allowed otherwise.
The standard quoting character for such an identifier is a double quote. To be ANSI SQL compatible, you should use them:
SELECT "username" FROM "users"
But note that quoted identifiers are case-sensitive as per ANSI SQL. However both mentioned products do not obey to this requirement. Whether such an identifier is case-sensitive or not depends on a several (different) configuration settings in MySQL and the database collation in MS SQL Server.
Both DBMS can (and in my opinion should) be configured to accept the ANSI standard quote characters as well.
I would strongly recommend to avoid any object name that requires quoting. Using identifiers that do not require quoting will save you a lot of trouble in the long run.