Alter Tables
Published: (March 29, 2026 at 12:20 PM EDT)
1 min read
Source: Dev.to
Source: Dev.to
Make the email column NOT NULL in customers
ALTER TABLE customers ALTER COLUMN email SET NOT NULL;Ensure username is unique in users
ALTER TABLE users ADD CONSTRAINT unique_username UNIQUE (username);Enforce that price is greater than 0 in products
ALTER TABLE products ADD CONSTRAINT price CHECK (price > 0);Set the default value of status to 'pending' in orders
ALTER TABLE orders ALTER COLUMN status SET DEFAULT 'pending';Add a salary column to employees (NOT NULL, greater than 10,000)
ALTER TABLE employees ADD COLUMN salary NOT NULL CHECK (salary > 10000);Add a cascade‑delete foreign key between employees and departments
ALTER TABLE employees ADD CONSTRAINT employees_department_id
FOREIGN KEY (department_id) REFERENCES departments(id) ON DELETE CASCADE;Remove the CHECK constraint that enforces balance >= 0 in accounts
ALTER TABLE accounts DROP CONSTRAINT accounts_balance;Ensure the combination of user_id and transaction_id is unique in payments
ALTER TABLE payments ADD CONSTRAINT unique_transaction UNIQUE (user_id, transaction_id);