Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating table with current date plus 30 days after current date

Tags:

database

mysql

Can someone help me create a table with the date being equal to current date plus 30 days. Is this right?

CREATE TABLE SAMPLE 
( datetoday DATETIME NOT NULL DEFAULT current_datetime(),
  dateafter30days DATETIME NOT NULL DEFAULT current_date(+30)
);

The logic is for example there is a subscriber that registered now so it will be recorded on the database and the expiration of his registration is after 30 days.

Meaning date of registration and date of expiration..

Many Thanks

Joey

like image 559
Database Admin Avatar asked Feb 02 '12 02:02

Database Admin


2 Answers

You may use calculated (and optionally persistent) field for this:

CREATE TABLE YourTableName
(
Subscriber INT PRIMARY KEY,
IssueDate DATETIME,
ExpireDate AS DATEADD(DAY, 30, IssueDate)
)
like image 120
Oleg Dok Avatar answered Oct 20 '22 13:10

Oleg Dok


Yes, here you try. When new subscriber data is inserted, registere date is current date, expire date is 30 days after current date. Your insert sql statement, don't need to mention these two columns (REGISTER_DT,EXPIRE_DT), these two will be updated auto on inserting statement.

Based on following table structure, your insert statement should be

INSERT INTO SAMPLE (SUBSCRIBER_NM) VALUES ('John');

-- table creation statement

 CREATE TABLE [dbo].[SAMPLE](
        [SUBSCRIBER_ID] [int] IDENTITY(1,1) NOT NULL,
        [SUBSCRIBER_NM] [nvarchar](50) NOT NULL,
        [REGISTER_DT] [datetime] NOT NULL CONSTRAINT [DF_SAMPLE_REGISTER_DT]  DEFAULT (getdate()),
        [EXPIRE_DT] [datetime] NOT NULL CONSTRAINT [DF_SAMPLE_EXPIRE_DT]  DEFAULT (dateadd(day,(30),getdate())),
     CONSTRAINT [PK_SAMPLE] PRIMARY KEY CLUSTERED 
    (
        [SUBSCRIBER_ID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
like image 1
Thit Lwin Oo Avatar answered Oct 20 '22 15:10

Thit Lwin Oo