Home Database Mysql Tutorial MYSQL中如何设置列默认值的介绍_MySQL

MYSQL中如何设置列默认值的介绍_MySQL

Jun 01, 2016 pm 01:31 PM
how

bitsCN.com

MYSQL中如何设置列默认值的介绍

 

MySQL目前不支持列的Default为函数的形式,如达到你某列的默认值为当前更新日期与时间的功能,你可以使用TIMESTAMP列类型,下面就详细说明TIMESTAMP列类型。

 

  TIMESTAMP列类型

 

  TIMESTAMP值可以从1970的某时的开始一直到2037年,精度为一秒,其值作为数字显示。

 

  TIMESTAMP值显示尺寸的格式如下表所示:

  +---------------+----------------+  | 列类型    | 显示格式    |  | TIMESTAMP(14) | YYYYMMDDHHMMSS |   | TIMESTAMP(12) | YYMMDDHHMMSS  |  | TIMESTAMP(10) | YYMMDDHHMM   |  | TIMESTAMP(8) | YYYYMMDD    |  | TIMESTAMP(6) | YYMMDD     |  | TIMESTAMP(4) | YYMM      |  | TIMESTAMP(2) | YY       |  +---------------+----------------+
Copy after login

  “完整”TIMESTAMP格式是14位,但TIMESTAMP列也可以用更短的显示尺寸创造,最常见的显示尺寸是6、8、12、和14。你可以在创建表时指定一个任意的显示尺寸,但是定义列长为0或比14大均会被强制定义为列长14。列长在从1~13范围的奇数值尺寸均被强制为下一个更大的偶数。

  所有的TIMESTAMP列都有同样的存储大小,使用被指定的时期时间值的完整精度(14位)存储合法的值不考虑显示尺寸。不合法的日期,将会被强制为0存储。

  

  这有几个含意:

  1、虽然你建表时定义了列TIMESTAMP(8),但在你进行数据插入与更新时TIMESTAMP列实际上保存了14位的数据(包括年月日时分秒),只不过在你进行查询时MySQL返回给你的是8位的年月日数据。如果你使用ALTER TABLE拓宽一个狭窄的TIMESTAMP列,以前被“隐蔽”的信息将被显示。

  2、同样,缩小一个TIMESTAMP列不会导致信息失去,除了感觉上值在显示时,较少的信息被显示出。

  3、尽管TIMESTAMP值被存储为完整精度,直接操作存储值的唯一函数是UNIX_TIMESTAMP();由于MySQL返回TIMESTAMP列的列值是进过格式化后的检索的值,这意味着你可能不能使用某些函数来操作TIMESTAMP列(例如HOUR()或SECOND()),除非TIMESTAMP值的相关部分被包含在格式化的值中。例如,一个TIMESTAMP列只有被定义为TIMESTAMP(10)以上时,TIMESTAMP列的HH部分才会被显示,因此在更短的TIMESTAMP值上使用HOUR()会产生一个不可预知的结果。

  4、不合法TIMESTAMP值被变换到适当类型的“零”值(00000000000000)。(DATETIME,DATE亦然)    

  

  你可以使用下列语句来验证:

  CREATE TABLE test ('id' INT (3) UNSIGNED AUTO_INCREMENT, 'date1' TIMESTAMP (8) PRIMARY KEY('id'));  INSERT INTO test SET id = 1;  SELECT * FROM test;  +----+----------------+  | id | date1     |  +----+----------------+  | 1 | 20021114    |  +----+----------------+  ALTER TABLE test CHANGE 'date1' 'date1' TIMESTAMP(14);  SELECT * FROM test;  +----+----------------+  | id | date1     |  +----+----------------+  | 1 | 20021114093723 |  +----+----------------+
Copy after login

  你可以使用TIMESTAMP列类型自动地用当前的日期和时间标记INSERT或UPDATE的操作。如果你有多个TIMESTAMP列,只有第一个自动更新。自动更新第一个TIMESTAMP列在下列任何条件下发生:

  

  1、列值没有明确地在一个INSERT或LOAD DATA INFILE语句中指定。

  2、列值没有明确地在一个UPDATE语句中指定且另外一些的列改变值。(注意一个UPDATE设置一个列为它已经有的值,这将不引起TIMESTAMP列被更新,因为如果你设置一个列为它当前的值,MySQL为了效率而忽略更改。)

  3、你明确地设定TIMESTAMP列为NULL. |

  4、除第一个以外的TIMESTAMP列也可以设置到当前的日期和时间,只要将列设为NULL,或NOW()。

   CREATE TABLE test (     'id' INT (3) UNSIGNED AUTO_INCREMENT,     'date1' TIMESTAMP (14),     'date2' TIMESTAMP (14),     PRIMARY KEY('id')     );    INSERT INTO test (id, date1, date2) VALUES (1, NULL, NULL);  INSERT INTO test SET id= 2;  +----+----------------+----------------+  | id | date1     | date2     |  +----+----------------+----------------+  | 1 | 20021114093723 | 20021114093723 |  | 2 | 20021114093724 | 00000000000000 |  +----+----------------+----------------+
