🐘 PostgreSQL
Beginner
How do you create a table in PostgreSQL?
Answer
Use the CREATE TABLE statement. Example: CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, email TEXT UNIQUE NOT NULL, created_at TIMESTAMPTZ DEFAULT NOW());. Key constraints: PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT value, CHECK (condition), REFERENCES other_table(column) (foreign key). Use SERIAL or BIGSERIAL for auto-incrementing IDs, or the modern GENERATED ALWAYS AS IDENTITY syntax. IF NOT EXISTS prevents errors: CREATE TABLE IF NOT EXISTS users (...). Describe the created table with \d users in psql.
Previous
What are the basic data types in PostgreSQL?
Next
How do you insert data into a PostgreSQL table?