赞
踩
一、定义和语法:
1、定义:NOW() 返回当前的日期和时间。
2、语法:NOW()
二、实例:
1、
- mysql> SELECT NOW(), CURDATE(), CURTIME();
- +---------------------+------------+-----------+
- | NOW() | CURDATE() | CURTIME() |
- +---------------------+------------+-----------+
- | 2020-01-03 12:39:31 | 2020-01-03 | 12:39:31 |
- +---------------------+------------+-----------+
- 1 row in set (0.00 sec)
2、下面的 SQL 创建带有日期时间列(OrderDate)的 "Orders" 表:
- CREATE TABLE Orders
- (
- OrderId int NOT NULL AUTO_INCREMENT,
- ProductName varchar(50) NOT NULL,
- OrderDate datetime NOT NULL DEFAULT NOW(),
- PRIMARY KEY (OrderId)
- )
请注意,OrderDate 列规定 NOW() 作为默认值。作为结果,当向表中插入行时,当前日期和时间自动插入列中。
现在,我们想要在 "Orders" 表中插入一条记录:
- mysql> CREATE TABLE Orders
- -> (
- -> OrderId int NOT NULL AUTO_INCREMENT,
- -> ProductName varchar(50) NOT NULL,
- -> OrderDate datetime NOT NULL DEFAULT NOW(),
- -> PRIMARY KEY (OrderId)
- -> );
- Query OK, 0 rows affected (0.03 sec)
-
- mysql> INSERT INTO Orders (ProductName) VALUES ("Jarlsberg Cheese");
- Query OK, 1 row affected (0.00 sec)
-
- mysql> SELECT * FROM Orders;
- +---------+------------------+---------------------+
- | OrderId | ProductName | OrderDate |
- +---------+------------------+---------------------+
- | 1 | Jarlsberg Cheese | 2020-01-03 12:57:11 |
- +---------+------------------+---------------------+
- 1 row in set (0.00 sec)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。