Changing the schema · 修改表结构
Changing a table's structure
A table's design is not fixed. ALTER TABLE … ADD adds a new column to an existing table:
Existing rows get NULL in the new column until you fill them in.
修改表的结构
表的设计不是一成不变的。ALTER TABLE … ADD 给已有的表添加一个新列:
ALTER TABLE product ADD weight DECIMAL(5, 2);
在你填入数据之前,已有的行在新列里都是 NULL。
Removing a table
DROP TABLE deletes a whole table — its structure and all its rows:
This cannot be undone, so use it with care. Dropping a table that other tables reference can break referential integrity.
删除一张表
DROP TABLE 删除整张表 —— 包括它的结构和所有行:
DROP TABLE product;
这无法撤销,所以要小心使用。删掉一张被其他表引用的表,可能破坏参照完整性。
Common mistakes
ALTER TABLEadds or changes a column.- Existing rows get NULL in a new column unless you set a default.
常见错误
ALTER TABLE添加或修改列。- 新列在已有行中为 NULL,除非你设了默认值。
Add a new column weight of type DECIMAL(5, 2) to the product table using ALTER TABLE. · 用 ALTER TABLE 给 product 表添加一个新列 weight,类型为 DECIMAL(5, 2)。
Click Run to see the output here. · 点击“运行”查看此处输出。
Delete the entire product table with DROP TABLE. (Afterwards only customer and orders remain.) · 用 DROP TABLE 删除整张 product 表。(之后只剩下 customer 和 orders。)
Click Run to see the output here. · 点击“运行”查看此处输出。