views:

47

answers:

2

I have some records that I need to store in a database table and I want to distinguish one record from other records on the basis of name field.

But as the datatype of name field is varchar, it will effect the performance because comparing varchar data with each other is time consuming process in comparision to a numeric field.

So I want each record to have a unique numeric field (say id). But if I make the id as primary key, then the more than one record can contain same name.

What's the solution to the above problem?

A: 

Name can be made unique index.

fastcodejava
+4  A: 

You can still create a UNIQUE constraint on the name field:

ALTER TABLE your_table ADD UNIQUE (name);

The UNIQUE constraint, like the PRIMARY KEY constraint, will guarantee that your column will contain only unique values.

Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint.

Daniel Vassallo
+1 ....... thanks
Yatendra Goel
Could you please look at the question at http://stackoverflow.com/questions/2301691/what-datatype-to-use-for-a-field-that-can-contain-a-value-from-pre-defined-values
Yatendra Goel