2011-12-20 40 views
5

पर विदेशी कुंजी मुझे एक समस्या है जो मुझे किसी अन्य तालिका में एक समग्र कुंजी के लिए एक विदेशी कुंजी को संदर्भित करने की आवश्यकता है।संयुक्त कुंजी

मेरे डेटाबेस संरचना

निम्नलिखित है:

CREATE TABLE available_trip (
trip_code integer not null, 
date datetime not null, 
primary key(trip_code, date), 
FOREIGN KEY (trip_code) REFERENCES trip (trip_code) 
); 

CREATE TABLE booking (
    available_trip_code integer not null, 
    customer_code integer not null, 
    date datetime not null, 
    deposit float not null, 
    total_price float not null, 
    has_paid float not null, 
    description_en nvarchar(12) null, 
    finance_type_code nvarchar(12) not null, 
    primary key(available_trip_code, customer_code, date), 
    FOREIGN KEY (available_trip_code) REFERENCES available_trip (trip_code, date), 


FOREIGN KEY (customer_code) REFERENCES customer (customer_code), 
      FOREIGN KEY (finance_type_code) REFERENCES finance_type (finance_type_code) 
     ); 

मेरे सवाल यह है: मैं कैसे available_trip.trip_code और available_trip.date को booking.available_trip_code संदर्भ दे सकता हूँ?

उत्तर

9

आप एक समग्र प्राथमिक कुंजी संदर्भ है, तो अपने विदेशी कुंजी भी उन सभी स्तंभों को शामिल करने की जरूरत है - तो आप की तरह कुछ की जरूरत है:

FOREIGN KEY (available_trip_code, date) 
      REFERENCES available_trip (trip_code, date) 

आप पहले से ही अपनी तालिका में उन सभी कॉलम मौजूद नहीं है, तो , तो आपको उन्हें जोड़ना होगा।

4
alter table booking add constraint FK_Booking_TripAndDate 
    foreign key (available_trip_code,date) 
    references available_trip(trip_code, date) 
संबंधित मुद्दे