Jump to content

Error when creating table with curdate()


3D-kreativ

Recommended Posts

Hi,

 

I only get this error message in MySQL Workbench, version 5.2.25, "Error Code: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURDATE' at line 5"

 

CREATE TABLE Orders

(

OrderId int NOT NULL,

ProductName varchar(50) NOT NULL,

OrderDate datetime NOT NULL DEFAULT CURDATE(),

PRIMARY KEY (OrderId)

)

 

The code is from http://www.w3schools.com/sql/func_curdate.asp I'm surprised they have an example that not works!?

 

Preciate some help! Thanks!

Link to comment
Share on other sites

You cannot use a function to supply a default value. From the mysql documentation  -

... the default value must be a constant; it cannot be a function or an expression.

 

The disclaimer from the bottom of the w3schools web page -

We do not warrant the correctness of its content. The risk from using it lies entirely with the user.

 

You can cause the current datetime to be entered into a column if you use a TIMESTAMP data type -

The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column. See Section 10.3.1.1, “TIMESTAMP Properties”.

 

Link to comment
Share on other sites

Hmm, so I can't use CURTIME and CURDATE when I create the table? I thought it was possible to get the time and date every time into each column when a new row is added to the table with other information? Could it be done in some other way? To use PHP date variables into the columns each time can't be better? If possible show me some code.

 

Preciate som help! Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.