简体   繁体   中英

SQL keep reflecting syntax error for getdate when i create table

SQL keep reflecting syntax error "A comma or a closing bracket was expected. (near "("" Any idea whats wrong? or how to fix it ..?

"CREATE TABLE `employee` (
  `id` int(6) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `username` varchar(15) NOT NULL ,
  `password` varchar(50) NOT NULL,
  `first_name` varchar(50) NOT NULL,
  `last_name` varchar(45) NOT NULL,
  `email` varchar(50) NOT NULL,
  `role` varchar(10) NOT NULL,
  'date_created' DATETIME NOT NULL DEFAULT GETDATE(),
   UNIQUE (id)
) ";

The error is on GETDATE() , i personally use timestamp and i think you want to use CURRENT_TIMESTAMP

The DEFAULT clause specifies a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP or (as of MySQL 5.6.5) DATETIME column. See Section 11.3.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.

http://dev.mysql.com/doc/refman/5.7/en/create-table.html

So your create statement should be

CREATE TABLE `employee` (
  `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(15) NOT NULL,
  `password` VARCHAR(50) NOT NULL,
  `first_name` VARCHAR(50) NOT NULL,
  `last_name` VARCHAR(45) NOT NULL,
  `email` VARCHAR(50) NOT NULL,
  `role` VARCHAR(10) NOT NULL,
  `date_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`));

GETDATE()在MySQL中不起作用,用CURRENT_TIMESTAMP替换它,如果您不是MySQL的最新用户,那么您就很满意,如果不是DATETIME类型,则不能使用CURRENT_TIMESTAMP作为默认值,并且您会需要改用TIMESTAMP类型

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM