简体   繁体   中英

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. And I don't drop primary key constraints. So I don't bother naming them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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