表     字段 1  字段2  字段3 
table1  no     type    bill
在想table中添加数据时,先判断字段bill,如果有了就不插:select count(*) cou from table where bill='123';if(cou<>0)"";这样避免对bill的重复,但是要是两个人同时插入一样的bill,而且数据库中没有的,一起插,就插了两条,请问有谁能告诉我怎么处理吗?oracle10g数据库。

解决方案 »

  1.   

    用primary key 约束。
      

  2.   

    忘记补充了,这个字段不能设为这张表中的主键,因为有的时候可以重复的。根据type来确定可不可以重复
      

  3.   

    这个地方又说可以重复
    看来只能循环declare 
      CURSOR get_Attr_ IS
      SELECT *
      FROM  table_name   A ;
      temp_ Number;
    Begin 
       FOR rec_ IN get_Attr_ LOOP
           Begin
              select count(*) Into temp_  from table where bill='123';
              If temp_ =0 Then
              Insert Into table_name(col)Values(col_value);   
              End If;       
           EXCEPTION
              WHEN OTHERS THEN
                 dbms_output.put_line('error:'||Sqlerrm);
           END;
       END LOOP;  
    end;
      

  4.   

    防止并发,select后加for update
      

  5.   

    那怎么选择锁住哪一行呢?要是修改或删除之类的直接for update of col,但是插入时这个col为空的啊
      

  6.   

    不太明白这个数据到底允不允许重复?
    这个的没有则插入,用MERGE函数很合适。
      

  7.   

    用primary key 约束。 
    也就是主键约束
      

  8.   

    这样行吗:新建一个表table2,这个表中只用一个字段bill,设置成主键,如果对在table中添加数据,就
    alter  table table add   constraint foreign  key   (bill) references table (bill);
      

  9.   

    alter  table table add  constraint foreign  key  (bill) references table2 (bill);
      

  10.   

    我晕, 把 type + bll 设成unique约束。