By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,178 Members | 971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,178 IT Pros & Developers. It's quick & easy.

INSERT and UPDATE of ALLBALLS/INFINITY dates and MOVE COLUMNS

P: n/a
Hi,
I need some examples of INSERT and UPDATE of DATE columns using
'ALLBALLS' and 'INFINITY' values. I could not find examples on the net.

I need, also, to move columns in tables (just like the MOVE BEFORE/MOVE
AFTER MySQL commands). Can you help me?

Thank you!
Have a nice day,
--
Marco Lazzeri
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Marco Lazzeri writes:
I need some examples of INSERT and UPDATE of DATE columns using
'ALLBALLS' and 'INFINITY' values. I could not find examples on the net.
These values are only supported by the type timestamp, not by date.
I need, also, to move columns in tables (just like the MOVE BEFORE/MOVE
AFTER MySQL commands). Can you help me?


This is not possible in PostgreSQL. Your best bet is to drop the table
and recreate it. Alternatively, do surgery using ADD COLUMN and DROP
COLUMN.

--
Peter Eisentraut pe*****@gmx.net
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #2

P: n/a
On Thu, 6 Nov 2003, Marco Lazzeri wrote:
I need, also, to move columns in tables (just like the MOVE BEFORE/MOVE
AFTER MySQL commands). Can you help me?


I do it with select into:

begin;
select field3, field2, field4, field1 into newtable from oldtable;
drop oldtable;
alter table newtable rename to oldtable;
commit; (or rollback; if something goes wrong).
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #3

P: n/a
Il gio, 2003-11-06 alle 16:00, scott.marlowe ha scritto:
I need, also, to move columns in tables (just like the MOVE BEFORE/MOVE
AFTER MySQL commands). Can you help me?


I do it with select into:

begin;
select field3, field2, field4, field1 into newtable from oldtable;
drop oldtable;
alter table newtable rename to oldtable;
commit; (or rollback; if something goes wrong).


Good idea! But you'll lose CONSTRAINTs and DEFAULTs. Isn't it?

Cheers,
Marco
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #4

P: n/a
On Thu, 6 Nov 2003, Marco Lazzeri wrote:
Il gio, 2003-11-06 alle 16:00, scott.marlowe ha scritto:
I need, also, to move columns in tables (just like the MOVE BEFORE/MOVE
AFTER MySQL commands). Can you help me?


I do it with select into:

begin;
select field3, field2, field4, field1 into newtable from oldtable;
drop oldtable;
alter table newtable rename to oldtable;
commit; (or rollback; if something goes wrong).


Good idea! But you'll lose CONSTRAINTs and DEFAULTs. Isn't it?


Correct. Also any views based on the underlying table will no longer
work.

Then again, the order of fields in a table is pretty esoteric, so I'd
expect this to be a one time thing. Me personally, I just live with them
in the order they were created in mostly.
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #5

P: n/a
On Thu, Nov 06, 2003 at 05:01:54 -0300,
MaRcElO PeReIrA <ga********@yahoo.com.br> wrote:

$ select * from products;
prod_id | description
--------+---------------------
1 | S470DXBLM
12 | S470DXABM
33 | RG250DX
--------+---------------------
(3 rows)

and it is ok to me, but not to the users.


Instead of using the MAX aggregate function, or the SELECT with LIMIT
clause, another approach is to use a stored procedure to increment a
sequence counter column you keep in a separate table. I have a database
that has a "master-detail" type relationship between a supplier table and
an employee table (zero or more employees work for one supplier). And I
keep a separately-incremented employee primary key sequence for the
employees of each supplier. To do that I define a column in the supplier
table holding the value of the most-recently issued employee key value,
and increment that inside a stored procedure using a trigger when a new
employee is inserted for a given supplier.

The supplier table is defined in part as

CREATE TABLE supplier
(
supplier_pk int4 NOT NULL,
...
employee_seq int4 NOT NULL DEFAULT 0,
CONSTRAINT supplier_pkey PRIMARY KEY (supplier_pk)
);

The employee table is defined in part as

CREATE TABLE paid.employee
(
supplier_pk int4 NOT NULL,
employee_pk int4 NOT NULL,
...
CONSTRAINT employee_pkey PRIMARY KEY (supplier_pk, employee_pk),
);

The sequencing procedure looks like:

CREATE OR REPLACE FUNCTION employee_seq_next(int4)
RETURNS int4 AS
'
DECLARE
l_supplier_pk ALIAS FOR $1;
BEGIN
UPDATE supplier
SET
employee_seq = (employee_seq + 1)
WHERE (supplier_pk = l_supplier_pk);

RETURN (SELECT employee_seq FROM supplier
WHERE (supplier_pk = l_supplier_pk));
END;'
LANGUAGE 'plpgsql' VOLATILE;

and the trigger procedure which calls the sequencing function looks like

CREATE OR REPLACE FUNCTION employee_bit()
RETURNS trigger AS
'
BEGIN
if new.employee_pk IS NULL THEN
SELECT INTO NEW.employee_pk employee_seq_next(new.supplier_pk);
END IF;
RETURN new;
END;
'
LANGUAGE 'plpgsql' VOLATILE;
I'm told that doing the UPDATE first inside the trigger creates a lock on
the supplier table until the trigger transaction completes, so (I would
suppose, but I'm not expert enough to assert this for sure that) this
would assure you of getting one sequence increment at a time.

This seems like a workable paradigm which I used in other cases as well.

Still end up with holes in the sequence, though, if an employee row is
deleted, for example. Using the MAX function or LIMIT clauses would
protect against that in the cases where the most-recently-added employee
row were deleted.

Something else you can do, is define all your foreign key constraints
with the ON UPDATE CASCADE clause, so that you can manually change your
primary key values to fill in the holes.

~Berend Tober


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

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.