MySQL創建數據表的三種方式

目錄

1. 常規 create table 方式
2. create table2 like table1 方式
3. 根據查詢 table1 的結果集來創建表 table2 方式


1. 常規 create table 方式

CREATE TABLE [if not exists] table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);

數據類型(data_type)規定了列可容納何種數據類型。下面的表格包含了SQL中最常用的數據類型:

數據類型 描述
integer(size)、int(size)、smallint(size)
tinyint(size)
僅容納整數。在括號內規定數字的最大位數。
decimal(size,d)、numeric(size,d) 容納帶有小數的數字。
“size” 規定數字的最大位數。“d” 規定小數點右側的最大位數。
char(size) 容納固定長度的字符串(可容納字母、數字以及特殊字符)。
在括號中規定字符串的長度。
varchar(size) 容納可變長度的字符串(可容納字母、數字以及特殊的字符)。
在括號中規定字符串的最大長度。
date(yyyymmdd) 容納帶有小數的數字。
容納日期。
  • 示例,創建一個簡單的產品訂單信息表:
CREATE TABLE if not exists Orders 
(
OrderId int(10) NOT NULL AUTO_INCREMENT,//自增ID
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT NOW(),//默認值是當前時間,當向表中插入行時,當前日期和時間自動插入列中。
PRIMARY KEY (OrderId)
);
  • 插入一條數據:
INSERT INTO Orders (ProductName) VALUES ('Mobile phone');
  • 結果:
mysql> SELECT * FROM Orders;
+---------+--------------+---------------------+
| OrderId | ProductName  | OrderDate           |
+---------+--------------+---------------------+
|       1 | Mobile phone | 2020-01-09 19:06:00 |
+---------+--------------+---------------------+
1 row in set (0.05 sec)

2. create table2 like table1 方式

  • 此方式參照已有table1的結構定義,來創建新的table2,不會將table1中數據拿過來。
  • 示例:
mysql> CREATE TABLE Orders_new LIKE Orders;
mysql> DESC Orders;
+-------------+-------------+------+-----+-------------------+----------------+
| Field       | Type        | Null | Key | Default           | Extra          |
+-------------+-------------+------+-----+-------------------+----------------+
| OrderId     | int(10)     | NO   | PRI | NULL              | auto_increment |
| ProductName | varchar(50) | NO   |     | NULL              |                |
| OrderDate   | datetime    | NO   |     | CURRENT_TIMESTAMP |                |
+-------------+-------------+------+-----+-------------------+----------------+
3 rows in set (0.09 sec)

mysql> DESC Orders_new;
+-------------+-------------+------+-----+-------------------+----------------+
| Field       | Type        | Null | Key | Default           | Extra          |
+-------------+-------------+------+-----+-------------------+----------------+
| OrderId     | int(10)     | NO   | PRI | NULL              | auto_increment |
| ProductName | varchar(50) | NO   |     | NULL              |                |
| OrderDate   | datetime    | NO   |     | CURRENT_TIMESTAMP |                |
+-------------+-------------+------+-----+-------------------+----------------+
3 rows in set (0.05 sec)

3. 根據查詢 table1 的結果集來創建表 table2 方式

  • 此方式可以自定義選擇table1中的字段,創建table2後, table1中對應字段數據也一併轉移至table2中;
  • 根據需求,可用於篩選table1中對應字段然後放入table2中,將當於精簡數據表的作用。
  • 示例:
mysql> CREATE TABLE Orders_new1 AS SELECT OrderId, OrderDate FROM Orders;
Query OK, 1 row affected (0.37 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM Orders;
+---------+--------------+---------------------+
| OrderId | ProductName  | OrderDate           |
+---------+--------------+---------------------+
|       1 | Mobile phone | 2020-01-09 19:06:00 |
+---------+--------------+---------------------+
1 row in set (0.05 sec)

mysql> SELECT * FROM Orders_new1;
+---------+---------------------+
| OrderId | OrderDate           |
+---------+---------------------+
|       1 | 2020-01-09 19:06:00 |
+---------+---------------------+
1 row in set (0.06 sec)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章