How to create primary key

Started by Reg Me Pleaseover 18 years ago10 messagesgeneral
Jump to latest
#1Reg Me Please
regmeplease@gmail.com

Hi all.

What'd be the syntax to create a primary key on an already build table?

Thanks.

--
Reg me Please

#2Richard Huxton
dev@archonet.com
In reply to: Reg Me Please (#1)
Re: How to create primary key

Reg Me Please wrote:

Hi all.

What'd be the syntax to create a primary key on an already build table?

Hmm - so you want to sort of alter a table and add a primary key
constraint to an existing column?

ALTER TABLE my_table ADD PRIMARY CONSTRAINT (existing_column);

All in the manuals:
http://www.postgresql.org/docs/8.2/static/sql-altertable.html

--
Richard Huxton
Archonet Ltd

In reply to: Reg Me Please (#1)
Re: How to create primary key

On 06/11/2007 08:54, Reg Me Please wrote:

What'd be the syntax to create a primary key on an already build table?

ALTER TABLE test ADD CONSTRAINT test_pk PRIMARY KEY (f1);

http://www.postgresql.org/docs/8.2/static/sql-altertable.html

---------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod@iol.ie
---------------------------------------------------------------

#4Samantha Atkins
sjatkins@mac.com
In reply to: Raymond O'Donnell (#3)
Re: How to create primary key

On Nov 6, 2007, at 1:15 AM, Raymond O'Donnell wrote:

On 06/11/2007 08:54, Reg Me Please wrote:

What'd be the syntax to create a primary key on an already build
table?

ALTER TABLE test ADD CONSTRAINT test_pk PRIMARY KEY (f1);

So, ALTER TABLE test ADD PRMARY KEY(f1 [, ... ] )
isn't enough? It appears in the examples. What more does the
explicit test_pk do that wouldn't happen anyway?

- samantha

#5Magnus Hagander
magnus@hagander.net
In reply to: Samantha Atkins (#4)
Re: How to create primary key

On Tue, Nov 06, 2007 at 01:38:31AM -0800, Samantha Atkins wrote:

On Nov 6, 2007, at 1:15 AM, Raymond O'Donnell wrote:

On 06/11/2007 08:54, Reg Me Please wrote:

What'd be the syntax to create a primary key on an already build
table?

ALTER TABLE test ADD CONSTRAINT test_pk PRIMARY KEY (f1);

So, ALTER TABLE test ADD PRMARY KEY(f1 [, ... ] )
isn't enough? It appears in the examples. What more does the
explicit test_pk do that wouldn't happen anyway?

It's not needed. It gives a name to the primary key. If you don't specify
one, the system will pick one for you automatically.

//Magnus

In reply to: Samantha Atkins (#4)
Re: How to create primary key

On 06/11/2007 09:38, Samantha Atkins wrote:

So, ALTER TABLE test ADD PRMARY KEY(f1 [, ... ] )
isn't enough? It appears in the examples. What more does the explicit
test_pk do that wouldn't happen anyway?

<confession>
To make sure I got it right, I tried it out using PgAdmin and copied the
generated SQL into my email - hence the pedantically complete statement. :-)
</confession>

Ray.

---------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod@iol.ie
---------------------------------------------------------------

#7Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Reg Me Please (#1)
Re: How to create primary key

Reg Me Please wrote:

What'd be the syntax to create a primary key on an already
build table?

As described in the documentation:

ALTER TABLE <name> ADD [CONSTRAINT <constraint name>]
PRIMARY KEY (<colname> [, ...])
[WITH (FILLFACTOR = <number>)] [USING INDEX TABLESPACE <tbsp name>]

Yours,
Laurenz Albe

#8Ron Johnson
ron.l.johnson@cox.net
In reply to: Richard Huxton (#2)
Re: How to create primary key

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 11/06/07 03:15, Richard Huxton wrote:

Reg Me Please wrote:

Hi all.

What'd be the syntax to create a primary key on an already build table?

Hmm - so you want to sort of alter a table and add a primary key
constraint to an existing column?

While I understand your feelings, I must disagree.

That's because adding a secondary index does not have the same sort
of syntax:
ALTER TABLE foo ADD INDEX (some, existing, columns);

ALTER TABLE my_table ADD PRIMARY CONSTRAINT (existing_column);

All in the manuals:
http://www.postgresql.org/docs/8.2/static/sql-altertable.html

- --
Ron Johnson, Jr.
Jefferson LA USA

Give a man a fish, and he eats for a day.
Hit him with a fish, and he goes away for good!

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHMFr/S9HxQb37XmcRAuqfAKCzmYeBU4dVXorvQxBUYIoQuw9YcgCeOve1
NUtFo4N/sC4julpZmMvLN0s=
=qcCO
-----END PGP SIGNATURE-----

#9Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Richard Huxton (#2)
Re: How to create primary key

Richard Huxton wrote:

What'd be the syntax to create a primary key on an already
build table?

ALTER TABLE my_table ADD PRIMARY CONSTRAINT (existing_column);

I'm sure it's just a typo, but that is wrong.
It is not ADD PRIMARY CONSTRAINT, but ADD PRIMARY KEY.

Yours,
Laurenz Albe

#10Richard Huxton
dev@archonet.com
In reply to: Laurenz Albe (#9)
Re: How to create primary key

Albe Laurenz wrote:

Richard Huxton wrote:

What'd be the syntax to create a primary key on an already
build table?

ALTER TABLE my_table ADD PRIMARY CONSTRAINT (existing_column);

I'm sure it's just a typo, but that is wrong.
It is not ADD PRIMARY CONSTRAINT, but ADD PRIMARY KEY.

It is indeed a typo. Thanks Albe.

Particularly irritating as I'd bothered give a link to the right manual
page too...

--
Richard Huxton
Archonet Ltd