alter table 語句用于創建后對表的修改, 基礎用法如下:
## 添加列
基本形式:
~~~
alter table 表名 add 列名 列數據類型 [after 插入位置];
~~~
**示例:**
在表的最后追加列 address:
~~~
alter table students add address char(60);
~~~
在名為 age 的列后插入列 birthday:
~~~
alter table students add birthday date after age;
~~~
## 修改列
基本形式:
~~~
alter table 表名 change 列名稱 列新名稱 新數據類型;
~~~
**示例:**
將表 tel 列改名為 telphone:
~~~
alter table students change tel telphone char(13) default "-";
~~~
將 name 列的數據類型改為 char(16):
~~~
alter table students change name name char(16) not null;
~~~
## 刪除列
基本形式: alter table 表名 drop 列名稱;
**示例:**
刪除 birthday 列:
~~~
alter table students drop birthday;
~~~
## 重命名表
基本形式:
~~~
alter table 表名 rename 新表名;
~~~
**示例:**
重命名 students 表為 workmates:
~~~
alter table students rename workmates;
~~~
## 刪除整張表
基本形式:
~~~
drop table 表名;
~~~
**示例:** 刪除 workmates 表:
~~~
drop table workmates;
~~~
## 刪除整個數據庫
基本形式:
~~~
drop database 數據庫名;
~~~
**示例:** 刪除 samp_db 數據庫:
~~~
drop database samp_db;
~~~