Company
Date Published
Author
Charlie Custer
Word count
1710
Language
English
Hacker News points
None

Summary

The article provides a comprehensive guide on how to safely add and drop columns from a SQL database using the `ALTER TABLE` command, specifically focusing on CockroachDB syntax, while noting its applicability to other SQL databases like MySQL and SQLite. It begins with a simple example of a `users` table, demonstrating the process of adding a column with constraints and the necessary steps to ensure data integrity, such as using `UPDATE` statements to populate new columns before applying constraints. The article also details how to add `UNIQUE` and `NOT NULL` constraints to an existing column and verifies these constraints using the `SHOW CONSTRAINTS` command. In addressing column removal, it emphasizes the potential risks of data loss and the need for caution, highlighting security features like `sql_safe_updates` and options like `RESTRICT` and `CASCADE` to manage dependencies. Additionally, it explains how to drop constraints, particularly `UNIQUE` and `NOT NULL`, with a focus on different SQL database syntax requirements. Finally, it suggests further learning by exploring foreign key relationships to enhance database relational structure.