Rename column in mysql (failed) - mysql

I have a database (some) with a table (exp) of over 800 records, I want to change the name of a column from "Nr._CRT" to "ID", keeping the type smallint.
Error is :
ERROR 1064 (42000): 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 '._CRT to ID' at line 1
So far I have tried the following
ALTER TABLE exp change Nr._CRT ID ;
ALTER TABLE exp change 'Nr._CRT' 'ID' ;
ALTER TABLE exp RENAME COLUMN Nr._CRT TO ID;
ALTER TABLE exp RENAME COLUMN 'Nr._CRT' TO 'ID';
ALTER TABLE exp CHANGE Nr._CRT ID ;
ALTER TABLE exp CHANGE 'Nr._CRT' 'ID' ;
ALTER TABLE exp RENAME COLUMN Nr._CRT TO ID;
ALTER TABLE exp RENAME COLUMN 'Nr._CRT' TO 'ID';
Any help please ...

Some of the statements in your list are valid MySQL syntax. The problem is how to properly quote the original column name, that contains special characters: you need backticks rather than single quotes (which are meant for litteral strings, not for identifiers).
For example:
ALTER TABLE exp RENAME COLUMN `Nr._CRT` TO id;

Related

Request for changing table's name [duplicate]

Renaming a table is not working in MySQL
RENAME TABLE group TO member;
The error message is
#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 'group
RENAME TO member' at line 1
The query is working fine on other tables for me, but not with the table group.
group is a keyword (part of GROUP BY) in MySQL, you need to surround it with backticks to show MySQL that you want it interpreted as a table name:
RENAME TABLE `group` TO `member`;
added(see comments)- Those are not single quotes.
Please try
RENAME TABLE `oldTableName` TO `newTableName`
The MySQL syntax for RENAME TABLE statement is the following:
RENAME TABLE <old_table_name> TO <new_table_name>
In your query, you've used group which is one of the keywords in MySQL. Try to avoid MySQL keywords for names while creating tables, field names and so on.
ALTER TABLE old_table_name RENAME new_table_name;
or
RENAME TABLE old_table_name TO new_table_name;
Table name change
RENAME TABLE old_table_name TO new_table_name;
Rename a table in MySQL :
ALTER TABLE current_name RENAME new_name;
group - is a reserved word in MySQL, that's why you see such error.
#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 'group
RENAME TO member' at line 1
You need to wrap table name into backticks:
RENAME TABLE `group` TO `member`;
ALTER TABLE `group` RENAME `member`
group is keyword so you must have to enclose into group
For Mysql 5.6.18 use the following command
ALTER TABLE `old_table` RENAME TO `new_table`
Also if there is an error saying ".... near RENAME TO ..." try removing the tick `
RENAME TABLE tb1 TO tb2;
tb1 - current table name.
tb2 - the name you want your table to be called.
According to mysql docs: "to rename TEMPORARY tables, RENAME TABLE does not work. Use ALTER TABLE instead."
So this is the most portable method:
ALTER TABLE `old_name` RENAME `new_name`;
Try any of these
RENAME TABLE `group` TO `member`;
or
ALTER TABLE `group` RENAME `member`;
Rename table
Syntax
The syntax to rename a table in MySQL is:
ALTER TABLE table_name
RENAME TO new_table_name;
Example
Let's look at an example that shows how to rename a table in MySQL using the ALTER TABLE statement. or example:
ALTER TABLE contacts
RENAME TO people;
Running The Alter Command
1.Click the SQL tab at the top.
2.In the text box enter the following command: ALTER TABLE exampletable RENAME TO new_table_name;
3.Click the go button.
source : https://my.bluehost.com/hosting/help/2158
You can use
RENAME TABLE `group` TO `member`;
Use back tick (`) instead of single quote (').
Without giving the database name the table is can't be renamed in my case, I followed the below command to rename the table.
RENAME TABLE current_db.tbl_name TO current_db.tbl_name;
Right Click on View > New Query
And Type:
EXEC sp_rename 'Table', 'NewName'
Then Click on Run button at the top left corner of the page.

Change type of a column in mysql

I want to change the DataType of a mySQL table from float to DECIMAL:
ALTER TABLE t_tapes ALTER COLUMN price DECIMAL(15,6);
but I got an 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 'DECIMAL(15,6)' at line 1
You Have to change only ALTER before COLUMN TO MODIFY
LIKE
ALTER TABLE t_tapes MODIFY COLUMN price DECIMAL(15,6);
need Modify instead of Alter
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
ALTER TABLE t_tapes
MODIFY COLUMN price DECIMAL(15,6);
Use the SQl Query for Change data type of Mysql Column
ALTER TABLE `db-name`.`table-name`
CHANGE COLUMN `column-name` `column-name` DECIMAL(15,6);

MySQL - Concatenate Existing Fields in New Generated Field

In my table, I have two fields: book and reference. Neither are required to be unique on their own. However, the concatenated value of these two values must be unique.
I'm trying to create a generated column that concatenates the two, but I'm receiving the following error message when running the SQL:
Executing:
ALTER TABLE `bibleverses`.`myverses`
ADD COLUMN `fullref` VARCHAR(20) GENERATED ALWAYS AS (CONCAT(book, reference)) STORED AFTER `mp3`;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 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 'GENERATED ALWAYS AS (CONCAT(book, reference)) STORED AFTER `mp3`' at line 2
SQL Statement:
ALTER TABLE `bibleverses`.`myverses`
ADD COLUMN `fullref` VARCHAR(20) GENERATED ALWAYS AS (CONCAT(book, reference)) STORED AFTER `mp3`
You can achieve this thing by just applying UNIQUE key constraint to both these columns and it will become a composite key so that you can store the unique values in a pair of these two columns. You can try following SQL statement :
ALTER TABLE bibleverses.myverses ADD UNIQUE(book, reference);
First execute an alter table to add your new column, then you run an update to fill out the fild, like:
ALTER TABLE <table_name> ADD COLUMN <column name, type, definition, etc>;
UPDATE TABLE <table_name> SET <field> = <value>;
To create a virtual generated column (which updates if the constituents changes)
ALTER TABLE producerADD COLUMNFullName varchar(32) as (CONCAT(FirstName,' ',Surname));

Error SQL ALTER TABLE #1064

I have an error in my SQL syntax, can anyone help me please beacause I really don't see the error...
Here is the request SQL :
ALTER TABLE t_personne Change email_personne to mail_pers ;
MySQL error
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 'to mail_pers' at line 1
please help
SQL ALTER TABLE Syntax
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype
To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name
To change the data type of a column in a table, use the following syntax:
SQL Server / MS Access:
ALTER TABLE table_name
ALTER COLUMN column_name datatype
If you are trying to rename the column, Then
Query
ALTER TABLE t_personne
CHANGE COLUMN email_personne mail_pers VARCHAR(255) NOT NULL;
Documentation

Rename a table in MySQL

Renaming a table is not working in MySQL
RENAME TABLE group TO member;
The error message is
#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 'group
RENAME TO member' at line 1
The query is working fine on other tables for me, but not with the table group.
group is a keyword (part of GROUP BY) in MySQL, you need to surround it with backticks to show MySQL that you want it interpreted as a table name:
RENAME TABLE `group` TO `member`;
added(see comments)- Those are not single quotes.
Please try
RENAME TABLE `oldTableName` TO `newTableName`
The MySQL syntax for RENAME TABLE statement is the following:
RENAME TABLE <old_table_name> TO <new_table_name>
In your query, you've used group which is one of the keywords in MySQL. Try to avoid MySQL keywords for names while creating tables, field names and so on.
ALTER TABLE old_table_name RENAME new_table_name;
or
RENAME TABLE old_table_name TO new_table_name;
Table name change
RENAME TABLE old_table_name TO new_table_name;
Rename a table in MySQL :
ALTER TABLE current_name RENAME new_name;
group - is a reserved word in MySQL, that's why you see such error.
#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 'group
RENAME TO member' at line 1
You need to wrap table name into backticks:
RENAME TABLE `group` TO `member`;
ALTER TABLE `group` RENAME `member`
group is keyword so you must have to enclose into group
For Mysql 5.6.18 use the following command
ALTER TABLE `old_table` RENAME TO `new_table`
Also if there is an error saying ".... near RENAME TO ..." try removing the tick `
RENAME TABLE tb1 TO tb2;
tb1 - current table name.
tb2 - the name you want your table to be called.
According to mysql docs: "to rename TEMPORARY tables, RENAME TABLE does not work. Use ALTER TABLE instead."
So this is the most portable method:
ALTER TABLE `old_name` RENAME `new_name`;
Try any of these
RENAME TABLE `group` TO `member`;
or
ALTER TABLE `group` RENAME `member`;
Rename table
Syntax
The syntax to rename a table in MySQL is:
ALTER TABLE table_name
RENAME TO new_table_name;
Example
Let's look at an example that shows how to rename a table in MySQL using the ALTER TABLE statement. or example:
ALTER TABLE contacts
RENAME TO people;
Running The Alter Command
1.Click the SQL tab at the top.
2.In the text box enter the following command: ALTER TABLE exampletable RENAME TO new_table_name;
3.Click the go button.
source : https://my.bluehost.com/hosting/help/2158
You can use
RENAME TABLE `group` TO `member`;
Use back tick (`) instead of single quote (').
Without giving the database name the table is can't be renamed in my case, I followed the below command to rename the table.
RENAME TABLE current_db.tbl_name TO current_db.tbl_name;
Right Click on View > New Query
And Type:
EXEC sp_rename 'Table', 'NewName'
Then Click on Run button at the top left corner of the page.