简体   繁体   English

从MySQL迁移到psql

[英]Migrating from MySQL to psql

I have project which was working in MySQL. 我有在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. 没有引号将无法在pgSQL中工作。

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. 最后两个版本与PostgreSQL相同,因为未加引号的标识符会自动折叠为小写。

Just just make sure there are no quotes everywhere (queries and in DDL) and be done with that part. 只要确保在所有地方 (查询 DDL中)都没有引号,然后完成该部分即可。

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: 编辑:使用引号语法创建表并且它们在psql shell中显示为大写或大小写混合时,则可以重命名表:

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: 这是一种生成命令的快速方法,您必须自己将其复制并粘贴到psql shell中:

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. 混合是不可能withou 多大的痛苦。

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. 这意味着,您必须根据PostgreSQL最佳实践重命名表。

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). MySQL使用与SQL不兼容的某些语法扩展(PostgreSQL也是如此),并且查询中存在一些主要差异,这些差异很难自动转换(GROUP BY,DISTINCT)。

TL;DR; TL; DR; you have no choice but to fix the queries manually and check they behave the exact same way (not a given). 您别无选择,只能手动修复查询并检查其行为是否完全相同(不是给定的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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