From: Paulo Roberto Siqueira <paulo.siqueira@usa.net>
To: PGSQL SQL <pgsql-sql@postgresql.org>
Subject: CREATE TABLE with foreign key and primary key
Date: Sun, 9 Jul 2000 22:49:14 -0300
Message-ID: <NEBBLMEAPMNMMCFDDILOGECCCBAA.paulo.siqueira@usa.net> (raw)
I'm trying to create a table that has three fields as primary key. These
fields must exist in two other tables. id_aluno is primary key in table
pessoa, id_curso and id_polo are primary key in table curso_polo. But it
seems it won't work. What am I doing wrong? See definitions below.
Specifically, I didn't understand:
NOTICE: Illegal FOREIGN KEY definition REFERENCES "curso_polo"
ERROR: number of key attributes in referenced table must be equal to
foreign key
I'm using PostgreSQL 7.0
ufgvirtual=# create table matricula (
ufgvirtual(# id_aluno char(15) references pessoa,
ufgvirtual(# id_curso int4 references curso_polo,
ufgvirtual(# id_polo int2 references curso_polo,
ufgvirtual(# local_prova varchar(50) not null,
ufgvirtual(# autorizado bool default 'f' not null,
ufgvirtual(# id_plano_pgto int2 references plano_pgto not null,
ufgvirtual(# data_matricula date default CURRENT_DATE not null,
ufgvirtual(# primary key(id_aluno,id_curso,id_polo));
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index
'matricula_pkey' for table 'matricula'
NOTICE: CREATE TABLE will create implicit trigger(s) for FOREIGN KEY
check(s)
NOTICE: Illegal FOREIGN KEY definition REFERENCES "curso_polo"
ERROR: number of key attributes in referenced table must be equal to
foreign key
Table "pessoa"
Attribute | Type | Modifier
---------------+----------+-------------------------------------------------
login | char(15) | not null
nome | char(50) | not null
.
.
.
Table "curso_polo"
Attribute | Type | Modifier
---------------+----------+----------
id_curso | integer | not null
id_polo | smallint | not null
coordenador | integer | not null
data_limite | date |
id_plano_pgto | smallint |
num_vagas | integer | not null
I have tables pessoa, curso, polo, curso_polo and matricula. Primary key in
curso_polo are id_curso (references curso) and id_polo (references polo). In
table matricula I want as primary key id_pessoa (references pessoa),
id_curso (references curso_polo) and id_polo (references curso_polo).
I hope I have provided enough information. I'm a little confused.
Paulo R. Siqueira
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: paulo.siqueira@usa.net
Subject: Re: CREATE TABLE with foreign key and primary key
In-Reply-To: <NEBBLMEAPMNMMCFDDILOGECCCBAA.paulo.siqueira@usa.net>
References: <NEBBLMEAPMNMMCFDDILOGECCCBAA.paulo.siqueira@usa.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox