Sobes.tech
Junior — Middle

Can you explain what a primary key in a database is?

sobes.tech AI

Answer from AI

A Primary Key in a database is a unique identifier for each record in a table. It guarantees that each row will be uniquely identified and will not be duplicated. Usually, a primary key is a single field or a set of fields whose values are unique and cannot be NULL.

Example: In a users table, the user_id field can be a primary key to uniquely identify each user.

CREATE TABLE users (
  user_id INT PRIMARY KEY,
  username VARCHAR(50),
  email VARCHAR(100)
);

The primary key helps quickly locate records and ensures data integrity.