MySQL 创建自增字段

1. 建表

1.1 表结构

  • 建表
    • 自增字段:auto_increment
    • 日期:date 类型
    • 时间:timestamp,默认已当前时间,如果不指定

1.2 建表

mysql> create table capacity(id bigint primary key auto_increment,capacity bigint,date date,time timestamp);
Query OK, 0 rows affected (0.02 sec)

mysql> desc capacity;
+----------+------------+------+-----+-------------------+-----------------------------+
| Field    | Type       | Null | Key | Default           | Extra                       |
+----------+------------+------+-----+-------------------+-----------------------------+
| id       | bigint(20) | NO   | PRI | NULL              | auto_increment              |
| capacity | bigint(20) | YES  |     | NULL              |                             |
| date     | date       | YES  |     | NULL              |                             |
| time     | timestamp  | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+----------+------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13

2. 插入数据测试

mysql> insert into capacity(capacity, date) values(180, '2019-10-26'),
    -> (190, '2019-10-27'),
    -> (200, '2019-10-28'),
    -> (210, '2019-10-29'),
    -> (224, '2019-10-30');
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from capacity;
+----+----------+------------+---------------------+
| id | capacity | date       | time                |
+----+----------+------------+---------------------+
|  1 |      180 | 2019-10-26 | 2019-10-30 09:34:48 |
|  2 |      190 | 2019-10-27 | 2019-10-30 09:34:48 |
|  3 |      200 | 2019-10-28 | 2019-10-30 09:34:48 |
|  4 |      210 | 2019-10-29 | 2019-10-30 09:34:48 |
|  5 |      224 | 2019-10-30 | 2019-10-30 09:34:48 |
+----+----------+------------+---------------------+
5 rows in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

reference