MySQL data tables created in three ways

table of Contents

Create table 1. Conventional embodiment
2. create table2 like table1 embodiment
3. According table2 table to create the result set of the query table1


Create table 1. The conventional way

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

Data type (data_type) column specifies what type of data can be received. The following table contains the most commonly used SQL data types:

type of data description
integer(size)、int(size)、smallint(size)
tinyint(size)
Only hold an integer. Specify the maximum number of digits in parentheses.
decimal(size,d)、numeric(size,d) Receiving with decimals.
"Size" specifies the maximum number of digits. "D" specifies the maximum number of digits to the right of the decimal point.
char(size) Receiving a fixed length string (accommodates letters, numbers and special characters).
A predetermined length of the string in the parenthesis.
varchar(size) Receiving a variable length string (accommodates letters, numbers and special characters).
The maximum length of the string specified in parentheses.
date(yyyymmdd) Receiving with decimals.
Accommodate date.
  • Example, create a simple product order information table:
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 a piece of data:
INSERT INTO Orders (ProductName) VALUES ('Mobile phone');
  • result:
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 方式

  • This way the existing reference table1 structure defined, to create a new table2 , will not table1 data to take over.
  • Example:
mysql> CREATE TABLE Orders_new LIKE Orders;
  • View table Orders and Orders_new structure, results were consistent with Table 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. table2 way to create a table based on the result set of a query table1

  • This embodiment can be customized to select table1 fields in creating table2 after, table1 corresponding field data to be collectively transferred table2 in;
  • The demand for screening table1 in the corresponding field and then placed in table2 , the action corresponds to streamline data table.
  • Example:
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)
UFI
Published 16 original articles · won praise 2 · Views 1200

Guess you like

Origin blog.csdn.net/qq_39075021/article/details/103911445