[SQL]

2013-10-08 Thread Kaleeswaran Velu
Hi Team,
I am using PostgreSQL 9.2.3 in Windows platform. I have created two databases 
in it. Now I want to refer the tables across the databases. Meaning would like 
to create Database link. Can anyone guide me on how to create a DB link?
 
Thanks and Regards
Kaleeswaran Velu

Re: [SQL]

2013-10-08 Thread Plugge, Joe R.
1 - why two databases?  Couldn't you have just created two separate SCHEMAS?
2 - if you insist on two separate databases:   
http://www.postgresql.org/docs/9.2/static/dblink.html


From: pgsql-sql-ow...@postgresql.org [mailto:pgsql-sql-ow...@postgresql.org] On 
Behalf Of Kaleeswaran Velu
Sent: Tuesday, October 08, 2013 11:23 AM
To: pgsql-sql@postgresql.org
Subject: [SQL]

Hi Team,
I am using PostgreSQL 9.2.3 in Windows platform. I have created two databases 
in it. Now I want to refer the tables across the databases. Meaning would like 
to create Database link. Can anyone guide me on how to create a DB link?

Thanks and Regards
Kaleeswaran Velu


Re: [SQL]

2013-10-08 Thread Andreas Kretschmer
Kaleeswaran Velu  wrote:

> Hi Team,
> I am using PostgreSQL 9.2.3 in Windows platform. I have created two databases
> in it. Now I want to refer the tables across the databases. Meaning would like
> to create Database link. Can anyone guide me on how to create a DB link?

You can use db_link, see
http://www.postgresql.org/docs/9.2/interactive/dblink.html.

9.3 contains more:
http://www.postgresql.org/docs/9.3/interactive/postgres-fdw.html

But in general: don't use different databases if you need a
cross-database-connection. You can use Schemas instead:
http://www.postgresql.org/docs/9.2/static/ddl-schemas.html


Andreas
-- 
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.  (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.  N 51.05082°, E 13.56889°


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


[SQL] Many to many link tables with history?

2013-10-08 Thread Craig R. Skinner
Hi folks, I'm new here. I hope this is the correct list


While creating a database of customer subscribed services with
DNS domains, I want to:
*) enable customers to subscribe to multiple services
*) each service can have multiple domain names per customer
*) a domain name must be unique per customer per timestamp


Example customers:
1 | Sun Microsystems (USA) Inc.
2 | Sun Microsystems (UK) Ltd.


Example domains:
1 | sun.com
2 | sun.co.nz
3 | sun.co.uk


If Sun USA subscribes with the .com & .nz domains in January, then
decides in February to have the offshore UK office manage these services
instead, along with the .uk domain. Therefore the customer ID changes at
a timestamp for billing in the different currency.

A domain name must be live (have no deleted_at timestamp) for only 1
customer per time. A customer may delete a domain & re-add it later, or
another customer may add it later.

For billing, I need to know which customer had what domains active on
what dates.

How do I manage the link between domains & subscriptions, and maintain a 
history?


CREATE TABLE subscription
(
  id serial NOT NULL,
  customer integer NOT NULL,
  service integer NOT NULL,
  created timestamp with time zone NOT NULL DEFAULT now(),
  suspended timestamp with time zone,
  ceased timestamp with time zone,
  CONSTRAINT subscription_pk PRIMARY KEY (id),
  CONSTRAINT subscription_customer_fk FOREIGN KEY (customer)
  REFERENCES customer (id) MATCH SIMPLE
  ON UPDATE CASCADE ON DELETE RESTRICT,
  CONSTRAINT subscription_service_fk FOREIGN KEY (service)
  REFERENCES service (id) MATCH SIMPLE
  ON UPDATE CASCADE ON DELETE RESTRICT
)


CREATE TABLE dns_domain
(
  id serial NOT NULL,
  name character varying(256) NOT NULL,
  added_at timestamp with time zone NOT NULL DEFAULT now(),
  CONSTRAINT dns_domain_pk PRIMARY KEY (id),
  CONSTRAINT dns_domain_uk UNIQUE (name)
)


This simple link table works, but without maintaining any history:

CREATE TABLE subscribed_dns_domain
(
  subscription integer NOT NULL,
  dns_domain integer NOT NULL,
  CONSTRAINT subscribed_dns_domain_pk PRIMARY KEY (subscription, dns_domain),
  CONSTRAINT subscribed_dns_domain_dns_domain_fk FOREIGN KEY (dns_domain)
  REFERENCES dns_domain (id) MATCH SIMPLE
  ON UPDATE CASCADE ON DELETE RESTRICT,
  CONSTRAINT subscribed_dns_domain_subscription_fk FOREIGN KEY (subscription)
  REFERENCES subscription (id) MATCH SIMPLE
  ON UPDATE CASCADE ON DELETE RESTRICT
)


If I want to allow multiple instances of the same subscribed domain on
different dates (only 1 having a NULL deleted_at timestamp, how do I
do that? This doesn't work:

DROP TABLE subscribed_dns_domain;
CREATE TABLE subscribed_dns_domain
(
  subscription integer NOT NULL,
  dns_domain integer NOT NULL,
  added_at timestamp with time zone NOT NULL DEFAULT now(),
  deleted_at timestamp with time zone,
  customer_add_reference character varying(40),
  customer_delete_reference character varying(40),
  CONSTRAINT subscribed_dns_domain_pk PRIMARY KEY (subscription, dns_domain, 
added_at),
  CONSTRAINT subscribed_dns_domain_dns_domain_fk FOREIGN KEY (dns_domain)
  REFERENCES dns_domain (id) MATCH SIMPLE
  ON UPDATE CASCADE ON DELETE RESTRICT,
  CONSTRAINT subscribed_dns_domain_subscription_fk FOREIGN KEY (subscription)
  REFERENCES subscription (id) MATCH SIMPLE
  ON UPDATE CASCADE ON DELETE RESTRICT
)

Should I revert to the first subscribed_dns_domain table, and also have
a subscribed_dns_domain_history table, with timestamps populated by
triggers on the subscribed_dns_domain table? Not sure how to populate
customer reference/ticket numbers in there too...

How about partial unique indexes or EXCLUDE USING gist () on the above table?

"Note: The preferred way to add a unique constraint to a table is
ALTER TABLE ... ADD CONSTRAINT. The use of indexes to enforce unique
constraints could be considered an implementation detail that should not
be accessed directly."
>From http://www.postgresql.org/docs/9.2/static/indexes-unique.html

Thoughts/tutorial websites?
-- 
Craig Skinner | http://twitter.com/Craig_Skinner | http://linkd.in/yGqkv7


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql