-2

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.

  • 1
    Constraints are not indexes. PS Where are you stuck in what textbook or other authoritative reference, or SO duplicate? Please don't ask for yet another to be written. Researched non-duplicate questions are expected. [ask] [help] PS A name lets you refer to a thing. If you don't name the constraint explicitly, it gets a name implicitly per the DBMS. If a constraint is violated, the system can tell you which one. If you want to change one, you can tell the system which one. This is explained in appropriate documentation/introductions. – philipxy Mar 20 '21 at 22:20

1 Answers1

0

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.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786