Q1. What is a primary key and why is it important?
basicAnswer
A primary key uniquely identifies each row in a table. It enforces entity integrity, cannot be NULL, must be unique, and is automatically indexed. It's used for lookups and foreign key relationships.
Example Code
1CREATE TABLE users (
2 id SERIAL PRIMARY KEY, -- Auto-incrementing primary key
3 email VARCHAR(255) UNIQUE NOT NULL,
4 created_at TIMESTAMP DEFAULT NOW()
5);
6
7-- Alternative: UUID primary key
8CREATE TABLE orders (
9 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
10 user_id INT REFERENCES users(id)
11);