How can i create a table that contains "." character? - mysql

I'm trying to create a table named "localhost.localdomain" and when I run this:
CREATE TABLE localhost.localdomain (User TEXT);
or this:
CREATE TABLE "localhost.localdomain" (User TEXT);
I get the error "no such table: localhost.localdomain".
I wanted to know is there any way to create a table with such names which contain "."?

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 table name with a backtick (`) - but as others also advised: you shouldn't do this to prevent any unnecessary trouble.
Check more in this question...

Related

SOLVED - Unable to update mySQL column name via mysql command line

I cannot change the column name in a mysql database I created.
I have tried the following commands, and none of them appear to work.
alter table (mytablename) CHANGE COLUMN (oldcolumnname) (newcolumnname) varchar(120);
alter table (mytablename) RENAME COLUMN (oldcolumnname) (newcolumnname) varchar(120);
ALTER TABLE (mytablename) CHANGE (oldcolumnname) (newcolumnname) varchar(120);
Where (mytablename) is the name of the table that I created, (oldcolumnname) is the original column name, and (newcolumnname) is the new column name.
This is a simple to-do list I created to learn MySQL with the following items:
id
todo
completed
1
Prepare for Take Off
Yes
2
Learn some MySQL
Yes
3
Remember that damn semicolon
No
In this case, I am trying to alter the column 'todo' to say either 'To Do' or 'To-Do' but every time I try these commands. I keep getting the famous "Check your SQL version manual".
Any hints as to what I might be doing wrong? TIA!
I have reviewed multiple tutorial websites and even reviewed another StackOverflow question
UPDATE
The ultimate solution was two-fold. First, I needed to use the TO phrase between the column names. Second, the column names do not like special characters.
The query that ultimately worked was:
ALTER TABLE mytablename CHANGE COLUMN todo TO ToDo
You should keep the column named todo; you can always change the output when you select like:
select id, todo as 'To Do', completed from ...
If you feel you really must include a space or - in the column name, in mysql you can use arbitrary identifiers that have not-usually allowed characters by enclosing them in backticks:
alter table ... rename column todo to `To Do`
but then every time you reference the column in sql you will need to enclose it in backticks:
select id,`To Do`,completed from ... where `To Do` like '%learn%'

Error when adding column with numeric name

I currently have a MariaDB database with columns named after dates : 20200105, 20200914 etc.
If I try to add a column using ALTER TABLE dates ADD COLUMN IF NOT EXISTS (test VARCHAR(255));, it works and the test column is created.
If I type ALTER TABLE dates ADD COLUMN IF NOT EXISTS (20201205 VARCHAR(255));, though (so, with a number replacing "test"), the creation does not work anymore and MariaDB tells me that there is an error with my SQL syntax.
I have tried to put quotes around the column name, but that does not work (not even with "test").
Is there something obvious I am missing ?
Use backticks to escape the column name:
ALTER TABLE dates ADD COLUMN IF NOT EXISTS (`20201205` VARCHAR(255));
But really best practice frowns upon the use of naming your database objects with mandatory backticks. The reason for using a name like 20201205 as a column name is that you will forever be needing to escape it using backticks. Also, from a data design point of view, your data should grow with new dates in terms of increasing the number of records, not columns.

ALTER TABLE xxx RENAME TO xxxx; success, but the table name did not change in the navicat

I execute this line SQL:
ALTER TABLE dinnertable RENAME TO dinnerTable;
And in the info console:
[SQL] ALTER TABLE dinnertable RENAME TO dinnerTable;
受影响的行: 0
时间: 0.001s
But in the navicat, you see below, the table name did not change:
sp_rename 'old_table_name','new_table_name';
I notice that you have refreshed the navicat. So this problem can be a mysql table name case sensitive setting problem. Please check the my.cnf or my.ini file of this mysql case. Change the parameter named lower_case_tables_name. If you don't find it you need to add
lower_case_tables_name = 0
below [mysqld]
Then restart the mysql case.
Please try by adding double quotes to your rename statement.
ALTER TABLE "dinnertable" RENAME TO "dinnerTable";
This could be refresh problem.
Please close the database connection or connect once again
Case sensitivity problem. Please try by adding double quotes.
Update Export table along with data using any of modern IDE's and import the same by updating the table name.
Nonquoted identifiers are not case sensitive.
(Please note this is for Oracle, not sure about mysql)
Oracle interprets them as uppercase. Quoted identifiers are case sensitive.
By enclosing names in double quotation marks, you can give the following names to different objects in the same namespace:
"employees"
"Employees"
"EMPLOYEES"
Refer to the link for more information.

Create SQL table with SQL keyword column

I am trying to create SQL table with SQL keyword column named "lock", but "lock" seems is a SQL keyword where the workbench thought I would want to execute the "lock" function.
Create table A(
Lock Varchar(255)
);
*Putting the "Lock" into a square bracket --> [lock] doesn't work for me.
You should put back ticks ` around the reserved word:
Create table A(
`Lock` Varchar(255)
);
You can find a good explanation about reserved words and the list of them here.
Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, tablespace, and other object names are known as identifiers.
If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it.
You should try to avoid the use of this words, it may be confusing for some people to watch a code with this name. Try adding a logical name to it , like TabName_Lock.
MySQL uses the ` character to escape object names:
Create table A(
`Lock` Varchar(255)
);

How to create a temporary MySQL table with a UUID name in Coldfusion?

I'm using Coldfusion8 and MySQL 5.0.91 and am trying to create a temporary table, import some data from CSV, validate, store in database and drop the temp table.
I can do all steps in MySQL directly, where it works without a problem. However when I try to create the temp table in Coldfusion I keep getting a MySQL error.
This is what I have:
<cfset variables.tt = CreateUUID()>
<cfquery datasource="#session.datasource#">
DROP TEMPORARY TABLE IF EXISTS ##variables.tt;
CREATE TEMPORARY TABLE ##variables.tt (
tmp_kto VARCHAR(7) DEFAULT ''
, tmp_kz VARCHAR(3) DEFAULT ''
, tmp_preisliste VARCHAR(1) DEFAULT ''
, tmp_eg VARCHAR(3) DEFAULT ''
) ENGINE=MEMORY;
</cfquery>
which does not work. I also tried #variables.tt# and plain variables.tt but I keep getting the same error, while it works fine when I run the code in MySQL directly (with table name "test")
Question:
I have no clue what I'm doing wrong. Is there a max length for table names perhaps?
Thanks for some insights.
EDIT:
I get the usual 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
'CREATE TEMPORARY TABLE #variables.tt (tmp_sa VARCHAR(3) DEFAULT ''
The most likely source of your problem is that the uuid is not suitable as a tablename. For one thing, it contains hyphens. I don't know if MySQL allows this, but I can't remember ever seeing hyphens in a table name.
We have a similar situation, but with redbrick. We want to create unique names for temporary tables. Our approach, written in a custom tag, is to use the string "temp" followed by a random number. We also have some try/catch stuff going on in case we get really unlucky with the random number.
I wrote that tag several years ago. If I were to do it again, I would probably replace the random number with the cftoken, cfid, and tickcount.
Also, you can't drop a table before you create it.
It looks to me you are missing the hash marks around your variable to output.
CREATE TEMPORARY TABLE ##variables.tt (
should be
CREATE TEMPORARY TABLE #variables.tt# (
The way you have it, MySQL is attempting to use the string #variables.tt (the doubled hash escapes the character in ColdFusion) when what you want is to create a temp table with the name of the value of variables.tt.