1
The following example may occur?
| Id | nome | data | qnt |
PK -> Id
FK -> (Id, qnt)
Or is there some kind of restriction?
1
The following example may occur?
| Id | nome | data | qnt |
PK -> Id
FK -> (Id, qnt)
Or is there some kind of restriction?
3
1) The only restriction for foreign keys is that they should point to a single key.
Note that all PK, by definition, is a single key.
Think about it: the foreign key indicates who is the parent record, on which the child record depends and without it would be orphaned. In addition the child registration can not have two parents (point!)
2) A key is a field that identifies the record, as a name of a person. A single key is a field that uniquely identifies a record, such as a CPF.
3) Any key may be composed of more than one column. That depends a lot on schema but in general a key is something that identifies the record.
4) A field that will be used as key should never be changeable. That is: it should never be a field whose value can be changed, for example your CPF, imagine that your CPF could change all the time, how could you be identified by this?
5) A field quantity is not a good candidate to be used as key, it is a (relatively) mutable field and does not identify the record. Actually, it’s just an attribute, so don’t use it as a key. If you do a lot of research filtering through this field it might be interesting to index it to improve performance but not elevate it to a key.
Edit
@Maniero The implementation of Constraint will depend on DBMS, examples:
and the reference to the concept of FK in Wikipedia
But even if the DBMS allows it, it’s a breach of relational integrity. Why? Well if you change the parent id record without changing the children records they will be orphaned. If a child can have "n parents" then it is better to reverse the hierarchy and make the record "ex-pai" reference the record "ex-son". If the relationship is N to M then it would be better to create a relationship table with Fks for "parents" and "children".
There may even be some need somewhere to create an FK for a key not unique
but the few times I saw such a thing were in serious modeling errors.
FK doesn’t have to be unique or immutable. It is highly desirable that it should be so, there is rarely a case where this rule is not applicable but say that must be is at least an exaggeration.
@I don’t think that’s an exaggeration. These are rules of relational integrity, and while the Dcms allow you not to follow the rules, I prefer to use them. This helps keep my data intact =)
But then you are stating in the answer something that is only your opinion. For those who do not know, the answer can mislead and the person may find it is mandatory.
2
The Id
, which is primary key, not only can it but is often used in Fks.
If you want to know if the key can be composed, ie use more than one column as FK? Yes, can and is also common happen.
qnt
besides being a bad name to indicate something (I’ll imagine that is quantity) probably is not a good choice to be part of a FK. But I don’t know, there may be a reason. I don’t follow golden rules, I see the need. Normally columns that have not very stable data are not recommended for use in FK. It is preferable to use columns that identify something. A quantity does not identify anything.
vlw! was just an example, I am quite specific in naming fields, or variables... not to get lost rsrs
A word off the subject, but maybe it will help you, start to improve on variable names, you will see that in the future, a concise but complete name is the differential not to get lost. Just now I realized how old this post is. Jesus, save me
1
Yes , an example :
Numero (pk) Valor Date
Number (pk) (fk nota_fiscal.numero) Num_item (pk) Desc_item Valor
Understanding clear that the model is a great simplification.
thank you very much! ^^
Browser other questions tagged sql modeling foreign-key primary-key
You are not signed in. Login or sign up in order to post.
It can happen. I just don’t think it’s right for a FK with a quantity...
– gmsantos
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero