Issues creating foreign key in Xampp/phpmyadmin - java

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;

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.

Getting "org.h2.jdbc.JdbcSQLException: Table "BOOK_PUBLISHERS" not found; SQL statement:"

Using Spring Boot, with Spring Data JPA and H2 in-memory database for process of mapping a many-to-many relationship.
I have two table with entities Book & Publisher. where they many to many relations ship
Created joining table to store the Book_Id and Publisher_id as foreign keys in book_publisher table as below.
create table book_publisher
(
book_id number not null,
publisher_id number not null,
PRIMARY KEY (book_id, publisher_id),
CONSTRAINT fk_bookpublisher_book FOREIGN KEY (book_id) REFERENCES CURRENCY (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_bookpublisher_publisher FOREIGN KEY (publisher_id) REFERENCES VENUE (id) ON DELETE CASCADE ON UPDATE CASCADE
);
I did not write the domain class for the book_publisher table.
But i am getting:
org.h2.jdbc.JdbcSQLException: Table "BOOK_PUBLISHERS" not found; SQL
statement: insert into book_publisher (book_id, publisher_ID) values
(?, ?) [42102-191] " while storting book object(where book class has
publisher objects as "SET" variable
How to solve this error?
If you're running the creation script you mentioned, it'll create a many_to_many relationship between the tables CURRENCY and VENUE. So I would say the code snippet you shared is wrong and this might be why the table BOOK_PUBLISHER was not created successfully.
The right SQL code to create many_to_many relationship (between the tables BOOK and PUBLISHER) should be:
create table book_publisher
(
book_id number not null,
publisher_id number not null,
PRIMARY KEY (book_id, publisher_id),
CONSTRAINT fk_bookpublisher_book FOREIGN KEY (book_id) REFERENCES BOOK (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_bookpublisher_publisher FOREIGN KEY (publisher_id) REFERENCES PUBLISHER (id) ON DELETE CASCADE ON UPDATE CASCADE
);

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)

What is the primary key of an audit table managed by Hibernate Envers?

I am using Hibernate Envers to audit some entities. I manually created the associated audit tables. However, I am having trouble determining what an audit table's primary key should be. For example, consider a fictional table designed to store customers:
CREATE TABLE CUSTOMER
(
CUSTOMER_ID INTEGER,
CUSTOMER_NAME VARCHAR(100),
PRIMARY KEY (CUSTOMER_ID)
)
And you create the audit table:
CREATE TABLE CUSTOMER_REVISION
(
REVISION_ID INTEGER,
REVISION_TYPE_ID INTEGER,
CUSTOMER_ID INTEGER,
CUSTOMER_NAME VARCHAR(100),
PRIMARY KEY (???)
)
Here were the options I considered:
Primary key: REVISION_ID
This cannot be the primary key because multiple entities of the same class may be modified during the same revision.
Primary key: (REVISION_ID, CUSTOMER_ID)
This seems more likely, but I'm not sure if Envers will insert multiple records per customer per revision.
Primary key: (REVISION_ID, REVISION_TYPE_ID, CUSTOMER_ID)
This seems like overkill, but it may be possible that Envers will insert different types of records (add, modify or delete) per customer per revision.
Primary key: A new column
Perhaps the primary key must simply be another column containing a synthetic primary key.
What is the true primary key of an audit table managed by Hibernate Envers?
Judging by the examples in the documentation, it appears that the primary key in my example would be (REVISION_ID, CUSTOMER_ID). Here is the example in the documentation:
create table Address (
id integer generated by default as identity (start with 1),
flatNumber integer,
houseNumber integer,
streetName varchar(255),
primary key (id)
);
create table Address_AUD (
id integer not null,
REV integer not null,
flatNumber integer,
houseNumber integer,
streetName varchar(255),
REVTYPE tinyint,
***primary key (id, REV)***
);
The primary key of audit table is the combination of original id(id) and revision number(rev) of the audit table.
As the official documentation there can be at most one historic entry for a given entity instance at a given revision, which simply means unique combination of above two column.

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