高分请教oracle的default sysdate 怎么让每次写数据库时自动更新最新时间表和情况如下:当我在WEB页面上修改密码时,入库的时间没有改变,入库的时间是用了Oracle default sysdate,若不用Oracle  default sysdate,用什么办法解决最新时间。之前想过用Java的Date,但想到JAVA的日期时间和数据库日期是有区别的。请高手们赐教。感激不尽。谢谢create table t_student
(
id number primary key not null,       --主键
name varchar2(20) not null,           --姓名
psw varchar2(20) not null,            --密码
exchange_time date default sysdate    --每次入库时间
);
commit;insert into t_student(id,name,psw) values (1,'csdn','csdn');
select * from t_student;1 csdn csdn 2011-4-11 上午 11:15:26
update t_student set psw='csdn123456' where id=1;
select * from t_student;1 csdn csdn123456 2011-4-11 上午 11:15:26

解决方案 »

  1.   

    insert into t_student(id,name,psw,exchange_time) values (1,'csdn','csdn',sysdate);
    把sysdate 写到sql中就行了 不用搞的太复杂
      

  2.   

    这个很简单啊,请看下例:
    SQL> create table tdate(id int primary key, col2 varchar(32), col3 date default sysdate);表已创建。SQL> insert into tdate (id, col2) values(1, 'wang');已创建 1 行。SQL> alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';会话已更改。SQL> select * from tdate;        ID COL2
    ---------- ----------------------------------------------------------------
    COL3
    -------------------
             1 wang
    2011-04-11 11:35:21
    SQL> update tdate set col2='wang_modified', col3=sysdate where id=1;已更新 1 行。SQL> select * from tdate;        ID COL2
    ---------- ----------------------------------------------------------------
    COL3
    -------------------
             1 wang_modified
    2011-04-11 11:36:49
      

  3.   

    update時再更新一遍每次入库时间就可以了..update t_student set psw='csdn123456',exchange_time = sysdate where id=1;
      

  4.   

    update t_student set psw='csdn123456',exchange_time = sysdate where id=1;
      

  5.   


    在开发的过程中是要结合Java代码,单在PLSQL里写SQL语句是没有问题的。PreparedStatement state= conn.prepareStatement("update t_student set psw=?,exchange_time =?  where id=?");
    state.setString(2,"sysdate");  //当设sysdate的时候,执行时是有问题的
      

  6.   


    在开发的过程中是要结合Java代码,单在PLSQL里写SQL语句是没有问题的。PreparedStatement state= conn.prepareStatement("update t_student set psw=?,exchange_time =?  where id=?");
    state.setString(2,"sysdate");  //当设sysdate的时候,执行时是有问题的
      

  7.   

    update t_student set psw=?,exchange_time =? where id=?
    直接写到SQL里
    这样update t_student set psw=?,exchange_time =sysdate where id=?
      

  8.   


    个人能力比较菜,没怎么用触发器 。不知有没有像Sequence一样每次都能自动生成更新的办法
      

  9.   

    楼主啊  就算是Sequence也是insert的时候才行 你的default sysdate也只在insert的时候起作用 update肯定不行 你还没理解default的机制 default是在插入时给个默认值 update不会触发
    10楼的语句你试过吗?把sysdate直接写进sql语句中 而不是以参数的形式传进去 我对java不熟 但是 如果sql没错 java语法不应该成为问题的还有 触发器其实很简单 随便百度一下就会的
      

  10.   

    create or replace trigger triggername
      before update on tablename
      for each row
    begin
      :NEW.exchange_time := sysdate;
    end;
    这是最简单的触发器 一行代码就能实现你的功能
      

  11.   


    --建表
    create table t_student(
           id number primary key,
           name varchar2(20) not null,
           psw varchar2(20) not null,
           exchange_time date);
    --建触发器
    create or replace trigger tri_exchange_time
    before insert or update 
    on t_student
    for each row
    begin
         select sysdate into :new.exchange_time from dual;
    end;
    --测试数据
    insert into t_student(id,name,psw)
    values(1001,'csdn','csdn');
    insert into t_student(id,name,psw)
    values(1002,'csdn_home','csdn_home');
    --查看数据
    select id,name,psw,to_char(exchange_time,'yyyy-mm-dd hh24:mi:ss') exchange_time from t_student;
            ID NAME                 PSW                  EXCHANGE_TIME
    ---------- -------------------- -------------------- -------------------
          1001 csdn                 csdn                 2011-04-11 15:01:37
          1002 csdn_home            csdn_home            2011-04-11 15:01:37
    --更新数据
    update t_student set psw='itlover'
    where id=1001;
    --
    SQL> select id,name,psw,to_char(exchange_time,'yyyy-mm-dd hh24:mi:ss') exchange_time from t_student;
            ID NAME                 PSW                  EXCHANGE_TIME
    ---------- -------------------- -------------------- -------------------
          1001 csdn                 itlover              2011-04-11 15:04:52
          1002 csdn_home            csdn_home            2011-04-11 15:01:37
      

  12.   


    sysdate不是string变量,当然不能了。
    PreparedStatement state= conn.prepareStatement("update t_student set psw=?,exchange_time =sysdate where id=?");
    // state.setString(2,"sysdate");state.setString(1, "<newpsw>");
    state.setInt(2, <newId>);.......
      

  13.   


    //在传参数的时候,将你的参数设置为id号和pwd;
    //你只需要传入需要修改密码的id号和新pwd就可以了,
    //关于修改时间问题,oracle触发器会为你解决的。
    PreparedStatement state= conn.prepareStatement("update t_student set psw=? where id=?");
    state.setString(2,"new_pwd");
      

  14.   

    多谢大家给的意见,oracle触发器是可以解决此问题,但对于上百张表都有exchange_time 字段来说,为每张表建立一个触发器那可是无法接受的。10楼的直接拼接SQL是可以实现,但对Code有小量的改动。。暂采用10楼的意见,不知有没有高手能提供更好的解决方案。期待中。
      

  15.   

    写个创建 对于每张都有exchange_time的表的触发器的过程呗
    pl/sql 编程不就是干这个用的
    有必要 几百张表都有exchange_time吗
    不会产生数据冗余吧.
    设计很重要的 后面改起来 
    工作量很大....
      

  16.   

    需求的是要这样实现,exchange_time字段能记录每一条数据的最后操作时间。每张表的用途又不一样,不会产生数据冗余。。