pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
mysql's "replace into..."
4+ messages / 2 participants
[nested] [flat]

* mysql's "replace into..."
@ 2001-02-28 18:40 Diehl, Jeffrey <jdiehl@sandia.gov>
  2001-02-28 19:56 ` Re: mysql's "replace into..." Josh Berkus <josh@agliodbs.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Diehl, Jeffrey @ 2001-02-28 18:40 UTC (permalink / raw)
  To: pgsql-sql

Hi all,

I'm in the process of migrating a production database and related
applicatoins from mysql to postgresql.  I've just not been very impressed
with mysql's stability, that is I'm trying to make my phone stop ringing.

However, when I wrote the applications, I took free advantage of mysql's
replace into feature which behaves like a smart insert that inserts new
records or updates old records.  I'm using perl DBI and need to figure out a
good way to migrate this to postgresql. 

Any ideas?

Mike Diehl.




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: mysql's "replace into..."
  2001-02-28 18:40 mysql's "replace into..." Diehl, Jeffrey <jdiehl@sandia.gov>
@ 2001-02-28 19:56 ` Josh Berkus <josh@agliodbs.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Josh Berkus @ 2001-02-28 19:56 UTC (permalink / raw)
  To: Diehl, Jeffrey <jdiehl@sandia.gov>; +Cc: pgsql-sql

Jeff,

> I'm in the process of migrating a production database and related
> applicatoins from mysql to postgresql.  I've just not been very impressed
> with mysql's stability, that is I'm trying to make my phone stop ringing.

Surprising.  I have a number of gripes with MySQL (of which REPLACE INTO
is one), but stability isn't one of them.

> However, when I wrote the applications, I took free advantage of mysql's
> replace into feature which behaves like a smart insert that inserts new
> records or updates old records.  I'm using perl DBI and need to figure out a
> good way to migrate this to postgresql.
> 
> Any ideas?

Well, you have two choices.  One is to get used to standard SQL and use
the SQL I will expound below.  The other is to actually hire a C
programmer to modify the PostgreSQL source to support Replace Into. 
Great Bridge might be willing to help.

Regardless, your SQL functionality requires the following steps:

1. All tables involved *must* have primary keys.  I reccommend against
using the OID for this purpose for several reasons; see the archives for
a discussion.

2. Build an if-then stucture for your update calls, that goes like this
(this assumes that your primary key is SERIAL):
	
IF primarykey_variable = 0 THEN
	INSERT INTO table1 (field1, field2, field3)
	VALUES (variable1, variable2, variable3);
ELSE
	UPDATE table1 SET field1 = variable1,
		field2 = variable2,
		field3 = variable3
	WHERE primarykey = primarykey_variable;
END IF;

-Josh Berkus

