mysql command line escape - sign - mysql

I am trying to create a database but I can not figure out how to escape the - sign in the database name.
mysql> create database happy-face;
gives me an error
mysql> create database happyface;
works without problems
How can I escape the - sign in the name of the database ?

You could quote the name with backticks
mysql> create database `happy-face`;
but you might want to avoid names that require backticks since it makes your life a little more complicated.

Related

Using special characters in mysql table or column name

I have seen other questions related to using special characters in column name but I didn't get my point. I want to use column name like
`first_name#abc|xyz|exy#`
Is it legal does it cause any security issue or any error while querying them?
MySQL 5.7 sets the encoding of the column names to utf-8 by default.
If you want to see the configuration in your system, execute the following command:
SHOW VARIABLES LIKE 'character_set_results';
Source: https://dev.mysql.com/doc/refman/5.7/en/charset-metadata.html

I am unable to delete a database in mysql ending in pound '#'

I created a database xyz# in MySQL accidentally when I was trying to create xyz. Now when I try to drop it, I get to the next line. I searched online but no luck. I tried escape characters / \ and single and double quote to consider # name of the database but no luck.
Any idea how to delete a database ending with #. Thanks
Use backticks:
DROP DATABASE `dbnameendingin#`

Replace instances of newlines in entire database

In MySQL Workbench I want to replace all instances of newlines with the explicit string "\\n" in my entire database, except where the string already exists. How would I form the query? Thanks.
[edit]
It just occurred to me that it might be easier to dump the database and use Notepad++ to replace the strings. But I was hoping there were a way to do this from within MySQL Workbench itself.
You will need to do this for each table and each column that you want to do the replacement. For example:
update table set column = REPLACE(column,'\n','\\n')

PHPMyAdmin Import/Export Same Server Fails

I've been trying to work out the best way of copying structure & data from one database to another but the PHPMyAdmin export seems to churn out pretty poor scripts. The most obvious example is if I export a database (structure & data) then try and re-import on the same server (using the drop tables function to prevent clashes), I get a syntax error!? I would have thought PHPMyAdmin would be able to parse its own exports.
The error I get is:
Error SQL query:
$$
DROP PROCEDURE IF EXISTS `CMS_identifyFileType`$$
MySQL said: Documentation
#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 '$
DROP PROCEDURE IF EXISTS `CMS_identifyFileType`' at line 1
It looks odd to me that the script has weird apostrophes?
Does anyone have any tips on what I might be doing wrong? I have to manually add the 'use myDatabasename;' to the script to get it to work, not sure if I'm missing some other stuff.
My MySQL version is 5.1.73-community running on a Windows Server 2008 R2 server.
Thanks
Bob
I suspect you've amended the output file.
For table and index definitions, mysqladmin uses the default delimiter ';' but for procedures and functions it uses '$$'. The DBMS needs to know that the delimiter has changed - hence in the export file there should be a line like this between a table definition and a procedure definition:
DELIMITER $$
BTW the weird apostrophes around (for example) CMS_identifyFileType are to be expected in MySQL - see Using backticks around field names for a discussion.
I have no idea if this is the correct way to do it but in the end this worked for me:
I did the export using the default options.
I opened the file in Dreamweaver (it seems to handle the length file better than Notepad++)
Added the 'USE mydatabaseName;' to the beginning
I removed all commented lines
I removed the 'delimiter $$' lines
I replaced any $$ at the end of lines with ;
I replaced any orphan $$ (on their own on a line) with a space
I replaced all backticks with a space
Uploaded the SQL file to PHPMyAdmin and it finally worked (I tried not doing each of the steps above and if I missed anyone of them, I got one of a number of different flavor syntax errors). Seems to me like PHPMyAdmin's Import/Export system really needs some work.
Caveat: My table, column and procedure names do not include any special characters, spaces or reserved words so I was able to get away without the backticks. If you have anything unusual you will need them.

Does MySQL allows to create database with dot?

Does MySQL allows to create database which has dot (.) in its name?
I'm using MySQL 5.1.22.
You can't use the dot in a database name. Also, I'd avoid using it in any identifier. A common convention is to use underscore instead. It will serve the same purpose and will avoid a LOT of confusion. If you do have a good reason for using strange and otherwise-illegal characters in a table or field name, then you have to escape it.
to escape identifiers in MySQL, use the backtick:
SELECT `select`, `some.field name`, `crazy()naming+here`
FROM `my-=+table`
Getting into the habit of backticking all field names regardless of whether you need to is a good practice in my opinion, but that's another story.
You can use . in names from MySQL 5.1.6 according to the documentation.
However, as has been said and will said again, please don't do it. For every problem you think you're solving now you'll be creating five which will bite you later on. Since . is used to qualify names - e.g. database.table or table.column you'll have to quote your database name every time you use it.*
You can do this with backticks:
CREATE TABLE `do.not.do.this` (col INT);
or using double quotes if you set the following option:
SET sql_mode='ANSI_QUOTES';
CREATE TABLE "asking.for.problems" (col INT);
* Not strictly true - you have to quote any character that's not alphanumeric or _ or $ , but . is a particularly troublesome option to have in your names.
Before MySQL 5.1.6, database and table names cannot contain /, \, ., or characters that are not allowed in file names (see 8.2. Schema Object Names).
In versions after 5.1.6 you have to quote your tablename with a backtick (`) - but as others also advised: you shouldn't do this to prevent any unnecessary trouble.
MySQL 5.0.22 doesn't appear to allow it:
% mysqladmin -uroot -pXXX create foo.bar
mysqladmin: CREATE DATABASE failed; error: 'Incorrect database name 'foo.bar''
Even it if it did allow it, I would strongly recommend against it.
At the very least you'd have to escape any reference to that database with backticks in every single query that ever uses it.