'Nested' foreign key relationshipHow can foreign key constraints be temporarily disabled using T-SQL?What's the best practice for primary keys in tables?How can I list all foreign keys referencing a given table in SQL Server?foreign key and primary key errorAdd Foreign Key relationship between two DatabasesMySQL whats wrong with my foreign keys?Foreign key constraints: When to use ON UPDATE and ON DELETESQL How to Set Foreign Keyspostgresql foreign key syntaxPrimary Key and Foreign key in same table

Have I saved too much for retirement so far?

Why isn't KTEX's runway designation 10/28 instead of 9/27?

Teaching indefinite integrals that require special-casing

What (else) happened July 1st 1858 in London?

Can I Retrieve Email Addresses from BCC?

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

What would you call a finite collection of unordered objects that are not necessarily distinct?

What will be the benefits of Brexit?

Pronouncing Homer as in modern Greek

How can I successfully establish a nationwide combat training program for a large country?

How can I raise concerns with a new DM about XP splitting?

Is it possible to have a strip of cold climate in the middle of a planet?

Organic chemistry Iodoform Reaction

Why are on-board computers allowed to change controls without notifying the pilots?

Simple image editor tool to draw a simple box/rectangle in an existing image

Did US corporations pay demonstrators in the German demonstrations against article 13?

Fast sudoku solver

What is the opposite of 'gravitas'?

What does the "3am" section means in manpages?

What is the smallest body in which a sling shot maneuver can be performed?

How do ultrasonic sensors differentiate between transmitted and received signals?

Is there enough fresh water in the world to eradicate the drinking water crisis?

Can one define wavefronts for waves travelling on a stretched string?

Are Warlocks Arcane or Divine?



'Nested' foreign key relationship


How can foreign key constraints be temporarily disabled using T-SQL?What's the best practice for primary keys in tables?How can I list all foreign keys referencing a given table in SQL Server?foreign key and primary key errorAdd Foreign Key relationship between two DatabasesMySQL whats wrong with my foreign keys?Foreign key constraints: When to use ON UPDATE and ON DELETESQL How to Set Foreign Keyspostgresql foreign key syntaxPrimary Key and Foreign key in same table













0















A Product has a code (and other attributes). A Product is available in various countries, with a different name in each. A Packaged Product is a product in a particular pack size.



An invoice detail line will hold a package_code and a country_code. The permissible countries for a PackagedProduct are restricted by the contents of the ProductCountry table, i.e. the PackagedProduct is only available in a given Country if the Product is available in that Country.



Here's my approach (Country and InvoiceDetail tables omitted):



create table Product (
product_code varchar(15),
strength varchar(10),
-- other Product attributes
constraint pk_Product primary key (product_code)
);

create table ProductCountry (
product_code varchar(15),
country_code char(2),
local_name varchar(100),
constraint pk_ProductCountry primary key (product_code, country_code),
foreign key (product_code) references Product(product_code),
foreign key (country_code) references Country(country_code)
);

create table PackagedProduct (
package_code varchar(20),
product_code varchar(15),
pack_size int,
constraint pk_PackagedProduct primary key (package_code),
foreign key (product_code) references Product(product_code)
);


My problem is that this design does not restrict Invoice Detail records to valid combinations of PackagedProduct and Country.



Can we improve this design?










share|improve this question






















  • You may want to look at CHECK constraints and triggers for ways of performing more complex data validation. Ponder carefully what update and delete statements should do, e.g. if a product is no longer offered in Elbonia what becomes of InvoiceDetails that reflect shipments to the muddy land.

    – HABO
    Mar 21 at 20:42











  • This is a faq. You can constrain this declaratively via compound PKs & FKs that include product & country.

    – philipxy
    Mar 22 at 6:45











  • @HABO yes I feared that I may have to resort to manual constraints. I think the Elbonia invoice remains valid - we just won't make any more in future!

    – Rik Bradley
    Mar 22 at 9:12











  • @philipxy by compound FK's, do you mean FKs referencing more than one column? I don't see how that addresses the problem, would you care to spell it out for me using my example?

    – Rik Bradley
    Mar 22 at 9:24











  • "compound/composite key" is a google away. I don't have time or my faq list now. Meanwhile from my comments list: Before considering posting please always google your error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names, & read many answers. If you post a question, use one phrasing as title. PS The idea is to have InvoiceDetail PK (invoice, product, country) & FK (product, country) to ProductCountry. (PK (product) to Product is then implied.)

    – philipxy
    Mar 22 at 19:21















0















A Product has a code (and other attributes). A Product is available in various countries, with a different name in each. A Packaged Product is a product in a particular pack size.



An invoice detail line will hold a package_code and a country_code. The permissible countries for a PackagedProduct are restricted by the contents of the ProductCountry table, i.e. the PackagedProduct is only available in a given Country if the Product is available in that Country.



Here's my approach (Country and InvoiceDetail tables omitted):



create table Product (
product_code varchar(15),
strength varchar(10),
-- other Product attributes
constraint pk_Product primary key (product_code)
);

create table ProductCountry (
product_code varchar(15),
country_code char(2),
local_name varchar(100),
constraint pk_ProductCountry primary key (product_code, country_code),
foreign key (product_code) references Product(product_code),
foreign key (country_code) references Country(country_code)
);

create table PackagedProduct (
package_code varchar(20),
product_code varchar(15),
pack_size int,
constraint pk_PackagedProduct primary key (package_code),
foreign key (product_code) references Product(product_code)
);


My problem is that this design does not restrict Invoice Detail records to valid combinations of PackagedProduct and Country.



Can we improve this design?










share|improve this question






















  • You may want to look at CHECK constraints and triggers for ways of performing more complex data validation. Ponder carefully what update and delete statements should do, e.g. if a product is no longer offered in Elbonia what becomes of InvoiceDetails that reflect shipments to the muddy land.

    – HABO
    Mar 21 at 20:42











  • This is a faq. You can constrain this declaratively via compound PKs & FKs that include product & country.

    – philipxy
    Mar 22 at 6:45











  • @HABO yes I feared that I may have to resort to manual constraints. I think the Elbonia invoice remains valid - we just won't make any more in future!

    – Rik Bradley
    Mar 22 at 9:12











  • @philipxy by compound FK's, do you mean FKs referencing more than one column? I don't see how that addresses the problem, would you care to spell it out for me using my example?

    – Rik Bradley
    Mar 22 at 9:24











  • "compound/composite key" is a google away. I don't have time or my faq list now. Meanwhile from my comments list: Before considering posting please always google your error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names, & read many answers. If you post a question, use one phrasing as title. PS The idea is to have InvoiceDetail PK (invoice, product, country) & FK (product, country) to ProductCountry. (PK (product) to Product is then implied.)

    – philipxy
    Mar 22 at 19:21













0












0








0








A Product has a code (and other attributes). A Product is available in various countries, with a different name in each. A Packaged Product is a product in a particular pack size.



An invoice detail line will hold a package_code and a country_code. The permissible countries for a PackagedProduct are restricted by the contents of the ProductCountry table, i.e. the PackagedProduct is only available in a given Country if the Product is available in that Country.



Here's my approach (Country and InvoiceDetail tables omitted):



create table Product (
product_code varchar(15),
strength varchar(10),
-- other Product attributes
constraint pk_Product primary key (product_code)
);

create table ProductCountry (
product_code varchar(15),
country_code char(2),
local_name varchar(100),
constraint pk_ProductCountry primary key (product_code, country_code),
foreign key (product_code) references Product(product_code),
foreign key (country_code) references Country(country_code)
);

create table PackagedProduct (
package_code varchar(20),
product_code varchar(15),
pack_size int,
constraint pk_PackagedProduct primary key (package_code),
foreign key (product_code) references Product(product_code)
);


My problem is that this design does not restrict Invoice Detail records to valid combinations of PackagedProduct and Country.



Can we improve this design?










share|improve this question














A Product has a code (and other attributes). A Product is available in various countries, with a different name in each. A Packaged Product is a product in a particular pack size.



An invoice detail line will hold a package_code and a country_code. The permissible countries for a PackagedProduct are restricted by the contents of the ProductCountry table, i.e. the PackagedProduct is only available in a given Country if the Product is available in that Country.



Here's my approach (Country and InvoiceDetail tables omitted):



create table Product (
product_code varchar(15),
strength varchar(10),
-- other Product attributes
constraint pk_Product primary key (product_code)
);

create table ProductCountry (
product_code varchar(15),
country_code char(2),
local_name varchar(100),
constraint pk_ProductCountry primary key (product_code, country_code),
foreign key (product_code) references Product(product_code),
foreign key (country_code) references Country(country_code)
);

create table PackagedProduct (
package_code varchar(20),
product_code varchar(15),
pack_size int,
constraint pk_PackagedProduct primary key (package_code),
foreign key (product_code) references Product(product_code)
);


My problem is that this design does not restrict Invoice Detail records to valid combinations of PackagedProduct and Country.



Can we improve this design?







sql tsql relational-database






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 21 at 14:40









Rik BradleyRik Bradley

12




