SQL Server 2008 Foreign Keys case sensitivity

前端 未结 3 2047
-上瘾入骨i
-上瘾入骨i 2021-01-16 10:01

Is it possible for SQL Server 2008 to have case insensitive data, for instance the following would return data...

SELECT mycolumn FROM mytable WHERE mycolumn         


        
3条回答
  •  执念已碎
    2021-01-16 10:33

    Case sensitivity is determined by the collation settings of the database. It affects everything, comparisons, foreign keys, etc...

    However, you can change the collation setting on a particular column or columns, to make comparisons with it case sensitive while the rest of the DB stays case insensitive. For example:

    ALTER COLUMN Name VARCHAR(50)  
    COLLATE SQL_Latin1_General_CP1_CS_AS 
    

    Now all comparisons with Name will be case sensitive.

    One way to perform a case insensitive comparison given a case sensitive collation is to specify a per column collation cast as part of the query. You may also want to familiarize yourself with collation precedence if you use this method to change collation on the fly. Here is an example of a collation cast being specified at the point of query:

    SELECT Name 
    FROM MyTable
    WHERE Name = 'CASE' COLLATE SQL_Latin1_General_CP1_CI_AS -- Use case insensitive coll.
    

    The collation of a column can be changed interactively using the Table Designer is SSMS (sorry for the large image):

    enter image description here

提交回复
热议问题