2014-09-25 119 views
0

我怎样才能组织更多的表格/表格来存储Order History如何组织在DB中存储历史记录的表?

现在我的数据库结构非常简单,看看下面的代码:

CREATE TABLE category_table (
    id SERIAL PRIMARY KEY NOT NULL, 
    name CHARACTER(250)  NOT NULL 
); 

CREATE TABLE order_table (
    id    SERIAL PRIMARY KEY NOT NULL, 
    total_price DECIMAL(12, 2), 
    date   TIMESTAMP WITH TIME ZONE DEFAULT now(), 
    product_amount INT 
); 

CREATE TABLE product_table (
    id   SERIAL PRIMARY KEY    NOT NULL, 
    name  CHARACTER VARYING(250)   NOT NULL, 
    price  DECIMAL(12, 2), 
    category_id INT, 
    CONSTRAINT category_fk FOREIGN KEY (category_id) REFERENCES category_table (id) 
); 

CREATE TABLE product_order_table (
    product_id INT, 
    order_id INT, 
    CONSTRAINT product_fk FOREIGN KEY (product_id) REFERENCES product_table (id), 
    CONSTRAINT order_fk FOREIGN KEY (order_id) REFERENCES order_table (id) 
); 

我需要整理表/表为Order History关于这两个限制:

    1.我们可以删除记录形式表`order_table`
    2.我们可以改变`order_table`

记录我在DB的设计需要咨询。

回答

0

用另一张表“order_table_history”跟踪历史记录是安全的。

为此表“order_table”创建触发器功能,并在插入/更新/删除触发器等任何操作时在历史表“order_table_history”中插入一条记录。

您还可以在历史记录表中添加更多列,例如“user_id/user_name”或“operation_status = delete/update”。

CREATE TABLE order_table_history (
    id    SERIAL PRIMARY KEY NOT NULL, 
    order_id  INT, 
    total_price DECIMAL(12, 2), 
    date   TIMESTAMP WITH TIME ZONE DEFAULT now(), 
    product_amount INT 
); 
+0

其实我不熟悉'触发函数'。 – Alex 2014-09-25 18:28:24

+0

您可以阅读以下链接的一些示例:http://www.postgresql.org/docs/9.3/static/sql-createtrigger.html或http://www.postgresqltutor.com/create-trigger.html – PlsqlDev 2014-09-25 18:38:11

+0

哦,感谢您的链接。 – Alex 2014-09-25 18:46:22

相关问题