pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedCREATE TABLE with foreign key and primary key
2+ messages / 2 participants
[nested] [flat]
* CREATE TABLE with foreign key and primary key
@ 2000-07-10 01:49 Paulo Roberto Siqueira <paulo.siqueira@usa.net>
2000-07-10 09:16 ` Re: CREATE TABLE with foreign key and primary key Oliver Elphick <olly@lfix.co.uk>
0 siblings, 1 reply; 2+ messages in thread
From: Paulo Roberto Siqueira @ 2000-07-10 01:49 UTC (permalink / raw)
To: pgsql-sql
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
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: CREATE TABLE with foreign key and primary key
2000-07-10 01:49 CREATE TABLE with foreign key and primary key Paulo Roberto Siqueira <paulo.siqueira@usa.net>
@ 2000-07-10 09:16 ` Oliver Elphick <olly@lfix.co.uk>
0 siblings, 0 replies; 2+ messages in thread
From: Oliver Elphick @ 2000-07-10 09:16 UTC (permalink / raw)
To: Paulo Roberto Siqueira <paulo.siqueira@usa.net>; +Cc: pgsql-sql
"Paulo Roberto Siqueira" wrote:
>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));
...
>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).
You can't use REFERENCES on a column if the target primary key is made
up of more than one column; you have to use a FOREIGN KEY table
constraint:
create table matricula (
id_aluno char(15) references pessoa,
id_curso int4 not null, -- I assume you want
id_polo int2 not null, -- not null here
local_prova varchar(50) not null,
autorizado bool default 'f' not null,
id_plano_pgto int2 references plano_pgto not null,
data_matricula date default CURRENT_DATE not null,
primary key(id_aluno,id_curso,id_polo),
FOREIGN KEY (id_curso, id_polo
REFERENCES curso_polo (id_curso,id_polo));
--
Oliver Elphick Oliver.Elphick@lfix.co.uk
Isle of Wight http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47 6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"Delight thyself also in the LORD; and he shall give
thee the desires of thine heart."
Psalms 37:4
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2000-07-10 09:16 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-07-10 01:49 CREATE TABLE with foreign key and primary key Paulo Roberto Siqueira <paulo.siqueira@usa.net>
2000-07-10 09:16 ` Oliver Elphick <olly@lfix.co.uk>
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