How to rename a MySQL column from long to something else - mysql

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

Related

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.

MYSQL error: A new statement was found but no delimiter between it and the previous one

I'm trying to update my column with that simple query:
UPDATE products SET desc='1' WHERE 1
But i get this error message : A new statement was found but no delimiter between it and the previous one. If I change desc column to another column than the query is working properly.
I saw THERE that this is a bug, I updated my phpmyadmin to the latest version 5.1, but I still get this error message, what could be the problem?
DESC is a reserved word in MySQL used in ordering the results of a query.
Keywords are words that have significance in SQL. Certain keywords,
such as SELECT, DELETE, or BIGINT, are reserved and require special
treatment for use as identifiers such as table and column names. This
may also be true for the names of built-in functions.
Nonreserved keywords are permitted as identifiers without quoting.
Reserved words are permitted as identifiers if you quote them as
described in Section 9.2, “Schema Object Names”:
As I have highlighted above, you can quote a reserved word if you want to use it as identifier, using
The identifier quote character is the backtick (`)
So you just need to change your query a bit adding the backticks.
UPDATE products SET `desc`='1' WHERE 1;
You can see a little example here.

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)
);

syntax error when trying to alter table column name mysql

I am working on a homework assignment. my instructions are this:
In the MATCHES table, change the column name MATCHNO to MATCH.
my syntax i wrote will be below, the problem I am having is with the word "match"
its giving me a syntax error of unexpected syntax error for it. I am assuming
that match is a keyword of some sort but after googling I'm not sure exactly.
the database I am using is the popular "tennis" one for teachings.
here is my code:
sql
ALTER TABLE MATCHES
CHANGE MATCHNO MATCH INT;
what can I do to fix this? If i change the name of "match" to something else it works but my instructions say to use "match"... is this possible?
The problem is that your query contains MySQL keywords (MATCH, cf. https://dev.mysql.com/doc/refman/5.5/en/keywords.html).
Put the table and column names in backticks (`) in order to prevent MySQL from interpreting those as keywords.
ALTER TABLE `MATCHES` CHANGE `MATCHNO` `MATCH` INT;

How to automatically convert a MySQL column to lowercase

Is there a property that I can add to a column so that it converts its value to lowercase? Instead of doing it for every single value through PHP?
You could probably do it through a trigger that fires on insert or update. Myself, I'd rather just create a view that has a lower-case version of the column in question. The SQL for the view might look like
SELECT ID, LOWER(MY_COLUMN) AS MY_COLUMN_LOWERCASE
FROM MY_TABLE;
Yes, but don't do it.
If you want exclusively lowercase characters in a column, convert them when you insert (or update) them.
If you need a column to be case insensitive in comparisons, use a case insensitive collation (which are used by default in e.g. utf8 columns)
Maybe you mean this?
UPDATE table SET column = LOWER(column)