473,795 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What datatype to use for PK?

Over the years I have always used the decimal(18,0) as the datatype for
primary keys. Aside from the number of significant numbers involved,
would BigInt type be better for performance or is decimal(18,0) still okay.
--
Don Vaillancourt
Director of Software Development
WEB IMPACT INC.
phone: 416-815-2000 ext. 245
fax: 416-815-2001
email: do**@web-impact.com <mailto:do**@we bimpact.com>
web: http://www.web-impact.com
Web Impact Inc. <http://www.web-impact.com>
This email message is intended only for the addressee(s) and contains
information that may be confidential and/or copyright.

If you are not the intended recipient please notify the sender by reply
email and immediately delete this email.

Use, disclosure or reproduction of this email by anyone other than the
intended recipient(s) is strictly prohibited. No representation is made
that this email or any attachments are free of viruses. Virus scanning
is recommended and is the responsibility of the recipient.
Jul 23 '05
35 3217
>> Over the years I have always used the decimal(18,0) as the datatype
for primary keys. <<

That's wrong! The best key is the 17-digit Hebrew number that God puts
on the bottom of everything in creation!
Sounds pretty silly, doesn't it? But this the same thing as your
question.

There is no "magical, Universal, one-size-fits-all" key. There are
industry standards and natural keys, which you discover with research.
There are techniques for designing keys when the research fails --
check digits, grep patterns, validation rules, etc.

If you have been blindly writing DECIMAL(18,0) as the key on your
tables, then you probably have never actually designed an RDBMS.
Instead, you have been faking pointer chains and have not had any
relational keys.

You might want to get a course in the basics and learn exactly what a
key is. I would also do a full data audit on what you have now.

Jul 23 '05 #21
Ok,
I think my example of address was taken a bit to literally. I used it
only as an example. Where I was going with this is that i can have a
table that I can mix foreign keys from different parents. Before you
fall over or start the flame war, here is a system, There are reports,
there are Contacts, Vehicles, and Property all attached to these
reports. All of these (reports,contac ts,vehicles, and property) can
have multiple images attached to them. I could have:

1. Seperate tables for ReportImages,Co ntactImages,Veh icleImages, and
PropertyImages all with there own pointers to the addresses.
I chose not to use this as it would mean 4 times the stored
procedures etc.

2. a single table with a column for parentID (int), and
ParentType(int) .
I chose against this as most of the UI controls work better
with one value

3. a single table with a column for parentID (GUID)
i chose this method because I could select from one place
using one Column as the criteria.
This method also allowed me to find images based upon their
description(var char(250)) no matter what parent they were associated
with. Sometimes a Smashed vehicle picture is attached to the report
not the vehicle and vice versa. Once I find the image then I can find
the parent record as needed.

well there is my defense hope it holds up.

tal

Jul 23 '05 #22
I have been designing databases for about 5 years. And from a
structural stand-point my designs have always been excellent. I have
always been able to add tables at a later time without falling into a
hole wishing I had designed the schema differently.

But, yes, I do lack knowledge in the area of what is the best types to
use for performance reasons. But I have never been corrected in my
selection of PK types even by well experienced DBAs.

What I am really interested in is if MSSQL (even, Oracle, MySQL, etc)
handles decimal differently than integer when generating keys and such.

Will an 8-byte BIGINT perform better than a 9-byte Decimal(18,0) when
dealing with millions of rows.

I wouldn't know how to test this since the time it would take to perform
such a test would probably differ in the nano-seconds if not
insignificant milli-seconds. Which is still even harder to test on a
multitasking OS when your result time is certainly always different.

--CELKO-- wrote:
Over the years I have always used the decimal(18,0) as the datatype


for primary keys. <<

That's wrong! The best key is the 17-digit Hebrew number that God puts
on the bottom of everything in creation!
Sounds pretty silly, doesn't it? But this the same thing as your
question.

There is no "magical, Universal, one-size-fits-all" key. There are
industry standards and natural keys, which you discover with research.
There are techniques for designing keys when the research fails --
check digits, grep patterns, validation rules, etc.

If you have been blindly writing DECIMAL(18,0) as the key on your
tables, then you probably have never actually designed an RDBMS.
Instead, you have been faking pointer chains and have not had any
relational keys.

You might want to get a course in the basics and learn exactly what a
key is. I would also do a full data audit on what you have now.

--
Don Vaillancourt
Director of Software Development
WEB IMPACT INC.
phone: 416-815-2000 ext. 245
fax: 416-815-2001
email: do**@web-impact.com <mailto:do**@we bimpact.com>
web: http://www.web-impact.com
Web Impact Inc. <http://www.web-impact.com>
This email message is intended only for the addressee(s) and contains
information that may be confidential and/or copyright.

If you are not the intended recipient please notify the sender by reply
email and immediately delete this email.

