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)
);
Related
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.
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...
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.
Revisiting MySQL, and trying to change the structure of the table. I want to change two rows from a varchar(30) to higher.
I have googled it, and tried what appears to be the correct statement but I'm getting a syntax error:
ALTER TABLE 'compdetails' CHANGE 'compName' varchar(60) not null;
This statement however gives me a syntax error. I have also tried without the '' around the table/column names without any luck.
"Error 2064 (42000): You have an error
in your SQL Syntax...."
Can anyone jog my memory?
Just so:
ALTER TABLE 'compdetails' CHANGE compName compName varchar(60) not null;
Firstly, if you want to quote table names, then you need to use back ticks not single quotes. A single quote is only used for string literals.
Secondly, CHANGE is used when you want to rename the column, whereas MODIFY allows you to change the column definition without renaming it. So you should be using:
ALTER TABLE compdetails MODIFY compName varchar(60) NOT NULL;
The online MySQL documentation is very good and I would encourage you to check it whenever you come across problems with syntax.
ALTER TABLE `compdetails` MODIFY COLUMN `compName` varchar(60) not null;
I have a database table with a column name as 'long'. It is a keyword of MySQL and not sure whether it is a keyword conflict or something, I cannot run some of the simple SQL script using this keyword-column name. I try to run the SQL to alter the column name, using: alter table mytable change column long lng double but it doesn't work.
The question is, what's the reason from prevent the above simple alter SQL from working? (For any other columns on hand, the above SQL works) and how can I make it work if it is a reason of keyword conflict? Is there any keyword escape symbol that I can use in MySQL?
Thanks very much.
Tried:
ALTER TABLE mytable CHANGE COLUMN `long` lng DOUBLE
(The backtick is, AFAIK, MySQL specific.)
An identifier may be quoted or unquoted. If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it. ... The identifier quote character is the backtick ("`") (source)
Try using a "right quote" around "long"
Like this :
`long`
try again query working properly alter table table_name change column long lng double but its neccessary the datatype of long should be integer then try may be your problem solved