On Wed, Dec 18, 2002 at 01:03:53AM +0100, Kai wrote:
> 
> I have attached a postgresql create table script, some plain SQL queries,

I had the idea of attaching.... Here they are.

-- 
Kai

96 pings make up a single Bong (Peter Salus)

Attachment: dbh-test.pl
Description: Perl program

-- no errors will occur:
BEGIN WORK;
INSERT INTO country (id, name) values (1, 'Netherlands');
INSERT INTO city (id, name, city_id) values (1, 'Amsterdam', 1);
COMMIT WORK;

-- no errors will occur:
BEGIN WORK;
SET CONSTRAINTS ALL DEFERRED;
INSERT INTO city (id, name, city_id) values (2, 'Brussels', 2);
INSERT INTO country (id, name) values (2, 'Belgium');
COMMIT WORK;

-- error will raise upon insert:
BEGIN WORK;
INSERT INTO city (id, name, city_id) values (3, 'Paris', 9);
ABORT WORK;

-- error will raise upon commit:
BEGIN WORK;
SET CONSTRAINTS ALL DEFERRED;
INSERT INTO city (id, name, city_id) values (3, 'Paris', 9);
COMMIT WORK;

BEGIN WORK;
DELETE ALL FROM city;
DELETE ALL FROM country;
COMMIT WORK;
/*
 * create 2 tables, one referencing the other.
 *
 * the foreign key constraint is of the 'DEFERRABLE' type
 * which has the side effect that the constraint is only
 * checked upon at the end of the transaction:
 */

/* 
 *  CREATE USER bar WITH CREATEDB;
 *  \connect - bar
 *  CREATE DATABASE foo;
 *  \connect foo bar
 */

CREATE TABLE country (
  id serial primary  key,
  name varchar(255) NOT NULL
);

CREATE TABLE city (
  id serial primary key,
  name varchar(255) NOT NULL,
  city_id int4 NOT NULL
    CONSTRAINT cityinacountry
    REFERENCES country (id) DEFERRABLE
);

Reply via email to