HSQLDB - Override the default name for the unique index created with a primary key - hsqldb

I can create a primary key specifying its name with some thing like this:
ALTER TABLE MY_TABLE ADD CONSTRAINT MY_TABLE_PK PRIMARY KEY (ID);
The engine also creates a unique index named something like SYS_IDX_11905.
I don't know of a way to specify its name at creation and if I try to rename it with
ALTER INDEX SYS_IDX_11905 RENAME TO MY_TABLE_ID_UINDEX
I get java.lang.RuntimeException: org.hsqldb.HsqlException: user lacks privilege or object not found: SYS_IDX_11905.
Is there any way in which I can specify the name of the index?

The user cannot drop or rename the automatically created indexes. See also http://hsqldb.org/doc/guide/dbproperties-chapt.html#dpc_sql_conformance for the sql.sys_index_names property, which can be set to use the same name for the index as the primary key constraint.

Related

HeidiSQL does not allow me to create a foreign key, what can I do?

I'm trying to create some tables in HeidiSQL, the main key allows me to create it without any problem but when trying to create a foreign key I get error (1064) I would like to know if they could help.
Table 1. Where the main key is located
Table 2. Where the foreign key should go
Log in with Root
Create the tables
fill the tables with the given information
Create the main key in id_clientes
Create the foreign key in the table t_clientes put in the cell with the same name bearing the main key (id_clientes)
Those are at least the steps I have been given for this task but I will appear error 1064, even if I try to switch to another cell does not allow me the foreign key.
try this, I believe it will work
ALTER TABLE t_clientes
ADD CONSTRAINT id_clientes_fkey FOREIGN KEY (id_clientes)
REFERENCES t_ventas(id_clientes) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT

How to add a foreign key for a table that is already created?

I have added a table to a database called settings. This table has a column called id (integer) which is the pirmary key. I have also added a column called settingsID to a table called sessions. What I want to do is add a foreign key to settingsID which references the primary key.
I don't want to create a new table as it is already created. All I want to do is to references the id from the settings table in settingsID which is in sessions table.
ALTER TABLE Sessions ADD FOREIGN KEY (_SettingsID) REFERENCES settings (id)
Here is my error:
near "FOREIGN": syntax error
Can someone tell me the right way to approach this?
Short answer: you can't.
This is documented as part of the SQL Features That SQLite Does Not Implement:
Only the RENAME TABLE, ADD COLUMN, and RENAME COLUMN variants of the ALTER TABLE command are supported. Other kinds of ALTER TABLE operations such as DROP COLUMN, ALTER COLUMN, ADD CONSTRAINT, and so forth are omitted.
Bold emphasis is mine.
So basically you would need to create the constraint at the time when the table is created. For your use case, one solution would be to:
create a new table (with the foreign key constraint)
copy the data from the old table
drop the old table (or better yet, rename it, so you have a backup)
rename the new table

Alter table in impala : make a column a primary key

Using Hue, how can I alter a table to make a prexisting column a primary key?
I check and things like :
ALTER TABLE table_name ADD CONSTRAINT colname PRIMARY KEY (cs_id);
is not syntactically correct.
NB: data is stored using Kudu file system.
First, Impala does not support alter contraint as an option in alter table.
Second, primary keys are very limited:
The primary key columns must be the first ones specified in the CREATE TABLE statement.
I don't think you can change the primary key after it has been defined. In Impala, the data is clustered (i.e. sorted) by the primary key, so any change would be quite expensive.
You probably need to recreate the table and reload it with data.
When you are storing as Kudu, you need to consider that the PK columns need to be all created at the creation of the table.
Impala does not support altering primary keys.
I'm afraid you need to delete and create the table again.

Index should be dropped but is preventing new primary key from being added

I have a strange situation that I don't understand. I am running a block of SQL in a merge module to update a oracle schema. I am trying to change the primary key of several tables so I am performing the following steps:
Drop FK constraints,
Drop PK,
Drop PK Index (if the index persists after the PK is dropped),
Add new PK,
Add FK's
Here's my problem. All is well until we get to the part where indexes are dropped. The primary is dropped and (to the best of my knowledge) any index that Oracle created based on that key will drop instantly as well. I was having issues with the index persisting so I added the DROP INDEX script to be certain it is removed, however, this is what happens:
Alter Table TABLE1 Drop Constraint TABLE1_PK
The command ran successfully
DROP INDEX TABLE1_PK
ORA-01418: specified index does not exist (This is exactly what I expected)
ALTER TABLE TABLE1 ADD (CONSTRAINT TABLE1_PK PRIMARY KEY (TABLE_KEY) ENABLE VALIDATE)
ORA-00955: name is already used by an existing object (The object being, in each case, the old index)
This of course prevents any of the FK's from linking because they are now based on the new key. When I run this in TOAD, the SQL works, but I can't figure out why it doesn't through the merge module. Can anyone help?

Syntax for naming foreign keys

Using:
ALTER TABLE dbo.Table1Name
ADD FOREIGN KEY (colname)
REFERENCES dbo.Table2Name (colname)
I get a foreign key with a name like:
FK___colname__673F4B05
I want it to be named:
FK_Tabl1Name_Table2Name,
...so that it will be easy to read when browsing the DB structure in SSMS. I know I can go back into the GUI and do this, but I want to be able to script it.
So What's the SQL sytnax for adding a name to the FK? Nothing I've found online seems to bother with this.
Here's how you can assign your chosen name to the foreign key constraint:
ALTER TABLE dbo.Table1Name
ADD CONSTRAINT FK_Tabl1Name_Table2Name
FOREIGN KEY (colname) REFERENCES dbo.Table2Name (colname)