~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/spi/autoinc.example

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
DROP SEQUENCE next_id;
 
2
DROP TABLE ids;
 
3
 
 
4
CREATE SEQUENCE next_id START -2 MINVALUE -2;
 
5
 
 
6
CREATE TABLE ids (
 
7
        id              int4,
 
8
        idesc           text
 
9
);
 
10
 
 
11
CREATE TRIGGER ids_nextid 
 
12
        BEFORE INSERT OR UPDATE ON ids
 
13
        FOR EACH ROW 
 
14
        EXECUTE PROCEDURE autoinc (id, next_id);
 
15
 
 
16
INSERT INTO ids VALUES (0, 'first (-2 ?)');
 
17
INSERT INTO ids VALUES (null, 'second (-1 ?)');
 
18
INSERT INTO ids(idesc) VALUES ('third (1 ?!)');
 
19
 
 
20
SELECT * FROM ids;
 
21
 
 
22
UPDATE ids SET id = null, idesc = 'first: -2 --> 2' 
 
23
        WHERE idesc = 'first (-2 ?)';
 
24
UPDATE ids SET id = 0, idesc = 'second: -1 --> 3' 
 
25
        WHERE id = -1;
 
26
UPDATE ids SET id = 4, idesc = 'third: 1 --> 4' 
 
27
        WHERE id = 1;
 
28
 
 
29
SELECT * FROM ids;
 
30
 
 
31
SELECT 'Wasn''t it 4 ?' as nextval, nextval ('next_id') as value;
 
32
 
 
33
insert into ids (idesc) select textcat (idesc, '. Copy.') from ids;
 
34
 
 
35
SELECT * FROM ids;