How can i create a Autonumber ? [message #370989] |
Sat, 29 July 2000 11:42 |
M.B.
Messages: 1 Registered: July 2000
|
Junior Member |
|
|
I want to create a unique number in a column which get higher if i use a insert stateman, well a autonumber.
Is there a simple way to do this. Mabye i can move the ROWID into a column as a default.
Thx for Help.
Maik
*If someone can write a create table statemant it would really help*
|
|
|
Re: How can i create a Autonumber ? [message #370990 is a reply to message #370989] |
Mon, 31 July 2000 02:33 |
Ivan
Messages: 180 Registered: June 2000
|
Senior Member |
|
|
I think you'd better use sequence and trigger to generate unique number. Something like that:
SQL>CREATE SEQUENCE geo_uni_s START WITH 1;
SQL>CREATE TABLE geo (
unicod INTEGER CONSTRAINT nn_geo_unicod NOT NULL,
name CHAR(45),
CONSTRAINT pk_geo_unicod PRIMARY KEY ( unicod )
)
;
SQL>CREATE OR REPLACE TRIGGER geoUni
BEFORE INSERT ON geo
FOR EACH ROW
DECLARE
CURSOR c IS
SELECT geo_uni_s.nextval FROM dual;
v INTEGER;
BEGIN
IF :new.unicod IS NULL THEN
OPEN c;
FETCH c INTO v;
:new.unicod:=v;
END IF;
END;
;
And now just:
SQL>INSERT INTO geo(name) VALUES('name1');
Hope it helps.
Any comments? e-mail me at
ivan@volmed.org.ru
|
|
|