HSQLDB - Referencing INFORMATION_SCHEMA in foreign key - java

I tried to create a foreign key on an existing table.
The FK should reference a column on a table contained into INFORMATION:SCHEMA.
However this doesn't seem to be possible.
Is that right?
UPDATE
This is what I execute (as the super user):
ALTER TABLE MY_TABLE ADD CONSTRAINT MY_TABLE_FK FOREIGN KEY (A, B, C) REFERENCES CATALOG.INFORMATION_SCHEMA.TABLES (TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME)
This is the error I get:
[42501][-5501] user lacks privilege or object not found: INFORMATION_SCHEMA.TABLES
java.lang.RuntimeException: org.hsqldb.HsqlException: user lacks privilege or object not found: INFORMATION_SCHEMA.TABLES
Not very helpful since the user I'm using as all the privileges possible and the object definitely exists.

The INFORMATION_SCHEMA.TABLES is a VIEW and as such has no PRIMARY KEY constraint. Therefore it is not possible to create a FOREIGN KEY to reference it.

Related

ALTER TABLE CREATE CONSTRAINT IF NOT EXIST possible?

I have an application which starts (AppStarter) a web server with a web application. The Web Application has migration scripts (flyway).
I want to write some data from AppStarter through JDBC in a table. But I want to create the table if it does not exist. The table also has some constraints.
Within the AppStarter I execute following command:
CREATE CACHED TABLE PUBLIC.CORE_USERROLE_TO_PARAMETER (
ID VARCHAR(32) PRIMARY KEY NOT NULL,
VERSION INTEGER,
USER_ID VARCHAR(32)NOT NULL,
ROLE_ID VARCHAR(32) NOT NULL,
PARAMETER VARCHAR(255) NOT NULL
);
ALTER TABLE PUBLIC.CORE_USERROLE_TO_PARAMETER ADD CONSTRAINT PUBLIC.CURTBP_USER_ID FOREIGN KEY(USER_ID) REFERENCES PUBLIC.CORE_USER(ID) NOCHECK;
ALTER TABLE PUBLIC.CORE_USERROLE_TO_PARAMETER ADD CONSTRAINT PUBLIC.CURTBP_ROLE_ID FOREIGN KEY(ROLE_ID) REFERENCES PUBLIC.CORE_USER_ROLE(ID) NOCHECK;
The web app also reads some information from this table and creates the tables.
Now I have a sql migration script
CREATE CACHED TABLE IF NOT EXISTS PUBLIC.CORE_USERROLE_TO_PARAMETER (
ID VARCHAR(32) PRIMARY KEY NOT NULL,
VERSION INTEGER,
USER_ID VARCHAR(32)NOT NULL,
ROLE_ID VARCHAR(32) NOT NULL,
PARAMETER VARCHAR(255) NOT NULL
);
But how do I create the constraint only if they does not already exist?
Thanks in advance
Currently I can get if the constraints exists with
select * from INFORMATION_SCHEMA.CONSTRAINTS WHERE CONSTRAINT_NAME='CURTRP_USER_ID'
but how do I build this into a if query with H2
Edit:
I could move the constraint part in total to the migration script, but this seems somehow wrong.
I am working with H2 Database.
Following my comment, this should be possible:
ALTER TABLE PUBLIC.CORE_USERROLE_TO_PARAMETER
ADD CONSTRAINT IF NOT EXISTS PUBLIC.CURTBP_USER_ID
FOREIGN KEY(USER_ID) REFERENCES PUBLIC.CORE_USER(ID) NOCHECK;
ALTER TABLE PUBLIC.CORE_USERROLE_TO_PARAMETER
ADD CONSTRAINT IF NOT EXISTS PUBLIC.CURTBP_ROLE_ID
FOREIGN KEY(ROLE_ID) REFERENCES PUBLIC.CORE_USER_ROLE(ID) NOCHECK;
Use this query to get the foreign key constraints
SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE CONSTRAINT_TYPE = 'REFERENTIAL'
You can try ALTER TABLE IF EXISTS like CREATE IF EXISTS. If its a responsibility of your application only, and not handled by another app or script.

