Re: Change key primary for key foreign - Mailing list pgsql-general

From Edson Richter
Subject Re: Change key primary for key foreign
Date
Msg-id BLU0-SMTP43546C2A8FFE59CEFD37EBECF9B0@phx.gbl
Whole thread Raw
In response to Re: Change key primary for key foreign  (Guilherme Rodrigues <lopesgrml@gmail.com>)
List pgsql-general
Em 18/09/2012 21:25, Guilherme Rodrigues escreveu:
> I created one table so:
>
> CREATE TABLE clima (
>      city     char(80),
>      cprc     int,
> );
>
> And have other table so:
>
> CREATE TABLE city (
>      name            char(80),
>      other_thing     int,
> );
>
> But now I want the table clima receive name table city as foreign key. understand?
> Sorry my bad english.
>
>
> Em terça-feira, 18 de setembro de 2012 17h37min59s UTC-3, Guilherme Rodrigues  escreveu:
>> Hello. I want to change one table already created. Where the comand? thank you.
>>
>>
Guilherme,

You should define the appropriate constraints. Example:

create table city (name char(80) not null constraint PK_CITY primary
key, other_thing int);
create table clima ( city char(80) not null references foreign key
(name), cprc int);

But be aware that you should not create tables without primary keys,
your application would quickly become a mess.

If you search a bit, you will find excellent references on how to deal
with Foreign Keys.

Regards,

Edson.

<portuguese>
Olá, Guilherme!

Você deverá definir as contraints apropriadas nas tabelas. Na tabela
cidade, você deverá utilizar uma Primary Key que será referenciada pela
outra tabela.
Uma Foreign Key só pode referenciar uma coluna da tabela City se houver
uma Primery Key ou uma Unique Key associada aquela coluna.
Você também deve tomar o cuidado de não criar tabelas sem definir uma
Primary Key, pois elas são a garantia que não existirão registros
duplicados em seu sistema mais tarde.

Infelizmente, não podemos trocar e-mail diretamente em português (regras
da comunidade), mas existem centenas de exemplos que você pode consultar
na web. Pesquise sobre Foreign Key e você vai achar milhares de exemplos
básicos e avançados.

Abraço,

Edson
</portuguese>





pgsql-general by date:

Previous
From: Chris Travers
Date:
Subject: Re: Column aliases in WHERE clauses
Next
From: Steve Haresnape
Date:
Subject: Re: RFE: Column aliases in WHERE clauses