views:

224

answers:

3

Hi.

I am using MySQL for my database. I have a requirement to store a list of users in the system. The user will have both first name and last name.

I cannot have the first name and second name as the primary key. So I need a indexing key. But I do not want to enter the index value every time to add a new row.

I want the system to handle this with auto increment of previous value every time I try to add a new user. Please let me know how to do this.

Also I am supposed to do a search operation on the list of user with the first name. Please let me know the efficient way to do the search as the number of records could be more than a million.

Please advise. Thanks in advance.

A: 

Add an INT() id column with auto_increment set. This way, the id column value will be incremented automatically on each INSERT.

Alan Haggai Alavi
A: 

To get a unique key without having to specify it, you need an auto_increment field:

create table people (
    person_id int primary key auto_increment,
    first_name varchar(50),
    : : :
    );

For efficiently searching first names, you just need an index on the first-name column. A couple of million rows is not a big table, so this should be reasonably efficient.

create index person_index using btree on people (first_name);
paxdiablo
You don't need auto_increment to get a unique key - but auto_increment will GIVE you a unique key AS LONG AS you don't explicitly specify the person_id in inserts. Apart from that, setting the person_id to unique instead of using it as a primary key is not a very good idea. And if you're expecting a couple of million rows int(3) is woefully inadequate
Fake51
Thanks, @Fake51, I modified the text to state what I meant rather than what I wrote :-) and fixed the index as well.
paxdiablo
+3  A: 
create table users (
 id int primary key auto_increment,
 name varchar(64),
 ...
)

See here for more info

nos