「Oracle」数据库字符集编码修改

作者:threedayman
来源:恒生LIGHT云社区
背景 项目默认需要使用ZHS16GBK,当使用UTF8进行编码时,会出现插入数据超过字段长度限制问题。
原因:gbk编码中文字符占用两个字节,utf8编码中文字符时占用三个字节。
为了使初始化脚本能够正常运行,需要将数据库UTF8的编码改成ZHS16GBK编码。
修改步骤 通过以下命令连接到数据库
bash-4.2$ sqlplus / as sysdbaSQL*Plus: Release 19.0.0.0.0 - Production on Thu Dec 9 19:17:39 2021 Version 19.3.0.0.0Copyright (c) 1982, 2019, Oracle.All rights reserved.Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production Version 19.3.0.0.0

查看当前使用的字符编码
SQL>select userenv('language') from dual; USERENV('LANGUAGE') ---------------------------------------------------- AMERICAN_AMERICA.AL32UTF8

【「Oracle」数据库字符集编码修改】关闭数据库
SQL> shutdown immediate; Database closed. Database dismounted. ORACLE instance shut down.

启动
SQL> startup mount ORACLE instance started.Total System Global Area 2.0267E+10 bytes Fixed Size12684864 bytes Variable Size3355443200 bytes Database Buffers1.6844E+10 bytes Redo Buffers54423552 bytes Database mounted.

修改
SQL> alter system enable restricted session; System altered.SQL> alter system set job_queue_processes=0; System altered.SQL> alter system set aq_tm_processes=0; System altered.SQL> alter database open; Database altered.

当运行修改字符集的命令时,出现错误提示,新的字符集需要时原先字符集的超集。
SQL> ALTER DATABASE CHARACTER SET ZHS16GBK; ALTER DATABASE CHARACTER SET ZHS16GBK * ERROR at line 1: ORA-12712: new character set must be a superset of old character set

跳过超集检查设置字符集
SQL> ALTER DATABASE character set INTERNAL_USE ZHS16GBK; Database altered.

关闭重启
SQL> shutdown immediate; Database closed. Database dismounted. ORACLE instance shut down. SQL> startup ORACLE instance started.Total System Global Area 2.0267E+10 bytes Fixed Size12684864 bytes Variable Size3355443200 bytes Database Buffers1.6844E+10 bytes Redo Buffers54423552 bytes Database mounted. Database opened. SQL> select userenv('language') from dual; USERENV('LANGUAGE') ---------------------------------------------------- AMERICAN_AMERICA.ZHS16GBK

通过查看当前字符集已经修改为ZHS16GBK。
注:生产环境字符集不要随意进行修改,以免造成不可挽回的损失。
想向技术大佬们多多取经?开发中遇到的问题何处探讨?如何获取金融科技海量资源?
恒生LIGHT云社区,由恒生电子搭建的金融科技专业社区平台,分享实用技术干货、资源数据、金融科技行业趋势,拥抱所有金融开发者。
扫描下方小程序二维码,加入我们!
「Oracle」数据库字符集编码修改
文章图片

    推荐阅读