Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database design - Should a Date be used as part of a primary key

What are the pros/cons for including a date field as a part of a primary key?

like image 282
rich Avatar asked Dec 04 '08 15:12

rich


People also ask

What should be used as primary key?

A primary key, also called a primary keyword, is a key in a relational database that is unique for each record. It is a unique identifier, such as a driver license number, telephone number (including area code), or vehicle identification number (VIN). A relational database must always have one and only one primary key.

What are three 3 rules that a database designer has to consider which choosing a primary key?

The criteria for consideration of a primary key are: Uniqueness. Irreducibility (no subset of the key uniquely identifies a row in the table) Simplicity (so that relational representation & manipulation can be simpler)

Can date be a composite primary key?

The primary key is composite made up of the date in which the part price is being modified, and a foreign key to the Part's unique ID on the Parts Table.

When designing a database table what is the primary key?

The primary key is a column that is used to uniquely identify each row. An example might be Product ID or Order ID. Look at each table and decide how the data in one table is related to the data in other tables. Add fields to tables or create new tables to clarify the relationships, as necessary.


2 Answers

Consider a table of parts inventory -- if you want to store the inventory level at the end of each day then a composite primary key on part_id and date_of_day would be fine. You might choose to make that a unique key and add a synthetic primary key, particularly if you have one or more tables referencing it with a foreign key constraint, but that aside, no problem.

So there's nothing necessarily wrong with it, but like any other method it can be used incorrectly as in Patrick's example.

Edit: Here's another comment to add.

I'm reminded of something I wrote a while ago on the subject of whether date values in databases really were natural or synthetic. The readable representation of a date as "YYYY-MM-DD" is certainly natural, but internally in Oracle this is stored as a numeric that just represents that particular date/time to Oracle. We can choose and change the representation of that internal value at any time (to different readable formats, or to a different calendar system entirely) without the internal value losing its meaning as that particular date and time. I think on that basis, a DATE data type lies somewhere between natural and synthetic.

like image 124
David Aldridge Avatar answered Oct 11 '22 03:10

David Aldridge


I am ok with it being part of the key, but would add that you should also have an auto-incrementing sequence number be a part of the PK, and enforce that any date is written to the database as UTC, with the downstream systems than converting to local time.

A system that I worked in decided that it would be a grand idea to have an Oracle trigger write to a database whenever another table was touched, and make the sysdate be part of the primary key with no sequence number. Only problem is that if you run an update query that hits the row more than once per second, it breaks the primary key on the table that is recording the change.

like image 35
Patrick Harrington Avatar answered Oct 11 '22 03:10

Patrick Harrington