TransWikia.com

SQL foreign key is not referenced by another table

Stack Overflow Asked by Pxaml on February 4, 2021

How could I relate the following foreign key to this table?

what I want is to have a relation between time_slot and checkup _appt table. I would like to be able to do a one to many relationships, or one one as well

CREATE TABLE time_slot
(
    slot_id varchar(4),
    adoption_id varchar(8),
    day varchar(1),
    req_appt_id varchar(8),
    start_hr numeric(2) check (start_hr >= 0 and start_hr < 24),
    start_min numeric(2) check (start_min >= 0 and start_min < 60),
    end_hr numeric(2) check (end_hr >= 0 and end_hr < 24),
    end_min numeric(2) check (end_min >= 0 and end_min < 60),

    primary key (slot_id, req_appt_id, adoption_id),
);
 
CREATE TABLE well_check_up 
(
    slot_id varchar(4),
    adoption_id varchar(8),
    req_appt_id varchar(8),
    day varchar(1),

    PRIMARY KEY (slot_id, req_appt_id, adoption_id),
    FOREIGN KEY (req_appt_id) REFERENCES time_slot (slot_id, req_appt_id, adoption_id)
);
 

One Answer

Your primary Key primary key (time_slot_id, day,start_min,req_appt_id,adoption_id),); on time slot, is a composed primary Key. You need to reference ALL of them on well_check, not only req_appt_id.

Like this

 Reference Time_slot(time_slot_id, 
 day,start_min,req_appt_id,adoption_id).

This means you Will have to add these columns on Well_check as well.

Answered by Tiago Silva on February 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP