Case Sensitivity Issues in Primary Keys - Oracle to SQL Server Migration (2024)

In Oracle, primary keys are case-sensitive, so if two values differ only in case they can be used as two different primary keys.

In SQL Server, by default, primary keys are case-insensitive and when you transfer data from Oracle to SQL Server and then try to create the primary key constraints, you may have duplicate key errors.

Last Update: Oracle 11g R2 and Microsoft SQL Server 2012

Case Sensitivity in Primary Keys

Assume there is a table with primary keys that differ in case only:

Oracle:

 CREATE TABLE states ( id CHAR(2) NOT NULL, name VARCHAR2(25) ); -- Insert 2 values that differ in case only INSERT INTO states VALUES ('CA', 'California'); INSERT INTO states VALUES ('Ca', 'California'); ALTER TABLE states ADD PRIMARY KEY (id); -- Table altered

Unlike Oracle, SQL Server does not allow you to create the primary key in this case:

SQL Server:

 CREATE TABLE states ( id CHAR(2) NOT NULL, name VARCHAR(25) ); -- Insert 2 values that differ in case only INSERT INTO states VALUES ('CA', 'California'); INSERT INTO states VALUES ('Ca', 'California'); ALTER TABLE states ADD PRIMARY KEY (id); -- Msg 1505, Level 16, State 1, Line 1 -- The CREATE UNIQUE INDEX statement terminated because a duplicate key was found -- The duplicate key value is (Ca).

By default, SQL Server treats values 'CA' and 'Ca' as equal.

Find Primary and Unique Keys that Differ in Case

You can use the following PL/SQL script to find all columns that are part of a primary or unique key, and differ in case only in Oracle for the current user:

Oracle:

 SET SERVEROUTPUT ON -- Cursor to select all character columns that are part of primary or unique key DECLARE CURSOR c1 IS SELECT c.table_name, cc.column_name FROM user_constraints c, user_cons_columns cc, user_tab_columns tc WHERE c.constraint_type IN ('P', 'U') AND c.constraint_name = cc.constraint_name AND c.table_name = cc.table_name AND cc.table_name = tc.table_name AND cc.column_name = tc.column_name AND tc.data_type IN ('VARCHAR2', 'NVARCHAR2', 'CHAR', 'NCHAR'); c2 SYS_REFCURSOR; stmt VARCHAR2(300); dup_value VARCHAR2(300); total_found NUMBER := 0; total_columns NUMBER := 0; count_column BOOLEAN; BEGIN DBMS_OUTPUT.PUT_LINE('Opening a cursor...'); -- For each column find duplicates - values that differ in case only FOR r1 IN c1 LOOP count_column := TRUE; -- Statement to select duplicate values  stmt := 'SELECT ' || r1.column_name || ' FROM ' || r1.table_name || ' WHERE UPPER(' || r1.column_name || ') <> ' || r1.column_name || ' AND ' || ' UPPER(' || r1.column_name || ') IN ' || ' (SELECT UPPER(' || r1.column_name || ') FROM ' || r1.table_name || ' GROUP BY UPPER(' || r1.column_name || ')' || ' HAVING COUNT(*) > 1)'; DBMS_OUTPUT.PUT_LINE('Checking ' || r1.table_name || '.' || r1.column_name || '...'); OPEN c2 FOR stmt; -- Output all duplicate values  LOOP FETCH c2 INTO dup_value; EXIT WHEN c2%NOTFOUND; DBMS_OUTPUT.PUT_LINE('''' || dup_value || ''''); IF count_column = TRUE THEN total_found := total_found + 1; count_column := FALSE; END IF; END LOOP; total_columns := total_columns + 1; END LOOP; DBMS_OUTPUT.PUT_LINE(total_found || ' duplicate columns found'); DBMS_OUTPUT.PUT_LINE(total_columns || ' columns checked'); END;/

Sample output:

 Opening a cursor... Checking ML_SUBSCRIPTION.SUBSCRIPTION_ID... Checking STATES.ID... 'Ca' 1 duplicate columns found 2 columns checked PL/SQL procedure successfully completed.

Note that this script checks each character primary key column individually, and even if duplicates are found, it may be not a problem for multi-column keys, but it is still worth checking.

Resolving Case-Sensitivity Issue in Primary Keys

If you transfer an Oracle table that contains primary keys that differ in case only, you will have errors when you try to create the primary key constraints in SQL Server, or data transfer errors if you create the primary key constraints before transferring the data.

Using Case-Sensitive Collation in SQL Server

The easiest way to solve this problem is to specify a case-sensitive collation for primary columns in SQL Server:

SQL Server:

 CREATE TABLE states ( id CHAR(2) COLLATE Latin1_General_CS_AS NOT NULL, name VARCHAR(25) ); -- Insert 2 values that differ in case only INSERT INTO states VALUES ('CA', 'California'); INSERT INTO states VALUES ('Ca', 'California'); ALTER TABLE states ADD PRIMARY KEY (id); -- Command(s) completed successfully.

CS in COLLATE Latin1_General_CS_AS collation specifies case-sensitivity for column data, and now values 'CA' and 'Ca' are different, so you can successfully create the primary key constraint.

Data Cleaning

In most cases, having multiple primary key values that differ in case only is a result of a data input error, i.e. multiple different primary keys points to the same entity. In this case it often makes sense to clean the data after you have transferred them to SQL Server, but before creating primary key constraints.

To do so, firstly you have to correct the foreign keys that refer to the table, and then remove duplicate primary keys. For more information, see Trailing Spaces in Primary Keys.

Resources

Author

Dmitry Tolpeko, dmtolpeko@sqlines.com - July 2012

Please contact me if you are interested in Oracle to SQL Server migration services, consulting, or training.

You could leave a comment if you were logged in.

Case Sensitivity Issues in Primary Keys - Oracle to SQL Server Migration (2024)

FAQs

How to avoid case sensitive in Oracle? ›

Enable and disable Case Sensitive password in Oracle
  1. Disable the Case Sensitive Password in Oracle. ALTER SYSTEM SET SEC_CASE_SENSITIVE_LOGON=FALSE SCOPE=BOTH;
  2. Enable the Case Sensitive Password in Oracle. ALTER SYSTEM SET SEC_CASE_SENSITIVE_LOGON=TRUE SCOPE=BOTH;
  3. Example.
Jan 5, 2021

How to remove case sensitive in Oracle SQL? ›

Use regular expressions. From Oracle 10g onwards REGEXP_LIKE() is available. You can specify the _match_parameter_ 'i' , in order to perform case-insensitive searching. In order to use this as an equality operator you must specify the start and end of the string, which is denoted by the carat and the dollar sign.

Is primary key case sensitive in SQL Server? ›

In SQL Server, by default, primary keys are case-insensitive and when you transfer data from Oracle to SQL Server and then try to create the primary key constraints, you may have duplicate key errors.

How do you deal with case sensitive in SQL? ›

To do a case-insensitive comparison, use the ILIKE keyword; e.g., column ILIKE 'aBc' and column ILIKE 'ABC' both return TRUE for 'abc' . In contrast, MySQL and MS SQL Server have case-insensitive behaviors by default. This means WHERE column = 'abc' returns TRUE for e.g., 'abc' , 'ABC' , or 'aBc' .

How do I ignore case sensitivity? ›

Conclusion. To recap, the grep command allows you to search for a pattern inside of files, and is case sensitive by default. To make grep case insensitive, all you have to do is add an -i or —ignore-case flag.

How do I turn off case sensitivity for an entire database? ›

You can turn off table name case sensitivity in MySQL by setting the parameter lower_case_table_names to 1. Column, index, stored routine, event names, and column aliases aren't case sensitive on either platform.

Is SQL case sensitive in Oracle? ›

Object name case sensitivity is different for Oracle and PostgreSQL. Oracle names aren't case sensitive. PostgreSQL names are case sensitive.

How do I SELECT without case sensitive in SQL? ›

Case insensitive SQL SELECT: Use upper or lower functions

or this: select * from users where lower(first_name) = 'fred'; As you can see, the pattern is to make the field you're searching into uppercase or lowercase, and then make your search string also be uppercase or lowercase to match the SQL function you've used.

Are Oracle SQL statements case sensitive? ›

PL/SQL keywords are not case-sensitive, so lower-case letters are equivalent to corresponding upper-case letters except within string and character literals.

How to check case sensitivity in SQL Server? ›

SQL Server is, by default, case insensitive; however, it is possible to create a case-sensitive SQL Server database and even to make specific table columns case sensitive. The way to determine if a database or database object is to check its "COLLATION" property and look for "CI" or "CS" in the result.

What is the best choice for primary key SQL Server? ›

Integer (number) data types are the best choice for primary key, followed by fixed-length character data types. SQL Server processes number data type values faster than character data type values because it converts characters to ASCII equivalent values before processing, which is an extra step.

How do I make SQL Server case sensitive? ›

To write a case-sensitive search in SQL Server, don't use UPPER or LOWER functions around the column to filter. Instead, use the COLLATE keyword with a case-sensitive collation followed by the comparison.

How to enable case sensitive feature for column or database in SQL Server? ›

You can also make column case sensitive by changing column's collation from case insensitive SQL_Latin1_General_CP1_CI_AI to case sensitive Latin1_General_CS_AS. Lets use Alter Command to change the column Name collation from case insensitive to case sensitive.

Is SQL case sensitive for column names? ›

MS SQL Server

The column names in a select statement are not case sensitive even if quoted.

Is SQL replace case sensitive? ›

Since the replace() function search is case sensitive, it will not consider 'Sql' the same as 'SQL.

What is an example of a case-sensitive issue? ›

In computers, case sensitivity defines whether uppercase and lowercase letters are treated as distinct (case-sensitive) or equivalent (case-insensitive). For instance, when users interested in learning about dogs search an e-book, "dog" and "Dog" are of the same significance to them.

What does it mean when answers are not case-sensitive? ›

Anything that is not case-sensitive means that any uppercase or lowercase character can be entered. For example, the Windows command line or MS-DOS is not case-sensitive, however, the Linux command line is case-sensitive.

What is case-sensitive error? ›

A case sensitive programming language means that the program will treat the uppercase and lowercase letters differently. Hence, we must use the exact case according to the syntax because changing the case, for example from print to Print, will result in an error.

Should a database be case sensitive? ›

Although database, table, and trigger names are not case-sensitive on some platforms, you should not refer to one of these using different cases within the same statement.

Are identifiers are case sensitive? ›

By default, delimited identifiers are not case sensitive. To comply with ANSI/ISO Entry SQL-92, however, delimited identifiers must be case sensitive. The DBMS Server treats database, user, group, role, cursor, and location names without regard to case. Mixed-case database or location names cannot be created.

How do I make a data validation list case sensitive? ›

Create data validation list in case sensitive
  1. Select a cell or cells and click Data > Data Validation > Data Validation.
  2. In the Data Validation dialog, click List form the Allow drop-down list, and type the choices of data validation list you need in to the Source textbox (use comma to divide these choices). ...
  3. Click OK.
Jun 7, 2023

How to set Oracle Database to case sensitive? ›

By default, Oracle identifiers (table names, column names, etc.) are case-insensitive. You can make them case-sensitive by using quotes around them (eg: SELECT * FROM "My_Table" WHERE "my_field" = 1 ). SQL keywords ( SELECT , WHERE , JOIN , etc.)

How to make a case-insensitive query in Oracle? ›

Starting with Oracle Database 10g Release 2 you can use the initialization parameters NLS_COMP and NLS_SORT to render all string comparisons case insensitive. We need to set the NLS_COMP parameter to LINGUISTIC, which will tell the database to use NLS_SORT for string comparisons.

Is Oracle schema name case sensitive? ›

Names are not case sensitive. A name must begin with an alphabetic character from your database character set unless surrounded by double quotation marks.

Are all SQL keywords not case sensitive? ›

The SQL keywords (SELECT, FROM, WHERE, etc.) are case-insensitive, yet they are frequently expressed in all capitals. Table and column names are case-sensitive in some settings.

What is alternative way for case in SQL? ›

Question: What is Alternative to CASE Statement in SQL Server? Answer: IIF Function.

How do I know if a column is case sensitive in SQL? ›

How do you check if a column is case sensitive in SQL Server?
  1. SELECT COLUMN_NAME, COLLATION_NAME.
  2. FROM INFORMATION_SCHEMA.COLUMNS.
  3. WHERE TABLE_NAME = '<Name of the table that contains the column(s) you want to check>'
Jul 2, 2019

How to find lower case letters in Oracle? ›

LCASE or LOWER takes a character expression as a parameter and returns a string in which all alpha characters have been converted to lowercase.

How to check lower case in SQL Server? ›

SQL Server LOWER() Function

The LOWER() function converts a string to lower-case.

What are the requirements for primary key in SQL Server? ›

Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).