Use, disclosure or reproduction of this email by anyone other than the
intended recipient(s) is strictly prohibited. No representation is made
that this email or any attachments are free of viruses. Virus scanning
is recommended and is the responsibility of the recipient.
Jul 23 '05 #23
David Portas (RE************ *************** *@acm.org) writes:
Say, two orders are placed simultaneously by different entities
(departments or whatever) and you don't record the information that
distinguishes them then how will you know which is which? The
artificial order number doesn't tell you that. In your hypothetical
scenario wouldn't the order confirmation emails go to the same address?
So even the customer's Accounts Payable department wouldn't know which
items on the order belonged to which department unless the end-user
recorded that information separately. Is there a benefit in issuing
multiple order numbers in this scenario? I'm not sure, but if there
were and you wanted to persist information about different, arbitrary
"sub-groups" of order items with all other attributes of the order
identical then in 3NF that information belongs as an attribute in the
Order Details table, not as a duplicate in the Order table. That's
unless the "department " attribute was recorded as well, in which case
Department would obviously become part of the Order table's key.


Most likely there is information to tell the orders apart, or if
they are duplicates. But that information is likely to be both
unpractical and uninteresting to use to identify the order.
The idea with a "natural key" as being something "outside the system"
may sound nice, but it does not model the reality for real-life system.


I thought "real-life" was "outside the system" but maybe your
definition of reality differs from mine. :-)


Yes, many real-life system lives outside the system known as theory.
All systems that stores customers for instance - another concept that
does not have any useful natural key...
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #24
I differ on whether natural keys are "unpractica l", "uninterest ing" and
"not useful". There are plenty of scenarios in which natural keys are
not only useful but essential - data integration betweeen heterogeneous
data sources for example.

At least we do after all seem to agree that, with the right model,
natural keys always exist - whether you choose to implement them or not
is simply a decision to be made at design time.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #25
David Portas (RE************ *************** *@acm.org) writes:
I differ on whether natural keys are "unpractica l", "uninterest ing" and
"not useful". There are plenty of scenarios in which natural keys are
not only useful but essential - data integration betweeen heterogeneous
data sources for example.
I certainly don't wish to imply that natural keys never are useful. There
are certainly cases where they are.

But I like to point out that what is a natural key for one system, often
is key generated by another system, and thus originally an artificial key.
In fact, this can happen within a system as well. If I identify a position
by account number and instrument id, that is a natural key composed by
two artificial keys.
At least we do after all seem to agree that, with the right model,
natural keys always exist - whether you choose to implement them or not
is simply a decision to be made at design time.


Certainly a disagreement on "right". I claim that "customer" is a concept
that in many situations does not have a natural key. Even if you in could
in theory find information that you could use as a natural key, it would
be a gross error of system design to include in your database. And even
if you would include it, it would be very difficult to verify that you
actually have the correct values in your database.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #26
You are correct that Customer data can pose some big challenges of data
verification. However, it would be an error NOT to enforce keys on this
data in my experience. Worst case is that you duplicate all the
customer attributes and allocate a new artifical key. In that scenario
there is likely to be little or no hope of ever determining which row
represents which physical customer or ensuring that referencing rows
point to the "right" customer. In other words you gain *nothing* from
having a redundant row in your table and you lose a lot from not having
a natural key. Use constraints to prevent duplicates and verify data at
entry or as part of a data integration process. There is plenty of
software out there to help verify and manage customer databases. It may
never be 100% accurate but it will be more useful and manageable with a
key than without one.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #27
David Portas (RE************ *************** *@acm.org) writes:
You are correct that Customer data can pose some big challenges of data
verification. However, it would be an error NOT to enforce keys on this
data in my experience. Worst case is that you duplicate all the
customer attributes and allocate a new artifical key. In that scenario
there is likely to be little or no hope of ever determining which row
represents which physical customer or ensuring that referencing rows
point to the "right" customer. In other words you gain *nothing* from
having a redundant row in your table and you lose a lot from not having
a natural key. Use constraints to prevent duplicates and verify data at
entry or as part of a data integration process. There is plenty of
software out there to help verify and manage customer databases. It may
never be 100% accurate but it will be more useful and manageable with a
key than without one.


Assume that you run a web shop. How do you verify new customers? Well,
many web shops seems to use a user id or the mail address. When a new
customer register, they should probably check that some other crucial
attrbutes are not already in use by other customers, such as e-mail address
(if you use a user id), national registration number and credit-card number.
However, few business would require you to register all three. In any case,
if a customer prefers to register a second time, there is very little you
can do to stop him.

For a system where the customers are registered by staff through a GUI, you
can add some checks to the registration form. And there is a whole lot of
point of having the checks there and not in the database, because such
checks can help to prevent the user from entering data by mistake, but
not stop him from entering data he actually has to enter. It's naïve to
believe that you thought of each and every case when you designed your
database.

For instance, in our system there is at one case where users will have to
register a customer a second time, and that is if the customers changes tax
countries in the middle of the year. Yes, this can be construed as a
weakness of our system that we don't keep a history of tax country.

