Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > PostgreSQL > [PSQL] Copy Data from Table to Table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-08-08, 04:54
Helios Co. Helios Co. is offline
Registered User
 
Join Date: Jan 2008
Posts: 1
[PSQL] Copy Data from Table to Table

Hello @ all,

as a nube in DB-Stuff i hope u can help me.
I need to move Data from one table (not the complete table, just one or morecolumns) to another (in the same DB).

INSERT...SELECT... didnt work.
COPY... also didnt work, it seems that COPY just works with files and one table
(moving data from file to table)

So a little more concretely:

I have two tables. The one (sourse) hase some Columns which i need in another table.
In this second table are also columns which shall be set to NULL, in this "copy-process".

THX
and sorry for my poor english
Reply With Quote
  #2 (permalink)  
Old 01-08-08, 04:59
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 654
Quote:
Originally Posted by Helios Co.
INSERT...SELECT... didnt work.
Without showing the SQL that you used, no one will be able to help.
Copying from one table to another using INSERT ... SELECT will work, even if you need to insert NULL values.
Reply With Quote
  #3 (permalink)  
Old 01-08-08, 12:00
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,359
First, get the select query working. Then, append it to the insert statement as the subquery.

(and, as shammat said, POST the non-working SQL.)

Without seeing the SQL, and as a pure guess - PostgreSQL IS case sensitive in regards to object naming, with a default of lower case.
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert


Last edited by loquin : 01-08-08 at 12:06.
Reply With Quote
  #4 (permalink)  
Old 01-08-08, 13:56
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 654
Quote:
Originally Posted by loquin
PostgreSQL IS case sensitive in regards to object naming, with a default of lower case.
But only if you quote the objects (e.g. "TheTable") which is compliant with the ANSI Standard
Reply With Quote
  #5 (permalink)  
Old 01-08-08, 14:38
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,359
yes, but if you don't quote a mixed/upper case object name, pg sql won't find it.

Other databases, and many programming languages are either case insensitive, may be configured to automatically fold the case, or may be configured to be case insensitive.
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
  #6 (permalink)  
Old 01-08-08, 17:36
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 654
Quote:
Originally Posted by loquin
yes, but if you don't quote a mixed/upper case object name, pg sql won't find it.
Only if you created it with quotes.
Quote:
Other databases, and many programming languages are either case insensitive, may be configured to automatically fold the case, or may be configured to be case insensitive.
That might be true, but the ANSI standard requires that identifiers are case-insensitive when not quoted and case sensitive when beeing quoted.
And that's exactly what PG does.

The only thing where PG is not compliant is it's folding to lower case. The ANSI standard requires a folding to uppercase when not quoted.

So there is nothing special about the way PG handles identifiers.
If a DBMS is always case sensitive it simply is non-standard.

As a rule of thumg one should simply never use (double) quotes in SQL statements and the statements will run happily on each standard-compliant DBMS.
Reply With Quote
  #7 (permalink)  
Old 09-17-08, 14:28
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,359
Quote:
Originally Posted by shammat
...
That might be true, but the ANSI standard requires that identifiers are case-insensitive when not quoted and case sensitive when beeing quoted.
And that's exactly what PG does....

I have a table named Test in schema sample, created with pgadmin. (and, if you hadn't created it with quotes, or with pgAdmin, it wouldn't be mixed case, since it gets folded to lower case if you don't quote it...)

Code:
Select * from sample.Test
produces
Quote:
Originally Posted by pg
ERROR: relation "sample.test" does not exist

********** Error **********

ERROR: relation "sample.test" does not exist
SQL state: 42P01
However,

Code:
Select * from sample."Test"

works fine.

So, 'case insensitive when not quoted' does NOT seem to apply to pg... If it were, it would find the mixed-case object. pg simply folds the identifier name to lower-case before it queries. IF pg were case insensitive, it would effectively fold both object name and identifier name to the same case before querying. Instead, it folds the identifier name only. Obviously, this will NOT result in a 'case insensitive' query.
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert


Last edited by loquin : 09-17-08 at 16:37.
Reply With Quote
  #8 (permalink)  
Old 09-17-08, 19:08
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 654
Quote:
Originally Posted by loquin
So, 'case insensitive when not quoted' does NOT seem to apply to pg...
Of course it does.
The way the object is created defines whether it's case sensitive or not.
And PGAdmin always creates the tables with quotes, so any object created with PGAdmin will be created in a case-sensitive manner.

The standard defines that any object created with quotes is case sensitive. Any object created without quotes is case-insensitive.

And that's precisely the way PG works. Just like Oracle, DB/2, Derby, Firebird, HSQLDB, H2. Even MySQL and MS SQL Server to some extent.

In MySQL it depends on the storage engine and the oparating system whether non-quoted names are case sensitive (MyISAM with *nix means case sensitive name always). And with SQL Server it depends on the collation defined for the database. So actually there you can change the behaviour on a daily basis if you feel like it.


Apart from those two (but they don't care about standards anyway) I don't know any DBMS that does not follow the ANSI standard.

The only place where PG violates the standard, is that it folds the names to lower case whereas the standard requires upper case.

The standard requires the following:

CREATE TABLE test (...);
SELECT * FROM TeST --> works
SELECT * FROM TEST --> works;

CREATE TABLE "Test" (...)
SELECT * FROM test --> should not work
SELECT * FROM TEST --> should not work
SELECT * FROM "Test" --> has to work.

PG violates the following rule:

CREATE TABLE test (...)
SELECT * FROM "TEST" --> should work according to the standard
Reply With Quote
  #9 (permalink)  
Old 09-18-08, 16:24
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,359
shammat, although it may meet the letter of the ansi spec, IMO, if pg were truly case insensitive, when querying, it would find the ANY case, upper, lower, or mixed, of an object when you do not quote the identifier. If you DO quote the identifier, then that should indicate that you need to find an object explicitly matching the exact case you have specified. i.e. assuming table name of "Test" (created with quotes)

Select * from test
Select * from TEST
Select * from tesT
Select * From "Test"

should all find the table, but

Select * from "TEst"

should not.

And, in looking back at the original problem, THIS is what I was pointing out to the OP... if the objects were created with mixed cased naming, you would need to match the case exactly when querying, else pg will not find the object....
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert


Last edited by loquin : 09-18-08 at 16:47.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On