मैं ज्यादातर तालिकाओं में शामिल होने में दिलचस्पी रखता हूँ, तो आप इस तरह एक स्कीमा के लिए होता है, जहां:
आप ऐसे ही एक स्कीमा नहीं होगा - यह तथ्य आप 'का प्रतिनिधित्व नहीं करता रुचि रखते हैं। आइए एसक्यूएल में कुछ टेबल स्केच करें। (PostgreSQL में परीक्षण) सबसे पहले, ग्राहकों और उत्पादों।
-- Customer names aren't unique.
create table customers (
cust_id integer primary key,
cust_name varchar(15) not null
);
insert into customers values (1, 'Foo'), (2, 'Bar');
-- Product names are unique.
create table products (
prod_id integer primary key,
prod_name varchar(15) not null unique
);
insert into products values
(150, 'Product 1'), (151, 'Product 2'), (152, 'Product 3');
उत्पादों के लिए विभिन्न श्रेणियां हैं।
create table categories (
cat_name varchar(15) primary key
);
insert into categories values ('Cable'), ('Networking'), ('Phones');
प्रत्येक उत्पाद कई श्रेणियों में दिखाई दे सकता है।
create table product_categories (
prod_id integer not null references products,
cat_name varchar(15) not null references categories,
primary key (prod_id, cat_name)
);
insert into product_categories values
(150, 'Cable'), (150, 'Networking'), (151, 'Networking'), (152, 'Phones');
एक ग्राहक को कई श्रेणियों के उत्पादों में रुचि हो सकती है।
create table customer_category_interests (
cust_id integer not null references customers,
cat_name varchar(15) not null references categories,
primary key (cust_id, cat_name)
);
-- Nobody's interested in phones
insert into customer_category_interests values
(1, 'Cable'), (1, 'Networking'), (2, 'Networking');
अगर मैं customerproducts पर विदेशी कुंजी है, यह सुनिश्चित करेंगे कि केवल वैध ग्राहकों और वैध उत्पादों है कि तालिका में मिलता है, लेकिन क्या अगर मैं करने के लिए फोन के श्रेणी से एक उत्पाद को जोड़ने का प्रयास के बारे में एक ग्राहक केवल एक कोपियर में दिलचस्पी रखने के रूप में निर्धारित किया गया है?
ग्राहक में उत्पाद अपनी पसंदीदा श्रेणियों में रुचि नहीं रखते हैं। अतिव्यापी विदेशी कुंजी बाधाओं पर ध्यान दें।
create table product_interests (
cust_id integer not null,
prod_id integer not null,
cat_name varchar(15) not null,
foreign key (cust_id, cat_name) references customer_category_interests,
foreign key (prod_id, cat_name) references product_categories,
primary key (cust_id, prod_id, cat_name)
);
insert into product_interests values
(1, 150, 'Cable'), (2, 150, 'Networking');
यह अगला सम्मिलन विफल हो जाएगा, क्योंकि ग्राहक 1 फोन में रूचि नहीं रखता है।
insert into product_interests values
(1, 152, 'Phones');
ERROR: insert or update on table "product_interests" violates foreign key constraint "product_interests_cust_id_fkey"
DETAIL: Key (cust_id, cat_name)=(1, Phones) is not present in table "customer_category_interests".
संभावित डुप्लिकेट: http://stackoverflow.com/questions/4262554/database-design-whats-the-point-of-identifying-foreign-keys, http://stackoverflow.com/ प्रश्न/18717/वे-विदेशी-कुंजी-वास्तव में आवश्यक-में-डेटाबेस-डिज़ाइन, http://stackoverflow.com/questions/83147/whats-wrong-with-foreign-keys – LittleBobbyTables
इन तीनों में से सभी प्रश्न हैं अनिवार्य रूप से पूछना क्यों? मैं पूछने की कोशिश कर रहा हूँ कैसे? बहुत बढ़िया हैंडल बीटीडब्ल्यू। –