Home > database >  SQL current_timestamp 14
SQL current_timestamp 14

Time:12-06

Hi I am having problem creating a table. Here's what I am trying to do: I have a table that automatically inserts current time and a current time 14 days.

Below is my code:

'''
create table borrowedbook (
studentEmail varchar(255) not null,
bookId varchar(255) not null,
borrowedDate datetime NOT NULL DEFAULT  current_timestamp(),
returnedDate datetime,
dueDate datetime NOT null default current_timestamp() 14,

FOREIGN KEY (studentEmail) REFERENCES student(studentEmail),
FOREIGN KEY (bookId) REFERENCES book(bookId)
);
'''

I need to make dueDate field work.

need help!

edit: I am using DBeaver, mysql

CodePudding user response:

You can use date_add(time_stamp, interval):

create table borrowedbook (
studentEmail varchar(255) not null,
bookId varchar(255) not null,
borrowedDate datetime NOT NULL DEFAULT  current_timestamp(),
returnedDate datetime,

dueDate datetime NOT null default DATE_ADD(current_timestamp, INTERVAL 14 DAY),

FOREIGN KEY (studentEmail) REFERENCES student(studentEmail),
FOREIGN KEY (bookId) REFERENCES book(bookId)
);

  • Related