Oracle auto add current date

前端 未结 1 1087
感情败类
感情败类 2021-01-02 10:26

I want create a table \'product\' and have a column date, is it possible that current date will be added when I add some info to table?

If yes please ex

相关标签:
1条回答
  • 2021-01-02 10:47

    Assuming that

    1. Your column is not actually named date since that is a reserved word
    2. Your column is actually defined as a date rather than as a number
    3. You want to populate the column when you insert a new row

    you can define a default value for the column.

    SQL> ed
    Wrote file afiedt.buf
    
      1  create table products (
      2  id number not null,
      3  dt date   default sysdate not null
      4* )
    SQL> /
    
    Table created.
    
    SQL>
    SQL> insert into products( id ) values( 1 );
    
    1 row created.
    
    SQL> select * from products;
    
            ID DT
    ---------- ---------
             1 20-NOV-12
    

    If you want to modify the dt column when you UPDATE the row, you would need a trigger

    CREATE OR REPLACE TRIGGER trg_products
      BEFORE INSERT OR UPDATE ON products
      FOR EACH ROW
    BEGIN
      :new.dt := sysdate;
    END;
    

    A trigger will override any value passed in as part of the INSERT or UPDATE statement for the dt column. A default value will not.

    0 讨论(0)
提交回复
热议问题