Back to articles
Understanding ALTER TABLE with Constraints in SQL

Understanding ALTER TABLE with Constraints in SQL

via Dev.to BeginnersAbinaya Dhanraj

_ In this task, I worked on modifying existing tables using ALTER TABLE. This helped me understand how to update table structure after it has already been created. I learned how to add constraints, change column properties, and manage relationships between tables. _ Modifying customers table Query: ALTER TABLE customers ALTER COLUMN email SET NOT NULL; Explanation: In this query, I modified the email column so that it cannot store NULL values. This ensures that every customer must have an email in future entries. Adding UNIQUE constraint in users table Query: ALTER TABLE users ADD CONSTRAINT unique_username UNIQUE (username); Explanation: Here, I added a UNIQUE constraint to the username column. This ensures that no two users can have the same username, which helps avoid duplicate records. Adding CHECK constraint in products table Query: ALTER TABLE products ADD CONSTRAINT check_price_positive CHECK (price > 0); Explanation: In this query, I added a CHECK condition to make sure that th

Continue reading on Dev.to Beginners

Opens in a new tab

Read Full Article
7 views

Related Articles