HSQL: Creating index if not existing - hsqldb

I'm initializing a HSQL database 2.2.9 via Spring using
<jdbc:initialize-database enabled="true">
<jdbc:script execution="INIT" location="classpath:./create-tables.sql"/>
</jdbc:initialize-database>
In create-tables.sql I use
CREATE TABLE IF NOT EXISTS MyTable(...);
The table also has an index. I'm looking for a better way than always dropping and creating the index.
I tried:
CREATE INDEX IF NOT EXISTS myIndex ...;
does not work
I can create a function indexExisting() checking the system tables and returning count(*) > 0 if the index is found, but if I write
IF indexExisting() = 0 THEN ...
directly into the .sql file, it says
java.sql.SQLSyntaxErrorException: unexcepted token: IF
Also a stored procedure does not seem to help, as they may not contain DROP statements, as far as I read.
So a solution other than dropping / creating the index would be appreciated.
Thank you

The latest version of HSQLDB supports both:
CREATE INDEX IF NOT EXISTS myIndex ...
DROP INDEX IF EXISTS myIndex

Related

Create index if not exists with locks in Postgresql

I have a code statement related with index creating
create index if not exists foo_table_index_any_id on paublic.foo_table (any_id);
Will the table be locked exclusively if the index has been already created there?

Delete primary index of a database table programmatically

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".

Postgres- Script to create a script to create existing indexes on given tables

I have a database with a lot of tables in it. Some of them have names which start with "mytable_". Those tables have some indexes. Now I need to move those indexes also to different server which has similar tables.
I would like to have a script which will create all those indexes that are in given tables on my local server.
Also I would like to make it this way that if this index is already created it will not crash.
How can it be done in Postgres (pg admin)
You can use pg_get_indexdef() to extra all the index definitions:
select replace(pg_get_indexdef(format('%I.%I', schemaname, indexname)::regclass), ' INDEX ', ' INDEX IF NOT EXISTS ')||';' as ddl
from pg_indexes
where schemaname = 'public'
and tablename like 'mytable\_%';
The replace is used to "inject" the IF NOT EXISTS so that the statement won't fail if such an index is already present in the target database.
Just spool the result of that query to a file.

oracle create table into new database with primary key and indexes

I have a table called TableA in DatabaseA and I want to create the same TableA in DatabaseB. I am able to do so but copying only the structure and the data, I seem unable to also create the primary keys and indexes. Is there an SQL statement I can use that copies the table structure, the table data, the primary keys and indexes please?
I am using Oracle 11G.
1. First Method
To get tables and indexes without data see following post
Stack Post
after creating table you can load data using
insert into dest_table as select * from source_table
2. Second Method
use expdp to take backup of source table using table=yourtable parameter as this will by default will take indexes and when you will import using impdp on destination database it will automatically rebuild those indexes.

How do I rename an Index in MySQL

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.