Back to articles
Alter Tables

Alter Tables

via Dev.to PythonAshiq Omar

ALTER TABLE constraints QN:1 You have a table customers with a column email that currently allows NULL values. Modify the table so that future entries must always have an email. Query : ALTER TABLE customers ALTER COLUMN email SET NOT NULL ; email should not be empty for future records we use SET NOT NULL QN:2 In the users table, ensure that the username column is unique across all records using an ALTER statement. Query : ALTER TABLE users ADD CONSTRAINT unique_username UNIQUE ( username ); username should not repeat we add UNIQUE constraint QN:3 In the products table, enforce that price must always be greater than 0 using an ALTER command. Query : ALTER TABLE products ADD CONSTRAINT check_price CHECK ( price > 0 ); price should always be greater than 0 we use CHECK constraint QN:4 Modify the orders table so that the status column defaults to 'pending' if no value is provided during insertion. Query : ALTER TABLE orders ALTER COLUMN status SET DEFAULT 'pending' ; if no value is given

Continue reading on Dev.to Python

Opens in a new tab

Read Full Article
2 views

Related Articles