12












  • You may want to look at CHECK constraints and triggers for ways of performing more complex data validation. Ponder carefully what update and delete statements should do, e.g. if a product is no longer offered in Elbonia what becomes of InvoiceDetails that reflect shipments to the muddy land.

    – HABO
    Mar 21 at 20:42











  • This is a faq. You can constrain this declaratively via compound PKs & FKs that include product & country.

    – philipxy
    Mar 22 at 6:45











  • @HABO yes I feared that I may have to resort to manual constraints. I think the Elbonia invoice remains valid - we just won't make any more in future!

    – Rik Bradley
    Mar 22 at 9:12











  • @philipxy by compound FK's, do you mean FKs referencing more than one column? I don't see how that addresses the problem, would you care to spell it out for me using my example?

    – Rik Bradley
    Mar 22 at 9:24











  • "compound/composite key" is a google away. I don't have time or my faq list now. Meanwhile from my comments list: Before considering posting please always google your error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names, & read many answers. If you post a question, use one phrasing as title. PS The idea is to have InvoiceDetail PK (invoice, product, country) & FK (product, country) to ProductCountry. (PK (product) to Product is then implied.)

    – philipxy
    Mar 22 at 19:21

















  • You may want to look at CHECK constraints and triggers for ways of performing more complex data validation. Ponder carefully what update and delete statements should do, e.g. if a product is no longer offered in Elbonia what becomes of InvoiceDetails that reflect shipments to the muddy land.

    – HABO
    Mar 21 at 20:42











  • This is a faq. You can constrain this declaratively via compound PKs & FKs that include product & country.

    – philipxy
    Mar 22 at 6:45











  • @HABO yes I feared that I may have to resort to manual constraints. I think the Elbonia invoice remains valid - we just won't make any more in future!

    – Rik Bradley
    Mar 22 at 9:12











  • @philipxy by compound FK's, do you mean FKs referencing more than one column? I don't see how that addresses the problem, would you care to spell it out for me using my example?

    – Rik Bradley
    Mar 22 at 9:24











  • "compound/composite key" is a google away. I don't have time or my faq list now. Meanwhile from my comments list: Before considering posting please always google your error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names, & read many answers. If you post a question, use one phrasing as title. PS The idea is to have InvoiceDetail PK (invoice, product, country) & FK (product, country) to ProductCountry. (PK (product) to Product is then implied.)

    – philipxy
    Mar 22 at 19:21
















You may want to look at CHECK constraints and triggers for ways of performing more complex data validation. Ponder carefully what update and delete statements should do, e.g. if a product is no longer offered in Elbonia what becomes of InvoiceDetails that reflect shipments to the muddy land.

– HABO
Mar 21 at 20:42





You may want to look at CHECK constraints and triggers for ways of performing more complex data validation. Ponder carefully what update and delete statements should do, e.g. if a product is no longer offered in Elbonia what becomes of InvoiceDetails that reflect shipments to the muddy land.

– HABO
Mar 21 at 20:42













This is a faq. You can constrain this declaratively via compound PKs & FKs that include product & country.

– philipxy
Mar 22 at 6:45





This is a faq. You can constrain this declaratively via compound PKs & FKs that include product & country.

– philipxy
Mar 22 at 6:45













@HABO yes I feared that I may have to resort to manual constraints. I think the Elbonia invoice remains valid - we just won't make any more in future!

– Rik Bradley
Mar 22 at 9:12





@HABO yes I feared that I may have to resort to manual constraints. I think the Elbonia invoice remains valid - we just won't make any more in future!

– Rik Bradley
Mar 22 at 9:12













@philipxy by compound FK's, do you mean FKs referencing more than one column? I don't see how that addresses the problem, would you care to spell it out for me using my example?

– Rik Bradley
Mar 22 at 9:24





@philipxy by compound FK's, do you mean FKs referencing more than one column? I don't see how that addresses the problem, would you care to spell it out for me using my example?

– Rik Bradley
Mar 22 at 9:24













"compound/composite key" is a google away. I don't have time or my faq list now. Meanwhile from my comments list: Before considering posting please always google your error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names, & read many answers. If you post a question, use one phrasing as title. PS The idea is to have InvoiceDetail PK (invoice, product, country) & FK (product, country) to ProductCountry. (PK (product) to Product is then implied.)

– philipxy
Mar 22 at 19:21





"compound/composite key" is a google away. I don't have time or my faq list now. Meanwhile from my comments list: Before considering posting please always google your error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names, & read many answers. If you post a question, use one phrasing as title. PS The idea is to have InvoiceDetail PK (invoice, product, country) & FK (product, country) to ProductCountry. (PK (product) to Product is then implied.)

– philipxy
Mar 22 at 19:21












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55282985%2fnested-foreign-key-relationship%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55282985%2fnested-foreign-key-relationship%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해