-1

In MySQL I want the current date as my default and not the time.

CREATE TABLE Order_T
(OrderID NUMERIC(11) NOT NULL,
OrderDate DATE DEFAULT NOW(),
CustomerID NUMERIC(11),
CONSTRAINT Order_PK PRIMARY KEY (OrderID),
CONSTRAINT Order_FK FOREIGN KEY (CustomerID) REFERENCES Customer_T (CustomerID));

I dont know why this doesn't work for the date.

I want the default date to be current date without the time stamp.


1 답변


0

use a trigger

DELIMITER ||

CREATE TRIGGER curr_date BEFORE INSERT ON Order_T FOR EACH ROW
BEGIN
  SET new.date = NOW();
END ||

DELIMITER ;

see this answer How to set default value of MySQL DateTime ( not TIMESTAMP ) to NOW() or Current_DateTIme?‌​t-value-of-mysql-dat‌​etime-not-timestamp-‌​to-now-or-current


  • I am quite new to the mysql and I am not quite sure where the trigger will come. Will it be inside the table or after creating the table? - Pratik Mandavgade
  • You need to create first the trigger because if you see the syntax you need asign the trigger to table, first create the table without default in date then create trigger - Judith Palacios
  • Thanks Judith !! It worked - Pratik Mandavgade

Linked


Related

Latest