mysql中DATETIME、DATE和TIMESTAMP的区别_MySQL
bitsCN.com
mysql中DATETIME、DATE和TIMESTAMP的区别
DATETIME、DATE和 TIMESTAMP类型是相似的。本篇描述了它们的特性以及它们的相似点与不同点。
DATETIME类型可用于需要同时包含日期和时间信息的值。MySQL 以 'YYYY-MM-DD HH:MM:SS'格式检索与显示 DATETIME 类型。支持的范围是'1000-01-0100:00:00' 到 '9999-12-3123:59:59'。(“支持”的含义是,尽管更早的值可能工作,但不能保证他们均可以。)
DATE类型可用于需要一个日期值而不需要时间部分时。MySQL 以 'YYYY-MM-DD' 格式检索与显示DATE值。支持的范围是 '1000-01-01' 到'9999-12-31'。
TIMESTAMP列类型提供了一种类型,通过它你可以以当前操作的日期和时间自动地标记 Insert 或Update 操作。如果一张表中有多个TIMESTAMP列,只有第一个被自动更新。
“完整”TIMESTAMP格式是14位,但TIMESTAMP列也可以用更短的显示尺寸创造
最常见的显示尺寸是6、8、12、和14。
你可以在创建表时指定一个任意的显示尺寸,但是定义列长为0或比14大均会被强制定义为列长14。
列长在从1~13范围的奇数值尺寸均被强制为下一个更大的偶数。
列如:
定义字段长度 强制字段长度
TIMESTAMP(0) -> TIMESTAMP(14)
TIMESTAMP(15)-> TIMESTAMP(14)
TIMESTAMP(1) -> TIMESTAMP(2)
TIMESTAMP(5) -> TIMESTAMP(6)
所有的TIMESTAMP列都有同样的存储大小,
使用被指定的时期时间值的完整精度(14位)存储合法的值不考虑显示尺寸。
不合法的日期,将会被强制为0存储
自动更新第一个 TIMESTAMP列在下列任何条件下发生:
列值没有明确地在一个 Insert 或LOAD DATAINFILE 语句中被指定。
列值没有明确地在一个 Update语句中被指定,并且其它的一些列值已发生改变。(注意,当一个 Update 设置一个列值为它原有值时,这将不会引起TIMESTAMP列的更新,因为,如果你设置一个列值为它当前值时,MySQL 为了效率为忽略更新。)
明确地以 NULL 设置 TIMESTAMP 列。
第一个列以外其它 TIMESTAMP列,可以设置到当前的日期和时间,只要将该列赋值 NULL 或 NOW()。
任何 TIMESTAMP列均可以被设置一个不同于当前操作日期与时间的值,这通过为该列明确指定一个你所期望的值来实现。这也适用于第一个TIMESTAMP列。这个选择性是很有用的,举例来说,当你希望 TIMESTAMP列保存该记录行被新添加时的当前的日期和时间,但该值不再发生改变,无论以后是否对该记录行进行过更新:
当该记录行被建立时,让 MySQL设置该列值。这将初始化该列为当前日期和时间。
以后当你对该记录行的其它列执行更新时,为TIMESTAMP列值明确地指定为它原来的值。
另一方面,你可能发现更容易的方法,使用DATETIME列,当新建记录行时以 NOW()初始化该列,以后在对该记录行进行更新时不再处理它。
示例(译者注):
mysql> Create TABLE `tA` (-> `id` int(3) unsigned NOT NULL auto_increment,-> `date1` timestamp(14) NOT NULL,-> `date2` timestamp(14) NOT NULL,-> PRIMARY KEY (`id`)-> ) TYPE=MyISAM;Query OK, 0 rows affected (0.01 sec)mysql> Insert INTO `tA` SET `id` = 1;Query OK, 1 row affected (0.02 sec)# 没有明确地指定第一个 timestamp 列值,该列值被设为插入的当前时刻# 没有明确地指定其它的 timestamp 列值,MySQL 则认为插入的是一个非法值,而该列值被设为0mysql> Insert INTO `tA` S (2, NOW(), NULL);Query OK, 1 row affected (0.01 sec)mysql> Select * FROM `tA`;+----+----------------+----------------+| id | date1 | date2 |+----+----------------+----------------+| 1 | 20030503104118 | 00000000000000 || 2 | 20030503104254 | 20030503104254 |+----+----------------+----------------+2 rows in set (0.00 sec)mysql> Update `tA` SET `id` = 3 Where `id` = 1;Query OK, 1 row affected (0.01 sec)Rows matched: 1 Changed: 1 Warnings: 0# 对某一记录行进行了更新,第一个 timestamp 列值也将被更新mysql> Update `tA` SET `id` = 2 Where `id` = 2;Query OK, 0 rows affected (0.00 sec)Rows matched: 1 Changed: 0 Warnings: 0# MySQL 忽略了这次操作,第一个 timestamp 列值不会被更新mysql> Select * FROM `tA`;+----+----------------+----------------+| id | date1 | date2 |+----+----------------+----------------+| 3 | 20030503104538 | 00000000000000 || 2 | 20030503104254 | 20030503104254 |+----+----------------+----------------+2 rows in set (0.00 sec)mysql> Update `tA` SET `id` = 1,`date1`=`date1` Where `id` = 3;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0# 明确地指定了第一个 timestamp 列值为它原有值,该值将不会被更新mysql> Select * FROM `tA`;+----+----------------+----------------+| id | date1 | date2 |+----+----------------+----------------+| 1 | 20030503104538 | 00000000000000 || 2 | 20030503104254 | 20030503104254 |+----+----------------+----------------+2 rows in set (0.00 sec)* 以上结果在 MySQL 4.0.12 中测试
bitsCN.com

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).

Detailed explanation of database ACID attributes ACID attributes are a set of rules to ensure the reliability and consistency of database transactions. They define how database systems handle transactions, and ensure data integrity and accuracy even in case of system crashes, power interruptions, or multiple users concurrent access. ACID Attribute Overview Atomicity: A transaction is regarded as an indivisible unit. Any part fails, the entire transaction is rolled back, and the database does not retain any changes. For example, if a bank transfer is deducted from one account but not increased to another, the entire operation is revoked. begintransaction; updateaccountssetbalance=balance-100wh
