create a foreign key on a primary key of another table

Elena Rondina
CREATE TABLE public.impiegato(
   CF varchar NOT NULL,
   codice_reparto int4 NOT NULL,
    mansione varchar NULL,
CONSTRAINT impiegato_pkey PRIMARY KEY (CF),
CONSTRAINT impiegato_fkey FOREIGN KEY (codice_reparto) REFERENCES   reparto(codice),
);

CREATE TABLE public.reparto(
codice int4 NOT NULL,
nome varchar NULL,
cf_responsabile varchar NULL,
nome_responsabile varchar NULL UNIQUE,
CONSTRAINT reparto_pkey PRIMARY KEY (codice),
CONSTRAINT reparto_cf_responsabile_fkey FOREIGN KEY (cf_responsabile) REFERENCES impiegato(CF)
);

When i run the sql code it tells me that the impiegato table doesn't exist. Can I run a foreign key on a primary key of another table?

sticky bit

The referenced table must exists when a foreign key is declared.

Since in your case the two tables reference each other, it's not possible to solve this by simply creating the right one first.

You have to create the first one (any of them) first without the foreign key constraint, create the second on and then add the foreign key constraint to the first one.

Something along the lines of:

CREATE TABLE public.impiegato
             (cf varchar
                 NOT NULL,
              codice_reparto int4
                             NOT NULL,
              mansione varchar
                       NULL,
              CONSTRAINT impiegato_pkey
                         PRIMARY KEY (cf));

CREATE TABLE public.reparto
             (codice int4
                     NOT NULL,
              nome varchar
                   NULL,
              cf_responsabile varchar
                              NULL,
              nome_responsabile varchar
                                NULL
                                UNIQUE,
              CONSTRAINT reparto_pkey
                         PRIMARY KEY (codice),
              CONSTRAINT reparto_cf_responsabile_fkey
                         FOREIGN KEY (cf_responsabile)
                                     REFERENCES impiegato
                                                (cf));

ALTER TABLE public.impiegato
            ADD CONSTRAINT impiegato_fkey
                           FOREIGN KEY (codice_reparto)
                                       REFERENCES reparto
                                                  (codice);

Unfortunately, you didn't tag your DBMS. From some details I guessed it might be Postgres and the code above hence is Postgres code. If you don't use Postgres, you might need to adapt the ALTER TABLE statement, they can differ between DBMS.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mapping of primary key as foreign key to another table

From Dev

Update primary key in one table which is foreign key in another table

From Dev

MySQL - Inserting Primary Key from one table to another (Foreign Key)

From Dev

I cannot send primary key of one table to another as foreign key?

From Dev

SQL create table primary key and foreign key syntax

From Dev

create primary and foreign key relationship

From Dev

Finding Primary Table for a Foreign Key

From Dev

Create table with both foreign keys and a composite primary key

From Dev

Use of Primary Key as Foreign Key in Foreign Key Table

From Dev

How to add a primary (Surrogate) key from one table into a foreign key of another table?

From Dev

Update Column Of All Rows In A Table From Another Table Using Primary Key And Foreign Key

From Dev

insert a primary key from one table as a foreign key to another table with php lastInsertId()

From Dev

How to display the last inserted ID (primary key) of a table in a html textbox that is in a relationship with a foreign key of another table in mvc

From Dev

Get foreign key field from primary key in another table that linked to many table

From Dev

Create a table who's name must be a primary key in another table

From Dev

Can a unique key ( not a primary key) be a foreign key to other table?

From Dev

query not working for foreign key as a composite primary key of another table oracle SQL

From Dev

Use Auto incremented unique id primary key as foreign key in another table

From Dev

How do I ensure that a foreign key exists as a primary key in another table when data is inserted?

From Dev

get primary key of recently added record and insert into another table as foreign key

From Dev

How do I get the primary key being referenced by a foreign key of another table?

From Dev

How to create two Foreign Key on pivot table linked to one Primary Key

From Dev

Insert into two tables with one table having an auto increment primary key and that same key is used as a foreign key in another table

From Dev

How to refer primary key as Foreign to various table

From Dev

Primary And Foreign key mapping between view and table

From Dev

Join on foreign key of table with the max primary key of that table

From Dev

mysql join table where foreign key is primary key of same table

From Dev

How to create foreign key in a table?

From Dev

Unable to create table with foreign key

Related Related

  1. 1

    Mapping of primary key as foreign key to another table

  2. 2

    Update primary key in one table which is foreign key in another table

  3. 3

    MySQL - Inserting Primary Key from one table to another (Foreign Key)

  4. 4

    I cannot send primary key of one table to another as foreign key?

  5. 5

    SQL create table primary key and foreign key syntax

  6. 6

    create primary and foreign key relationship

  7. 7

    Finding Primary Table for a Foreign Key

  8. 8

    Create table with both foreign keys and a composite primary key

  9. 9

    Use of Primary Key as Foreign Key in Foreign Key Table

  10. 10

    How to add a primary (Surrogate) key from one table into a foreign key of another table?

  11. 11

    Update Column Of All Rows In A Table From Another Table Using Primary Key And Foreign Key

  12. 12

    insert a primary key from one table as a foreign key to another table with php lastInsertId()

  13. 13

    How to display the last inserted ID (primary key) of a table in a html textbox that is in a relationship with a foreign key of another table in mvc

  14. 14

    Get foreign key field from primary key in another table that linked to many table

  15. 15

    Create a table who's name must be a primary key in another table

  16. 16

    Can a unique key ( not a primary key) be a foreign key to other table?

  17. 17

    query not working for foreign key as a composite primary key of another table oracle SQL

  18. 18

    Use Auto incremented unique id primary key as foreign key in another table

  19. 19

    How do I ensure that a foreign key exists as a primary key in another table when data is inserted?

  20. 20

    get primary key of recently added record and insert into another table as foreign key

  21. 21

    How do I get the primary key being referenced by a foreign key of another table?

  22. 22

    How to create two Foreign Key on pivot table linked to one Primary Key

  23. 23

    Insert into two tables with one table having an auto increment primary key and that same key is used as a foreign key in another table

  24. 24

    How to refer primary key as Foreign to various table

  25. 25

    Primary And Foreign key mapping between view and table

  26. 26

    Join on foreign key of table with the max primary key of that table

  27. 27

    mysql join table where foreign key is primary key of same table

  28. 28

    How to create foreign key in a table?

  29. 29

    Unable to create table with foreign key

HotTag

Archive