Oracle中添加自动编号的序列

 创建表zuocheng

create table zuocheng(id int  ,address char(25), pay int);
 
 
2. 创建自增序列
create sequence zc      //创建名为zc的序列
  increment   by   1    //自增长度为1
  start   with   1     //从1开始计数
  minvalue 1     //最小值为1
  nomaxvalue    //没有最大值
  nocache;      //不设置缓存
 
 
3. 为表zuocheng创建触发器
create or replace trigger zuocheng_id  //将触发器绑定在 id 这一列
before insert
on zuocheng
for each row
when(new.id is null)
begin
select zc.nextval into:new.id from dual;
end;
 
 
4. 插入数据
insert into zuocheng (address,pay) values('anh3u1i',345);
或者
insert into zuocheng values(zc.nextval,'anh3u1i',345);
 
 
==============================================
 
另外一种高级方法:
 
create table BHL_CLIENTLOG(  
   C_ID                   NUMBER(9) not null, 
   CLANNO                VARCHAR2(50),  
   CLANNAME               VARCHAR2(50),  
   DBNO               VARCHAR2(50),
   DBNAME              VARCHAR2(50),
   FILENO              VARCHAR2(50),  
    DATETIME              VARCHAR2(50),  
   STATUS                NUMBER(1),
   constraint BHL_CLIENTLOG primary key (C_ID)
);
 
create sequence SEQ_BHL_CLIENTLOG
increment by 1
start with 1
 minvalue 1
 cache 20
 maxvalue 999999999;
 
 
在项目数据库表ClientLog.htm.xml中设置
 
<hibernate-mapping>
<class name="com.bhl.datasync.bo.ClientLog" table="BHL_CLIENTLOG"> 
<id name="cId" type="java.lang.Integer" column="C_ID" length="9"> 
<generator class="native">
<param name="sequence">SEQ_BHL_CLIENTLOG</param>
</generator>
</id>
<property name="clanNo" type="java.lang.String" column="CLANNO" length="50" />
<property name="clanName" type="java.lang.String" column="CLANNAME" length="50" /> 
<property name="dbNo" type="java.lang.String" column="DBNO" length="50"/>
<property name="dbName" type="java.lang.String" column="DBNAME" length="50" />
<property name="fileNo" type="java.lang.String" column="FILENO" length="50" />
<property name="dateTime" type="java.lang.String" column="DATETIME" length="50" />
<property name="status" type="java.lang.Integer" column="STATUS" length="1" />
</class>
</hibernate-mapping>
 
 
 

posted on 2013-01-22 11:29  、八月份的前奏  阅读(5405)  评论(0编辑  收藏  举报

导航