日期数据类型timestamp

文章目录

  • 1.1 timestamp说明
  • 1.2 实践环境的说明
  • 1.3 timestamp理解

1.1 timestamp说明

类型 timestamp
显示格式 年-月-日 时:分:秒
有效范围 UTC时间下: 1970-01-01 00:00:01 至 2037-12-31 23:59:59
占用空间 4字节(bytes)
插入方式 insert into test1(mydate) values(now());
insert into test1(mydate) values(“2019-04-10 19:40:32”);
insert into test1(mydate) values(20190410194030);

1.2 实践环境的说明

## 当前数据库的版本和存储引擎
mysql> select @@version,@@default_storage_engine;
+------------+--------------------------+
| @@version  | @@default_storage_engine |
+------------+--------------------------+
| 5.7.28-log | InnoDB                   |
+------------+--------------------------+
1 row in set (0.00 sec)

## 当前事务的提交方式
mysql> select @@global.autocommit,@@autocommit;
+---------------------+--------------+
| @@global.autocommit | @@autocommit |
+---------------------+--------------+
|                   1 |            1 |
+---------------------+--------------+
1 row in set (0.00 sec)

## 创建chenliang库,字符集为utf8
mysql> create database if not exists chenliang character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

mysql> show databases like "chenliang";
+----------------------+
| Database (chenliang) |
+----------------------+
| chenliang            |
+----------------------+
1 row in set (0.00 sec)

## 进入到chenliang库下面,并查看是否成功进入到chenliang库下面
mysql> use chenliang;
Database changed

mysql> select database();
+------------+
| database() |
+------------+
| chenliang  |
+------------+
1 row in set (0.00 sec)

1.3 timestamp理解

## 创建test1表,mydate字段的数据类型为timestamp
mysql> create table if not exists test1(
    ->  mydate timestamp
    -> )engine=innodb character set utf8 collate utf8_general_ci;
Query OK, 0 rows affected (0.01 sec)

## 查看test1表的表结构
mysql> desc test1;
+--------+-----------+------+-----+---------+-------+
| Field  | Type      | Null | Key | Default | Extra |
+--------+-----------+------+-----+---------+-------+
| mydate | timestamp | YES  |     | NULL    |       |
+--------+-----------+------+-----+---------+-------+
1 row in set (0.00 sec)

## 往test1表中插入数据
## 第一种方式(这种是以mysql所在的服务器上的时间为准)
mysql> insert into test1(mydate) values(now());
Query OK, 1 row affected (0.00 sec)

mysql> select * from test1;
+---------------------+
| mydate              |
+---------------------+
| 2020-02-27 15:02:00 |
+---------------------+
1 row in set (0.00 sec)


## 第二种方法
mysql> insert into test1(mydate) values(20200227160200);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test1;
+---------------------+
| mydate              |
+---------------------+
| 2020-02-27 15:02:00 |
| 2020-02-27 16:02:00 |
+---------------------+
2 rows in set (0.00 sec)

## 第三种方法
mysql> insert into test1(mydate) values("2020-02-27 17:02:00");
Query OK, 1 row affected (0.00 sec)

mysql> select * from test1;
+---------------------+
| mydate              |
+---------------------+
| 2020-02-27 15:02:00 |
| 2020-02-27 16:02:00 |
| 2020-02-27 17:02:00 |
+---------------------+
3 rows in set (0.00 sec)

## timestamp可以以整数的方式显示其内容
mysql> select mydate,mydate+0 from test1;
+---------------------+----------------+
| mydate              | mydate+0       |
+---------------------+----------------+
| 2020-02-27 15:02:00 | 20200227150200 |
| 2020-02-27 16:02:00 | 20200227160200 |
| 2020-02-27 17:02:00 | 20200227170200 |
+---------------------+----------------+
3 rows in set (0.00 sec)

你可能感兴趣的:(日期数据类型timestamp)