📖 Scenario: You are working as a database assistant for a small bookstore. The bookstore wants to keep their database tidy and up-to-date by removing old tables and modifying existing tables to add new information.
🎯 Goal: Build SQL commands to remove an unnecessary table and modify an existing table by adding a new column.
📋 What You'll Learn
Create a table named OldBooks with specific columns
Create a variable to hold the name of the table to drop
Write a SQL command to drop the OldBooks table using the variable
Write a SQL command to add a new column Publisher to the Books table
💡 Why This Matters
🌍 Real World
Database administrators often need to remove old tables and update existing tables to keep data organized and relevant.
💼 Career
Knowing how to safely drop tables and alter table structures is essential for database maintenance and evolution in many IT and data roles.
Progress0 / 4 steps
1
Create the OldBooks table
Write a SQL statement to create a table called OldBooks with columns BookID as integer primary key, Title as text, and YearPublished as integer.
SQL
Hint
Use CREATE TABLE OldBooks followed by column definitions inside parentheses.
2
Set the table name to drop
Create a SQL variable called @tableToDrop and set it to the string 'OldBooks'.
SQL
Hint
Use DECLARE @tableToDrop VARCHAR(50) = 'OldBooks'; to create the variable.
3
Drop the OldBooks table using the variable
Write a SQL statement to drop the table named in the variable @tableToDrop. Use dynamic SQL with EXEC or equivalent to drop the table.
SQL
Hint
Use EXEC('DROP TABLE ' + @tableToDrop); to drop the table dynamically.
4
Add a new column Publisher to the Books table
Write a SQL statement to add a new column called Publisher of type TEXT to the existing table Books using ALTER TABLE.
SQL
Hint
Use ALTER TABLE Books ADD Publisher TEXT; to add the new column.
Practice
(1/5)
1. What does the SQL command DROP TABLE do?
easy
A. Adds a new column to the table.
B. Changes the data type of a column.
C. Deletes the entire table and all its data permanently.
D. Renames the table without deleting data.
Solution
Step 1: Understand the purpose of DROP TABLE
The DROP TABLE command removes the whole table and all its data from the database permanently.
Step 2: Compare with other commands
ALTER TABLE changes structure but keeps data; DROP TABLE deletes everything.
Final Answer:
Deletes the entire table and all its data permanently. -> Option C
Quick Check:
DROP TABLE = Delete table and data [OK]
Hint: DROP TABLE removes table and data completely [OK]
Common Mistakes:
Thinking DROP TABLE only deletes data but keeps structure
Confusing DROP TABLE with ALTER TABLE
Assuming DROP TABLE can be undone easily
2. Which of the following is the correct syntax to add a new column named age of type INTEGER to a table called users?
easy
A. DROP TABLE users ADD COLUMN age INTEGER;
B. ALTER TABLE users ADD COLUMN age INTEGER;
C. ALTER TABLE users DROP COLUMN age INTEGER;
D. ALTER users ADD age INTEGER;
Solution
Step 1: Identify correct ALTER TABLE syntax
To add a column, use: ALTER TABLE table_name ADD COLUMN column_name data_type;
Step 2: Check each option
ALTER TABLE users ADD COLUMN age INTEGER; matches correct syntax; others misuse DROP or omit TABLE keyword.
Final Answer:
ALTER TABLE users ADD COLUMN age INTEGER; -> Option B
Quick Check:
ALTER TABLE ADD COLUMN = ALTER TABLE users ADD COLUMN age INTEGER; [OK]
Hint: Use ALTER TABLE ... ADD COLUMN to add columns [OK]
Common Mistakes:
Using DROP TABLE instead of ALTER TABLE
Omitting TABLE keyword after ALTER
Using ADD without COLUMN keyword
3. Given the table employees with columns id, name, and salary, what will be the result of this SQL command?
ALTER TABLE employees DROP COLUMN salary;
medium
A. The table employees will be deleted.
B. All data in the employees table will be deleted.
C. A new column named salary will be added.
D. The salary column will be removed from the employees table.
Solution
Step 1: Understand ALTER TABLE DROP COLUMN
This command removes the specified column from the table structure but keeps the table and other data intact.
Step 2: Analyze options
Only The salary column will be removed from the employees table. correctly describes removing the salary column; others describe deleting data or table, or adding columns.
Final Answer:
The salary column will be removed from the employees table. -> Option D
Quick Check:
ALTER TABLE DROP COLUMN removes column [OK]
Hint: ALTER TABLE DROP COLUMN removes only that column [OK]
Common Mistakes:
Thinking DROP COLUMN deletes all data
Confusing DROP COLUMN with DROP TABLE
Assuming DROP COLUMN adds a column
4. What is wrong with this SQL command?
ALTER TABLE customers DROP age;
medium
A. The command is missing the keyword COLUMN after DROP.
B. DROP cannot be used with ALTER TABLE.
C. The table name is incorrect.
D. The command should be DROP TABLE customers;
Solution
Step 1: Check ALTER TABLE DROP syntax
To remove a column, the syntax requires DROP COLUMN column_name, not just DROP column_name.
Step 2: Validate other options
DROP is valid with ALTER TABLE if used correctly; table name is correct; DROP TABLE deletes whole table, not column.
Final Answer:
The command is missing the keyword COLUMN after DROP. -> Option A
Quick Check:
ALTER TABLE DROP COLUMN required [OK]
Hint: Use DROP COLUMN, not just DROP, to remove columns [OK]
Common Mistakes:
Omitting COLUMN keyword after DROP
Confusing DROP TABLE with DROP COLUMN
Assuming DROP alone removes columns
5. You want to rename the column fullname to name in the table students. Which SQL command correctly does this in a standard way?
hard
A. ALTER TABLE students RENAME COLUMN fullname TO name;
B. ALTER TABLE students CHANGE fullname name VARCHAR(100);
C. DROP TABLE students RENAME COLUMN fullname TO name;
D. ALTER TABLE students DROP COLUMN fullname ADD COLUMN name VARCHAR(100);
Solution
Step 1: Identify standard rename column syntax
The standard SQL command to rename a column is ALTER TABLE table_name RENAME COLUMN old_name TO new_name;
Step 2: Evaluate options
ALTER TABLE students RENAME COLUMN fullname TO name; matches standard syntax; A wrongly uses DROP TABLE; B is MySQL-specific CHANGE syntax; D drops and adds column losing data.
Final Answer:
ALTER TABLE students RENAME COLUMN fullname TO name; -> Option A
Quick Check:
Rename column = ALTER TABLE RENAME COLUMN [OK]
Hint: Use ALTER TABLE RENAME COLUMN to rename columns safely [OK]
Common Mistakes:
Using DROP TABLE instead of ALTER TABLE
Dropping and adding columns loses data
Using non-standard syntax without checking compatibility
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.