How to distinguish rows from each other in a database table?

I have records that I need to save in a database table, and I want to select one record from other records based on a field name.

But since the data type is in the name field varchar, it will affect performance, since comparing data varcharwith each other is a time-consuming process compared to a numerical field.

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

What is the solution to the above problem?

+3
source share
2 answers

You can create a restriction UNIQUEin the field name:

ALTER TABLE your_table ADD UNIQUE (name);

A constraint UNIQUE, like a constraint PRIMARY KEY, ensures that your column contains only unique values.

Please note that there can be many restrictions for each table UNIQUE, but only one restriction PRIMARY KEY.

+4
source

The name can be made by a unique index.

0
source

Source: https://habr.com/ru/post/1733648/


All Articles