Set default values for Mysql table fields (text tutorials and attention details) _mysql

Source: Internet
Author: User
Tags datetime mysql in
Environment
MySQL 5.1 + command line tools
problem
MySQL table field Set default value
Solve
Copy Code code as follows:

--sql:
CREATE TABLE Test (
i_a int not NULL DEFAULT 1,
Ts_b Timestamp not NULL DEFAULT now (),
C_c char (2) Not NULL DEFAULT ' 1 '
);
--The following SQL is not valid
--time_d time is not NULL DEFAULT curtime (),
--date_e date not NULL DEFAULT curdate (),
--datetime_f datetime not NULL DEFAULT now (),


Summary
int type: The default value must also be an integral type, and no () bracket behind default.
Char type: The default value is to use single quotes.

DateTime type: the Now () function returns the current date time with ' Yyyy-mm-dd HH:MM:SS ', and can be saved directly to the DateTime field. Using system defaults is not supported.

Date type: Curdate () returns today's date in ' YYYY-MM-DD ' format and can be stored directly in the Date field. Using system defaults is not supported.

Time Type: Curtime () returns the current period in ' HH:MM:SS ' format, which can be stored directly in the Duration field. Using system defaults is not supported.
reference materials
MySQL table field default value
Error occurred for table field defaults when creating tables with SQL statements.
For example: in MSSQL
Copy Code code as follows:

CREATE TABLE Dnt_forums (
AA int not NULL DEFAULT ('),
bb Date not NULL DEFAULT (getdate ()),
CC char NOT NULL DEFAULT (NULL)
}

How to modify the above SQL statements in MySQL in order to use

AA is the int type, the default value is integral, and no () bracket behind default
The BB date type does not support the use of system defaults, changed to timestamp, and can take system time over Now ()
CC is not allowed to be null (NOT NULL) so it cannot be null by default and can be changed to an empty string
Copy Code code as follows:

CREATE TABLE Dnt_forums (
AA int not NULL DEFAULT 2,
bb Timestamp not NULL DEFAULT now (),
CC Char not NULL DEFAULT '
);

MySQL gets the system's current time function
Http://www.jb51.net/article/29124.htm
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.