Join two unrelated tables with Criteria API - sql

Suppose I have three tables:
CREATE TABLE "C" (
id bigint NOT NULL,
cc integer,
CONSTRAINT "C_ID" PRIMARY KEY (id)
);
ALTER TABLE "C" OWNER TO ***;
CREATE TABLE "B" (
id bigint NOT NULL,
c_id bigint references "C"(id),
bb integer,
CONSTRAINT "B_ID" PRIMARY KEY (id)
);
ALTER TABLE "B" OWNER TO ***;
CREATE TABLE "A" (
id bigint NOT NULL,
c_id bigint references "C"(id),
aa integer,
CONSTRAINT "A_ID" PRIMARY KEY (id)
);
ALTER TABLE "A" OWNER TO ***;
And I need to select rows from the table A which have common foreign keys (c_id) with rows from the table B. Though, I have the following SQL query
SELECT "A".aa AS "AA", "B".bb AS "BB", "C".id AS "C_ID"
FROM "A"
INNER JOIN "C" on "C".id = "A".c_id
INNER JOIN "B" on "C".id = "B".c_id;
In Java
#Entity
class A {
...
#ManyToOne
C cObj;
...
}
#Entity
class B {
..
#ManyToOne
C cObj;
..
}
#Entity
class C {
..
}
So question: how can I write an equivalent Criteria API Query?
Thanks.

Related

sqlite insert into table with NULL foreign key

