Reputation: 177
I am designing a database. I have one relation which have all the attributes for time log. And I have to make it foreign key in other relation which has items. But I am not sure whether to use date and time as separate attributes and make them a both primary key(composite key) or use a single dateTime attribute as a primary key. Thanks
Upvotes: 1
Views: 682
Reputation: 10693
For the attribute itself, use a single datetime column. It only makes sense to split if there is some semantics behind date and time taken separately.
Apart from that, it is rare to use a datetime column as a key. Usually you have a numeric (maybe surrogate) primary key and datetime is just an attribute. Maybe you don't expect two records occuring at exactly the same moment. Most often it turns out eventually to be a wrong assumption.
Upvotes: 0
Reputation: 752
When it comes to optimizing database queries its always good to have less joins between your different relationships and tables. Also if you save attribute with type as DATETIME it gives you more control of what you can do by performing single fetch of the column instead of fetching two columns and applying your application logic. Also it will help you overall keep the size of your metadata of database lower as well.
So in the light of above its highly recommended to keep both attributes in single column with data type as DATETIME.
Hope it helps
Upvotes: 1