Sobes.tech
Junior — Middle

What is the role of a primary key in an SQL database table?

sobes.tech AI

Answer from AI

A Primary Key in an SQL database table performs several important functions:

  • Unique record identification: Each primary key value is unique, allowing for unambiguous identification of each row in the table.
  • Data integrity enforcement: The primary key helps prevent duplicate records in the database.
  • Relationship with other tables: The primary key is often used as a foreign key in other tables to establish relationships between data.

Example of creating a table with a primary key:

CREATE TABLE Users (
    UserID INT PRIMARY KEY,
    UserName VARCHAR(100),
    Email VARCHAR(100)
);

Here, UserID is the primary key that guarantees the uniqueness of each record in the Users table.