PostgreSQL for MySQL Users: Unterschied zwischen den Versionen
Aus Geoinformation HSR
Stefan (Diskussion | Beiträge) (→MySQL's Unsigned integer (Cardinal)) |
Stefan (Diskussion | Beiträge) (→Data types of MySQL versus PostgreSQL) |
||
Zeile 52: | Zeile 52: | ||
== Data types of MySQL versus PostgreSQL == | == Data types of MySQL versus PostgreSQL == | ||
− | | Mysql | | PostgreSQL | | + | {| |
− | | Typ | Beschreibung | Typ | Beschreibung | | + | |- |
− | | tinyint | -128 | + | | Mysql || || PostgreSQL || |
− | | smallint | -32768 | + | |- |
− | | int | -2147483648 | + | | Typ || Beschreibung || Typ || Beschreibung |
− | | | | int8 | +/- 18 Dezimalstellen | | + | |- |
− | | float(n) | n E {4;8}, Einf./doppelte Genauig.| float4 | 6 Dez.Stellen| | + | | tinyint || -128..127 || - || - |
− | | | | float8 | 15 Dezimalstellen | | + | |- |
− | | date | YYYY-MM-DD Datumsformat | date | Datum, Datumformate mit SET DATESTYLE=Value einstellbar | | + | | smallint || -32768..+32767 || int2 || -32767 ... +32768 |
− | | time | HH-MM-SS Zeitformat | time | Uhrzeit, Auflösung 1 Mikrosekunde | | + | |- |
− | | char(m) | Zeichenkette mit fester Länge | char | ein Zeichen | | + | | int || -2147483648.. +2147483647 || int4 || -2147483648 ... +2147483647 |
− | | varchar(m) | variable Länge, max. m Zeichen | varchar(n) | 4+n Bytes | | + | |- |
− | | blob | Binary Large Object, wird für Texte gebraucht, "TINY", "", "LONG" | bytea | | + | | || || int8 || +/- 18 Dezimalstellen |
− | | | | money | 4 Byte, -21474836,48 ... +21474836,47 | | + | |- |
− | | | | text | Variable Länge| | + | | float(n) || n E {4;8}, Einf./doppelte Genauig.|| float4 || 6 Dez.Stellen |
− | | | | bool | Kann den Wert 't' oder 'f' annehmen | | + | |- |
+ | | || || float8 || 15 Dezimalstellen | ||
+ | |- | ||
+ | | date || YYYY-MM-DD Datumsformat || date || Datum, Datumformate mit SET DATESTYLE=Value einstellbar | ||
+ | |- | ||
+ | | time || HH-MM-SS Zeitformat || time || Uhrzeit, Auflösung 1 Mikrosekunde | ||
+ | |- | ||
+ | | char(m) || Zeichenkette mit fester Länge || char || ein Zeichen | ||
+ | |- | ||
+ | | varchar(m) || variable Länge, max. m Zeichen || varchar(n) || 4+n Bytes | ||
+ | |- | ||
+ | | blob || Binary Large Object, wird für Texte gebraucht, "TINY", "", "LONG" || bytea | ||
+ | |- | ||
+ | | || || money || 4 Byte, -21474836,48 ... +21474836,47 | ||
+ | |- | ||
+ | | || || text || Variable Länge | ||
+ | |- | ||
+ | | || || bool || Kann den Wert 't' oder 'f' annehmen | ||
+ | |} |
Version vom 13. September 2011, 20:07 Uhr
Siehe auch:
Licensing:
- MySQL: Dual Licensed
- PostgreSQL: BSD-Style
Inhaltsverzeichnis
MySQL's INSERT UPDATE
Option 1) Write a function like:
create function doinsert(_id integer, _value text) returns void as $$ begin update thetable set value = _value where id = _id; if not found then insert into thetable(id, value) values (_id, _value); end if end; $$ language plpgsql;
Option 2) Use two SQL statements:
-- update the existing update realTable set value = (select value from tmp where tmp.id = realTable.id) where exists (select value from tmp where tmp.id = realTable.id); -- insert the missing insert into realTable(id, value) select id, value from tmp where not exists(select 1 from realTable where tmp.id = realTable.id);
MySQL's AUTO INCREMENT
MySQL's DROP TABLE IF EXIST
- Existiert auch in PostgreSQL ab Version 8.2
- Bei früheren Versionen gibt es einen Workaround:
- % select count(*) from pg_class where relname = 'mytable'
- return 1 if mytable exists, and 0 if it doesn't
- so you can run a DROP TABLE command (or not) based on the returned value.
MySQL's INT UNSIGNED (Cardinal)
- MySQL: column INT UNSIGNED
- PostgreSQL: column INT CHECK (column > 0)
MySQL's Index-Typ
- MySQL: INDEX idx_id(id)
- PostgreSQL: CREATE INDEX idx_id ON TABLENAME(id);
MySQL's Query Resultset Limit
- MySQL: SELECT * FROM tbl LIMIT 10, 5
- PostgreSQL: SELECT * FROM tbl LIMIT 5 OFFSET 10
Data types of MySQL versus PostgreSQL
Mysql | PostgreSQL | ||
Typ | Beschreibung | Typ | Beschreibung |
tinyint | -128..127 | - | - |
smallint | -32768..+32767 | int2 | -32767 ... +32768 |
int | -2147483648.. +2147483647 | int4 | -2147483648 ... +2147483647 |
int8 | +/- 18 Dezimalstellen | ||
float(n) | n E {4;8}, Einf./doppelte Genauig. | float4 | 6 Dez.Stellen |
float8 | 15 Dezimalstellen | ||
date | YYYY-MM-DD Datumsformat | date | Datum, Datumformate mit SET DATESTYLE=Value einstellbar |
time | HH-MM-SS Zeitformat | time | Uhrzeit, Auflösung 1 Mikrosekunde |
char(m) | Zeichenkette mit fester Länge | char | ein Zeichen |
varchar(m) | variable Länge, max. m Zeichen | varchar(n) | 4+n Bytes |
blob | Binary Large Object, wird für Texte gebraucht, "TINY", "", "LONG" | bytea | |
money | 4 Byte, -21474836,48 ... +21474836,47 | ||
text | Variable Länge | ||
bool | Kann den Wert 't' oder 'f' annehmen |