473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Question about numeric keys...

how do they work?
so i saw someone posting like...
Expand|Select|Wrap|Line Numbers
  1. Create Table login
  2.  (Id int(4) NOT NULL,
  3.   UserName varchar(20) NOT NULL,
  4.   Password varchar(6) NOT NULL,
  5.   securityQuestion varchar(50) NOT NULL,
  6.   SecurityAnswer varchar(50) NOT NULL,
  7.      Primary key (Id));
This person is clearly using numeric key...
but how do they work?
If I need information regarding a certain person's username and password... how is numeric key going to help the query?
any help would be greatly appreciated
Aug 21 '07 #1
2 1673
Atli
5,058 Expert 4TB
Are you talking about using a numeric field as a primary key?

Primary keys are meant to be a unique value for each row; a way to identify a single row out of the rest, even if all other fields in the table contain identical data.
Integer values are perfect for this, as they can be automatically incremented for each row and they take a relatively small amount of disk space, compared to strings and such.

You could of course use a string value as a primary key, for example: a username, but usernames usually take up a lot more space than integers, which in large relational databases will take up a lot of extra disk space, as all your foreign key's will be strings rather than integers.

There are probably many more reasons that I am to tired to remember, but just the foreign key thing should be reason enough to use integers as primary keys.
Aug 22 '07 #2
pbmods
5,821 Expert 4TB
Heya, prognoob.

The main benefit to using a numeric primary key is that it is unchanging, and it never needs to be changed.

One problem with using, for example, the Username as the primary key, in addition to the warnings Atli posted, is that you will run into all kinds of headaches if your User wants to change his Username! Using a non-data primary key means that you can edit any of the data fields in your table, and you don't have to worry about your foreign keys going out of sync.

Once caveat about the table that you're looking at is that the `id` only has 4 digits, and it doesn't auto_increment. The problems with this setup are that you can only have 9999 (well, technically 10,000) Users. Also, you would have to manually increment the counter each time.

A better way to create this table would be:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE
  2.         `Users`
  3.         (
  4.             `Id`
  5.                 BIGINT(20)
  6.                 UNSIGNED
  7.                 NOT NULL
  8.                 AUTO_INCREMENT,
  9.             `UserName`
  10.                 VARCHAR(60)
  11.                 NOT NULL,
  12.             `Password`
  13.                 CHAR(40)
  14.                 NOT NULL,
  15.             `SecurityQuestion`
  16.                 VARCHAR(50)
  17.                 NOT NULL,
  18.             `SecurityAnswer`
  19.                 VARCHAR(50)
  20.                 NOT NULL,
  21.             PRIMARY KEY
  22.                 (`Id`)
  23.         )
  24.         ENGINE=MyISAM
  25.         DEFAULT CHARSET=utf8;
  26.  
Note the following changes:
  • The table has been renamed to `Users`, because `login` implies that you are using this table to track login sessions, which is not the case.
  • `SecurityQuestion` is capitalized (probably a typo).
  • `Password` is now a CHAR(40) instead of a VARCHAR(6). Why? Because when you save a password, you want to encrypt it, and as it happens, you can use SHA1(), which is very secure and always results in a 40-character string.
Aug 22 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tommo | last post by:
Hello All, I have a slight problem that goes like this. I have created a Perl hash where the keys are made up of numeric values, I was then sorting the 'keys' for this hash on their value (a<=>b...
11
by: Dave [Hawk-Systems] | last post by:
have the table "numbercheck" Attribute | Type | Modifier -----------+------------+---------- svcnumber | integer | not null svcqual | varchar(9) | svcequip | char(1) | svctroub ...
1
by: Dino Buljubasic | last post by:
Hi, Can somebody help me with this problem please. I need to get the value, not just to check for it, of a number pressed but it has to work for both, numbers above QUERTY and numbers entered...
2
by: nickdevx | last post by:
Hi Say if I have a mixed array: $array = array("item1", "2"=>"item2", "5", "item4key"=>"item4") Is it possible while looping through the array (foreach ($array as $key=>$val)) to check if...
5
by: Reny | last post by:
can any one tell how can i restrict my user to type just numeric character in the textbox.I am using VS.NET 2003 (VB.NET)
11
by: John | last post by:
I am coding a radix sort in python and I think that Python's dictionary may be a choice for bucket. The only problem is that dictionary is a mapping without order. But I just found that if the...
0
by: EgoSum | last post by:
Can someone help me with custom text box? I want change behavior custom date text box - disallow entry and pass entry from numeric keyboard to a text box. Code below disallow entry, but how I can...
2
by: EgoSum | last post by:
Can someone help me with custom text box? I want change behavior custom date text box - disallow entry and pass entry from numeric keyboard to a text box. Code below disallow entry, but how I can...
14
by: ablock | last post by:
I have an array to which i have a added a method called contains. I would like to transverse this array using for...in...I understand fully that for...in is really meant for Objects and not Arrays,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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,...
0
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...

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.