SQL TABLE 關鍵字
CREATE TABLE
CREATE TABLE
命令在資料庫中建立一個新表。
以下 SQL 建立了一個名為 "Persons" 的表,該表包含五個列:PersonID、LastName、FirstName、Address 和 City。
示例
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
使用另一個表建立 CREATE TABLE
也可以使用 CREATE TABLE
命令複製一個現有表。
以下 SQL 建立了一個名為 "TestTables" 的新表(它是 "Customers" 表的副本):
示例
CREATE TABLE TestTable AS
SELECT customername, contactname
FROM customers;
ALTER TABLE
ALTER TABLE
命令用於新增、刪除或修改表中的列。
ALTER TABLE
命令還用於新增和刪除表中的各種約束。
以下 SQL 在“Customers”表中添加了“Email”列
示例
ALTER TABLE Customers
ADD Email varchar(255);
以下 SQL 從“Customers”表中刪除了“Email”列
示例
ALTER TABLE Customers
DROP COLUMN Email;
DROP TABLE
The DROP TABLE
command deletes a table in the database. (DROP TABLE 命令用於刪除資料庫中的表。)
The following SQL deletes the table "Shippers" (以下 SQL 語句刪除 "Shippers" 表)
示例
DROP TABLE Shippers;
Note: Be careful before deleting a table. Deleting a table results in loss of all information stored in the table! (注意:刪除表前請務必小心。刪除表將導致其中儲存的所有資訊丟失!)
TRUNCATE TABLE
TRUNCATE TABLE
命令刪除表中的資料,但不刪除表本身。
以下 SQL 截斷了 "Categories" 表:
示例
TRUNCATE TABLE Categories;