CA 40 - Alter Tables
Today I practiced ALTER TABLE in SQL. This is used when we want to change an existing table. 1. Make Email NOT NULL (customers) ALTER TABLE customers MODIFY email VARCHAR(100) NOT NULL; Now email i...

Source: DEV Community
Today I practiced ALTER TABLE in SQL. This is used when we want to change an existing table. 1. Make Email NOT NULL (customers) ALTER TABLE customers MODIFY email VARCHAR(100) NOT NULL; Now email is required 2. Make Username UNIQUE (users) ALTER TABLE users ADD CONSTRAINT unique_username UNIQUE (username); No duplicate usernames 3. Add CHECK on Price (products) ALTER TABLE products ADD CONSTRAINT check_price CHECK (price > 0); price must be > 0 4. Default Status (orders) ALTER TABLE orders ALTER COLUMN status SET DEFAULT 'pending'; if not given → pending 5. Add Salary Column (employees) ALTER TABLE employees ADD salary INT NOT NULL CHECK (salary > 10000); salary required + > 10000 6. Auto Delete Employees (FK) ALTER TABLE employees DROP CONSTRAINT employees_department_id_fkey; ALTER TABLE employees ADD CONSTRAINT employees_department_id_fkey FOREIGN KEY (department_id) REFERENCES departments(id) ON DELETE CASCADE; delete department → employees also deleted 7. Remove CHECK (