PostgreSQL "modi stru"

Alles zum SQL-Dialekt

Moderator: Moderatoren

Antworten
Benutzeravatar
AUGE_OHR
Marvin
Marvin
Beiträge: 12903
Registriert: Do, 16. Mär 2006 7:55
Wohnort: Hamburg
Hat sich bedankt: 19 Mal
Danksagung erhalten: 44 Mal

PostgreSQL "modi stru"

Beitrag von AUGE_OHR »

hi,

in PostgreSQL kann ich so

Code: Alles auswählen

cQuery += "CREATE TABLE " + cTable + " ( "
   // Fields
cQuery += " )"
eine neue Table anlegen.

wenn ich ein neues Field "anhängen" will geht das mit

Code: Alles auswählen

cQuery += "ALTER TABLE " + cTable + " ADD COLUMN "
   // Fields
cQuery += " )"
wenn ich ein Field "umbenennen" will, muss ich wie bei Xbase++, den "Inhalt" "um-schaufeln"

ich möchte nun nur die Field "Länge" ändern ... z.b. "N" 10,2 auf 12,3 ... wie mache ich das ?
gruss by OHR
Jimmy
georg
Der Entwickler von "Deep Thought"
Der Entwickler von "Deep Thought"
Beiträge: 2823
Registriert: Fr, 08. Feb 2008 21:29
Hat sich bedankt: 95 Mal
Danksagung erhalten: 13 Mal

Re: PostgreSQL "modi stru"

Beitrag von georg »

Guten Morgen, Jimmy -


ein Blick in die Dokumentation fördert folgendes zutage:

5.5.6. Changing a Column's Data Type

To convert a column to a different data type, use a command like:

ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);

This will succeed only if each existing entry in the column can be converted to the new type by an implicit cast. If a more complex conversion is needed, you can add a USING clause that specifies how to compute the new values from the old.

PostgreSQL will attempt to convert the column's default value (if any) to the new type, as well as any constraints that involve the column. But these conversions might fail, or might produce surprising results. It's often best to drop any constraints on the column before altering its type, and then add back suitably modified constraints afterwards.
Zu finden hier: http://www.postgresql.org/docs/9.1/stat ... alter.html

HTH,


Georg
Liebe Grüsse aus der Eifel,

Georg S. Lorrig
Redakteur der Wiki des Deutschprachigen Xbase-Entwickler e.V.
Benutzeravatar
AUGE_OHR
Marvin
Marvin
Beiträge: 12903
Registriert: Do, 16. Mär 2006 7:55
Wohnort: Hamburg
Hat sich bedankt: 19 Mal
Danksagung erhalten: 44 Mal

Re: PostgreSQL "modi stru"

Beitrag von AUGE_OHR »

georg hat geschrieben:Guten Morgen, Jimmy -
ein Blick in die Dokumentation fördert folgendes zutage:
5.5.6. Changing a Column's Data Type

To convert a column to a different data type, use a command like:

ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);
Zu finden hier: http://www.postgresql.org/docs/9.1/stat ... alter.html
DANKE !!! =D>

in meinem PG Handbuch steht es leider nicht :banghead:

Code: Alles auswählen

ALTER TABLE [ ONLY ] tabelle [ * ]
ADD [ COLUMN ] spalte typ [ spalten_constraint [ ... ] ]
ALTER TABLE [ ONLY ] tabelle [ * ]
DROP [ COLUMN ] spalte [ RESTRICT | CASCADE ]
ALTER TABLE [ ONLY ] tabelle [ * ]
ALTER [ COLUMN ] spalte { SET DEFAULT wert | DROP DEFAULT }
ALTER TABLE [ ONLY ] tabelle [ * ]
ALTER [ COLUMN ] spalte { SET | DROP } NOT NULL
ALTER TABLE [ ONLY ] tabelle [ * ]
ALTER [ COLUMN ] spalte SET STATISTICS ganze_zahl
ALTER TABLE [ ONLY ] tabelle [ * ]
ALTER [ COLUMN ] spalte SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
ALTER TABLE [ ONLY ] tabelle [ * ]
RENAME [ COLUMN ] spalte TO neue_spalte
ALTER TABLE tabelle
RENAME TO neue_tabelle
ALTER TABLE [ ONLY ] tabelle [ * ]
ADD tabellen_constraint
ALTER TABLE [ ONLY ] tabelle [ * ]
DROP CONSTRAINT constraint_name [ RESTRICT | CASCADE ]
ALTER TABLE tabelle
OWNER TO neuer_eigentümer
es ist keine Option "TYPE" aufgeführt und ich konnte unter "ALTER TABLE" auch keinen Hinweis finden.
hm ... "TYPE" als Suchbegriff finde ich nur "CREATE TYPE" und ""FOR TYPE" ... und etwas mit "%TYPE"
gruss by OHR
Jimmy
georg
Der Entwickler von "Deep Thought"
Der Entwickler von "Deep Thought"
Beiträge: 2823
Registriert: Fr, 08. Feb 2008 21:29
Hat sich bedankt: 95 Mal
Danksagung erhalten: 13 Mal

Re: PostgreSQL "modi stru"

Beitrag von georg »

Hallo, Jimmy -


das ist der Stand PostgreSQL 9.1.


Gruss,

Georg
Liebe Grüsse aus der Eifel,

Georg S. Lorrig
Redakteur der Wiki des Deutschprachigen Xbase-Entwickler e.V.
Benutzeravatar
AUGE_OHR
Marvin
Marvin
Beiträge: 12903
Registriert: Do, 16. Mär 2006 7:55
Wohnort: Hamburg
Hat sich bedankt: 19 Mal
Danksagung erhalten: 44 Mal

Re: PostgreSQL "modi stru"

Beitrag von AUGE_OHR »

georg hat geschrieben:das ist der Stand PostgreSQL 9.1.
hehehe ... also "UPGRADE" fahren ... mal sehen was pgDBE dazu sagt. :badgrin:
gruss by OHR
Jimmy
Benutzeravatar
AUGE_OHR
Marvin
Marvin
Beiträge: 12903
Registriert: Do, 16. Mär 2006 7:55
Wohnort: Hamburg
Hat sich bedankt: 19 Mal
Danksagung erhalten: 44 Mal

Re: PostgreSQL "modi stru"

Beitrag von AUGE_OHR »

hi,

als Newbie frage ich mich : reichen bei SQL Table die 4 Felder Name,Type,Len,Dec ?

ich meine ... hat vielleicht SQL "noch was" was man in einem Data-Dic aufnehmen sollte ?
gruss by OHR
Jimmy
georg
Der Entwickler von "Deep Thought"
Der Entwickler von "Deep Thought"
Beiträge: 2823
Registriert: Fr, 08. Feb 2008 21:29
Hat sich bedankt: 95 Mal
Danksagung erhalten: 13 Mal

Re: PostgreSQL "modi stru"

Beitrag von georg »

Hallo, Jimmy -


Standard SQL sieht vor:

Feldname
Feldattribut (also INT, FLOAT, TEXT, CHAR(nn), etc.)

Weitere Spezifikationen wie z.B. eine Beschreibung des Feldes, Standard-Überschrift für Spalten (z.B. in Browses) etc. sind immer eigene Implemtierungen des jeweiligen Servers.

Aus diesem Grund habe ich eine Tabelle syslayout, die ich als Data Dictionnary verwende:

Code: Alles auswählen

mysql> describe syslayout;
+----------------+----------+------+-----+---------+-------+
| Field          | Type     | Null | Key | Default | Extra |
+----------------+----------+------+-----+---------+-------+
| slname         | char(32) | NO   | PRI |         |       |
| slsequence     | int(11)  | NO   | PRI | 0       |       |
| slfield        | text     | YES  |     | NULL    |       |
| slfielddef     | char(16) | YES  |     | NULL    |       |
| slbrowseq      | int(11)  | YES  |     | NULL    |       |
| slbrowseheader | text     | YES  |     | NULL    |       |
+----------------+----------+------+-----+---------+-------+
6 rows in set (0.03 sec)

Code: Alles auswählen

mysql> select * from syslayout where slname = 'XLateCP' order by slname, slsequence;
+---------+------------+------------------------------+------------+-----------+----------------+
| slname  | slsequence | slfield                      | slfielddef | slbrowseq | slbrowseheader |
+---------+------------+------------------------------+------------+-----------+----------------+
| XLateCP |         10 | XLPrio                       | CHAR(1)    |        10 | xlate prio     |
| XLateCP |         20 | XLBase                       | INT        |        20 | start values   |
| XLateCP |         30 | XLUniCode                    | CHAR(9)    |        30 | UniCode value  |
| XLateCP |         40 | XLWestern                    | INT        |        40 | Western value  |
| XLateCP |         50 | XLCode                       | CHAR(1)    |        50 | displayed as   |
| XLateCP |       9000 | PRIMARY KEY (XLPrio, XLBase) |            |         0 |                |
+---------+------------+------------------------------+------------+-----------+----------------+
6 rows in set (0.00 sec)
Wobei das noch "Work in Progress" ist.

