deleting column containing white spaces in mysql - mysql

I was wondering how do I drop a column, containing spaces, in mysql.
Tried the following, and got the following exceptions:
alter table test17 drop column [added column2];
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 '[added column2]' at line 1
alter table test17 drop column 'added column2';
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 ''added column2'' at line 1
alter table test17 drop column (added column2);
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 '(added column2)' at line 1
Thanks:)

You should try using back ticks ("`") to quote your column name.
ALTER TABLE test17 DROP COLUMN `added column2`;
OR
Without COLUMN word
ALTER TABLE test17 DROP `added column2`;

Try:
alter table test17 drop column `added column2`;
Note that the quote characters are tilted left.
By the way, if you include spaces as part of identifier names (e.g. functions, procedures, tables, columns, etc.), that is a very, VERY bad practice.

First, column name with space is BAD PRACTICE. Then, if you really want to drop a column, use backtick instead:
ALTER TABLE test17 DROP COLUMN `added_column2`;

Related

mysql VARBINARY datatype is not working while alter table column

mysql> ALTER TABLE googly MODIFY Data VARBINARY;
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 '' at line 1
mysql>
I am trying to alter my mysql database column using above query.
But it is giving me above error.
The reason i am using VARBINARY instead of BINARY is my data is too long.
BINARY giving me below error.
mysql.connector.errors.DataError: 1406 (22001): Data too long for column 'Data' at row 1
Is there any way to fix it ?
Please take a look.

Add the column in mysql and has the same data like the column existing

I want to add the column name "payroll_date_on" in the table and has the default value as the other table column named "jo_time_on".
ALTER TABLE jobs ADD COLUMN payroll_time_on INT(11) AS (jo_time_on) PERSISTED;
While running this I got an error code
Error Code: 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 'AS (jo_time_on) PERSISTED' at line 1
Help me to solve this error and clone the column jo_time_on
I am using the mysql version 5.6.38
try this:
ALTER TABLE jobs ADD COLUMN payroll_time_on INT(11);
UPDATE TABLE SET payroll_time_on = jo_time_on;

I can't rename a mysql table name... it has a space in it

I've imported a csv into MySQL. PHPMyAdmin helpfully created a table called TABLE 8 (with a space).
When I try to rename in SQL:
RENAME TABLE 8 to gender
I get the 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 '`TABLE 8` to `gender`' at line 1
I have tried back-ticks, quotes... seemingly everything...
I guess it's the space that's causing the problem but I'm out of ideas.
The syntax is wrong, you're missing the table keyword (and yes, note the `s to escape the table name containing a space):
RENAME TABLE `TABLE 8` TO gender
Alternatively, you could use the fuller syntax:
ALTER TABLE `TABLE 8` RENAME TO gender

How to solve #1064 error in MySQL?

I got MySQL syntax Error 1064 while I am add new column.
ALTER TABLE `customerdetails` ADD `mobile` DOUBLE(12) NOT NULL ;
where customerdetails is the name of the table.
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 ') NOT NULL' at line 1
Can somebody help me to solve this problem?
When you are defining column type as DOUBLE you have to define how many decimal places it will use
ALTER TABLE customerdetails ADD mobile DOUBLE(12,2) NOT NULL ;
Also first number (Characteristic) needs to be bigger than second number (Mantissa)

can't drop index because its name is an integer

I have some indexes in a table which name is a number(1,2,3,4...)
But when i use the drop command to remove the index i get a 1064 error
DROP INDEX 1 ON table_name;
So my guess is that i cant drop an index when its name is a number? or there is something else i dont know here?
How can it work locally and not on the server?
Error output:
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 '1 on table_name' at line 1
Surround 1 with backticks, i.e.
DROP INDEX `1` ON table_name;