Then again, the driving force when we develop our system is what our
customer are prepared to pay for, and not fulfilment of theoretic rules.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #28
> Assume that you run a web shop. How do you verify new customers?
Well,
many web shops seems to use a user id or the mail address. When a new customer register, they should probably check that some other crucial attrbutes are not already in use by other customers, such as e-mail address (if you use a user id), national registration number and credit-card number. However, few business would require you to register all three. In any case, if a customer prefers to register a second time, there is very little you can do to stop him.
Isn't email address the key here? Sites I've used don't let me register
a second time with the same address and that seems like the only
sensible policy if the site issues its own user ID to the user. If the
user supplies his own user login name then that might be preferred to
email address as a key. In either case, I don't see a problem.
Then again, the driving force when we develop our system is what our
customer are prepared to pay for, and not fulfilment of theoretic

rules.

If the customer won't pay for it that is indeed a good reason not to do
it! Of course they may end up paying more in the end for you to come
back and fix it :-)

--
David Portas
SQL Server MVP
--

Jul 23 '05 #29
David Portas (RE************ *************** *@acm.org) writes:
Isn't email address the key here? Sites I've used don't let me register
a second time with the same address and that seems like the only
sensible policy if the site issues its own user ID to the user. If the
user supplies his own user login name then that might be preferred to
email address as a key. In either case, I don't see a problem.


Sure, you can use an email as a key! But the difference between an
email address or a system-generated customer number is slim. Both are
unverifiable combinations of random bits. What you can verify with
an email address is that it has a an @ and a . and ends in a known domain.
Of course, you can send out a mail and ask for acknowledge before you
create the user. But that mail address could disappear just seconds
later.

One advantage with a customer number is that if I change my mail address,
I don't have to register as a new customer at the site.

What is really horrible is system that uses users names that looks like
email addresses, but aren't. This means that someone who feels like can
use someone else's mail address as his user name.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
13454
by: Sanjay Minni | last post by:
What is the datatype to be used for Primary Key columns for most optimised access given that - There will be a single column primary key only - The values will only be integers (but as strings) at least 12 digits (characters) long - all positions will be occupied (no leading 0's) - Tables may have upto 1m+ rows
8
1952
by: Eternally | last post by:
Hi folks, I've got a program which has a function which uses templates to accept parameters of any type. Works well, but there's one certain datatype which I want to special case and do an extra thing to. The datatype is a class I made. Is there anyway for me to test a parameters datatype in a template using function?
0
1688
by: SoYouKnowBrig | last post by:
Hi All, I am using Microsoft.ApplicationBlocks.Cache.CacheManager to persist a System.Data.Dataset object. This Dataset object has a DataTable that is created from an existing DataTable using the Clone() method. Before I add the new DataTable to the DataSet, I change the DataType of a DataColumn from System.String to System.Int64. I then add data to the new table and then add it to the DataSet. Then DataSet is then added to the Cache....
14
3017
by: Elias Farah | last post by:
Hi All, What are people's experience in timing Query Performance? I have tried in vain, but get many unpredictable results, with execution proceeding in the background and problems of timing precision. I am trying to assess my applications performance difference using FK using BYTE instead of LONG type. and not enforcing integrity at the database level. (ie, Form level). This only becomes SIGNIFICANT when the field is used highly in...
0
929
by: Jim Heavey | last post by:
I just can not figure out how to provide the information correctly to this method... When creating a DataParameter, one of the properties which need to be set is the datatype. To do this, I would do the following.. dc.DataType = typeof(string) This of course assumes the datatyp that I an trying to set up is "string".
2
45904
by: ad | last post by:
I use ado.net to fill a Excel wroksheet into a DataTable. The data in the Excel wroksheet is digital. After the data filled into the DataTable, the DataType of each column is set to Double, but I want to treat them as string, I use the code to covert them DataTable dt = ds.Tables0]; int iColCount = dt.Columns.Count; for (int j = 0; j < iColCount; j++)
2
3840
by: Johan Delimon | last post by:
"The active directory datatype cannot be converted to/from a native DS datatype" I have an ASP.NET web page that uses queries to AD to identify current users. Delegation is configured in AD and works as expected. But sometimes I get an error : "The active directory datatype cannot be converted to/from a native DS datatype" And the only way to resolve this error is to kill the aspnet_wp.exe manually.
3
3345
by: Sri | last post by:
In VB, to know the field type of a column stored in a recordset the command I use is If rsQuery.Fields(k).Type = adCurrency Then How will I achieve the same in ASP.net. I could not find a currency data type in asp.net and the type is idenfied as decimal. I have two fields defined in Sql-Server, one is money and other is decimal. In asp.net both are identified as decimal.
1
1480
by: Prabu Subroto | last post by:
Dear my friends... I want to change the datatype of a completed table (has had already content). But I don't know how. Here is my try: " kv=# alter table customer alter column address set type char(150); ERROR: syntax error at or near "type" at character 47
1
1182
by: Bryan | last post by:
I have a class called "Prop". I want that class to have a property called "DataType" where the user can select and store a datatype. How can I store a DataType value in a class property. how the code would work in my mind: dim TempProp as new Prop TempProp.DataType = String
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10435
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6779
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.