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