Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Primary Key Choice

Author  Topic 

karlman
Starting Member

11 Posts

Posted - 2009-12-17 : 12:41:24
I have a table (SQL 2005) used to hold job ticket information. The selection for the job number has already been made.

YYMMDDSSSSS

YY - Last 2 digits of year
MM - Month
DD - Day
SSSSS - Sequence for that day (resets each day)

Example: 09121700001

I am not exactly sure the best way to make this my primary key. It is too large for an INT so I figured I would either make it a BIGINT or perhaps make the key a combination of a date field and an int field (sequence). The two field PK seems cleaner but I suspect that the BIGINT would have better performance. Any other sugestions? Perhaps a surrogate key that is simply an int and hold the job number in another field?

Also, what is the best method of creating the keys during runtime to prevent duplicate keys? Would this best be handled by a stored procedure?

Thank you!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-17 : 12:47:48
You can't use int, bigint, or any of the number data types if you are going to lead the data with a 0 for 2009. You should probably use a CHAR instead, since it's fixed width.

I wouldn't split the data up into two columns.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

karlman
Starting Member

11 Posts

Posted - 2009-12-17 : 13:52:10
quote:
Originally posted by tkizer

You can't use int, bigint, or any of the number data types if you are going to lead the data with a 0 for 2009. You should probably use a CHAR instead, since it's fixed width.

I wouldn't split the data up into two columns.



If used BIGINT then I would store the full year and display as required in reporting, folder and file names.

Any idea what kind of impact to performance I would have using a CHAR(13) compared with a BIGINT?

Thank you!
Karl
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-17 : 19:58:40
I would bet you wouldn't notice a performance difference, but you should test it to be sure.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

karlman
Starting Member

11 Posts

Posted - 2009-12-18 : 00:55:59
I will.

Thank you!
Karl
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-18 : 01:04:12
You're welcome.

BIGINT is 8 bytes and CHAR(13) is 13 bytes, so not a huge difference at all.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -