简体   繁体   中英

creating table having foreign key that reference another table

I am using PGAdminIII database.

I have one table named STOCKREGISTER which contains composite primary key consisting of three fields ie stockregisterId,applicationId and date .

I have to create another table STOCK which has a foreignkey field that reference the field stockregisterId of STOCKREGISTER .If I am trying to create STOCK table,an error message is shown.The error message is "there is no unique contraint matching keys for referenced table STOCKREGISTER" .What another step I have to take next

this first table

CREATE TABLE stock_register
(
  stock_register_id bigint NOT NULL,
  application_id bigserial NOT NULL,
  production_date date NOT NULL,
  opening_bal bigint DEFAULT 0,
  quantity_produced bigint,
  total_quantity bigint 
  CONSTRAINT primarykey PRIMARY KEY (stock_register_id, application_id, production_date),
  CONSTRAINT "foreignKey" FOREIGN KEY (application_id)
      REFERENCES application (application_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

below is second table.Here I cannot make stock_register_id as a foreign key

    CREATE TABLE Stock
(
  stock_id bigint NOT NULL,
  stock_register_id bigint,
  dimension bigserial NOT NULL,
  CONSTRAINT "stockid" PRIMARY KEY (stock_id)
)

I guess that syntax should be:

CREATE TABLE Stock
(
  stock_id bigint NOT NULL,
  stock_register_id bigint,
  dimension bigserial NOT NULL,
  CONSTRAINT "stockid"
    FOREIGN KEY (stock_id)
    REFERENCES stock_register (stock_register_id) 
)
CREATE TABLE Stock
(
stock_id bigint NOT NULL,
stock_register_id bigint,
dimension bigserial NOT NULL,
CONSTRAINT primaryKey PRIMARY KEY (stock_id),
CONSTRAINT foreignKey FOREIGN KEY(stock_register_id)
REFERENCES stock_register (stock_register_id)
)

That should be everything you need. You'll also have to make sure the DB table engines, collations and charsets match up when using Foreign Keys.

For the unique constraint issue, there doesn't seem to be a problem with your stock_register_id PK in the stock_register table. Based on the name STOCKREGISTER in the error message I suspect it wasn't finding the table stock_register in your second Create statement.

What is a foreign key? A pointer to a specific record in another table.

How is a specific record in stock_register identified according to your DDL? By the unique combination of (stock_register_id, application_id, production_date).

Therefore stock_register_id = 1 could appear on a thousand different records so long as application_id and production_date are different.

Therefore, if all you have is a stock_register_id, there is no way to know which stock_register record it is pointing to and therefore no way for the DBMS to enforce the foreign key.

You must either add application_id and production_date to the stock table and make all three columns together the FK to the composite key on stock_register, or you must remove application_id and production_date from the PK on stock_register so the FK and PK columns match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM