Home > Back-end >  what's the difference between those two sql statements?
what's the difference between those two sql statements?

Time:11-24

what's the difference between those two sql statements to set primary key constraints?

first one:

CREATE TABLE Persons (
    ID int NOT NULL,
    PRIMARY KEY (ID)
);

second one:

CREATE TABLE Persons (

ID INT NOT NULL ,
constraint pk_Persons primary key (ID)
)

thanks in advance

CodePudding user response:

In the second version, you can give a name to your constraint and you can make primary key with multiple columns, allowing you to drop it using the name instead of having to name every column of a composite constraint.

This is a duplicate question from What is the difference between Primary Key only and Primary Key constraint?

CodePudding user response:

You don't need to actually write NOT NULL because you add a Primary Key constraint. the Primary Key constraint has 3 rules:

  1. Unique
  2. Integrity (not null)
  3. Relational (every Foreign Key needs to be linked to a correct Primary Key)
  • Related