Home > Database > Mysql Tutorial > body text

Oracle-如何让表的一个列自动增长

WBOY
Release: 2016-06-07 17:11:33
Original
1176 people have browsed it

1 创建订单表t_order create table t_order (order_NO number,order_name varchar2(20),order_desc varchar2(20)); 本例将使表中

1 创建订单表t_order

 create table t_order (order_NO number,
order_name varchar2(20),
order_desc varchar2(20)
);

本例将使表中的订单编号order_NO自动增加。

2 创建序列

create sequence seq_order_NO  
start with 1
maxvalue 99999
minvalue 1
nocycle
cache 100;

3 创建触发器trigger_t_order
create or replace trigger trigger_t_order
    before insert on t_order
    for each row
      begin
        select seq_order_no.nextval into :NEW.order_NO from dual;
        end;

4 往表t_order中插入数据。
insert into t_order (order_name, order_desc)
values ('光盘采购','采购公司需要的光盘');
insert into t_order (order_name, order_desc)
values ('大米采购','要求来自泰国的大米');
commit;

5 查看字段order_NO是不是自动增加

select * from t_order;

  ORDER_NO ORDER_NAME           ORDER_DESC
---------- -------------------- --------------------
         1 光盘采购             采购公司需要的光盘
         2 大米采购             要求来自泰国的大米

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template