如何区分数据库表中的各行?

时间:2021-07-29 00:21:19

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.

我有一些需要存储在数据库表中的记录,我想根据name字段区分一条记录和其他记录。

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.

但是由于name字段的数据类型为varchar,这就会影响性能,因为在将varchar数据与其他数据进行比较时,对数值字段的比较是一个耗时的过程。

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.

所以我希望每个记录都有一个唯一的数字字段(比如id)。但是如果我将id作为主键,那么不止一个记录可以包含相同的名称。

What's the solution to the above problem?

解决上述问题的办法是什么?

2 个解决方案

#1


4  

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

您仍然可以在name字段上创建唯一的约束:

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.

注意,每个表可以有许多惟一的约束,但是只有一个主键约束。

#2


0  

Name can be made unique index.

可以使名称成为唯一索引。

#1


4  

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

您仍然可以在name字段上创建唯一的约束:

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.

注意,每个表可以有许多惟一的约束,但是只有一个主键约束。

#2


0  

Name can be made unique index.

可以使名称成为唯一索引。