Alter Tables
Hi! 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 customers ALTER COLUMN email SET NOT NULL; here the alter is used to set the column email that should not be blank. 2. In the users
Anjana R.K.
Hi!
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 customers ALTER COLUMN email SET NOT NULL;
here the alter is used to set the column email that should not be blank.
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);
here an new contraint unique_name is used to keep unique user name thus ensure that the username column is unique across all records.
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 (price>0);
here the table prducts is altered with price that has values greater than 0.
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';
the oders table is modified by setting the default value of status with 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 NOT NULL CHECK (salary>10000);
the column salary is added and it should not be null and check condition that 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 ADD CONSTRAINT employees_department_id FOREIGN KEY (department_id) REFERENCES departments(id) ON DELETE CASCADE;
the on delete cascade is used so that when a department is deleted,all related employees are automatically removed .
7. In the accounts table, remove an existing CHECK constraint that enforces balance >= 0
ALTER TABLE accounts DROP CONSTRAINT accounts_balance;
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_transaction UNIQUE (user_id, transaction_id);
creates a Unique Constraint on table payments.It ensures that the exact same user_id and transaction_id combination can never be recorded twice, effectively preventing duplicate payment entries for the same transaction.
Found this useful? Share it!
Read the Full Story
Continue reading on Dev.to
Related Stories
Stop Copying Skills Between Claude Code, Cursor, and Codex
about 6 hours ago
Agentic Architectures โ Article 2: Advanced Coordination and Reasoning Patterns
about 6 hours ago
Agentic Architectures โ Article 1: The Agentic AI Maturity Model
about 6 hours ago
Reimagining Creativity: Inside IdeaForge
about 6 hours ago