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);
0 views
Back to Blog

Related posts

Read more »

CA 40 – Alter Tables

Make email NOT NULL in customers table sql ALTER TABLE customers ALTER COLUMN email SET NOT NULL; Ensures that future rows must have an email value. Make usern...

CA 40 - Alter Tables

Practice ALTER TABLE Statements 1. Make Email NOT NULL customers sql ALTER TABLE customers MODIFY email VARCHAR100 NOT NULL; Result: email becomes a required f...

ALTER TABLE

Making email compulsory in the customers table Suppose we already have a table called customers, and its email column currently allows NULL values. We now want...

Modifying Tables in SQL using ALTER

Making a Column NOT NULL sql ALTER TABLE customers ALTER COLUMN email SET NOT NULL; Adding a Unique Constraint sql ALTER TABLE users ADD CONSTRAINT unique_user...