6. DDL Deep Dive — ALTER, DROP & SSMS Table Designer
DDL Deep Dive — ALTER, DROP & SSMS Table Designer
In the last module, you learned how to create databases and tables from scratch using CREATE. But databases are never static. In the real world, requirements change after a system is already running — a client asks for a new column, a mistake needs to be corrected, a redundant table needs to be removed. This module teaches you how to modify and manage existing database structures using ALTER and DROP, and introduces you to the powerful visual tools built directly into SSMS that let you do much of this work without typing a single line of SQL.
Getting Set Up in SSMS
Open SSMS and connect to your server. In the Object Explorer on the left, expand Databases and then expand VoidX_Academy. Make sure you can see the Tables folder with dbo.Department and dbo.Student inside it. These are the tables you created in Module 5.
Click New Query at the top of SSMS to open a fresh editor window, then tell SQL Server to use your database:
<div class='code-block' data-lang='text'> USE VoidX_Academy;
Press F5. The database name in the top-left dropdown of the SSMS toolbar should now show VoidX_Academy. You are ready.
ALTER TABLE — Adding a New Column
The most common modification to an existing table is adding a column that was not in the original design. This happens constantly in real projects — a requirement was missed, the business changed, or new information needs to be tracked.
The command is ALTER TABLE followed by the table name, then ADD followed by the new column definition. The column definition is written exactly the same way as when you define columns inside CREATE TABLE.
Add a phone number column to the Student table:
<div class='code-block' data-lang='text'> ALTER TABLE Student ADD PhoneNumber VARCHAR(20);
Press F5. Now go to Object Explorer, expand VoidX_Academy, expand Tables, expand dbo.Student, and expand Columns. Right-click the Columns folder and click Refresh. You will see PhoneNumber now listed at the bottom of the column list.
You can add a column with a constraint at the same time. Add a column for the student's nationality with a default value:
<div class='code-block' data-lang='text'> ALTER TABLE Student ADD Nationality VARCHAR(50) DEFAULT 'Ghanaian';
Press F5. Every existing row in the Student table now has the value Ghanaian in the Nationality column automatically — because of the DEFAULT constraint. Any new row inserted without specifying nationality will also receive this value automatically.
ALTER TABLE — Modifying an Existing Column
Sometimes a column already exists but its data type or size needs to change. For example, you created PhoneNumber as VARCHAR(20) but realise international phone numbers with country codes can be longer. Use ALTER COLUMN to change it.
<div class='code-block' data-lang='text'> ALTER TABLE Student ALTER COLUMN PhoneNumber VARCHAR(30);
Press F5. The column now accepts up to 30 characters instead of 20. The existing data in that column is not affected — only the rule about what it can hold going forward changes.
Important: You cannot shrink a column's size if existing data would be cut off. For example, changing VARCHAR(100) to VARCHAR(5) when some rows already contain 20-character values will fail. SQL Server protects your existing data.
ALTER TABLE — Dropping a Column
Dropping a column permanently removes it and all its data from every row in the table. This cannot be undone. Use it carefully.
Remove the PhoneNumber column:
<div class='code-block' data-lang='text'> ALTER TABLE Student DROP COLUMN PhoneNumber;
Press F5. Refresh the Columns folder in Object Explorer — PhoneNumber is gone. Every row in the table no longer has that column, and all the data that was in it is permanently deleted.
ALTER TABLE — Adding a Constraint to an Existing Table
You can add constraints to a table that already exists — even if it already has data in it. This is useful when you realise a rule needs to be enforced that was not set up during CREATE TABLE.
Add a CHECK constraint to enforce that GPA must be between 0 and 4 — but only if the column does not already have one:
<div class='code-block' data-lang='text'> ALTER TABLE Student ADD CONSTRAINT CHK_StudentGPA CHECK (GPA >= 0.0 AND GPA <= 4.0);
Press F5. The constraint is now active. Any future INSERT or UPDATE that violates it will be rejected.
Add a UNIQUE constraint to ensure no two students share the same email:
<div class='code-block' data-lang='text'> ALTER TABLE Student ADD CONSTRAINT UQ_StudentEmail UNIQUE (Email);
Notice the naming convention: CHK_ for check constraints and UQ_ for unique constraints. Naming your constraints clearly is important because it makes error messages meaningful — when a constraint is violated, SQL Server displays the constraint name in the error. A name like CHK_StudentGPA immediately tells you what rule was broken.
ALTER TABLE — Dropping a Constraint
To remove a constraint from a table, you use ALTER TABLE with DROP CONSTRAINT followed by the constraint name. This is why naming constraints matters — you cannot drop a constraint you cannot name.
Drop the check constraint you just added:
<div class='code-block' data-lang='text'> ALTER TABLE Student DROP CONSTRAINT CHK_StudentGPA;
Press F5. The rule is removed. You can now insert any GPA value without it being checked. To see all the constraints currently on a table, expand the table in Object Explorer and look inside the Constraints folder.
DROP TABLE — Removing a Table Permanently
DROP TABLE permanently deletes an entire table — its structure, all its columns, all its data, and all its constraints. There is no undo. The table simply ceases to exist.
First create a temporary table to practise with:
<div class='code-block' data-lang='text'> CREATE TABLE TempTest ( TestID INT PRIMARY KEY, TestNote VARCHAR(100) );
Press F5 and verify it appears in Object Explorer under Tables. Now drop it:
<div class='code-block' data-lang='text'> DROP TABLE TempTest;