2013-09-23 42 views
0

我使用PostgreSQL和创建见下表:日期字段将自动填写

CREATE TABLE "TrainingMatrix" 
(
    payroll text NOT NULL, 
    "TrainingName" text NOT NULL, 
    "Institute" text, 
    "TrainingDate" date NOT NULL, 
    "ExpiryDate" date, 
    "RecorderName" text, 
    "EnteringDate" date, 
    CONSTRAINT "TrainingMatrix_pkey" PRIMARY KEY (payroll, "TrainingName", "TrainingDate") 
) 

我想让EnteringDate被自动由当前的日期在机器每次进入记录填写。

回答

1
CREATE TABLE "TrainingMatrix" 
(
    payroll text NOT NULL, 
    "TrainingName" text NOT NULL, 
    "Institute" text, 
    "TrainingDate" date NOT NULL, 
    "ExpiryDate" date, 
    "RecorderName" text, 
    "EnteringDate" date NOT NULL default current_date,--"EnteringDate" date, 
    CONSTRAINT "TrainingMatrix_pkey" PRIMARY KEY (payroll, "TrainingName", "TrainingDate") 
) 
0

通过使用默认的系统日期,你可以achive所需的输出。

CREATE TABLE "TrainingMatrix" 
(
    payroll text NOT NULL, 
    "TrainingName" text NOT NULL, 
    "Institute" text, 
    "TrainingDate" date NOT NULL, 
    "ExpiryDate" date, 
    "RecorderName" text, 
    "EnteringDate" date default sysdate, 
    CONSTRAINT "TrainingMatrix_pkey" PRIMARY KEY (payroll, "TrainingName", "TrainingDate") 
) 
+1

Postgres中没有'sysdate'。 –

1
CREATE TABLE "TrainingMatrix" 
(
    payroll text NOT NULL, 
    "TrainingName" text NOT NULL, 
    "Institute" text, 
    "TrainingDate" date NOT NULL, 
    "ExpiryDate" date, 
    "RecorderName" text, 
    "EnteringDate" date not null default current_timestamp, 
    CONSTRAINT "TrainingMatrix_pkey" PRIMARY KEY (payroll, "TrainingName", "TrainingDate") 
) 

CURRENT_TIMESTAMP是equivelent到现在()将给当前日期&时间(包括时区)http://www.postgresql.org/docs/8.1/static/functions-datetime.html

定义列不空也将有助于确保没有记录明确地进入没有时间戳。