KAEDE Hack blog

JavaScript 中心に ライブラリなどの使い方を解説する技術ブログ。

MySQL RENAME Fake News

MySQL RENAME Fake News

f:id:kei_s_lifehack:20190927010813j:plain I cannot change databases column by the ordinary command. It was wrong, its fake news!!!

My Env

MacOS Mojave, MySQL Server version: 10.1.39-MariaDB Source distribution

why

Making a CRUD app, but I want to change table column, from text to desc, so I searched and used alter command, but right SQL command returns error messages.

happend

Then I searched Google for solve, but some are wrong.
So I was stacked!!

My table

MariaDB [cake_cms]> describe interns;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| email    | varchar(255) | NO   |     | NULL    |                |
| name     | varchar(64)  | NO   |     | NULL    |                |
| text     | varchar(255) | NO   |     | NULL    |                |
| location | varchar(64)  | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

MariaDB [cake_cms]> Alter Table interns Rename Column text to desc;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to
use near 'Column text to desc' at line 1

Refered

https://www.dbonline.jp/mysql/table/index18.html
says to use

ALTER TABLE table_name
CHANGE COLUMN old_name TO new_name;

https://stackoverflow.com/questions/30290880/rename-a-column-in-mysql This site says:

ALTER TABLE tableName RENAME COLUMN "oldcolname" TO "newcolname" datatype(length);

So I write

alter table interns rename column "name" to "newname" varchar(255);

But returned syntax error message....

I do not know what to do. Please help me!

from comment of Stack Over Flow

My Question stackoverflow.com

ALTER TABLE table CHANGE COLUMN name newName varchar(5)

Then I tried

alter table interns change column name newName varchar(5)
MariaDB [cake_cms]> alter table interns change column name newName varchar(5); 
Query OK, 6 rows affected, 2 warnings (0.02 sec) Records: 6 Duplicates: 0 Warnings: 2

-> it worked!!!

Inconclude

The document was wrong.
rename column old to new
change column old to new
rename column old new
were not exist!!!
No TO!! No rename!!!

https://www.geeksforgeeks.org/sql-alter-rename/
was fake!! https://www.1keydata.com/sql/alter-table-rename-column.html
was right.
https://www.quora.com/How-do-I-rename-a-column-of-a-table-in-SQL
This former query was right.

I think many ppl are coufused.

Be careful!!!