Postgres如何使用自动create_by创建表

时间:2017-11-03 20:03:19

标签: postgresql

如果我想创建一个包含创建条目的用户自动填充的create_by列的表,那么DDL是什么样的? 想知道Postgres是否可以像create_at

那样做
e.g. create_at TIMESTAMP NOT NULL DEFAULT NOW() 
有点像。

1 个答案:

答案 0 :(得分:1)

SQL Fiddle

PostgreSQL 9.6架构设置

CREATE TABLE foo
    (
        id serial primary key
      , "bar" varchar(1)
      , created_by text NOT NULL DEFAULT current_user
      , created_at timestamp DEFAULT current_timestamp
    )
;

INSERT INTO foo
    ("bar")
VALUES
    ('a'),
    ('b'),
    ('c')
;

查询1

select *
from foo

<强> Results

| id | bar |    created_by |                  created_at |
|----|-----|---------------|-----------------------------|
|  1 |   a | user_17_3a66a | 2017-11-04T05:05:18.161681Z |
|  2 |   b | user_17_3a66a | 2017-11-04T05:05:18.161681Z |
|  3 |   c | user_17_3a66a | 2017-11-04T05:05:18.161681Z |
相关问题