Click here to Skip to main content
16,020,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i create 1 to many relationship within a create table statement in MSSQL?
where I've 5 create table queries??
please give me an example at your earliest.
Posted

1 solution

If I understand the question correctly, you define a foreign key when creating the child table. For example
SQL
CREATE TABLE Parent (
  ParentId int          NOT NULL PRIMARY KEY,
  SomeData varchar(100) NOT NULL
);

CREATE TABLE Child (
  ChildId   int          NOT NULL PRIMARY KEY,
  ParentId  int          NOT NULL FOREIGN KEY REFERENCES Parent (ParentId),
  OtherData varchar(100) NOT NULL
);

...

For more information, see Create Foreign Key Relationships[^]
 
Share this answer
 
Comments
[no name] 28-Sep-15 14:49pm    
Now for about 70% I can agree (so a 5 is ok I think). But to be _very_ exactly: The primary key of the Detail table needs to be theoretically the Primary key of it's master and "something other" like a line number.
Bruno

[Edit]
I have repeatedly violated this law and later for several times punished
[/Edit]
Wendelius 28-Sep-15 15:07pm    
Thanks for the comment.

I don't quite understand why a compound key would be required when creating a one-to-many relationship, could you explain a bit more?
[no name] 28-Sep-15 15:19pm    
Quote: "I don't quite understand why a compound key would be required when creating a one-to-many relationship"... I don't belive realy that you don't understand this ... it is more probably my English :(

Maybe this explains,but I'm not sure about:https://en.wikipedia.org/wiki/Compound_key[^]

For me it is simply:
To make a Detail unique, one needs a compound key (Line number what ever else). A seperate/additional primary/sequence key I also add usually, but the _real primary_ is still the detail's reference to the primary and "something like" a line number of the Detail. Simply according to https://en.wikipedia.org/wiki/Referential_integrity[^]


Not really more informative.... ooooh my English...sorry.
Bruno
Wendelius 28-Sep-15 15:43pm    
I'm still quite puzzled. I do know what a compound key is but what I don't understand is why the foreign key column should be part of the key of the child table. I can see that this could be a design principle of a specific system but in general I haven't heard of such requirement.

Let's take an example; You have a Person table (in this example it's a parent) and then you have an Order table. Among all other columns the Order table has these four columns:
- ReceivedBy, foreign key to Person, nullable
- PackedBy, foreign key to Person, nullable
- ShippedBy, foreign key to Person, nullable
- BilledBy, foreign key to Person, nullable

Now each one of these columns defines a separate one-to-many relationship between Person and Order, however, none of these is logically part of a key. After all the same combination is most likely to exist several times. So the order would probably have some completely other primary key.

Personally I like to use surrogate key[^]. If you think about the concept of a surrogate key it is forbidden to use any actual data as part of the key, otherwise it won't be surrogate. However, using a surrogate key does not mean that I cannot define a one-to-many relationship. The key of the child table is separate from the relationship definition.


Perhaps there's something I'm missing or misunderstood...

Oh, and from what I see there's nothing wrong with your English :)
[no name] 28-Sep-15 16:23pm    
Dear Mika Wendelius
First of all, thank you very much that you are willing to discuss this with a low rep mem, I really appreciate this!

Now on a fist glance I would not "unite" "ReceivedBy", "PackedBy", "ShippedBy" and "BilledBy" in one and the same table. For me this are different actions, which Need also different Detail data.

Give me please "un rato" to answer more precisely.

Thank you, Bruno

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900