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);
Related
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;
I need a short answers how to make constraints when
I have table A, B, C, E
Table E have E_ID (E id is primary key)
Table B have B_ID, E_ID (E id are foreign keys)
Table C have C_ID, E_ID (E id are foreign key)
Table A have A_ID,B_ID,C_ID (B and C id are foreign keys)
where id are primary keys.
I want constrain to make sure I table A i have records where C_ID and B_ID has same E_ID.
And it should still have 3rd normal form.
You need the following four constraints:
alter table a add constraint fk1 foreign key (b_id) references b (b_id);
alter table a add constraint fk2 foreign key (c_id) references c (c_id);
alter table c add constraint fk3 foreign key (e_id) references e (e_id);
alter table b add constraint fk4 foreign key (e_id) references e (e_id);
One method is to repeat e_id in table a and then use this for foreign key constraints.
Note the foreign key relationships to the unique keys in this data model:
create table e (eid int primary key);
create table b (bid int primary key,
eid int references e(eid),
unique (bid, eid)
);
create table c (cid int primary key,
eid int references e(eid),
unique (cid, eid)
);
create table a (aid int primary key,
bid int references b(bid),
cid int references c(cid),
eid int references e(eid),
foreign key (bid, eid) references b(bid, eid),
foreign key (cid, eid) references c(cid, eid)
);
Here is a SQL Fiddle.
create table a (
a_id integer,
a_name varchar2(10),
PRIMARY KEY (a_id)
)
create table b(
b_id,
b_name varchar2(10),
a_id integer,
PRIMARY KEY (a_id),
FOREIGN KEY (a_id) REFERENCES a(a_id)
)
If the children have FKs linking them to the parent, then you can use DELETE CASCADE on the parent.
CREATE TABLE book
( book_id int not null,
book_name varchar(50) not null,
CONSTRAINT book_pk PRIMARY KEY (book_id)
);
CREATE TABLE bookdetails
( detail_id int not null,
book_id int not null,
CONSTRAINT fk_book
FOREIGN KEY (book_id)
REFERENCES book(book_id)
ON DELETE CASCADE
);
Complete Procedure as you required just change the tables name in procedure
visit https://anzblog.com/2017/04/29/create-procedure-delete-child-table-data-parent-data-deleted/
It will work for you.As per my understanding.
First of all I´m working with DB2.
My problem:
I have a table A with a primary key.
A table B with 2 primary keys (one of them is a foreign key of A)
A table C with a primary key
A table D which has got the primary keys of B and C
How can I create table D?
My first idea was that, but it doesn't work:
Create Table D(
A varchar(20) not null references B(A),
B varchar(20) not null references B(name of prim key attribute from B),
C varchar(20) not null references C,
primary key(A,B,C)
);
Hope you understand my problem and can help me.
Foreign keys reference the names of columns, not the names of constraints.
The set up . . .
create table A (
col_a int primary key
);
create table B (
col_a int not null,
col_b int not null,
primary key (col_a, col_b),
foreign key (col_a) references A (col_a)
);
create table C (
col_c int primary key
);
And the execution . . .
create table D (
col_a int not null,
col_b int not null,
col_c int not null,
primary key (col_a, col_b, col_c),
foreign key (col_a, col_b) references B (col_a, col_b),
foreign key (col_c) references C (col_c)
);
As Marc_s commented there can only be ONE primary key in a table ie, you cannot have more than one primary key in a table.
As a solution to your problem you can probably combine two columns of table B and table C which can together act as a primary key for table D
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;