Fantastic! Now, let's analyze two SQL statements that may look similar at first sight. Suppose the flight
table was coded as follows:
CREATE TABLE flight (
id integer PRIMARY KEY,
booking_ref varchar(8),
airplane varchar(16),
operated_by varchar(32)
);
We want to remove the operated_by
column. As you may recall, we've learned the following syntax:
ALTER TABLE flight DROP COLUMN operated_by;
However, could we also delete the entire table and create it again without the given column?
DROP TABLE flight;
CREATE TABLE flight (
id integer PRIMARY KEY,
booking_ref varchar(8),
airplane varchar(16)
);
You see, both methods result in a table named flight
which contains three columns: id
, booking_ref
, and airplane
. However, ALTER TABLE
keeps the data in the other, unchanged columns, while DROP TABLE
+ CREATE TABLE
removes all the data from all the columns. Keep this information in mind whenever you need to change the structure of a table.