1997-10-03 02:01:57 +08:00
|
|
|
DROP SEQUENCE next_id;
|
|
|
|
DROP TABLE ids;
|
|
|
|
|
|
|
|
CREATE SEQUENCE next_id START -2 MINVALUE -2;
|
|
|
|
|
|
|
|
CREATE TABLE ids (
|
|
|
|
id int4,
|
|
|
|
idesc text
|
|
|
|
);
|
|
|
|
|
2010-11-24 04:27:50 +08:00
|
|
|
CREATE TRIGGER ids_nextid
|
1997-10-03 02:01:57 +08:00
|
|
|
BEFORE INSERT OR UPDATE ON ids
|
2010-11-24 04:27:50 +08:00
|
|
|
FOR EACH ROW
|
1997-10-03 02:01:57 +08:00
|
|
|
EXECUTE PROCEDURE autoinc (id, next_id);
|
|
|
|
|
|
|
|
INSERT INTO ids VALUES (0, 'first (-2 ?)');
|
|
|
|
INSERT INTO ids VALUES (null, 'second (-1 ?)');
|
|
|
|
INSERT INTO ids(idesc) VALUES ('third (1 ?!)');
|
|
|
|
|
|
|
|
SELECT * FROM ids;
|
|
|
|
|
2010-11-24 04:27:50 +08:00
|
|
|
UPDATE ids SET id = null, idesc = 'first: -2 --> 2'
|
1997-10-03 02:01:57 +08:00
|
|
|
WHERE idesc = 'first (-2 ?)';
|
2010-11-24 04:27:50 +08:00
|
|
|
UPDATE ids SET id = 0, idesc = 'second: -1 --> 3'
|
1997-10-03 02:01:57 +08:00
|
|
|
WHERE id = -1;
|
2010-11-24 04:27:50 +08:00
|
|
|
UPDATE ids SET id = 4, idesc = 'third: 1 --> 4'
|
1997-10-03 02:01:57 +08:00
|
|
|
WHERE id = 1;
|
|
|
|
|
|
|
|
SELECT * FROM ids;
|
|
|
|
|
|
|
|
SELECT 'Wasn''t it 4 ?' as nextval, nextval ('next_id') as value;
|
|
|
|
|
|
|
|
insert into ids (idesc) select textcat (idesc, '. Copy.') from ids;
|
|
|
|
|
|
|
|
SELECT * FROM ids;
|