Is primary key always unique in SQL Server? ›

If a primary key consists of more than one column, duplicate values are allowed in one column, but each combination of values from all the columns in the primary key must be unique.

How to check all primary keys in SQL Server? ›

Here are a few lines of sql query using which we can get the primary column name.
  1. select C.COLUMN_NAME FROM.
  2. INFORMATION_SCHEMA.TABLE_CONSTRAINTS T.
  3. JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE C.
  4. ON C.CONSTRAINT_NAME=T.CONSTRAINT_NAME.
  5. WHERE.
  6. C.TABLE_NAME='Employee'
  7. and T.CONSTRAINT_TYPE='PRIMARY KEY'
Jan 4, 2021

How to encrypt sensitive data in SQL Server? ›

Applies to: SQL Server.
  1. Create a master key.
  2. Create or obtain a certificate protected by the master key.
  3. Create a database encryption key and protect it by using the certificate.
  4. Set the database to use encryption.
Mar 3, 2023

Are SQL schemas case sensitive? ›

By default, SQL Server names are case insensitive. However, you can create a case sensitive SQL Server database by changing the COLLATION property. In PostgreSQL, object names are case insensitive. By default, the AWS Schema Conversion Tool (AWS SCT) uses object names in lowercase for PostgreSQL.

How to add case in where clause in SQL Server? ›

