C
C#4mo 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
Angius4mo ago
Just don't use a composite key?
Dachi
Dachi4mo ago
Idea then break. It then woul've meant that comment can exist by itself
Angius
Angius4mo ago
Use (AuthorId, FeatureId) then, since a comment is left by a user on a feature
Dachi
Dachi4mo ago
You mean to add Feature entity featureId?
Angius
Angius4mo ago
No?
Angius
Angius4mo ago
No description
Angius
Angius4mo ago
I mean this
Dachi
Dachi4mo 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
Angius4mo 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
Dachi4mo ago
Yes, i am trying to apply normalizations to construct this Database
Jimmacle
Jimmacle4mo ago
is this a repost? i swear i saw this question earlier today
Dachi
Dachi4mo ago
i did it yesterday i chnaged that diagram removed some unneccessary keys but the problem stays the same
Jimmacle
Jimmacle4mo 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
Dachi4mo ago
How else would you identify comment uniquely?
Jimmacle
Jimmacle4mo ago
the same way most tables identify things uniquely
Angius
Angius4mo ago
By a unique primary key
Dachi
Dachi4mo ago
Yes but there is a rule that dictates that all columns have to be identified by primary key
Angius
Angius4mo ago
All columns? Whuh?
Dachi
Dachi4mo ago
of an entity
Jimmacle
Jimmacle4mo ago
i think there's some terminology confusion
Angius
Angius4mo ago
Columns are identified by their name Columns don't have keys
Jimmacle
Jimmacle4mo 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
Dachi4mo 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
Jimmacle4mo ago
based on your diagram you can use a normal integer PK and have FKs to the feature and author
Dachi
Dachi4mo 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
Jimmacle4mo ago
huh? your diagram clearly shows a one to many relationship between a feature and comments
Dachi
Dachi4mo ago
yes
Jimmacle
Jimmacle4mo ago
the product/feature relationship is not relevant here
Dachi
Dachi4mo ago
are not you suppose to identify uniquely that feature? what if that feature was written several times on several products
Angius
Angius4mo ago
It would have a unique ID, wouldn't it?
Jimmacle
Jimmacle4mo 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
Dachi4mo 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
Jimmacle4mo ago
why would you need to do that the unique identifier for a feature should be a single unique value, like a number
Angius
Angius4mo ago
In order to find a comment you need that comment's ID
Dachi
Dachi4mo ago
Sure and you then apply FK?
Jimmacle
Jimmacle4mo ago
it seems like you're trying to embed information in the row that you should be connecting with joins in your queries
Dachi
Dachi4mo ago
to see where it was written?
Angius
Angius4mo ago
Wym "apply FK"?
Dachi
Dachi4mo ago
How do you find where that comment belongs?
Jimmacle
Jimmacle4mo ago
if you want to get all the comments for a feature, just select the comments where the feature ID matches the feature
Angius
Angius4mo ago
You use an FK, yes The comment would have an FK to feature
Dachi
Dachi4mo ago
Sure now stay with me That FK is connected to Feature right?
Angius
Angius4mo ago
Yeah
Jimmacle
Jimmacle4mo ago
yes
Dachi
Dachi4mo ago
now you have to findout where that feature is written on what product
Jimmacle
Jimmacle4mo ago
then you use the FK between the feature and product
Dachi
Dachi4mo ago
so now you have to find out product rifght?
Angius
Angius4mo ago
Sure, the feature has FK to product Nothing simpler
Dachi
Dachi4mo ago
sure and then to find product you need to find user who wrote that
Jimmacle
Jimmacle4mo ago
again, the FK
Dachi
Dachi4mo ago
yes,
Jimmacle
Jimmacle4mo ago
to get from a comment to a user you'd just join across all 4 tabes on the relevant FKs
Dachi
Dachi4mo ago
so in order for you to uniquely idnetify comment you need to go to all of these tables. right?
Angius
Angius4mo 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
Dachi4mo ago
Sure but that unique primary key doesn't know what feature it references. Feature FK idnetifys that comment
Jimmacle
Jimmacle4mo ago
it doesn't need to
Angius
Angius4mo ago
Primary key is not there to reference anything
Jimmacle
Jimmacle4mo ago
its only purpose is to uniquely identify the comment not its relationships
Angius
Angius4mo ago
It's there to be referenced
Jimmacle
Jimmacle4mo ago
you identify relationships using FKs
Dachi
Dachi4mo ago
But that what composite keys are used for
Jimmacle
Jimmacle4mo 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
Dachi4mo ago
Okay, lets say i add comment ID
Angius
Angius4mo ago
Composite keys are rarely used either for a natural key, or to ensure that a combination of n properties is unique
Dachi
Dachi4mo ago
To uniquely identify it.
Angius
Angius4mo ago
They are not and should not be your first choice for a unique identifier
Dachi
Dachi4mo 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
Jimmacle4mo ago
then you use joins
Dachi
Dachi4mo ago
then why do i need commentId?
Angius
Angius4mo ago
To identify the comment...?
Jimmacle
Jimmacle4mo ago
...to uniquely identify each comment
Dachi
Dachi4mo ago
if all these FK are identifying
Jimmacle
Jimmacle4mo ago
they are not identifying the FKs tell you relationships not identity
Angius
Angius4mo 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
Jimmacle4mo 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
Dachi4mo ago
But why can't those FK be PK?
Jimmacle
Jimmacle4mo 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
Angius4mo ago
Why would they be PKs?
Dachi
Dachi4mo ago
But i will then have to overload these tables
Jimmacle
Jimmacle4mo ago
?
Angius
Angius4mo ago
what
Jimmacle
Jimmacle4mo ago
if you have no PK what will the FK reference
Dachi
Dachi4mo ago
to use less unused PK. I will use FK as PK
Jimmacle
Jimmacle4mo ago
unused?
Dachi
Dachi4mo ago
Yes cuz for me to search comment i will have to search it by FKs
Jimmacle
Jimmacle4mo ago
huh?
Angius
Angius4mo 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
Jimmacle4mo ago
i don't get why you're overcomplicating what's really a basic relational database concept
Dachi
Dachi4mo ago
I am not. It's just the way it will then be graded
Jimmacle
Jimmacle4mo ago
this is why you need PKs
Dachi
Dachi4mo ago
it has to follow normalizations
Jimmacle
Jimmacle4mo ago
the feature FK on a comment needs to reference the PK of the feature
Angius
Angius4mo 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
Jimmacle4mo 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
Dachi4mo ago
damn i gotta think
Dachi
Dachi4mo ago
No description
Dachi
Dachi4mo ago
What Pk of exam should be? it is composite key of StudID and exam part
Jimmacle
Jimmacle4mo 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
Dachi4mo ago
No this is the answer
Jimmacle
Jimmacle4mo ago
literally, all a PK is is a value that tells different rows in the same table apart
Dachi
Dachi4mo ago
pk of exam is composite key of studId and exam_part Like that you say destroys the idea of weak entity
Jimmacle
Jimmacle4mo ago
you didn't mention anything about a "weak entity" until now
Dachi
Dachi4mo ago
that beacause i din;t want to talk using terminologies
Jimmacle
Jimmacle4mo 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
Dachi4mo 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
Jimmacle4mo ago
then no there is no better way to do what you want other than modeling the data differently
Dachi
Dachi4mo ago
Sure, i'll think about it a little more and then i guess will chnage it thanks for the time