我一时手滑,建错了表,或者想给现在的表加一列“法宝等级”,该怎么办?难道要毁掉重练?
若是彻底不要了,便用 DROP 毁尸灭迹;若是只想修补,便用 ALTER 易筋洗髓。
ALTER TABLE 语句用于在已有的表中添加、删除或修改列。
给 Disciples 表增加一个 Level 列:
ALTER TABLE Disciples
ADD Level int;
如果我想把 Level 列从整数改成小数(更精确),不同门派有不同的法门:
-- SQL Server (焚香谷)
ALTER TABLE Disciples
ALTER COLUMN Level decimal(10,2);
-- MySQL (青云门) / Oracle (南疆)
ALTER TABLE Disciples
MODIFY Level decimal(10,2);
-- 注:Oracle 10g+ 也可以用 MODIFY column_name data_type
-- PostgreSQL (天音寺)
ALTER TABLE Disciples
ALTER COLUMN Level TYPE decimal(10,2);
把 Age 列删掉(修仙之人不论岁数):
ALTER TABLE Disciples
DROP COLUMN Age;
DROP 是极其危险的法术,能将对象彻底从数据库中抹去。
DROP TABLE Disciples; —— 删除表(表结构和数据全没了)。DROP DATABASE ZhuXianWorld; —— 删除整个数据库(世界毁灭)。TRUNCATE TABLE Disciples; —— 清空表内数据,但保留表结构(只杀人,不拆房)。