Copy after login

第一条指令因设date1、date2为NULL,所以date1、date2值均为当前时间,第二条指令因没有设date1、date2列值,第一个TIMESTAMP列date1为更新为当前时间,而二个TIMESTAMP列date2因日期不合法而变为“00000000000000”

  UPDATE test SET id= 3 WHERE id=1;  +----+----------------+----------------+  | id | date1     | date2     |  +----+----------------+----------------+  | 3 | 20021114094009 | 20021114093723 |  | 2 | 20021114093724 | 00000000000000 |  +----+----------------+----------------+
Copy after login

这条指令没有明确地设定date2的列值,所以第一个TIMESTAMP列date1将被更新为当前时间。

  UPDATE test SET id= 1,date1=date1,date2=NOW() WHERE id=3;  +----+----------------+----------------+  | id | date1     | date2     |  +----+----------------+----------------+  | 1 | 20021114094009 | 20021114094320 |  | 2 | 20021114093724 | 00000000000000 |  +----+----------------+----------------+
Copy after login

这条指令因设定date1=date1,所以在更新数据时date1列值并不会发生改变,而因设定date2=NOW(),所以在更新数据时date2列值会被更新为当前时间。此指令等效为:

UPDATE test SET id= 1,date1=date1,date2=NULL WHERE id=3;
Copy after login

  因MySQL返回的 TIMESTAMP 列为数字显示形式,你可以用DATE_FROMAT()函数来格式化 TIMESTAMP 列。

  SELECT id,DATE_FORMAT(date1,'%Y-%m-%d %H:%i:%s') As date1,      DATE_FORMAT(date2,'%Y-%m-%d %H:%i:%s') As date2 FROM test;  +----+---------------------+---------------------+  | id | date1        | date2        |  +----+---------------------+---------------------+  | 1 | 2002-11-14 09:40:09 | 2002-11-14 09:43:20 |  | 2 | 2002-11-14 09:37:24 | 0000-00-00 00:00:00 |  +----+---------------------+---------------------+    SELECT id,DATE_FORMAT(date1,'%Y-%m-%d') As date1,      DATE_FORMAT(date2,'%Y-%m-%d') As date2 FROM test;       +----+-------------+-------------+  | id | date1    | date2    |  +----+-------------+-------------+  | 1 | 2002-11-14 | 2002-11-14 |  | 2 | 2002-11-14 | 0000-00-00 |  +----+-------------+-------------+
Copy after login

 

 

  在某种程度上,你可以把一种日期类型的值赋给一个不同的日期类型的对象。然而,而尤其注意的是:值有可能发生一些改变或信息的损失: 

  

  1、如果你将一个DATE值赋给一个DATETIME或TIMESTAMP对象,结果值的时间部分被设置为'00:00:00',因为DATE值中不包含有时间信息。

 

  2、如果你将一个DATETIME或TIMESTAMP值赋给一个DATE对象,结果值的时间部分被删除,因为DATE类型不存储时间信息。 

 

  3、尽管DATETIME, DATE和TIMESTAMP值全都可以用同样的格式集来指定,但所有类型不都有同样的值范围。例如,TIMESTAMP值不能比1970早,也不能比2037晚,这意味着,一个日期例如'1968-01-01',当作为一个DATETIME或DATE值时它是合法的,但它不是一个正确TIMESTAMP值!并且如果将这样的一个对象赋值给TIMESTAMP列,它将被变换为0。 

  

  当指定日期值时,当心某些缺陷: 

  

  1、允许作为字符串指定值的宽松格式能被欺骗。例如,,因为“:”分隔符的使用,值'10:11:12'可能看起来像时间值,但是如果在一个日期中使用,上下文将作为年份被解释成'2010-11-12'。值'10:45:15'将被变换到'0000-00-00',因为'45'不是一个合法的月份。 

     

  2、以2位数字指定的年值是模糊的,因为世纪是未知的。MySQL使用下列规则解释2位年值: 在00-69范围的年值被变换到2000-2069。 在范围70-99的年值被变换到1970-1999。

 

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is there a future for employment in clinical pharmacy at Harbin Medical University? (What are the employment prospects for clinical pharmacy at Harbin Medical University?) Is there a future for employment in clinical pharmacy at Harbin Medical University? (What are the employment prospects for clinical pharmacy at Harbin Medical University?) Jan 02, 2024 pm 08:54 PM

