简体   繁体   中英

Migrating from MySQL to psql

I have project which was working in MySQL.

It has hundreds of queries and also some table names and column names are in upper case that's why

query like

 select  * from TEST

would not work in pgSQL without quote.

So can anyone give me the solution which would not make change in all queries?

The easiest thing regarding table names is: Create tables without any quoting around them as in

create table "TEST"     -- BAD
create table TEST       -- good
create table test       -- even better

Also your queries should not contain any quotes around table names or column names.

select "TEST"."COLUMN" from "TEST"    -- BAD
select TEST.COLUMN from TEST          -- good
select test.column from test          -- even better

The last two versions are identical to PostgreSQL since unquoted identifiers are folded to lower case automatically.

Just just make sure there are no quotes everywhere (queries and in DDL) and be done with that part.

EDIT: When you have created tables using the quote syntax and they show up as upper case or mixed case in the psql shell, then you can rename the tables:

alter table "TEST" rename to TEST;    -- or "to test" - doesn't matter

Here is a quick way to generate the commands, which you have to copy&paste into a psql shell yourself:

select 
    'alter table "' || table_schema || '"."' || table_name || '" to ' ||
    lower(table_name) 
from information_schema.tables 
where table_type = 'BASE TABLE' and table_name != lower(table_name);

              ?column?               
-------------------------------------
 alter table "public"."TEST" to test

Rationale You have to use one standard for all your queries: Either all unquoted (then the real table names must have been folded to lowercase) or all quoted (then the real table names must match literally). Mixing is not possible withou MUCH pain.

Usually nobody makes the fuss and add quotes into hand-written queries, therefore my expectation is, that settling on this standard is less work than otherwise. This means, you have to rename your tables according to PostgreSQL best practices.

There isn't one. MySQL uses some syntax extension that are not compatible with SQL (so does PostgreSQL) and there are some major differences in queries that would be extremely hard to convert automatically (GROUP BY, DISTINCT).

TL;DR; you have no choice but to fix the queries manually and check they behave the exact same way (not a given).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM