Just somewhere to keep my notes while I'm playing.

Thursday, May 8, 2008

CD Table with foreign key contraints!

CREATE TABLE cdtable
(
pubnum int4 NOT NULL,
artnum int4 NOT NULL,
cdtitle varchar(50) NOT NULL,
datepublished date,
CONSTRAINT artnum FOREIGN KEY (artnum) REFERENCES artist (artnum) ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT pubnum FOREIGN KEY (pubnum) REFERENCES publisher (pubnum) ON UPDATE NO ACTION ON DELETE NO ACTION
) WITHOUT OIDS
TABLESPACE ts_cddb_1;

2 comments:

Tim said...

cddb=> insert into cdtable (pubnum, artnum, cdtitle) values (1,5,"test");
ERROR: column "test" does not exist
cddb=> \e
ERROR: insert or update on table "cdtable" violates foreign key constraint "artnum"
DETAIL: Key (artnum)=(5) is not present in table "artist".
cddb=> \e
ERROR: insert or update on table "cdtable" violates foreign key constraint "pubnum"
DETAIL: Key (pubnum)=(5) is not present in table "publisher".
cddb=>

Tim said...

cddb=> insert into cdtable (artnum, pubnum, cdtitle, datepublished)
cddb-> values (1,3,'Heavenbound','02apr2002');
INSERT 0 1
cddb=> select * from cdtable;
pubnum | artnum | cdtitle | datepublished
--------+--------+-------------+---------------
3 | 1 | Heavenbound | 2002-04-02
(1 row)

cddb=>