Question about Foreign key!

Started by Fariba Noorbakhshover 24 years ago7 messagesgeneral
Jump to latest
#1Fariba Noorbakhsh
fNoorbakhsh@tecways.com

Hi everybody,

Is there anyway to define foreign Key ,.. (constraints) after creating
table?
As far I know, you can't do it with ALTER table!!!

Thanks in advance,
Fariba

#2Patrick Welche
prlw1@newn.cam.ac.uk
In reply to: Fariba Noorbakhsh (#1)
Re: Question about Foreign key!

On Fri, Jul 27, 2001 at 04:33:24PM +0200, Fariba Noorbakhsh wrote:

Hi everybody,

Is there anyway to define foreign Key ,.. (constraints) after creating
table?
As far I know, you can't do it with ALTER table!!!

template1=# \h alter table
Command: ALTER TABLE
...
ALTER TABLE table
ADD table constraint definition
...

eg:
Table "tblPerson"
Attribute | Type | Modifier
---------------------------+--------------------------+----------
ID | integer |
...
Table "tblAddress"
Attribute | Type | Modifier
------------+--------------------------+----------
ID | integer |
PersonID | integer |
...

create unique index tblperson_pkey on "tblPerson"("ID");
alter table "tblAddress"
add constraint addfk
foreign key("PersonID") references "tblPerson"("ID") match full;

Hope this helps,

Patrick

#3Fariba Noorbakhsh
fNoorbakhsh@tecways.com
In reply to: Fariba Noorbakhsh (#1)
Re: delete columns from table!

Fariba Noorbakhsh wrote:

Show quoted text

Hi Patrick,

Do you know how I can drop(delete) a column from a table?! Just a column. I don't
think you can do it with Alter table

Thanks in advance,
Fariba

#4Patrick Welche
prlw1@newn.cam.ac.uk
In reply to: Fariba Noorbakhsh (#1)
Re: delete columns from table!

On Mon, Aug 06, 2001 at 05:42:09PM +0200, Fariba Noorbakhsh wrote:

Hi Patrick,

Do you know how I can drop(delete) a column from a table?! Just a column. I don't
think you can do it with Alter table

Thanks in advance,
Fariba

According to the TODO list:

COMMANDS
...
* ALTER
...
o Add ALTER TABLE DROP COLUMN feature [drop]

so the way forward may be to
pg_dump -t your_table -D your_database > table.txt
and have fun editing out all reference to said column from table.txt. Then
drop the table and reload from table.txt. YMMV..

Cheers,

Patrick

#5Tim Barnard
tbarnard@povn.com
In reply to: Fariba Noorbakhsh (#1)
Re: delete columns from table!

Another way SELECT INTO a new table, omitting the column you want to delete.

Tim

----- Original Message -----
From: "Patrick Welche" <prlw1@newn.cam.ac.uk>
To: "Fariba Noorbakhsh" <fNoorbakhsh@tecways.com>
Cc: <pgsql-general@postgresql.org>
Sent: Monday, August 13, 2001 7:36 AM
Subject: Re: [GENERAL] delete columns from table!

On Mon, Aug 06, 2001 at 05:42:09PM +0200, Fariba Noorbakhsh wrote:

Hi Patrick,

Do you know how I can drop(delete) a column from a table?! Just a

column. I don't

Show quoted text

think you can do it with Alter table

Thanks in advance,
Fariba

According to the TODO list:

COMMANDS
...
* ALTER
...
o Add ALTER TABLE DROP COLUMN feature [drop]

so the way forward may be to
pg_dump -t your_table -D your_database > table.txt
and have fun editing out all reference to said column from table.txt. Then
drop the table and reload from table.txt. YMMV..

Cheers,

Patrick

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

#6Kevin J. Drewiske
drewiskk@msoe.edu
In reply to: Tim Barnard (#5)
RE: delete columns from table!

Another method is to create a new table with only the desired fields with
the SELECT INTO query

i.e. SELECT <desired query> INTO TABLE <new table> FROM <old table>

then, after deleting the old table, you will need to rename the new,
temporary table, to that of your old table. Your conditions will need to
be reset with the new table, as it will only contain data.

Hope that help

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Kevin Drewiske '01 CE
MSOE Webmaster Team: http://www.msoe.edu/
KJDesign Website Development: http://www.drewiske.com/KJDesign/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-----Original Message-----
From: Patrick Welche [SMTP:prlw1@newn.cam.ac.uk]
Sent: Monday, August 13, 2001 09:37
To: Fariba Noorbakhsh
Cc: pgsql-general@postgresql.org
Subject: Re: delete columns from table!

On Mon, Aug 06, 2001 at 05:42:09PM +0200, Fariba Noorbakhsh wrote:

Hi Patrick,

Do you know how I can drop(delete) a column from a table?! Just a column.

I don't

think you can do it with Alter table

Thanks in advance,
Fariba

According to the TODO list:

COMMANDS
...
* ALTER
...
o Add ALTER TABLE DROP COLUMN feature [drop]

so the way forward may be to
pg_dump -t your_table -D your_database > table.txt
and have fun editing out all reference to said column from table.txt. Then
drop the table and reload from table.txt. YMMV..

Cheers,

Patrick

----------------------------------------------------
NetZero Platinum
Sign Up Today - Only $9.95 per month!
http://my.netzero.net/s/signup?r=platinum&amp;refcd=PT97

#7Fariba Noorbakhsh
fNoorbakhsh@tecways.com
In reply to: Fariba Noorbakhsh (#1)
Re: delete columns from table!

Thanks, it helps!

Tim Barnard wrote:

Show quoted text

Another way SELECT INTO a new table, omitting the column you want to delete.

Tim

----- Original Message -----
From: "Patrick Welche" <prlw1@newn.cam.ac.uk>
To: "Fariba Noorbakhsh" <fNoorbakhsh@tecways.com>
Cc: <pgsql-general@postgresql.org>
Sent: Monday, August 13, 2001 7:36 AM
Subject: Re: [GENERAL] delete columns from table!

On Mon, Aug 06, 2001 at 05:42:09PM +0200, Fariba Noorbakhsh wrote:

Hi Patrick,

Do you know how I can drop(delete) a column from a table?! Just a

column. I don't

think you can do it with Alter table

Thanks in advance,
Fariba

According to the TODO list:

COMMANDS
...
* ALTER
...
o Add ALTER TABLE DROP COLUMN feature [drop]

so the way forward may be to
pg_dump -t your_table -D your_database > table.txt
and have fun editing out all reference to said column from table.txt. Then
drop the table and reload from table.txt. YMMV..

Cheers,

Patrick

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/users-lounge/docs/faq.html