SQL ALTER TABLE

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

SQL ALTER TABLE

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement adds a new column called email to the students table?
A ALTER TABLE students ADD email VARCHAR(50);
B UPDATE TABLE students ADD email VARCHAR(50);
2. Which statement removes the house column from the students table?
A DELETE COLUMN house FROM students;
B ALTER TABLE students DROP COLUMN house;
3. True or false: ALTER TABLE ... ADD deletes the existing rows in the table.
A False: the table and its rows stay, and the new column is added
B True: the table is emptied and rebuilt
4. What value do existing rows have in a newly added column (with no default)?
A NULL
B 0
5. Which command would you use to add a FOREIGN KEY to the existing orders table so studentId links to students?
A UPDATE orders SET FOREIGN KEY (studentId) REFERENCES students(studentId);
B ALTER TABLE orders ADD FOREIGN KEY (studentId) REFERENCES students(studentId);

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1ALTER TABLE students ADD email VARCHAR(50); ADROP on its own is not enough here. Removing a column needs DROP COLUMN followed by the column name.
2ALTER TABLE students DROP COLUMN house; BThe TABLE keyword is missing. The command must start with ALTER TABLE before the table name.
3Link an existing orders table to the students table by adding a foreign key on studentId. CALTER TABLE products ADD discount INT;
4Change the structure of the products table by adding a whole number column called discount. DRemoves the house column, and all the values stored in it, from the students table.
5ALTER students ADD email VARCHAR(50); EALTER TABLE orders ADD FOREIGN KEY (studentId) REFERENCES students(studentId);
6ALTER TABLE students DROP house; FAdds a new column called email to the existing students table without deleting the table or its rows.

Definitions

ALTER TABLE

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
_____ TABLE students ADD email VARCHAR(50);
2.
ALTER TABLE students ___ email VARCHAR(50);
3.
ALTER TABLE products ____ COLUMN stock;
4.
ALTER TABLE orders ADD FOREIGN KEY (productId) __________ products(productId);
5.
ALTER TABLE results ADD ___________ (subjectId) REFERENCES subjects(subjectId);

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A ALTER TABLE students
B ADD email;
C SELECT firstName, email
D FROM students;
Question 2
A ALTER products
B ADD discount DECIMAL(4,2);
C SELECT name, discount
D FROM products;
Question 3
A ALTER TABLE students
B ADD nickname VARCHAR(20);
C ALTER TABLE students
D DELETE COLUMN nickname;
Question 4
A UPDATE TABLE students
B ADD FOREIGN KEY (houseId)
C REFERENCES houses(houseId);
Question 5
A ALTER TABLE students
B ADD FOREIGN KEY (houseId)
C REFERENCE houses(houseId);

Answer Key

SQL ALTER TABLE
Section A: Quick Questions
Q1
A — ALTER TABLE students ADD email VARCHAR(50);
Q2
B — ALTER TABLE students DROP COLUMN house;
Q3
A — False: the table and its rows stay, and the new column is added
Q4
A — NULL
Q5
B — ALTER TABLE orders ADD FOREIGN KEY (studentId) REFERENCES students(studentId);
Section B: Match the Pairs
Q1
ALTER TABLE students ADD email VARCHAR(50); → Adds a new column called email to the existing students table without deleting the table or its rows.
Q2
ALTER TABLE students DROP COLUMN house; → Removes the house column, and all the values stored in it, from the students table.
Q3
Link an existing orders table to the students table by adding a foreign key on studentId. → ALTER TABLE orders ADD FOREIGN KEY (studentId) REFERENCES students(studentId);
Q4
Change the structure of the products table by adding a whole number column called discount. → ALTER TABLE products ADD discount INT;
Q5
ALTER students ADD email VARCHAR(50); → The TABLE keyword is missing. The command must start with ALTER TABLE before the table name.
Q6
ALTER TABLE students DROP house; → DROP on its own is not enough here. Removing a column needs DROP COLUMN followed by the column name.
Section C: Definitions
Q1
ALTER TABLE — An SQL command that changes the structure of an existing table, such as adding a column with ADD, removing one with DROP COLUMN, or adding a FOREIGN KEY, without deleting the table.
Section D: Fill in the Blanks
Q1
ALTER TABLE students ADD email VARCHAR(50);
Q2
ALTER TABLE students ADD email VARCHAR(50);
Q3
ALTER TABLE products DROP COLUMN stock;
Q4
ALTER TABLE orders ADD FOREIGN KEY (productId) REFERENCES products(productId);
Q5
ALTER TABLE results ADD FOREIGN KEY (subjectId) REFERENCES subjects(subjectId);
Section E: Spot the Error
Q1
B — ADD needs a data type after the new column name. It should be ADD email VARCHAR(50);
Q2
A — The command is ALTER TABLE, so the TABLE keyword is missing. It should be ALTER TABLE products.
Q3
D — ALTER TABLE removes a column with DROP COLUMN, not DELETE COLUMN. It should be DROP COLUMN nickname;
Q4
A — UPDATE changes rows of data. To change a table's structure you use ALTER TABLE students.
Q5
C — The keyword is REFERENCES, with an S. It should be REFERENCES houses(houseId);