473,651 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String vs Numeric Type

would it matter if I decided to make a primary key a string of numbers
vs actual numbers ? would it make any difference to DB2 in terms of
efficiency ? why would you choose numeric over a string for a key that
does not need to be in numeric format ?

thanks
--
reply to newsgroupsurfer ATyahooDOTcom
Nov 14 '05 #1
54 6263
Ian
Victor Mehta wrote:
would it matter if I decided to make a primary key a string of numbers
vs actual numbers ? would it make any difference to DB2 in terms of
efficiency ? why would you choose numeric over a string for a key that
does not need to be in numeric format ?


Probably the most significant difference at a physical database level is
the fact that INTEGERs require 4 bytes of storage, while you would need
CHAR(10) to store the same range of values.

However, this should never be decided at a physical database level.
Your data model will (should) always determine the proper domain for
your attributes.
Nov 15 '05 #2
Ian wrote:
Victor Mehta wrote:
would it matter if I decided to make a primary key a string of numbers
vs actual numbers ? would it make any difference to DB2 in terms of
efficiency ? why would you choose numeric over a string for a key that
does not need to be in numeric format ?

Probably the most significant difference at a physical database level is
the fact that INTEGERs require 4 bytes of storage, while you would need
CHAR(10) to store the same range of values.

However, this should never be decided at a physical database level.
Your data model will (should) always determine the proper domain for
your attributes.

Indeed! I've just been butting heads with a customer where a "device ID"
which is an "encoding" of "bit ranges" was implemented as INTEGER.
Next thing I hear is "Why doesn't DB2 support UNSIGNED INTEGER because
we loose the 0x8000000 bit."
After some patient explanation and pointing to little and big endian in
a mixed environment they are inching towards CHAR(4) FOR BIT DATA (aka
BINARY) now.
Data types are no afterthought... .

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 15 '05 #3
Ultimately, all data is stored as bits, so the data type INDEXed should
be irrelevant. Personally, i try to use INTEGER as my PRIMARY KEYs,
because i like to refer to TABLEs as objects, and the individual
records as instances of those objects. Having a COLUMN in most every
TABLE called "Id" and having it always be an INTEGER is very convenient
and consistant. Plus, if another TABLE is to REFERENCE this TABLE, a
long character string is not needed, just a (relatively) small INTEGER.

There are other reasons i like it. I find that when text is used for an
identifier, the text may change over time. A non-descript INTEGER
shouldn't and therefore makes an excellent PRIMARY KEY. I'll then make
the character field UNIQUE instead.

So, i simply find the use of an INTEGER to be better fitted.

A string can be used just as well.

B.

Nov 15 '05 #4
Brian Tkatch wrote:
Ultimately, all data is stored as bits, so the data type INDEXed should
be irrelevant.


It depends what is important to you. From a conceptual point you are right.
But practically, there are some differences that might be important.
Storing an integer as char(10) requires 10 bytes. That wastes space in the
index and makes comparison operations slower (byte-wise instead of a single
processor instruction). So the internal representation of the data does
matter.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 16 '05 #5
Knut Stolze wrote:
Brian Tkatch wrote:
Ultimately, all data is stored as bits, so the data type INDEXed should
be irrelevant.

It depends what is important to you. From a conceptual point you are right.
But practically, there are some differences that might be important.
Storing an integer as char(10) requires 10 bytes. That wastes space in the
index and makes comparison operations slower (byte-wise instead of a single
processor instruction). So the internal representation of the data does
matter.


And comparing integers gives funny results if the strings are left
justified rather than right justified...

--
Jonathan Leffler #include <disclaimer.h >
Email: jl******@earthl ink.net, jl******@us.ibm .com
Guardian of DBD::Informix v2005.02 -- http://dbi.perl.org/
Nov 16 '05 #6
Ah, i see i misread the original question for numric versus
alpha-numeric.

I might as well ask though....
Storing an integer as char(10) requires 10 bytes. That wastes space in the index and
makes comparison operations slower (byte-wise instead of a single processor instruction).


Isn't there something about compacting INDEXes? Or that when most of
the first part of the INDEX values are the same, it really doesn't
matter much when traversing the INDEX?

That is, being numbers are relatively the same:

0001
0002
0003
0004
0005
0006

Here the first three digits do not need to be searched at all since
they are always the same.

B.

Nov 16 '05 #7
Ian
Brian Tkatch wrote:
Ultimately, all data is stored as bits, so the data type INDEXed should
be irrelevant. Personally, i try to use INTEGER as my PRIMARY KEYs,
because i like to refer to TABLEs as objects, and the individual
records as instances of those objects. Having a COLUMN in most every
TABLE called "Id" and having it always be an INTEGER is very convenient
and consistant. Plus, if another TABLE is to REFERENCE this TABLE, a
long character string is not needed, just a (relatively) small INTEGER.