I have the following tables: (notice that SUPP may be NULL)
CREATE TABLE IF NOT EXISTS A
(
ID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE IF NOT EXISTS B
(
ID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE IF NOT EXISTS C
(
ID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE IF NOT EXISTS D
(
SID INTEGER NOT NULL,
DID INTEGER NOT NULL,
SUPP INTEGER,
PRIMARY KEY (SID, DID),
FOREIGN KEY (SID) REFERENCES A (ID),
FOREIGN KEY (DID) REFERENCES B (ID),
FOREIGN KEY (SUPP) REFERENCES C (ID)
);
Assuming table A contains ID=2 and table B contains ID=5.
I want to perform the following insert:
INSERT OR REPLACE INTO D VALUES (2,5,NULL);
However I get the following error:
Foreign key mismatch - D referencing C (INSERT INTO "main"."D" ...
I'm assuming the issue stems from the NULL foreign key but I don't know what I did wrong or how to fix it.
You need to use valid referenced value if you are inserting into child table. The ID 2 in table A and ID 5 in table B has to be present.
CREATE TABLE IF NOT EXISTS A
(
ID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE IF NOT EXISTS B
(
ID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE IF NOT EXISTS C
(
ID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE IF NOT EXISTS D
(
SID INTEGER NOT NULL,
DID INTEGER NOT NULL,
SUPP INTEGER,
PRIMARY KEY (SID, DID),
FOREIGN KEY (SID) REFERENCES A (ID),
FOREIGN KEY (DID) REFERENCES B (ID),
FOREIGN KEY (SUPP) REFERENCES C (ID)
);
INSERT INTO A VALUES (2);
INSERT INTO B VALUES (5);
INSERT INTO D VALUES (2, 5, NULL);

PostgreSQL foreign key with inheritance

I have a PostgreSQL database, 3 tables and my schema as follows.
CREATE TABLE table_a (
id SERIAL PRIMARY KEY,
name_a VARCHAR(255)
);
CREATE TABLE table_b (
name_b VARCHAR(255)
) INHERITS (table_a);
CREATE TABLE table_c (
name_c VARCHAR(255)
) INHERITS (table_a);
insert into table_b (name_a, name_b) values('table A','table B1');
insert into table_b (name_a, name_b) values('table A','table B2');
insert into table_c (name_a, name_c) values('table A','table C1');
insert into table_c (name_a, name_c) values('table A','table C2');
select * from table_a;
select * from table_b;
select * from table_c;
Now i want to add an association between Table Band Table C like this:
I do not know if this is possible when we inherit the same table ?
I do not see how I can create this association ?
You need a unique identifier or primary key on table_b. Quoting docs:
All check constraints and not-null constraints on a parent table are automatically inherited by its children, unless explicitly specified otherwise with NO INHERIT clauses. Other types of constraints (unique, primary key, and foreign key constraints) are not inherited.
CREATE TABLE table_a (
id SERIAL PRIMARY KEY,
name_a VARCHAR(255)
);
CREATE TABLE table_b (
name_b VARCHAR(255),
primary key (id) --> here you set the PK
) INHERITS (table_a);
CREATE TABLE table_c (
name_c VARCHAR(255),
id_b int references table_b(id) --> the fk to b through pk
) INHERITS (table_a);
Alternative way
In your second diagram, do you have a kind of identifier for table_b. It is right because you can reference table by a unique field. In this case, the DDL will be like this:
CREATE TABLE table_a (
id SERIAL PRIMARY KEY,
name_a VARCHAR(255)
);
CREATE TABLE table_b (
name_b VARCHAR(255),
id_b SERIAL UNIQUE --> Here the unique id for b
--, primary key (id) -- optionally
) INHERITS (table_a);
CREATE TABLE table_c (
name_c VARCHAR(255),
id_b int references table_b(id_b) --> the fk to b through unique
) INHERITS (table_a);
I prefer the first approach, but I have posted also this one just for academical purposes.

Check primary keys values

These are tables in SQLite: especie_similar
CREATE TABLE especie_similar (
id_especie INTEGER NOT NULL,
id_similar INTEGER NOT NULL,
PRIMARY KEY (
id_especie,
id_similar
),
FOREIGN KEY (
id_especie
)
REFERENCES especie (id_especie) ON DELETE CASCADE,
FOREIGN KEY (
id_similar
)
REFERENCES especie (id_especie) ON DELETE CASCADE
);
And especie table:
CREATE TABLE especie (
id_especie INTEGER PRIMARY KEY AUTOINCREMENT
NOT NULL,
nombre_especie VARCHAR NOT NULL,
cient_especie VARCHAR NOT NULL,
desc_especie TEXT,
nidificacion TEXT NOT NULL,
dimorfismo INTEGER NOT NULL,
genero_id INTEGER NOT NULL,
endemismo_id INTEGER NOT NULL,
abundancia_id INTEGER NOT NULL,
FOREIGN KEY (
genero_id
)
REFERENCES genero (id_genero),
FOREIGN KEY (
endemismo_id
)
REFERENCES endemismo (id_endemismo),
FOREIGN KEY (
abundancia_id
)
REFERENCES abundancia (id_abundancia)
);
My situation: Table especie has multiple datas and I want to insert in especie_similar ids from table especie.
How can i code a trigger or sql statement checking if id_especie and id_similar are not equals?
How can i code to retrieve all especies distinct of target id
(ex: table especie_similar has id_especie=1 and id_similar=2
indicating that species id 1 is similar to another species id 2 but
cannot insert id_especie=2 and id_similar=1 due to previous
duplicate data)?
Thanks and sorry my english is not good.
For question #1 you can use a standard SQL constraint:
create table especie_similar (
...
check (id_especie <> id_similar) -- your condition here
);
For question #2 you can use a standard "Recursive CTE". See SQLite WITH Clause. Example:
with recursive node as (
select id from especie_similar
where id_especie = 123 -- first node criteria here
union all
select s.id_especie
from node n
join especie_similar s on s.id_similar = n.id
)
select * from node;

Reference from one table to another entire table and specified row

I've got three tables: A, B and C. I want to create table A, and it should have 3 columns, first one should be a PRIMARY KEY.
1) How to create second column, with is a refference to table B,
2) and third with is a refference to 'C.id' row from table C. A.id = C.id
CREATE TABLE A
(
id SERIAL PRIMARY KEY,
// ? - reference to table B
// ? - reference to C.id row. A.id = C.id
)
Database: postgresql
I assume you use mysql database.
CREATE TABLE A
(
id INT NOT NULL PRIMARY KEY,
b_id INT NOT NULL,
c_id INT NOT NULL,
FOREIGN KEY (b_id) REFERENCES B (id),
FOREIGN KEY (c_id) REFERENCES C (id)
) TYPE = INNODB;
Update for using postgresql:
CREATE TABLE "A"
(
id integer NOT NULL,
b_id integer NOT NULL,
c_id integer NOT NULL,
CONSTRAINT id PRIMARY KEY (id),
CONSTRAINT b_id FOREIGN KEY (b_id) REFERENCES "B" (id)
ON UPDATE NO ACTION ON DELETE NO ACTION, --with no action restriction
CONSTRAINT c_id FOREIGN KEY (c_id) REFERENCES "C" (id)
ON UPDATE CASCADE ON DELETE CASCADE --with cascade restriction
)
WITH (
OIDS = FALSE
)
;
ALTER TABLE "C" OWNER TO postgres;

Business-Logic Relationships in SQL

Can anyone please tell me how can one estabilish 1 to 0..1 and 1 to 1..* relationships between tables in SQL (Server)?
Thank you very much.
1 to 1..*
Create a Foreign key from a parent table to the primary key of the child (lookup table).
CREATE TABLE A
(
id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
Somecolumn int,
SomeOtherColumn Varchar(50),
B_id int CONSTRAINT FOREIGN KEY REFERENCES B(id),
-- ...other columns
)
CREATE TABLE B
(
id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
Name Varchar(50)
)
1 to 0..1
Create a table with the primary key also defined as a Foreign key to the parent table
CREATE TABLE [Master]
(
id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
Somecolumn int,
SomeOtherColumn Varchar(50),
-- ...other columns
)
CREATE TABLE [Child]
(
id int NOT NULL PRIMARY KEY,
OtherColumn Varchar(50),
)
ALTER TABLE Child
ADD CONSTRAINT FK_Master FOREIGN KEY (id) REFERENCES Master(id)
One to many
Define two tables (example A and B), with their own primary key
Define a column in Table A as having a Foreign key relationship based on the primary key of Table B
This means that Table A can have one or more records relating to a single record in Table B.
If you already have the tables in place, use the ALTER TABLE statement to create the foreign key constraint:
ALTER TABLE A ADD CONSTRAINT FOREIGN KEY fk_b ( b_id ) references b(id)
* fk_b: Name of the foreign key constraint, must be unique to the database
* b_id: Name of column in Table A you are creating the foreign key relationship on
* b: Name of table, in this case b
* id: Name of column in Table B