insert student values('1','li',20,'class3')
if not exists(select * from student where sname='li')
如上面的语句  如果我想实现姓名不重复插入  ,如果不支持exists语句的话,应该怎么写啊??
我用的是Mysql 5.0   
急急急!!!  谢谢!!!!

解决方案 »

  1.   

    两种方法:
    1、用存储过程:delimiter ||
    create procedure sp_test()
    begin
      if not exists(select 1 from student where sname='li') then
        insert into student values('1','li',20,'class3');
      end if;
    end||
    delimiter ;
    调用:
    call sp_test();
    2、用唯一键属性。create unique key idx_sname on student(sname);
    insert ignore into student values('1','li',20,'class3');
      

  2.   

    insert student 
    select distinct '1','li',20,'class3' from student where sname<>'li' 
      

  3.   

    用replace
    没有记录的话就insert了
    如果有并且不一致的话就update了
    好好琢磨一下吧
      

  4.   

    用inert into update 主键=主键+1 的语句
    反正5.1是可以的。5.0我就不知道了
      

  5.   

    如果是主键的话,可以考虑用ignore,主键重复的话就忽略