Topic: oracle的字段定义(自增)

  Print this page

1.oracle的字段定义(自增) Copy to clipboard
Posted by: chengbd
Posted on: 2004-07-16 12:00

SmileSmileSmileSmile哦,我想在oracle中我想定义一张表:id(主建,自增:种子为:1,自增为:1),consumeType(varchar)该怎么写sql语句!

2.Re:oracle的字段定义(自增) [Re: chengbd] Copy to clipboard
Posted by: windsnow
Posted on: 2004-07-22 15:18

定义序列

3.Re:oracle的字段定义(自增) [Re: chengbd] Copy to clipboard
Posted by: Julian13
Posted on: 2004-07-26 14:06

i used to use trigger with sequence for this:

DROP TRIGGER tri_consume_pk
;

DROP SEQUENCE seq_consume_pk
;

DROP TABLE consume
;

CREATE TABLE consume (
  consume_id NUMBER,
  consume_type VARCHAR2(10)
)
;

ALTER TABLE consume ADD CONSTRAINT pk_consume PRIMARY KEY (
  consume_id
)
;

CREATE SEQUENCE seq_consume_pk
  START WITH 1
  INCREMENT BY 1
  NOMAXVALUE
;

CREATE TRIGGER tri_consume_pk
  BEFORE INSERT ON consume
  FOR EACH ROW
BEGIN
  SELECT seq_consume_pk.NEXTVAL INTO :NEW.consume_id FROM dual;
END
;

4.Re:oracle的字段定义(自增) [Re: chengbd] Copy to clipboard
Posted by: array
Posted on: 2004-07-27 11:36

自增字段应为Number类型
你首先要有CREATE SEQUENCE或者CREATE ANY SEQUENCE权限,
CREATE SEQUENCE emp_sequence
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
CACHE 10;

一旦定义了emp_sequence,你就可以用CURRVAL,NEXTVAL
CURRVAL=返回 sequence的当前值
NEXTVAL=增加sequence的值,然后返回 sequence 值
比如:
emp_sequence.CURRVAL
emp_sequence.NEXTVAL

可以使用sequence的地方:
- 不包含子查询、snapshot、VIEW的 SELECT 语句
- INSERT语句的子查询中
- NSERT语句的VALUES中
- UPDATE 的 SET中

可以看如下例子:
INSERT INTO emp VALUES
(empseq.nextval, 'LEWIS', 'CLERK',7902, SYSDATE, 1200, NULL, 20);

SELECT empseq.currval FROM DUAL;

5.Re:oracle的字段定义(自增) [Re: chengbd] Copy to clipboard
Posted by: yanxue1129
Posted on: 2004-08-18 21:03

上面这位给出的答案是比较详细的,希望你能理解。

6.Re:oracle的字段定义(自增) [Re: chengbd] Copy to clipboard
Posted by: laoli97
Posted on: 2004-09-09 14:07

不错定得很详细,对我有启发,谢谢了


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923