如何将密钥对设为主密钥?

时间:2023-01-25 09:37:59

I have table that doesn't need a specific field to be the primary key. I could add a auto-increment ID field, but it won't be used at all within database queries...

我有一个表不需要特定的字段作为主键。我可以添加一个自动增量ID字段,但在数据库查询中根本不会使用它...

Can I make a UNIQUE KEY to be primary too?

我可以制作一个独特的钥匙也是主要的吗?

the UNIQUE KEY consists of two fields paired together:

UNIQUE KEY由两个配对的字段组成:

a VARCHAR(64) NOT NULL,
b VARCHAR(64) NOT NULL,
UNIQUE KEY uk_ab(a,b)

3 个解决方案

#1


6  

Sure (no need for UNIQUE KEY):

当然(不需要UNIQUE KEY):

PRIMARY KEY (a, b)

Have a look at the documentation:

看看文档:

A PRIMARY KEY is a unique index where all key columns must be defined as NOT NULL. If they are not explicitly declared as NOT NULL, MySQL declares them so implicitly (and silently). A table can have only one PRIMARY KEY. The name of a PRIMARY KEY is always PRIMARY, which thus cannot be used as the name for any other kind of index.

PRIMARY KEY是一个唯一索引,其中所有键列必须定义为NOT NULL。如果它们没有显式声明为NOT NULL,那么MySQL会隐式地(并且默默地)声明它们。一个表只能有一个PRIMARY KEY。 PRIMARY KEY的名称始终为PRIMARY,因此不能用作任何其他类型索引的名称。

and

A PRIMARY KEY can be a multiple-column index. However, you cannot create a multiple-column index using the PRIMARY KEY key attribute in a column specification. Doing so only marks that single column as primary. You must use a separate PRIMARY KEY(index_col_name, ...) clause.

PRIMARY KEY可以是多列索引。但是,您无法使用列规范中的PRIMARY KEY键属性创建多列索引。这样做只会将单列标记为主列。您必须使用单独的PRIMARY KEY(index_col_name,...)子句。

#2


1  

Not sure if this would work...

不确定这是否有效......

PRIMARY KEY uk_ab(a,b)

#3


1  

If your table already has a unique constraint, then you need to add primary key (ALTER TABLE [table_name] ADD CONSTRAINT [constraint_name] PRIMARY KEY (a,b)), and drop the existing unique constraint. Otherwise, you will have 2 unique indexes on the same columns.
If you create a new table, use PRIMARY KEY(a,b) instead of UNIQUE(a,b) as proposed by others.

如果表已具有唯一约束,则需要添加主键(ALTER TABLE [table_name] ADD CONSTRAINT [constraint_name] PRIMARY KEY(a,b)),并删除现有唯一约束。否则,您将在同一列上拥有2个唯一索引。如果您创建一个新表,请使用PRIMARY KEY(a,b)而不是其他人提出的UNIQUE(a,b)。

#1


6  

Sure (no need for UNIQUE KEY):

当然(不需要UNIQUE KEY):

PRIMARY KEY (a, b)

Have a look at the documentation:

看看文档:

A PRIMARY KEY is a unique index where all key columns must be defined as NOT NULL. If they are not explicitly declared as NOT NULL, MySQL declares them so implicitly (and silently). A table can have only one PRIMARY KEY. The name of a PRIMARY KEY is always PRIMARY, which thus cannot be used as the name for any other kind of index.

PRIMARY KEY是一个唯一索引,其中所有键列必须定义为NOT NULL。如果它们没有显式声明为NOT NULL,那么MySQL会隐式地(并且默默地)声明它们。一个表只能有一个PRIMARY KEY。 PRIMARY KEY的名称始终为PRIMARY,因此不能用作任何其他类型索引的名称。

and

A PRIMARY KEY can be a multiple-column index. However, you cannot create a multiple-column index using the PRIMARY KEY key attribute in a column specification. Doing so only marks that single column as primary. You must use a separate PRIMARY KEY(index_col_name, ...) clause.

PRIMARY KEY可以是多列索引。但是,您无法使用列规范中的PRIMARY KEY键属性创建多列索引。这样做只会将单列标记为主列。您必须使用单独的PRIMARY KEY(index_col_name,...)子句。

#2


1  

Not sure if this would work...

不确定这是否有效......

PRIMARY KEY uk_ab(a,b)

#3


1  

If your table already has a unique constraint, then you need to add primary key (ALTER TABLE [table_name] ADD CONSTRAINT [constraint_name] PRIMARY KEY (a,b)), and drop the existing unique constraint. Otherwise, you will have 2 unique indexes on the same columns.
If you create a new table, use PRIMARY KEY(a,b) instead of UNIQUE(a,b) as proposed by others.

如果表已具有唯一约束,则需要添加主键(ALTER TABLE [table_name] ADD CONSTRAINT [constraint_name] PRIMARY KEY(a,b)),并删除现有唯一约束。否则,您将在同一列上拥有2个唯一索引。如果您创建一个新表,请使用PRIMARY KEY(a,b)而不是其他人提出的UNIQUE(a,b)。