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.
ALTER TABLE customer
ALTER COLUMN email SET NOT NULL;
2. In the users table, ensure that the username column is unique across all records using an ALTER statement.
ALTER TABLE users
ADD CONSTRAINT unique_username UNIQUE (username);
the reason we cant deal with this like previous question is because , unique is a table level property can not be alter as a column property so we add it as a constraint
3. In the products table, enforce that price must always be greater than 0 using an ALTER command.
ALTER TABLE products
ADD CONSTRAINT price_check CHECK (price > 0);
checking values can be can be updated only as a constraint
4. Modify the orders table so that the status column defaults to 'pending' if no value is provided during insertion.
ALTER TABLE orders
ALTER COLUMN status SET DEFAULT 'pending';\
5. Alter the employees table by adding a new column salary such that
It cannot be NULL,It must always be greater than 10,000
ALTER TABLE employees
ADD COLUMN salary DECIMAL NOT NULL CHECK (salary > 10000);
6. Modify the foreign key constraint between employees and departments so that when a department is deleted, all related employees are automatically removed.
ALTER TABLE employees
DROP CONSTRAINT employees_department_id_fkey;
ALTER TABLE employees
ADD FOREIGN KEY (department_id)
REFERENCES departments(id)
ON DELETE CASCADE;
7. In the accounts table, remove an existing CHECK constraint that enforces balance >= 0.
ALTER TABLE accounts
DROP CONSTRAINT accounts_balance_check;
8. In the payments table, ensure that the combination of user_id and transaction_id is unique using an ALTER TABLE statement.
ALTER TABLE payments
ADD CONSTRAINT unique_payment UNIQUE (user_id, transaction_id);
Top comments (0)