MySQL to PostgreSQL table create conversion - charset and collation

别等时光非礼了梦想. 提交于 2019-12-23 02:49:27

问题


I want to migrate from MySQL to PostgreSQL.My query for create table is like this.

CREATE TABLE IF NOT EXISTS conftype 
(
  CType char(1) NOT NULL,
  RegEx varchar(300) default NULL,  
  ErrStr varchar(300) default NULL,
  Min integer default NULL,
  Max integer default NULL, 
  PRIMARY KEY (CType)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin;

What is the converted form of this query. I am confused with DEFAULT CHARSET=latin1 COLLATE=latin1_bin part. How can I convert this part?


回答1:


That one would mean that the table uses only latin-1 (iso-8859-1) character set and latin-1 binary sorting order. In PostgreSQL the character set is database-wide, there is no option to set it on table level.

You could create a mostly compatible database with:

 CREATE DATABASE databasenamegoeshere WITH ENCODING 'LATIN1' LC_COLLATE='C'
     LC_CTYPE='C' TEMPLATE=template0;

However, I personally would consider a MySQL->PostgreSQL port also worthy of switching to UTF-8/Unicode.




回答2:


The character set is defined when you create the database, you can't overwrite that per table in Postgres.

A non-standard collation can be defined only on column level in Postgres, not on table level. I think(!) that the equivalent to latin1_bin in MySQL would be the "C" collation in Postgres.

So if you do need a different collation, you need something like this

RegEx varchar(300) default NULL collate "C",  
ErrStr varchar(300) default NULL collate "C",

min and max are reserved wordds in SQL and you shouldn't use them as column names (although using them as column names will work I strongly suggest you find different names to avoid problems in the future)



来源:https://stackoverflow.com/questions/35794722/mysql-to-postgresql-table-create-conversion-charset-and-collation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!