I have accidentally created hundreds of indexes that have double quotes in the name. Is it possible to drop them altogether or change their name?
I have tried to delete them individually with drop index '"thing"somethingelse';, with double quotes "drop index '"thing"somethingelse";, with escaped quotes "drop index '\"thing\"somethingelse"';, without quotes, with square brackets, but none of them work.
You need to follow the same rules as with single quotes: double them:
drop index "thing""somethingelse";
would drop an index with the name thing"somethingelse
In Postgres, if an index has double quotes in its name, you need to use the same double quotes when referencing the index in the DROP INDEX statement.
For example, if the index name is "my_index", you would use the following command to drop it:
DROP INDEX "my_index";
It's also possible to use the pgAdmin tool to drop the index, you can navigate to the index you want to drop and then right-click and choose the "Delete/Drop" option.
Please be careful when using the DROP INDEX statement as it will permanently remove the index and any data stored within it. It's recommended to create a backup of your database before performing any destructive operation.
Also, it's important to consider the impact of dropping an index in the performance of your queries. If you drop an index that is frequently used by your queries, the performance of those queries may degrade.
Related
I find myself in a position that I'm sure many others have before. I generated my PostgreSQL schema using a generator and now I'm in production(!), I realise I should really try to remove the double quotes from the table and column names that the generator added i.e.
CREATE TABLE "myschema"."mytable" (....
should have been
CREATE TABLE myschema.mytable (....
I'm writing a PL/pgsql function to loop through information_schema.tables and information_schema.columns and then to execute ALTER TABLE statements to drop the doube quotes.
What I can't grasp from the documentation and searhcing is how to issue that statement.
I know it's an EXECUTE I need but can someone help me with the syntax to rename "mytable" to mytable using that. I'm lost with $$ and format() and quote_ident() and think I'm overcomplicating how to indicate that my original table names will have double quotes.
The double-quotes in your examples are not part of the names, just decorators to preserve original spelling of identifiers. See:
Are PostgreSQL column names case-sensitive?
While working with lower-case, legal identifiers (like your examples suggest), those double-quotes are purely optional. Add them or leave them.
If you've been unwise enough to create objects with illegal names or with mixed case, those double-quotes are required at all times. The cure is to stick to legal, lower-case identifiers.
Start by reading the manual about identifiers.
I need to delete a Primary key indexes on some database tables. Afterwards indexes will be re-created. Meanwhile some calculation will be performed.
I have following SQL Commands:
DROP INDEX "SAPSR3"."KNA1~0";
that are working correctly.
But once called within ABAP program:
EXEC SQL.
DROP INDEX SAPSR3.KNA1~0
ENDEXEC.
The message "SQL error 911 occurred when executing Native SQL" has been raised.
I've also tried with:
CALL FUNCTION 'RSDG_KEY_INDEX_DEL'
EXPORTING
i_tablnm = lv_tblname. " containing "KNA1"
This also doesn't work.
Is there any FM or similar way to perform such activity?
I also tried:
EXEC SQL.
DROP INDEX KNA1~0
ENDEXEC.
What you tried, in the SQL console (DROP INDEX "SAPSR3"."KNA1~0";), and in the ABAP program (DROP INDEX SAPSR3.KNA1~0), are not written identically as you can see!
I have just tried in ABAP below the version that you tried in the SQL console, and it dropped the index as intended!
EXEC SQL.
DROP INDEX "SAPSR3"."KNA1~0"
ENDEXEC.
So, why, in your ABAP attempt, did you remove the double quotes around the index name? (and also for the scheme name, but I guess it has less undesired effect).
In the SQL console, I'm really not sure that DROP INDEX SAPSR3.KNA1~0 works! Because there is the special "tilde" character, you need to add double quotes around the index name, the following is the minimal working syntax: DROP INDEX SAPSR3."KNA1~0".
I have to change my column data type and it is indexed. Altering the column giving me error that I need to drop the index, disabling won't work.
The question is how am I going to drop and recreate the exact index automatically? I can drop them but I have got no idea how I will be recreating them.
Cheers
What DBMS are you using? Some DB Management Systems have the ability to script their database objects, but the commands differ between them.
If you can create a script, you'd do so, drop the index, change the field's datatype, save, then run the script to re-create the index.
If you can't create a script for your index with your particular DBMS, you'll have to write down all the relevant fields and settings by hand, then drop the index, change the field's datatype, save, then manually recreate the index from your notes.
One way to do this would be to create your new index field then do an update query like:
UPDATE yourTable
SET newIdx = oldIdx
Then once the new index is filled just set it as the index field and remove the old field.
I have a table with a space in the name generated by a system.
I am trying to alter the table name to remove the space so that it can be processed by a library the pre-exists.
I am trying:
ALTER TABLE 'My Table'
RENAME TO 'MyTable';
I have also tried double quotes, no luck.
Any pointers?
[This will not work in MS-Access. Tables cannot be renamed in Access. Not clear if original question applied to MS Access.]
Square brackets:
ALTER TABLE [My Table]
RENAME TO [MyTable];
Square brackets can't enclose the entire object "path" so this won't work:
ALTER TABLE [MyDatabase.dbo.My Table]
but this will
ALTER TABLE [MyDatabase].[dbo].[My Table]
For MySQL, single quotes, double quotes or brackets did NOT work for me. Only backticks (aka backquotes) worked.
So, try this:
ALTER TABLE `My Table`
RENAME TO MyTable;
[My Table]
You can use square brackets in SQL to get around this.
It has many functions, you can use keywords in tables, put spaces and periods in table names or schemas, etc.
For example you can have the schema [Work.Employees]. With the square bracket it would be [Work.Employees].Addresses (schema, table). However, if you forget the brackets it will attempt to find the database Work -> schema Employees -> Table Addresses.
However, it is generally good practice to avoid doing any of the above :)
This is one of those things that is a lot easier to achieve using the Access GUI!
To do the same in SQL DDL you must 'clone' the table, for which you must already have knowledge of all the attribute names, types, constraints, etc, noting it may have features that are not creatable via SQL DDL e.g. Validation Rules. Then you need to populate it using the original table. The you drop the original. Phew!
I would like to rename an index. I've looked at the alter table documentation, but I can't figure out the syntax to simply rename an index. When doing it through the MySQL GUI, it drops the index, and creates a new one. While this works, I would like to avoid rebuilding the entire index just to change the name of an index.
[ADDITIONAL INFO]
In the alter table documentation it states
Alterations that modify only table
metadata and not table data can be
made immediately by altering the
table's .frm file and not touching
table contents. The following changes
are fast alterations that can be made
this way:
* Renaming a column or index.
However, when I tried to rename the index by editing the .frm file (on a test database) and restarting the server, it now states "Could not fetch columns" in the UI when trying to list the columns, and when trying to run a query, it returns the error "Unknown table engine ''". The .frm file has a lot of binary content. Is there a good tool for editing the binary info.
I answered this question in 2009. At that time there was no syntax in MySQL to rename an index.
Since then, MySQL 5.7 introduced an ALTER TABLE RENAME INDEX syntax.
http://dev.mysql.com/doc/refman/5.7/en/alter-table.html says in part:
RENAME INDEX old_index_name TO new_index_name renames an index. This is a MySQL extension to standard SQL. The content of the table remains unchanged. old_index_name must be the name of an existing index in the table that is not dropped by the same ALTER TABLE statement. new_index_name is the new index name, which cannot duplicate the name of an index in the resulting table after changes have been applied. Neither index name can be PRIMARY.
Earlier versions of MySQL, e.g. 5.6 and earlier, support no syntax in ALTER TABLE to rename an index (or key, which is a synonym).
The only solution was to ALTER TABLE DROP KEY oldkeyname, ADD KEY newkeyname (...).
There is no ALTER INDEX command in MySQL. You can only DROP INDEX and then CREATE INDEX with the new name.
Regarding your update above: perhaps the documentation isn't precise enough. Regardless, there's no SQL syntax to rename an index.
An index is a data structure that can be rebuilt from the data (in fact it's recommended to rebuild indexes periodically with OPTIMIZE TABLE). It takes some time, but it's a commonplace operation. Indexes data structures are separate from table data, so adding or dropping an index shouldn't need to touch the table data, as the documentation says.
Regarding the .frm file, MySQL does not support editing the .frm file. I wouldn't do it for any reason. You are 100% guaranteed to corrupt your table and make it unusable.
For MySQL 5.7:
ALTER TABLE tbl_name RENAME INDEX old_index_name TO new_index_name
For MySQL older versions:
ALTER TABLE tbl_name DROP INDEX old_index_name, ADD INDEX new_index_name (...)
See http://dev.mysql.com/doc/refman/5.7/en/alter-table.html
This question was asked ages ago, and was last updated over half a year ago.
Still I feel the need to add this tip:
If the indexed column is used elsewhere as a foreign key, you may encounter an error related to that. Doing this may help:
SET FOREIGN_KEY_CHECKS = 0;
ALTER TABLE tbl DROP INDEX index_name;
ALTER TABLE tbl ADD INDEX new_index_name (indexed_column);
SET FOREIGN_KEY_CHECKS = 1;
Hope someone finds this useful.