MariaDB unable to drop column named "`" - sql

Not sure how this happened, but a column got created named: "`". (just the back tick). When I attempt to drop this column, I end up with a syntax error.
Outside of taking a SQL dump and fixing the dump in a text editor, Does anybody have a suggestion to fix this query?
ALTER TABLE tableName DROP COLUMN "`";

You need to escape the inner backtick with another backtick, so:
alter table tableName drop column ````;

Related

Moving a table from one schema to another in Exasol

I am trying to move a table which resides in a certain schema to a different schema with the same table name. I have tried the following but they do not work:
rename <OLD_SCHEMA_NAME>.<TABLE_NAME> TO <NEW_SCHEMA_NAME>.<TABLE_NAME>;
The error that appears is:
SQL Error [42000]: invalid identifier chain for new name [line 1, column 100] (Session: 1722923178259251200)
and
ALTER TABLE <OLD_SCHEMA_NAME>.<TABLE_NAME> RENAME <NEW_SCHEMA_NAME>.<TABLE_NAME>;
The error that appears is:
SQL Error [42000]: syntax error, unexpected IDENTIFIER_PART_, expecting COLUMN_ or CONSTRAINT_ [line 1, column 62] (Session: 1722923178259251200)
Many Thanks!
According to Exasol documentation there is no way to move table between schemas using RENAME statement:
Schema objects cannot be shifted to another schema with the RENAME
statement. For example, 'RENAME TABLE s1.t1 TO s2.t2' is not allowed.
I would move the table this way:
create table <NEW_SCHEMA_NAME>.<TABLE_NAME>
like <OLD_SCHEMA_NAME>.<TABLE_NAME>
including defaults
including identity
including comments;
insert into <NEW_SCHEMA_NAME>.<TABLE_NAME>
select *
from <OLD_SCHEMA_NAME>.<TABLE_NAME>;
drop table <OLD_SCHEMA_NAME>.<TABLE_NAME>;

I'm trying this query to change attribute name

alter table car
rename column office-id to office_id
Why is it giving out "missing to keyword" error?
This seems to be a potential solution:
Error trying to rename columns with space in oracle table. Error - SQL Error : ORA- 00946 : missing TO keyword
alter table car rename column "office-id" to office_id
You'll likely need to enclose the column name in quotes
alter table car rename column "office-id" to office_id
Also you don't specify the dialect (the db is it Oracle, MS SQL, Postgres?)

Renaming tables

I keep getting a syntax error in PSQL and I can't figure out why. Any help would be greatly appreciated
SELECT page_code, developer
FROM page
WHERE data_approved=null and approved_by=null
ALTER TABLE page
RENAME COLUM page_code TO unapproved;
You are, probably, looking for alias, not for altering table:
SELECT page_code AS unapproved,
developer
FROM page
WHERE data_approved IS null
AND approved_by IS null
Note, that page table will stay intact while its page_code field will be represented in this particular query as unapproved. Please, be very careful with altering tables since doing this you change the rules for all the users.
Another issue is = null which returns null (neither true nor false). IS null is the right syntax.
ALTER TABLE table_name RENAME new_table_name;
this command will help you to change table name
Rename is used to change the table name and
change is used to rename column name

rename a column starting with # in Postgresql

I add some shape files in Postgres and there is a column which is named #id. I wanted to select this column but there is a syntax error because of #.
I have many tables which containing this field, how can I change it in not manually way?
You actually can select #id by either using the unicode character or by surrounding the column name with double-quotes:
SELECT U&"\0040id" FROM tablename;
SELECT "#id" FROM tablename;
You can use either method to rename the column as well:
ALTER TABLE tablename RENAME COLUMN U&"\0040id" to "id";
ALTER TABLE tablename RENAME COLUMN "#id" to "id";
The principle is the same, you just have to make sure you have the name enclosed in double quotes.
ALTER TABLE "some_table"
RENAME COLUMN "old_name" TO "new_name";

Rename a column with space in it

Someone mistakenly created a table in which all the column names has a leading space in it. For example: 'accountid' is now ' accountid'.
I am going to write a SQL statement to rename these columns. The one I wrote is:
ALTER TABLE mytable RENAME COLUMN ' accountid' TO 'accountid';
However, I got the following error:
Error : ERROR: syntax error at or near "' accountid'"
Can someone instruct me how to rename these? How to change my statement to make it runnable? I use PostgreSQL.
Many thanks.
In PostgreSQL, you use double-quotes for identifiers (if necessary): "
ALTER TABLE mytable RENAME COLUMN " accountid" TO "accountid";
See here and browse to 4.1.1
You can even put other characters:
select c.comment "actor.comment" from post p join comment c on p.id = c.post_id;