Back to articles
CA 40 – Alter Tables

CA 40 – Alter Tables

via Dev.to BeginnersMohammed Azim J

This exercise was about ALTER TABLE. Instead of creating table again, we modify existing table like adding constraints, defaults, columns etc. Make email NOT NULL in customers table sql id="h4r9b2" ALTER TABLE customers ALTER COLUMN email SET NOT NULL; This makes sure future rows must have email. Make username unique in users table sql id="9zv1qx" ALTER TABLE users ADD CONSTRAINT unique_username UNIQUE (username); This prevents duplicate usernames. Add check constraint price > 0 in products table sql id="g6p8la" ALTER TABLE products ADD CONSTRAINT price_check CHECK (price > 0); Now price cannot be zero or negative. Set default status 'pending' in orders table sql id="s8k3po" ALTER TABLE orders ALTER COLUMN status SET DEFAULT 'pending'; If status not given, it will automatically become pending. Add salary column in employees table with conditions sql id="x2w7mn" ALTER TABLE employees ADD COLUMN salary INT NOT NULL; ALTER TABLE employees ADD CONSTRAINT salary_check CHECK (salary > 10000)

Continue reading on Dev.to Beginners

Opens in a new tab

Read Full Article
6 views

Related Articles