Unable to drop database due to illegal character - mysql

How can i drop a database containing the "-" symbol?
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| vms01 |
| vms-0.1.0 |
+--------------------+
4 rows in set (0.00 sec)
mysql> drop database vms-0.1.0;
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 n
ear '-0.1.0' at line 1
mysql>

You can quote identifiers (for example table and column names) with backticks:
drop database `vms-0.1.0`
See the documentation for more details: Schema Object Names.
The identifier quote character is the backtick ("`"):

Related

In MySQL 8.x the string function FROM_BASE64(str) returns result in hexadecimal form instead of a string

I have the following queries -
SELECT "Abc" AS result;
+--------+
| result |
+--------+
| Abc |
+--------+
SELECT TO_BASE64("Abc") AS result;
+--------+
| result |
+--------+
| QWJj |
+--------+
SELECT FROM_BASE64(TO_BASE64("Abc")) AS result;
+----------------+
| result |
+----------------+
| 0x416263 |
+----------------+
--binary-as-hex page in mysql dev site says -
To disable hexadecimal notation, use --skip-binary-as-hex
I tried the following but got error -
mysql> SELECT FROM_BASE64(TO_BASE64("Abc")) --skip-binary-as-hex AS result;
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 'as-hex AS result' at line 1
mysql> SELECT FROM_BASE64(TO_BASE64("Abc") --skip-binary-as-hex) AS result;
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 'as-hex) AS result' at line 1
mysql> SELECT FROM_BASE64(TO_BASE64("Abc" --skip-binary-as-hex)) AS result;
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 'as-hex)) AS result' at line 1
In the same page they have said how can I use the USING utf8mb4 clause to get the result in case of CHAR() and CONVERT() functions but they have not stated anything about FROM_BASE64() function. Nevertheless, I tried it and got error -
SELECT FROM_BASE64(TO_BASE64("Abc") USING utf8mb4) AS result;
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 'USING utf8mb4) AS result' at line 1
I tried the UNHEX() function -
ELECT UNHEX(FROM_BASE64(TO_BASE64("Abc"))) AS result;
+----------------+
| result |
+----------------+
| 0x0ABC |
+----------------+
Clearly this is not the desired result as well as all are in capital. Here LCASE() wouldn't work as well as it will turn every word to lower case.
Even, this result is not in string as is evident from -
SELECT SUBSTRING(UNHEX(FROM_BASE64(TO_BASE64("Abc"))) FROM 4) AS result;
+----------------+
| result |
+----------------+
| 0x |
+----------------+
So the only option seems to be to disable -binary-as-hex
But I can't find out a way to do this.
Here as similar question is there in stackoverflow -
'FROM_BASE64' Function Returns Hex Value
But it is on MySQL Version 5.6.14. I am using MySQL Version 8.0.27 -
mysql --version
mysql Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)
So my case is different.
The --skip-binary-as-hex option is to be used as an option to the mysql command when you open that from a shell prompt. It's not an option to be used within SQL syntax. See https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html#option_mysql_binary-as-hex
That said, you can convert binary to strings even if binary-as-hex is enabled:
mysql> SELECT FROM_BASE64(TO_BASE64("Abc")) AS result;
+----------------+
| result |
+----------------+
| 0x416263 |
+----------------+
1 row in set (0.00 sec)
mysql> SELECT CONVERT(FROM_BASE64(TO_BASE64("Abc")) USING utf8mb4) AS result;
+--------+
| result |
+--------+
| Abc |
+--------+
1 row in set (0.00 sec)
You may need to use a different character encoding. Mine is utf8mb4, but yours may be different.

1064 error in MySQL Command line client when trying to view table but works fine in MySQL Workbench

So i created a database in MySQL Workbench and I am trying to view them on the MySQL Command Line Client because i want to do my queries there instead of the workbench.
Here's whats happening:
mysql> use policestation(crime);
Database changed
mysql> show tables;
+--------------------------------+
| Tables_in_policestation(crime) |
+--------------------------------+
| accused |
| case |
| complainant |
| investigation_officer |
| outcome |
| section_of_law |
+--------------------------------+
6 rows in set (0.04 sec)
mysql> select * from case;
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 'case' at line 1
The same query works fine in MySQL Workbech. Also it works for all other tables in Command Line Client.
It's b/c Case is a MySQL reserved keyword: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
You'll need to seround reserved keywords with tick marks not quotes.
Example:
SELECT * FROM `case`
Not the same as
SELECT * FROM 'case'
Also here is another helpful link:
Syntax error due to using a reserved word as a table or column name in MySQL

Ubuntu 16.04,MySQL 5.7.19,Unable to delete database

I am new to MySQL and in the learning curve ,
I wanted somehelp ..
I tried importing the temp database from https://github.com/datacharmer/test_db ... I am having diffulties in importing the same .
I think I have wrongly imported and I wanted to drop the table , But its not allowing me .
I get the below error .
mysql> show databases;
+-------------------------+
| Database |
+-------------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| #mysql50#test_db-master |
+-------------------------+
5 rows in set (0.00 sec)
I am not getting any response in Terminal , but when I tried in MySQL Workbench , I am getting the below error
*drop '#mysql50#test_db-master'
22:04:30 drop '#mysql50#test_db-master' 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 ''#mysql50#test_db-master'' at line 1 0.00034 sec*
I wanted to delete this table & wanted to create a new one again ...
https://dev.mysql.com/doc/refman/5.7/en/drop-database.html shows the syntax for DROP DATABASE.
Pro tip: Read documentation. You can get answers more quickly than by posting on Stack Overflow.

Unable to select table in mysql

This is mysql table:
+---------------------+
| Tables_in_myproject |
+---------------------+
| reds |
| zxy |
| abcd |
| release |
+---------------------+
4 rows in set (0.46 sec)
When I try to select or show release table it throws the following error:
mysql> select * from release;
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 'release' at line 1
What am I doing wrong? I am able to select all the other tables.
release is a reserved keyword in MySQL and needs to be escaped by backticks.
select * from `release`;
release is a reserved word. use backticks arround or better change the table name.
select * from `release`;

MySQL extratvalue range, output and parsing issues

I'm learning MySQL. My query is follows:
mysql>select extractvalue(1,1111111111111111111); // 19 1's.
output:
`+-------------------------------------+
| extractvalue(1,1111111111111111111) |
+-------------------------------------+
| 1111111111111111111 |
+-------------------------------------+
1 row in set (0.00 sec)`
But for 20 1's
mysql>select extract(1,11111111111111111111);
+--------------------------------------+
| extractvalue(1,11111111111111111111) |
+--------------------------------------+
| -7335632962598440505 |
+--------------------------------------+
1 row in set (0.00 sec)
And the following show me different errors:
mysql> select extractvalue(rand(),5.5.28));
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 '.28))
' at line 1
mysql> select extractvalue(rand(),version());
ERROR 1105 (HY000): XPATH syntax error: '.28'
Can someone explain me?
I tried out these, it seems to work.
SELECT #rand := RAND();
select extractvalue(1,#rand);
However, extract doesn't seem to work and gives the error. Either way I have not managed to use extract or extractvalue to wrap rand(). Reason could be that extractvalue only returns CDATA. Your intention of application doesn't seem to be supported by this function. It's used on XPATH
for e.g.
Select allhttp://www.blablabla.com/error.php?id=null
and extractvalue(rand(),concat(0x3a,(
select concat(0x3a,username,0x3a,password) from users)))
Found this to be interesting: reference