Wenn ich diese Tabelle anzeige, lese ich die syslayout und entnehme ihr, welche Felder im Browse angezeigt werden sollen, und wie die Spaltenüberschriften sein sollen.

Analog kann ich prüfen, ob der definierte Aufbau der vorhandenen entspricht, und gegebenenfalls ein ALTER TABLE ausführen.


Gruss,

Georg
Liebe Grüsse aus der Eifel,

Georg S. Lorrig
Redakteur der Wiki des Deutschprachigen Xbase-Entwickler e.V.
Benutzeravatar
AUGE_OHR
Marvin
Marvin
Beiträge: 12903
Registriert: Do, 16. Mär 2006 7:55
Wohnort: Hamburg
Hat sich bedankt: 19 Mal
Danksagung erhalten: 44 Mal

Re: PostgreSQL "modi stru"

Beitrag von AUGE_OHR »

georg hat geschrieben:Standard SQL sieht vor:

Feldname
Feldattribut (also INT, FLOAT, TEXT, CHAR(nn), etc.)

Weitere Spezifikationen wie z.B. eine Beschreibung des Feldes, Standard-Überschrift für Spalten (z.B. in Browses) etc. sind immer eigene Implemtierungen des jeweiligen Servers.
OK ... Feldattribut ... ich werde mal suchen
georg hat geschrieben:Aus diesem Grund habe ich eine Tabelle syslayout, die ich als Data Dictionnary verwende:
...

Wobei das noch "Work in Progress" ist.

Wenn ich diese Tabelle anzeige, lese ich die syslayout und entnehme ihr, welche Felder im Browse angezeigt werden sollen, und wie die Spaltenüberschriften sein sollen.

Analog kann ich prüfen, ob der definierte Aufbau der vorhandenen entspricht, und gegebenenfalls ein ALTER TABLE ausführen.
JAJAJA genau so was ist es was ich damit machen will ;)

Frage : wenn ich so eine Table und "function" auf einem SQL Server "ablege" kann man dann mit den "Command Tools", wie PgAdmin.EXE , die direkt ansprechen als wenn ich eine Client Application hätte ?

ich meine damit das du ja

Code: Alles auswählen

select * from syslayout where slname = 'XLateCP' order by slname, slsequence;
verwendest. wenn das "SELECT " nun mit einer "function" arbeiten würde ...

Code: Alles auswählen

CREATE FUNCTION Net_Use(who) 
...
RETURN cString

select * from syslayout where Net_Use(who)
kann man in SQL "function" so etwas machen ?
gruss by OHR
Jimmy
Benutzeravatar
AUGE_OHR
Marvin
Marvin
Beiträge: 12903
Registriert: Do, 16. Mär 2006 7:55
Wohnort: Hamburg
Hat sich bedankt: 19 Mal
Danksagung erhalten: 44 Mal

Re: PostgreSQL "modi stru"

Beitrag von AUGE_OHR »

hi,
georg hat geschrieben:
ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);
ich habe jetzt doch die "schrittweise" Method verwendet.

Code: Alles auswählen

cQuery := "ALTER TABLE " + cTable + " ADD COLUMN "
cQuery += cPguuuuuu

// hier die Fields je nach Type
      cQuery += " character(" + ALLTRIM( STR( aWorkStru[ i, 3 ] ) ) + "), "
      cQuery += " numeric(" + ALLTRIM( STR( aWorkStru[ i, 3 ] ) ) + ',' + ALLT
      cQuery += " date, "
      cQuery += " text, "
      cQuery += " boolean, "
      cQuery := SUBSTR( cQuery, 1, LEN(cQuery) - 2 )

oMain:OutMsg(cQuery)
oPG:exec( cQuery )
   cQuery := "UPDATE "+ cTable + " SET "+cPguuuuuu+"="+cFeld

   oPG:exec( cQuery )
      cQuery := "ALTER TABLE "+ cTable + " DROP COLUMN "+cFeld

      oPG:exec( cQuery )
         cQuery := "ALTER TABLE "+ cTable + " RENAME "+cPguuuuuu+" TO "+cFeld

         oPG:exec( cQuery )
damit werden auf jeden Fall die vorhandenen Daten "dupliziert".
einziger "Nachteil" : ein neues Field wird ja "hinten" angehängt ...
gruss by OHR
Jimmy
Antworten