PRIMARY KEY Constraint
PRIMARY KEYS serve as distinct identifiers for each row in a table. They can be values found in a table's single column or a combination of the table's several columns. The PRIMARY KEY column needs to be UNIQUE and cannot be NULL. The primary key in the table's value is a unique identifier for a specific row in the parent table that links the row to additional information present in the child table, where the same distinct identity also exists as a FOREIGN KEY. The PRIMARY KEY constraint can be set either when the table is created or at a later time using an alter statement. The following query creates a PRIMARY KEY on the Customer_Id column in the Customer table.
1CREATE TABLE Customer (
2 Customer_Id int NOT NULL UNIQUE,
3 Age int,
4 City varchar(255) DEFAULT ‘Unknown City’,
5 CHECK (Age>18)
6);
If we’d like to add a PRIMARY KEY after the creation of the table, we’d have to run the following ALTER statement:
1ALTER TABLE Customer (
2ADD PRIMARY KEY (Customer_Id);
3);