Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a Character Set for a column For oracle database tables

I am running following query in SQL*Plus

CREATE TABLE  tbl_audit_trail (
  id NUMBER(11) NOT NULL,
  old_value varchar2(255) NOT NULL,
  new_value varchar2(255) NOT NULL,
  action varchar2(20) CHARACTER SET latin1 NOT NULL,
  model varchar2(255) CHARACTER SET latin1 NOT NULL,
  field varchar2(64) CHARACTER SET latin1 NOT NULL,
  stamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  user_id NUMBER(11) NOT NULL,
  model_id varchar2(65) CHARACTER SET latin1 NOT NULL,
  PRIMARY KEY (id),  
  KEY idx_action (action)
);

I am getting following error:

action varchar2(20) CHARACTER SET latin1 NOT NULL,
                      *
ERROR at line 5:
ORA-00907: missing right parenthesis

Can you suggest what am I missing?

like image 764
Ashwin N Bhanushali Avatar asked Mar 31 '12 13:03

Ashwin N Bhanushali


People also ask

How do I select a character set for a database?

Answer. The database character set value of an Oracle database can be determined by running the following command in Oracle's SQL*Plus or PDSQL: select * from NLS_DATABASE_PARAMETERS where parameter='NLS_CHARACTERSET'; The Oracle character set must be compatible with the client code page that ClearQuest is utilizing.

What is character set in Oracle database?

Oracle Database uses character sets for the following: Data stored in SQL character data types ( CHAR , VARCHAR2 , CLOB , and LONG ). Identifiers such as table names, column names, and PL/SQL variables. Stored SQL and PL/SQL source code, including text literals embedded in this code.

Can we change character set in Oracle database?

To change the national character set, use the ALTER DATABASE NATIONAL CHARACTER SET statement. The syntax of the statement is as follows: ALTER DATABASE [ db_name ] NATIONAL CHARACTER SET new_NCHAR_character_set ; db_name is optional.

How do I change the character set in Oracle SQL Developer?

To select an encoding setting in Oracle SQL Developer: On the Tools menu, click Preferences. Under Environment, in the Encoding box, select the encoding setting you want to use.


2 Answers

The simple answer is that, unlike MySQL, character sets can't be defined at column (or table) level. Latin1 is not a valid Oracle character set either.

Character sets are consistent across the database and will have been specified when you created the database. You can find your character by querying NLS_DATABASE_PARAMETERS,

select value
  from nls_database_parameters
 where parameter = 'NLS_CHARACTERSET'

The full list of possible character sets is available for 11g r2 and for 9i or you can query V$NLS_VALID_VALUES.

It is possible to use the ALTER SESSION statement to set the NLS_LANGUAGE or the NLS_TERRITORY, but unfortunately you can't do this for the character set. I believe this is because altering the language changes how Oracle would display the stored data whereas changing the character set would change how Oracle stores the data.

When displaying the data, you can of course specify the required character set in whichever client you're using.

Character set migration is not a trivial task and should not be done lightly.

On a slight side note why are you trying to use Latin 1? It would be more normal to set up a new database in something like UTF-8 (otherwise known as AL32UTF8 - don't use UTF8) or UTF-16 so that you can store multi-byte data effectively. Even if you don't need it now it's wise to attempt - no guarantees in life - to future proof your database with no need to migrate in the future.

If you're looking to specify differing character sets for different columns in a database then the better option would be to determine if this requirement is really necessary and to try to remove it. If it is definitely necessary1 then your best bet might be to use a character set that is a superset of all potential character sets. Then, have some sort of check constraint that limits the column to specific hex values. I would not recommend doing this at all, the potential for mistakes to creep in is massive and it's extremely complex. Furthermore, different character sets render different hex values differently. This, in turn, means that you need to enforce that a column is rendered in a specific character, which is impossible as it falls outside the scope of the database.

1. I'd be interested to know the situation

like image 177
Ben Avatar answered Sep 20 '22 19:09

Ben


According to provided DDL statement it's some need to use 2 character sets. The implementation of this functionality in Oracle is different from MySQL and done with n* data types like nvarchar2, nchar... Latin1 is similar to some Western European character set that might be default. So you able to define for example "Latin1" (WE**) and some Unicode (UTF8..).

The NVARCHAR2 datatype was introduced by Oracle for databases that want to use Unicode for some columns while keeping another character set for the rest of the database (which uses VARCHAR2). The NVARCHAR2 is a Unicode-only datatype. The reason you want to use NVARCHAR2 might be that your DB uses a non-Unicode character and you still want to be able to store Unicode data for some columns. Columns in your example would be able to store the same data, however the byte storage will be different.

like image 38
Yuri Levinsky Avatar answered Sep 17 '22 19:09

Yuri Levinsky