Re: Composite Keys

From: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
To: RPK <rohitprakash123(at)indiatimes(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Composite Keys
Date: 2007-02-24 05:57:11
Message-ID: 45DFD3B7.6060506@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

RPK wrote:
> First, can a primary key field be only one? I mean can I define a primary
> containing two fields.

As many as you like :)

>
> I have a table called "Payments" where there are two fields, "ReceiptNo" and
> "PaymentDate". If I take "ReceiptNo" only as a primary key then chances are
> of "primary key violation". Because in some places the receipt books have
> numbers that start with 1. I mean Receipt Book 1 nos. 1-100. Receipt Book 2
> nos. 1-100, and so on.
>
> So I want to take ReceiptNo and PaymentDate as primary key field. Is it
> possible to define composite key in PGSQL. Please also suggest if any other
> approach would be fine.

CREATE TABLE payments (receiptno serial, paymentdate date, primary key
(reciptno,paymentdate))

Sincerely,

Joshua D. Drake

--

=== The PostgreSQL Company: Command Prompt, Inc. ===
Sales/Support: +1.503.667.4564 || 24x7/Emergency: +1.800.492.2240
Providing the most comprehensive PostgreSQL solutions since 1997
http://www.commandprompt.com/

Donate to the PostgreSQL Project: http://www.postgresql.org/about/donate
PostgreSQL Replication: http://www.commandprompt.com/products/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Magnus Hagander 2007-02-24 07:15:02 Re: problem installing NPGSQL and pgOLEDB with .NET
Previous Message RPK 2007-02-24 05:55:31 problem installing NPGSQL and pgOLEDB with .NET