简体   繁体   中英

What is the benefit of specifying a custom constraint index with primary key, foreign key or unique etc. in SQL?

I am trying to understand the index concept in SQL. It is clear to me why we use the index for columns but check these 2 variants of codes:

ALTER TABLE Titles ADD CONSTRAINT Titles_pri_key PRIMARY KEY (title_id);

and

ALTER TABLE Titles PRIMARY KEY (title_id);

I couldn't come up with an idea about why specifying "Titles_pri_key" would be a good idea.

Thanks in advance for all your answers.

The two are functionally equivalent -- both define primary keys, which involve creating a unique index of some type (in some databases, this is a clustered index).

The difference is the naming of the primary key constraint. In most databases, the constraint name will be used in the error message when the constraint is violated. Having a user-defined name can help in understanding the error message.

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