What are the employment prospects of clinical pharmacy at Harbin Medical University? Although the national employment situation is not optimistic, pharmaceutical graduates still have good employment prospects. Overall, the supply of pharmaceutical graduates is less than the demand. Pharmaceutical companies and pharmaceutical factories are the main channels for absorbing such graduates. The demand for talents in the pharmaceutical industry is also growing steadily. According to reports, in recent years, the supply-demand ratio for graduate students in majors such as pharmaceutical preparations and natural medicinal chemistry has even reached 1:10. Employment direction of clinical pharmacy major: After graduation, students majoring in clinical medicine can engage in medical treatment, prevention, medical research, etc. in medical and health units, medical research and other departments. Employment positions: Medical representative, pharmaceutical sales representative, sales representative, sales manager, regional sales manager, investment manager, product manager, product specialist, nurse

How to clean temp folder How to clean temp folder Feb 22, 2024 am 09:15 AM

How to clean the temp folder As we use the computer, temporary files (temp files) will gradually accumulate. These temporary files are generated when we use the computer, such as cache files when browsing the web, temporary files during software installation, etc. Failure to clean the temp folder for a long time may occupy a large amount of disk space and affect the speed of the computer. Therefore, cleaning the temp folder regularly is a necessary step to maintain computer performance. Below, we will introduce some simple ways to clean the temp folder. Method 1: Manually clean t

How to download win10 image quickly How to download win10 image quickly Jan 07, 2024 am 11:33 AM

Recently, some friends reported how to download win10 image files. Because there are so many image files on the market, what should I do if I want to find a regular file to download? Today, the editor has brought you the link to download the image and the detailed solution steps. Let’s take a look at them together. win10 image quick download and installation tutorial download link >>> System Home Ghostwin101909 image 64-bit version v2019.11<<<>>>Win10 image 64-bit v2019.07<<<>>>Win10 image 32-bit v2019.07<< <1. Search through the Internet

How to reset Win10 system How to reset Win10 system Jun 29, 2023 pm 03:14 PM

How to reset Win10 system? Nowadays, many friends like to use computers with Win10 system. However, they will inevitably encounter some unsolvable problems when using computers. At this time, you can try to reset the system. So how should you do it? Let’s follow the editor to watch the tutorial on resetting the Win10 system. Users in need should not miss it. Tutorial on resetting the Win10 system 1. Click Windows and select Settings. 2. Click Update and Security. 3. Select Restore. 4. Click Start on the right to reset this computer. The above is the entire content of [How to reset Win10 system - Tutorial on resetting Win10 system]. More exciting tutorials are available on this site!

How to check win11 computer configuration How to check win11 computer configuration Jun 29, 2023 pm 12:15 PM

How to check win11 computer configuration? The win11 system is a very practical computer operating system version. This version provides users with rich functions, allowing users to have a better computer operating experience. So many friends who use computers are curious about their computers. Specific configuration, how to perform this operation in win11 system? Many friends don’t know how to operate in detail. The editor has compiled a tutorial on how to view the win11 computer configuration below. If you are interested, follow the editor and read on! Win11 computer configuration view tutorial 1. Click the windows icon on the taskbar below or press the "windows key" on the keyboard to open the start menu. 2. Find "Settings" or "sett" in the start menu.

Solve the problem of environment detection when reinstalling the system Solve the problem of environment detection when reinstalling the system Jan 08, 2024 pm 03:33 PM

How to solve the problem that the environment test fails when reinstalling the system and needs to be rewritten. The reason is: the mobile phone is poisoned. You can install anti-virus software such as Mobile Manager for anti-virus. 2. Many junk files are stored inside the mobile phone, causing the running memory of the mobile phone to be occupied. Just clear the phone cache to solve this problem. 3. The phone memory is occupied too much by saved software and files. It is no problem to delete unnecessary files and software frequently. As long as your hardware configuration meets the installation requirements, you can use the new one directly. Reinstall the system from the system disk! You can use a USB flash drive or hard disk to install, which is very fast. But the key is to use a system disk with good compatibility (supports installation in IDE, ACHI, and RAID modes), and it can be automatically and permanently activated, which has been verified. so

How to add the values ​​of HTML elements? How to add the values ​​of HTML elements? Sep 16, 2023 am 08:41 AM

This article will teach you how to add the value of an element in HTML. We have a basic understanding of the value attribute in HTML and the situations in which it is used. Let's look forward to a better understanding of the HTMLvalue attribute. In HTML, the value attribute is used to describe the value of the element it is used with. It has different meanings for various HTML components. Usage - It can be used with,,,,,, and, elements. -When the value attribute is present, it indicates what the default value of the input element is. It has different meanings for various types of input: when the button appears in "button," "reset," and &qu

how to reset password in mysql how to reset password in mysql Feb 18, 2024 pm 12:41 PM

MySQL is an open source relational database management system that is widely used in various types of application development. When using the MySQL database, you often need to change the password to improve the security of the database. This article will introduce how to change the MySQL password through specific code examples. In MySQL, you can change the password by following the following steps: Log in to the MySQL database server: Open a command prompt or terminal window and execute the following command: mysql-uroo

See all articles