CASE STATEMENT IN WHERE CLAUSE:

We can use a case statement in Where, Order by and Group by clause. In the Customer table, I have displayed the First Name is Ram or the Last Name is Sharma's salary. So, by using a CASE statement with the where condition displays the result.

Which function is case sensitive in nature in SQL? ›

By default, table aliases are case sensitive. You can specify an alias in any lettercase (upper, lower, or mixed), but if you use it multiple times in a statement, you must use the same lettercase each time.

Which databases are case sensitive? ›

Most databases treat table column names as case-insensitive. However, some databases, for example Sybase, treat table column names as case-sensitive.

Which database name is case sensitive? ›

Database, table, table aliases and trigger names are affected by the systems case-sensitivity, while index, column, column aliases, stored routine and event names are never case sensitive. Log file group name are case sensitive.

Is SQL Server string comparison case sensitive? ›

In summary, comparisons are case sensitive if they involve a binary literal string or string expression, or a CHAR BINARY , VARCHAR BINARY , or BLOB column. Comparisons are not case sensitive if they involve only non-binary literal strings or string expressions, or CHAR , VARCHAR , ENUM , SET , or TEXT columns.

How to make a query case-insensitive in Oracle? ›

Starting with Oracle Database 10g Release 2 you can use the initialization parameters NLS_COMP and NLS_SORT to render all string comparisons case insensitive. We need to set the NLS_COMP parameter to LINGUISTIC, which will tell the database to use NLS_SORT for string comparisons.

How to avoid special characters in Oracle? ›

