473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

INSERT and UPDATE of ALLBALLS/INFINITY dates and MOVE COLUMNS

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
5 4408
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the...
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
6
by: pk | last post by:
Sorry for the piece-by-piece nature of this post, I moved it from a dormant group to this one and it was 3 separate posts in the other group. Anyway... I'm trying to bulk insert a text file of...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
2
by: Russell Smith | last post by:
Timestamps support infinity. However if appears dates do not. When timestamps are cast to dates, there is no output. Is this an acceptable option or not? Below are a number of examples...
3
by: teddysnips | last post by:
I need a trigger (well, I don't *need* one, but it would be optimal!) but I can't get it to work because it references ntext fields. Is there any alternative? I could write it in laborious code...
10
by: MLH | last post by:
Suppose, in a multi-user environment, you have append query SQL in a VBA procedure that looks like INSERT INTO MyTable... and the next line reads MyVar=DMax("","MyTable... You can never be...
0
by: mwenz | last post by:
I am trying to update an Access table using OLEDB in VB.Net 2005. I can add rows but I cannot update them. Code to instantiate the Access database and table... Dim conn As New...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.