C
C#10mo ago
Dachi

Db design

I have a problem with DB design. In this ER diagram, i am trying to build a db that allows users to place product under which other users can request a feature. This feature can then have comments to discuss. The problem is that composite key of a comment is UserID, productId, featureId, authorId, Date (when the comment was written). As you can see it is way too long. Is there a better way to design this? Thanks.
No description
108 Replies
Angius
Angius10mo ago
Just don't use a composite key?
Dachi
DachiOP10mo ago
Idea then break. It then woul've meant that comment can exist by itself
Angius
Angius10mo ago
Use (AuthorId, FeatureId) then, since a comment is left by a user on a feature
Dachi
DachiOP10mo ago
You mean to add Feature entity featureId?
Angius
Angius10mo ago
No?
Angius
Angius10mo ago
No description
Angius
Angius10mo ago
I mean this
Dachi
DachiOP10mo ago
Again feature depends on a product. If i were to add that it means that feature doesn't need product to be identified uniquely
Angius
Angius10mo ago
You can have the composite key of the feature be (Id, ProductId) still Though, I usually just use sequential IDs and simple FKs, composite keys only when I need to enforce that a given pair of values needs to stay unique So you're operating on levels of database normalization or whatever that not even my college professors taught us about lmao Maybe people in the #database channel would be more familiar with such... unique concepts
Dachi
DachiOP10mo ago
Yes, i am trying to apply normalizations to construct this Database
Jimmacle
Jimmacle10mo ago
is this a repost? i swear i saw this question earlier today
Dachi
DachiOP10mo ago
i did it yesterday i chnaged that diagram removed some unneccessary keys but the problem stays the same
Jimmacle
Jimmacle10mo ago
why do you need a composite key at all? that will complicate retrieving comments just have a normal PK and non-nullable foreign keys to the entities that own the comments
Dachi
DachiOP10mo ago
How else would you identify comment uniquely?
Jimmacle
Jimmacle10mo ago
the same way most tables identify things uniquely
Angius
Angius10mo ago
By a unique primary key
Dachi
DachiOP10mo ago
Yes but there is a rule that dictates that all columns have to be identified by primary key
Angius
Angius10mo ago
All columns? Whuh?
Dachi
DachiOP10mo ago
of an entity
Jimmacle
Jimmacle10mo ago
i think there's some terminology confusion
Angius
Angius10mo ago
Columns are identified by their name Columns don't have keys
Jimmacle
Jimmacle10mo ago
why do you think you need to make all this information part of the comment's key the key is just an identifier, you can have columns for a comment that aren't part of any key
Dachi
DachiOP10mo ago
A relation that is in First Normal Form and every non-primary-key attribute is fully functionally dependent on the primary key, then the relation is in Second Normal Form (2NF).
Jimmacle
Jimmacle10mo ago
based on your diagram you can use a normal integer PK and have FKs to the feature and author
Dachi
DachiOP10mo ago
Sure but when retrieving that you would've went for that FK of a feature where you're would've went for it's FK which is prodID
Jimmacle
Jimmacle10mo ago
huh? your diagram clearly shows a one to many relationship between a feature and comments
Dachi
DachiOP10mo ago
yes
Jimmacle
Jimmacle10mo ago
the product/feature relationship is not relevant here
Dachi
DachiOP10mo ago
are not you suppose to identify uniquely that feature? what if that feature was written several times on several products
Angius
Angius10mo ago
It would have a unique ID, wouldn't it?
Jimmacle
Jimmacle10mo ago
then you would use the key of the feature the comment is for? that's the whole reason you give unique items unique IDs
Dachi
DachiOP10mo ago
In order to find Comment you need to uniqely identify feature. In order for you to uniqely identify feature, you need to find product and the user that wrote that product.
Jimmacle
Jimmacle10mo ago
why would you need to do that the unique identifier for a feature should be a single unique value, like a number
Angius
Angius10mo ago
In order to find a comment you need that comment's ID
Dachi
DachiOP10mo ago
Sure and you then apply FK?
Jimmacle
Jimmacle10mo ago
it seems like you're trying to embed information in the row that you should be connecting with joins in your queries
Dachi
DachiOP10mo ago
to see where it was written?
Angius
Angius10mo ago
Wym "apply FK"?
Dachi
DachiOP10mo ago
How do you find where that comment belongs?
Jimmacle
Jimmacle10mo ago
if you want to get all the comments for a feature, just select the comments where the feature ID matches the feature
Angius
Angius10mo ago
You use an FK, yes The comment would have an FK to feature
Dachi
DachiOP10mo ago
Sure now stay with me That FK is connected to Feature right?
Angius
Angius10mo ago
Yeah
Jimmacle
Jimmacle10mo ago
yes
Dachi
DachiOP10mo ago
now you have to findout where that feature is written on what product
Jimmacle
Jimmacle10mo ago
then you use the FK between the feature and product
Dachi
DachiOP10mo ago
so now you have to find out product rifght?
Angius
Angius10mo ago
Sure, the feature has FK to product Nothing simpler
Dachi
DachiOP10mo ago
sure and then to find product you need to find user who wrote that
Jimmacle
Jimmacle10mo ago
again, the FK
Dachi
DachiOP10mo ago
yes,
Jimmacle
Jimmacle10mo ago
to get from a comment to a user you'd just join across all 4 tabes on the relevant FKs
Dachi
DachiOP10mo ago
so in order for you to uniquely idnetify comment you need to go to all of these tables. right?
Angius
Angius10mo ago
No You need a unique primary key That's it An entity is not defined by the entire chain of relationships it has It would be nonsensical
Dachi
DachiOP10mo ago
Sure but that unique primary key doesn't know what feature it references. Feature FK idnetifys that comment
Jimmacle
Jimmacle10mo ago
it doesn't need to
Angius
Angius10mo ago
Primary key is not there to reference anything
Jimmacle
Jimmacle10mo ago
its only purpose is to uniquely identify the comment not its relationships
Angius
Angius10mo ago
It's there to be referenced
Jimmacle
Jimmacle10mo ago
you identify relationships using FKs
Dachi
DachiOP10mo ago
But that what composite keys are used for
Jimmacle
Jimmacle10mo ago
which are columns that store the primary keys of the other entities being referenced no there is no reason to use a composite key for any of this
Dachi
DachiOP10mo ago
Okay, lets say i add comment ID
Angius
Angius10mo ago
Composite keys are rarely used either for a natural key, or to ensure that a combination of n properties is unique
Dachi
DachiOP10mo ago
To uniquely identify it.
Angius
Angius10mo ago
They are not and should not be your first choice for a unique identifier
Dachi
DachiOP10mo ago
But if i do so and have COmmentId in COmment entity. And i have to query specific comment under specific feature under specific product
Jimmacle
Jimmacle10mo ago
then you use joins
Dachi
DachiOP10mo ago
then why do i need commentId?
Angius
Angius10mo ago
To identify the comment...?
Jimmacle
Jimmacle10mo ago
...to uniquely identify each comment
Dachi
DachiOP10mo ago
if all these FK are identifying
Jimmacle
Jimmacle10mo ago
they are not identifying the FKs tell you relationships not identity
Angius
Angius10mo ago
FKs are referencing PKs are identifying You are uniquely identified by the name Dachi It does not reference the chair you sit on Your chair can have a plaque that says Dachi's Chair It references its owner
Jimmacle
Jimmacle10mo ago
practically every table in your database should have an "ID" column that serves as the primary key, and it can be a simple integer
Dachi
DachiOP10mo ago
But why can't those FK be PK?
Jimmacle
Jimmacle10mo ago
then you use those IDs to create FKs and create relationships between different tables because they aren't for the same purpose that's like saying you are not dachi, you are "dachi's mom's child"
Angius
Angius10mo ago
Why would they be PKs?
Dachi
DachiOP10mo ago
But i will then have to overload these tables
Jimmacle
Jimmacle10mo ago
?
Angius
Angius10mo ago
what
Jimmacle
Jimmacle10mo ago
if you have no PK what will the FK reference
Dachi
DachiOP10mo ago
to use less unused PK. I will use FK as PK
Jimmacle
Jimmacle10mo ago
unused?
Dachi
DachiOP10mo ago
Yes cuz for me to search comment i will have to search it by FKs
Jimmacle
Jimmacle10mo ago
huh?
Angius
Angius10mo ago
"Why is my fork leaking?" you ask, "Forks are made for soup!" "Use a spoon," we say' "forks are not great for eating soup." "No but forks is how you eat soup! How else will you eat the vegetables?"
Jimmacle
Jimmacle10mo ago
i don't get why you're overcomplicating what's really a basic relational database concept
Dachi
DachiOP10mo ago
I am not. It's just the way it will then be graded
Jimmacle
Jimmacle10mo ago
this is why you need PKs
Dachi
DachiOP10mo ago
it has to follow normalizations
Jimmacle
Jimmacle10mo ago
the feature FK on a comment needs to reference the PK of the feature
Angius
Angius10mo ago
I have never seen a normalization that would require the PK to be a composite key referencing all the relationships an entity ever had
Jimmacle
Jimmacle10mo ago
^ that sounds more denormalized than anything why should a comment need to know about the entire chain of relationships when its only direct relationship is with a feature?
Dachi
DachiOP10mo ago
damn i gotta think
Dachi
DachiOP10mo ago
No description
Dachi
DachiOP10mo ago
What Pk of exam should be? it is composite key of StudID and exam part
Jimmacle
Jimmacle10mo ago
a number that distinguishes it from all the other exams no stop thinking that PKs need to be composite keys of related entities
Dachi
DachiOP10mo ago
No this is the answer
Jimmacle
Jimmacle10mo ago
literally, all a PK is is a value that tells different rows in the same table apart
Dachi
DachiOP10mo ago
pk of exam is composite key of studId and exam_part Like that you say destroys the idea of weak entity
Jimmacle
Jimmacle10mo ago
you didn't mention anything about a "weak entity" until now
Dachi
DachiOP10mo ago
that beacause i din;t want to talk using terminologies
Jimmacle
Jimmacle10mo ago
... that is critical information you asked us how to do one thing, we told you the right way to do it, you argued that we were wrong, then you decided to tell us you wanted to do something else entirely
Dachi
DachiOP10mo ago
The relationship depicts that it is a weak entity ER diagram itself represents that that's why inside square there is another square to depicts it's weak that's why i did't use terminology
Jimmacle
Jimmacle10mo ago
then no there is no better way to do what you want other than modeling the data differently
Dachi
DachiOP10mo ago
Sure, i'll think about it a little more and then i guess will chnage it thanks for the time
Want results from more Discord servers?
Add your server