SQL CREATE TABLE 簡單使用

本次環境使用的是MySQL5.7版

1.根據括弧內所輸入的欄位與資料類型建立表(table)
CREATE TABLE table_name(
    column1 datatype,
    column2 datatype,
    column3 datatype,
    ...
);
mysql> CREATE TABLE Persons( 
PersonID int, 
    -> LastName varchar(255), 
    -> FirstName varchar(255), 
    -> Address varchar(255), 
    -> City varchar(255) 
    -> );
Query OK, 0 rows affected (0.17 sec)
2.根據舊表欄位新增新表
CREATE TABLE newTableName As
SELECT column1,column2 ...
FROM  oldTableName
WHERE ...;
mysql> CREATE TABLE Persons_copy As
    -> SELECT *
    -> FROM Persons
    -> Where PersonID=11;
Query OK, 0 rows affected (0.20 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> INSERT INTO Persons_copy VALUES (11,"Lester","Mark","No","No");
Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM Persons_copy;
+----------+----------+-----------+---------+------+
| PersonID | LastName | FirstName | Address | City |
+----------+----------+-----------+---------+------+
|       11 | Lester   | Mark      | No      | No   |
+----------+----------+-----------+---------+------+
1 row in set (0.00 sec)

留言

這個網誌中的熱門文章

linux uname指令介紹

PHP - 透過內建curl擷取內容

Ruby入門 - 下載與安裝