Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Foreign keys case-insensitive?

Posted on 2004-10-29
6
Medium Priority
?
771 Views
Last Modified: 2011-10-03
I have a state table with primary keys like AK, AL, etc. (state abbreviations)  I have another table with a column that references it, but there's entries in the column like Ak, aL, etc.  What's up with that?  I thought foreign key relationships validated exact matches.  Is there a flag or setting in SQL Server to make foreign key relationships case-sensitive?
0
Comment
Question by:rramaiah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 12448050
Not unless the instance itself is case sensitive.  Best might be to add a CHECK to the table to verify that only upper case values are stored in it:

CHECK(ASCII(LEFT(stateColumn, 1)) BETWEEN 65 AND 90 AND ASCII(SUBSTRING(stateColumn, 2, 1)) BETWEEN 65 AND 90)
0
 

Author Comment

by:rramaiah
ID: 12448106
Scott,
What do you mean by "Not unless the instance itself is case sensitive".  Are you referring to the database instance?  Is there an option in the database to make it case sensitive?  It seems odd to have to add a foreign key AND a check constraint to get it to do this.
0
 
LVL 8

Accepted Solution

by:
sigmacon earned 500 total points
ID: 12448199
If you are using MS SQL 2000, You can make columns case sensitive by specifying a "collation":

alter table SomeTable
    alter column SomeColumn varchar(100) collate Latin1_General_BIN -- binary collation

I copied the following from my response to http:Q_21186541.html

A collation determines not only case-sensitivity or insensitivity, but also how something is sorted. There are two fundamental sets of collations, original SQL Server collations, which existed since SQL Server 7.0 and the Windows-compatible collations. It is interesting to note that the selected collation influences string comparison, and as such index construction and performance. Be careful with changing collations. Not many users seem to be fond with that and you can quickly run in collation conflicts when you are dealing with more than one database or more then one server. The collation for an entire server can be changed with sp_configure.

BTW: AS stands for Accent-sensitive, AI would be accent-insensitive. The difference in collations has more pronounced performance impact for nvarchar columns, because comparison in Unicode is a complex issue.

Also, in a foreign key relation, both columns will need to have the same collation.
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

Author Comment

by:rramaiah
ID: 12448272
Thanks for the prompt and detailed response!
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 12448295
>>  Are you referring to the database instance? <<

Yes.  You can choose to make the instance case-sensitive when you install SQL.


>>  Is there an option in the database to make it case sensitive? <<

In theory.  But it's *very* tricky once the db has already been created on a case-insensitive instance.  I've never known anyone who actually got that to work really properly.
You can try sigmacon's changes, but allow yourself plenty of time for debugging and testing -- very thorough testing.


>> It seems odd to have to add a foreign key AND a check constraint to get it to do this. <<

Not necessarily.  Remember, when you installed SQL you told it that you wanted it to be case insensitive.  It's simply following that rule.
0
 
LVL 8

Expert Comment

by:sigmacon
ID: 12448907
Scott's is correct about the Collation issue always being tricky. The only reason why I personally am comfortable with is because I HAD to use it quite a lot to control how things are sorted or indexed in text columns. All kinds of collation-combatibility issues WILL arise - within a table, between tables, between DBs and even across servers. Be warned!

One more thing. I try to use natural keys when possible. There are many different thoughts about this, and often the argument against natural keys is performance. I have compared index performance for integer columns with small varchar columns using the binary collation Latin1_General_BIN, and found NO performance difference. So its safe to assume that chosing the right collation does make a difference. There are many real-life applications that require case-sensitivity, so getting accustomed to this may not be bad.

Caution, if you create an entire DB to be case sensitive, then EVERYTHING in that db is case sensitive, table names and so on included. If your entire SERVER INSTANCE is case sensitive, then you are going to have lots of fun with other people's scripts that are written on a case-insensitve system, because even if you create a case-insensitive DB on a case-sensitive instance, all DDL statements are still case sensitive.

For the latter reason I run two instances on my development system. One case-sensitive, for all the stuff where I have the last word. And one case-insensitive for the stuff where I am collaborating with others.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question