有人可以帮我创建一个日期等于当前日期加上30天的表格。这是对的吗?
CREATE TABLE SAMPLE
( datetoday DATETIME NOT NULL DEFAULT current_datetime(),
dateafter30days DATETIME NOT NULL DEFAULT current_date(+30)
);
逻辑是例如有一个订户现在注册,因此它将被记录在数据库中,并且他的注册到期时间为30天。
注册登记日期和到期日期。
非常感谢
乔伊
答案 0 :(得分:4)
您可以使用计算(和可选的持久性)字段:
CREATE TABLE YourTableName
(
Subscriber INT PRIMARY KEY,
IssueDate DATETIME,
ExpireDate AS DATEADD(DAY, 30, IssueDate)
)
答案 1 :(得分:1)
create table SAMPLE
(
SUBSCRIBER_ID INT Primary Key,
REGISTER_DT DATETIME NOT NULL,
EXPIRE_DT DATETIME NOT NULL
)
您可以在注册新用户时更新过期日期,如下所示。
INSERT INTO SAMPLE(SUBSCRIBER_ID,REGISTER_DT,EXPIRE_DT)
VALUES (1,GETDATE(), DATEADD(DAY,30,GETDATE())
答案 2 :(得分:1)
是的,在这里你试试。插入新订户数据时,注册日期为当前日期,到期日期为当前日期后30天。你的insert sql语句,不需要提这两列(REGISTER_DT,EXPIRE_DT),这两个会在插入语句时自动更新。
根据以下表格结构,您的insert语句应为
INSERT INTO SAMPLE (SUBSCRIBER_NM) VALUES ('John');
- 表创建语句
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]
答案 3 :(得分:1)
在插入时创建表和触发器 * 表*
create table test
(
sub_id int primary key,
issueDate datetime,
expDate datetime
)
CREATE TRIGGER test_trigger BEFORE INSERT ON `test`
FOR EACH ROW SET NEW.issueDate = IFNULL(NEW.issueDate,NOW()),
NEW.expDate= TIMESTAMPADD(DAY,30,NEW.issueDate)