Back to articles
Alter Queries

Alter Queries

via Dev.toChristina Sharon S

In this assignment, I worked on modifying existing tables using ALTER TABLE . This helped me understand how to update constraints without recreating tables. 1.Make email NOT NULL in customers ALTER TABLE customers ALTER COLUMN email SET NOT NULL ; 2.Make username UNIQUE in users ALTER TABLE users ADD CONSTRAINT unique_username UNIQUE ( username ); 3.Add CHECK constraint on price > 0 in products ALTER TABLE products ADD CONSTRAINT price_check CHECK ( price > 0 ); 4.Set default 'pending' for status in orders ALTER TABLE orders ALTER COLUMN status SET DEFAULT 'pending' ; 5.Add salary column with constraints in employees ALTER TABLE employees ADD COLUMN salary INT NOT NULL CHECK ( salary > 10000 ); 6.Modify foreign key with CASCADE ALTER TABLE employees DROP CONSTRAINT employees_department_id_fkey ; ALTER TABLE employees ADD CONSTRAINT employees_department_id_fkey FOREIGN KEY ( department_id ) REFERENCES departments ( id ) ON DELETE CASCADE ; 7.Remove CHECK constraint on balance in account

Continue reading on Dev.to

Opens in a new tab

Read Full Article
3 views

Related Articles