Oy. Data modelers are rolling in their graves. A generic "ID" column
is really, really confusing. At least give it a useful name, like
CUSTOMER_ID or ORDER_ID. What do you do when you need do use the "ID"
column from the CUSTOMER table in the ORDERS table (which already has
an "ID" column as the primary key?)

Nov 16 '05 #8
Jonathan Leffler wrote:
Knut Stolze wrote:
Brian Tkatch wrote:
Ultimately, all data is stored as bits, so the data type INDEXed should
be irrelevant.


It depends what is important to you. From a conceptual point you are
right. But practically, there are some differences that might be
important. Storing an integer as char(10) requires 10 bytes. That
wastes space in the
index and makes comparison operations slower (byte-wise instead of a
single
processor instruction). So the internal representation of the data does
matter.

And comparing integers gives funny results if the strings are left
justified rather than right justified...


Funny results when comparing integers is a minor issue when compared to
the potential problems in data entry. Many years ago, I worked on an
application that was storing (key value) integers in a character field
and discovered that there were a number of unique entries in the
database for key "1". Between left, right, center justification and the
inclusion or lack of leading zeros, it was impossible to locate the
correct data for this key.

Phil Sherman
Nov 16 '05 #9
Actually, it drives me batty when people do that. To me, nearly every
TABLE should have three standard COLUMNs with the *exact* same names,
Id, Name, Description. That way, each TABLE is an object, and each
record is an instantiation of that object. To do it otherwise, to me,
is to completely misunderstand what a database is.

If there is a COLUMN in the customer TABLE called Id, it *by defintion*
is the customer's id. Calling it "customer_i d" is redundant.

When a TABLE REFERENCES another TABLE, the _TABLE name_ should be used,
*not* the COLUMN name. Thus, in the orders TABLE, the REFERECEing
COLUMN name should be "customer" not "customer_i d".

B.

Nov 16 '05 #10

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

Similar topics

1
1650
by: Owen Jacobson | last post by:
Salve. Does anyone have any suggestions for writing a portable 'byte' numeric type? I'm aware that (signed) char is a numeric type and can be used as such, and I assume this is the fundamental building block for what I'm trying to do; however, I can't think of a way to typedef this that won't stomp on the definition of char on at least some platforms. Ideally, I'd like to be able to have the following:
1
1758
by: Nagib Abi Fadel | last post by:
Hi, does specifiying the precision and the scale of a numeric type increases the speed of calculations on a column of that type ??? (I read the docs : nothing mentioed about that.) Thx
4
17674
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way to do a generic cast or conversion on the type? Here is sort of what I want to do: object MyFunc(Type T, String Str) { object o;
6
9208
by: M.A. Oude Kotte | last post by:
Hi All, I hope this is the correct mailing list for this question. But neither postgresql.org nor google could help me out on this subject. I did find one disturbing topic on the mailing list archives (http://archives.postgresql.org/pgsql-admin/2000-05/msg00032.php), but since it was quite old I'm posting my question anyway. I'm writing a generic database layer that should support a fixed number of generic numeric types on a number of...
2
2052
by: Frank | last post by:
Hello, I am attempting to retrieve a numeric type froma a table. I get an invalid cast exception when I use the following code in my data layer: //The problem definitley occurs in the line"id = (int) cmdSelect.ExecuteScalar();"
2
1323
by: Brian Tkatch | last post by:
>A) If you have a 17-way join where the aliases are all one letter, >you should fire all your programmers and get people who think. My DBA slammed this at me when he saw i did that with a 14-way join. Ha! :) My excuse is: One PROCEDURE that has one query, that SELECTs from one TABLE and gets names off of FKs to 13 lookup TABLEs, so i put it in a join (as opposed
4
9165
by: Hyun-jik Bae | last post by:
Is that not allowed to assume generic type numeric type? As far as I've tried, I got an error with the following code: public class AAA<T> { public int Foo(T a) { return a; // error: Cannot implicitly convert type 'T' to 'int' } }
5
1608
by: Andreas Beyer | last post by:
There has been quite some traffic about mutable and immutable data types on this list. I understand the issues related to mutable numeric data types. However, in my special case I don't see a better solution to the problem. Here is what I am doing: I am using a third party library that is performing basic numerical operations (adding, multiplying, etc.) with objects of unknown type. Of course, the objects must support the numerical...
2
3998
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The functions strtol, strtod, and strtoul, defined in <cstdlib>, convert a null- terminated character string to a long int, double, or unsigned long. You can use them to convert numeric strings of any base to a numeric
3
2082
by: aris1234 | last post by:
Hi.. i have field in record using type : numeric, how to make validation and i want viewing messagebox if user inputing character or empty? this code just work for empty field, not for numeric type, if(WithoutContent(document.add.sprice.value)) { errormessage += "\n\n\"PRICE\" still Empty."; } ----------
0
8352
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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
8802
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...
1
8465
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,...
0
8579
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7297
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
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.