简体   繁体   English

SQL 查询中的主键约束

[英]primary key constraint in SQL queries

Why and what is the use of naming to a primary key constraint?为什么以及对主键约束进行命名有什么用?

create table users(
   id int
   , lastname varchar(255)
   , firstname varchar(255)
   , age int
   , **constraint pk** primary key(id, lastname)
);

The purpose of naming a constraint is primarily so you can understand error messages when the constraint is violated.命名约束的目的主要是让您可以在违反约束时理解错误消息。

A secondary reason is for dropping the constraint.第二个原因是放弃约束。 It is much easier to find the constraint when it has a meaningful name.当约束具有有意义的名称时,它更容易找到约束。

That said, in tables that I create, the primary key is usually a synthetic key -- set automatically via auto_increment for instance.也就是说,在我创建的表中,主键通常是合成键——例如通过auto_increment自动设置。 And I don't drop primary key constraints.而且我不会放弃主键约束。 So I don't bother naming them.所以我懒得命名它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM