F
F
furyon2015-11-25 10:31:40
PostgreSQL
furyon, 2015-11-25 10:31:40

Why FOREIGN KEY without CONSTRAINT?

Hello!
In general, I create 2 tables and a foreign key from the 2nd to the 1st:

CREATE TABLE tbl_1 (
  id SERIAL PRIMARY KEY
);

CREATE TABLE tbl_2 (
  id     SERIAL PRIMARY KEY,
  tbl_id INT
);
ALTER TABLE tbl_2 ADD CONSTRAINT tbl_2_tbl_1__fk FOREIGN KEY (tbl_id) REFERENCES tbl_1;

I'm trying to insert without an associated entry and I get an error, that's right: Next, I remove the CONSTRAINT and the entry is inserted
INSERT INTO tbl_2 (tbl_id) VALUES (1);
ALTER TABLE tbl_1 DROP CONSTRAINT tbl_1_pkey CASCADE;
INSERT INTO tbl_2 (tbl_id) VALUES (1);

I can’t understand, FOREIGN KEY remains but in fact does nothing (or does it?), Why is it done like that?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Mokhov, 2015-11-25
@furyon

FOREIGN KEY is CONSTRAINT tbl_1_pkey
i.e. this team
you deleted FOREIGN KEY

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question