H2 database: referring to a table in root schema from a foreign key constraint

Given a table in root schema:
CREATE TABLE user (
username VARCHAR(50),
password VARCHAR(50));
and a table in Quiz schema:
CREATE TABLE Quiz.Results (
username VARCHAR(50),
points INT,
FOREIGN KEY (username) REFERENCES user(username));
I'm unable to actually create the foreign key, because the database claims the table user does not actually exist. Neither can I subsequently add the foreign key:
ALTER TABLE QUIZ.RESULTS
ADD FOREIGN KEY (username) REFERENCES user (username)
Both tables are, of course, stored in the same database.
Since this is just a piece of homework, I'm more than happy to simply skip adding a foreign key. But I'm curious if this is indeed a limitation in H2, a bug, or if it works as intended.
Can I somehow refer to table user outside the quiz schema?
You would need to explicitly set the schema name if you refer to a table in a different schema. The default schema name for H2 is public. Example:
CREATE TABLE user (
username VARCHAR(50),
password VARCHAR(50));
create schema quiz;
CREATE TABLE Quiz.Results (
username VARCHAR(50),
points INT,
FOREIGN KEY (username)
REFERENCES public.user(username));
To create the foreign key constraint later, use:
ALTER TABLE QUIZ.RESULTS
ADD FOREIGN KEY (username)
REFERENCES public.user(username) ;
yes very much possible. You need to use corresponding Schema name for both tables.
suppose your defualt schema name is DefaultSchema then your query will be
ALTER TABLE QUIZ.RESULTS
ADD FOREIGN KEY (username) REFERENCES DefaultSchema.user (username)

Hibernate not creating foreign key constraint on MySQL

I am using Hibernate and MySQL when my mapping for table is done and when I look at my DB table its created successfully but their is no foreign key constraint but column is created.
When I try to insert record in child table and when I put id which not exist in parent table in foreign key column then also that row get inserted.
My table engine is innoDB.
If I change dialect to MS-SQL then table get created with foreign key constraint.
Sorry users,
I got issue with this actually my DBA not given permission for ALTER command.
so thats why, when an table with foreign Key is going to create first CREATE command is executed and then ALTER command is executed.
In my case CREATE command is executed successfully but ALTER command does not coz its dnt have permission.

Issues creating foreign key in Xampp/phpmyadmin

this is my piece of code ..
ALTER TABLE ADDRESS
ADD CONSTRAINT ADDRESS_PK PRIMARY KEY(ID),
ADD CONSTRAINT ADDRESS_FK FOREIGN KEY (ID) REFERENCES CUSTOMER (ID)
I created constraints in SQL YoG but in xampp it shows some issues. Any suggestion
Foreign key checklist:
both tables must be InnoDB; recently (MySQL 5.5.5) started shipping InnoDB as the default;
both columns must be defined with the same datatype
including length;
including UNSIGNED qualifiers;
the referenced key must be UNIQUE;
the referenced key must not be NULL-able;

Can't create foreign key

I'm trying to add a foreign key to an existing table, and was having issues. I figured that I had an error in my syntax, so I updated my hibernate.cfg.xml file to auto-update.
As it turns out, hibernate had the same error. Here's my SQL to add the foreign key:
alter table pbi add index FKEA3F7BDE9BAB051 (FK_idP), add constraint FKEA3F7BDE9BAB051 foreign key (FK_idP) references p (idP)
and the error is:
Cannot add or update a child row: a foreign key constraint fails (`db`.`#sql-6f8_3`, CONSTRAINT `FKEA3F7BDE9BAB051` FOREIGN KEY (`fk_idP`) REFERENCES `p` (`idP`))
Can anyone think of a reason why this would fail?
This error means that constraint can not be applied because there are existing records that would violate it.
In your case, pbi table has rows whose FK_idP column has a value for which there are no matching records with that value in idP column of p table.

Categories