-- 
______AGLIO DATABASE SOLUTIONS___________________________
                                        Josh Berkus
   Complete information technology      josh@agliodbs.com
    and data management solutions       (415) 565-7293
   for law firms, small businesses       fax  621-2533
    and non-profit organizations.       San Francisco



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* RE: mysql's "replace into..."
@ 2001-02-28 20:40 Diehl, Jeffrey <jdiehl@sandia.gov>
  2001-02-28 22:06 ` Re: mysql's "replace into..." Josh Berkus <josh@agliodbs.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Diehl, Jeffrey @ 2001-02-28 20:40 UTC (permalink / raw)
  To: 'Josh Berkus ' <josh@agliodbs.com>; Diehl, Jeffrey <jdiehl@sandia.gov>; +Cc: pgsql-sql

Thanx for your help.  I found replace into to be a nice convience since my
primary keys are often expressions build from several fields which I want to
keep unique.  Replace into enforces this uniqueness.  I was learning SQL
when I started this project so I didn't know this was non-standard.  Bummer.

But since my primary keys are expressions, I can't use the technique you
suggested.  I'm thinking of simply trying the insert and checking to see if
I get an error.  If I get an error, I do an update...  Not sure how well
this will work, tho.

Any suggestions?

Thanx for your time.

-----Original Message-----
From: Josh Berkus
To: Diehl, Jeffrey
Cc: 'pgsql-sql@postgreSQL.org'
Sent: 2/28/01 12:56 PM
Subject: Re: [SQL] mysql's "replace into..."

Jeff,

> I'm in the process of migrating a production database and related
> applicatoins from mysql to postgresql.  I've just not been very
impressed
> with mysql's stability, that is I'm trying to make my phone stop
ringing.

Surprising.  I have a number of gripes with MySQL (of which REPLACE INTO
is one), but stability isn't one of them.

> However, when I wrote the applications, I took free advantage of
mysql's
> replace into feature which behaves like a smart insert that inserts
new
> records or updates old records.  I'm using perl DBI and need to figure
out a
> good way to migrate this to postgresql.
> 
> Any ideas?

Well, you have two choices.  One is to get used to standard SQL and use
the SQL I will expound below.  The other is to actually hire a C
programmer to modify the PostgreSQL source to support Replace Into. 
Great Bridge might be willing to help.

Regardless, your SQL functionality requires the following steps:

1. All tables involved *must* have primary keys.  I reccommend against
using the OID for this purpose for several reasons; see the archives for
a discussion.

2. Build an if-then stucture for your update calls, that goes like this
(this assumes that your primary key is SERIAL):
	
IF primarykey_variable = 0 THEN
	INSERT INTO table1 (field1, field2, field3)
	VALUES (variable1, variable2, variable3);
ELSE
	UPDATE table1 SET field1 = variable1,
		field2 = variable2,
		field3 = variable3
	WHERE primarykey = primarykey_variable;
END IF;

-Josh Berkus

-- 
______AGLIO DATABASE SOLUTIONS___________________________
                                        Josh Berkus
   Complete information technology      josh@agliodbs.com
    and data management solutions       (415) 565-7293
   for law firms, small businesses       fax  621-2533
    and non-profit organizations.       San Francisco




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: mysql's "replace into..."
  2001-02-28 20:40 RE: mysql's "replace into..." Diehl, Jeffrey <jdiehl@sandia.gov>
@ 2001-02-28 22:06 ` Josh Berkus <josh@agliodbs.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Josh Berkus @ 2001-02-28 22:06 UTC (permalink / raw)
  To: Diehl, Jeffrey <jdiehl@sandia.gov>; pgsql-sql

Jeff,

> Thanx for your help.  I found replace into to be a nice convience since my
> primary keys are often expressions build from several fields which I want to
> keep unique.  Replace into enforces this uniqueness.  I was learning SQL
> when I started this project so I didn't know this was non-standard.  Bummer.

No, what you're describing are called "composite keys".  They are a
standard part of SQL, but most developers end up using numerical
surrogate keys because they are easier to handle.

> But since my primary keys are expressions, I can't use the technique you
> suggested.  I'm thinking of simply trying the insert and checking to see if
> I get an error.  If I get an error, I do an update...  Not sure how well
> this will work, tho.

Better to do it the other way. Search for that key; if it's returned, do
an update; if the results are NULL, do an insert.  If your table has few
fields (<15) you can even do this through a function, passing the field
values as parameters of the function.

					-Josh Berkus

-- 
______AGLIO DATABASE SOLUTIONS___________________________
                                        Josh Berkus
   Complete information technology      josh@agliodbs.com
    and data management solutions       (415) 565-7293
   for law firms, small businesses       fax  621-2533
    and non-profit organizations.       San Francisco



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2001-02-28 22:06 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-02-28 18:40 mysql's "replace into..." Diehl, Jeffrey <jdiehl@sandia.gov>
2001-02-28 19:56 ` Josh Berkus <josh@agliodbs.com>
2001-02-28 20:40 RE: mysql's "replace into..." Diehl, Jeffrey <jdiehl@sandia.gov>
2001-02-28 22:06 ` Josh Berkus <josh@agliodbs.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox