爱问知识人 爱问教育 医院库

如何在MySQL&Oracle下创建自动递增字段?

首页

如何在MySQL&Oracle下创建自动递增字段?

如何在MySQL&Oracle下创建自动递增字段?

提交回答

全部答案

    2018-03-29 05:24:53
  •   如何在MySQL&Oracle下创建自动递增字段在MySQL下创建自动递增字段:create table article
    //先创建一个表。(
    id int primary key auto_increment,
    //设置该字段为自动递增字段。
       title varchar(255));insert into article values (null,'a'); //向数据库中插入数据。select * from article; 结果如下:IdTitle 1ainsert into article values (null,’b’);insert into article values (null,'c');insert into article (title) values ('d');select * from article; 结果如下:IdTitle 1a2b3 c4d但是oracle没有这样的功能,但是通过触发器(trigger)和序列(sequence)可以实现。
      假设关键字段为id,建一个序列,代码为:create sequence seq_test_idsminvalue 1maxvalue 99999999start with 1increment by 1nocacheorder;建解发器代码为:create or replace trigger tri_test_id before insert on test_table for each rowdeclare nextid number;begin IF :new。
      id IS NULLor :new。id=0 THEN select seq_test_id。nextval into nextid from sys。dual; :new。id:=nextid; end if;end tri_test_id;OK,上面的代码就可以实现自动递增的功能了。
      

    李***

    2018-03-29 05:24:53

类似问题

换一换

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):