Use braces to escape a string of characters or symbols. Everything within a set of braces in considered part of the escape sequence. When you use braces to escape a single character, the escaped character becomes a separate token in the query. Use the backslash character to escape a single character or symbol.

Is Oracle database case sensitive? ›

Oracle names aren't case sensitive.

How to avoid block corruption in Oracle? ›

Force clear blocks
  1. Create a user and grant the user access to the database. ...
  2. Identify the corrupt block against the data file. ...
  3. Find the free space. ...
  4. Reformat first corrupted block. ...
  5. Verify that the corrupted blocks are fixed.
Mar 20, 2019

How to set Oracle database to case sensitive? ›

By default, Oracle identifiers (table names, column names, etc.) are case-insensitive. You can make them case-sensitive by using quotes around them (eg: SELECT * FROM "My_Table" WHERE "my_field" = 1 ). SQL keywords ( SELECT , WHERE , JOIN , etc.)

How do I make SQL like case sensitive? ›

You can easy change collation in Microsoft SQL Server Management studio.
  1. right click table -> design.
  2. choose your column, scroll down i column properties to Collation.
  3. Set your sort preference by check "Case Sensitive"
Feb 19, 2013

Which syntax do you use to make a query case-insensitive? ›

=~ (equals) operator

Filters a record set for data with a case-insensitive string. For more information about other operators and to determine which operator is most appropriate for your query, see datatype string operators.

How to handle special characters in Oracle SQL query? ›

B Handling Special Characters

Oracle recommends that you enclose parameter and value pairs in double quotation marks. These special characters must be escaped using double quotation marks (") around the special character or around the parameter value containing the special character.

How to remove first 2 characters in Oracle SQL? ›

The Oracle LTRIM() function is used to remove all specified characters from the left end side of a string. Optionally you can specify an initial character or characters to trim to, or it will default to a blank.

What is chr 13 in Oracle? ›

The CHR() function is used to insert control characters into a string. CHR(10) is used to insert line breaks, CHR(9) is for tabs, and CHR(13) is for carriage returns.

How do I know if MySQL database is case sensitive? ›

SQL Server is, by default case insensitive; however, it is possible to create a case sensitive SQL Server database and even to make specific table columns case sensitive. The way to determine a database or database object is by checking its “COLLATION” property and look for “CI” or “CS” in the result.

Which SQL is case sensitive? ›

The SQL keywords (SELECT, FROM, WHERE, etc.) are case-insensitive, yet they are frequently expressed in all capitals. Table and column names are case-sensitive in some settings. MySQL provides a setting that allows you to enable or disable it.

Is MySQL query is case sensitive? ›

Table names are stored in lowercase on disk and name comparisons are not case-sensitive. MySQL converts all table names to lowercase on storage and lookup. This behavior also applies to database names and table aliases.

How to check the block corruption in Oracle? ›

Oracle also provides many ways to find corrupt blocks, dbv, rman and dbms_repair:
  1. Using rman to find corrupted blocks: RMAN corrupted_blocks.htm.
  2. Using dbv to find corrupt blocks. Using db verify to find corruption.
  3. Find corrupt blocks with dbms_repair. Using dbms_repair to detect corruption.

How to find database corruption in Oracle? ›

Fortunately, there are multiple ways that Oracle can detect and repair data block corruption:
  1. Oracle Recovery Manager (RMAN)
  2. DBVerify.
  3. ANALYZE command.
  4. DB_BLOCK_CHECKING parameter.
  5. Block media recovery (BMR)
  6. DBMS_REPAIR.
May 8, 2019

How to fix database corruption in SQL Server? ›

How To Repair a Corrupted SQL Database
  1. Step 1 – Attempt Repair with SQL Server Management Studio (Optional) ...
  2. Step 2 – Choose a Good Database Repair Tool (Recommended) ...
  3. Step 3 – Download Your SQL Repair Tool. ...
  4. Step 4 – Run Your SQL Database Repair Tool. ...
  5. Step 5 – Scan the Corrupted SQL Database.

Top Articles
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 6249

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.