PaulCheung wrote:
I created a MySQL database with tables. In one of the tables, the
Customer Name& Address table, I use as the PRIMARY KEY a numeric field
that I call LICENCE which is a number generated through a random number
generator.
When I set the table up I set the LICENCE to INT(8). When populating
this field I was expecting any number larger than 8 digits to be
truncated and any number less than 8 digits to be padded out with zeros,
in both cases this is not happening. LICENCE is being populated with a
random number of varying size returned from the random number generator.
sometimes 9 digits, 8 digit, 7 digits and so on; but each time a unique
number. I thought INT(8) meant an interger of 8 digits long, no more and
no less. Does anybody know a way around this?
Paul
The way interpret the size setting is that it is the maximum length in
characters. So I am a bit surprised that you can stuff something longer
in the field. I never would expect MySQL to pad anything as there is no
means to know by MySQL what you want it to be padded with.
I also wonder if one can limit an Integer field by character length
anyway. Before crawling through the ifs and buts of MySQL, I'd just pad
the number, if that is necessary at all. You should be able to set the
random number generator to spit out integers of fixed and thus the same
length.
Keep in mind that there is no real randomization with those generators.
They use an algorithm that on millions of tries likely generates
millions of different numbers, but does not guarantee to do so. That
scam is the same in any programming language I ever came across. I'd go
with something that is really unique, such as the unix time stamp.
Unless you anticipate more than one entry per second that will be as
unique as it gets as there will no second be repeated ever
again....well, unless the system clock doesn't run on GMT and gets
potentially set back from a time server sync. You can combine unix time
stamp with the numerical portions of the current session ID (assuming
you use sessions). That ought to do it.
Is there any specific significance to the LICENSE field (I doubt it)?
Maybe you can make it to be the primary key and set it to autonumber or
read the highest field value and add 1 through code.
David
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk
NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com
Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php