Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. DB Case Sensitivity, unexpected/surprising behaviour

DB Case Sensitivity, unexpected/surprising behaviour

Scheduled Pinned Locked Moved Database
databasesql-serverhelpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I'm surprised, that the COLLATION used in a DB Create Statement does have influence in executing SQL statements. Following I testet: A. Insensitive: Creating a DB explicitely "Case Insensitive" CREATE DATABASE CIDB COLLATE Latin1_General_CI_AI; GO USE CIDB; CREATE TABLE TEST (ID INTEGER); SELECT * FROM TEST; select * FROM TEST; SELECT * FROM Test; select * FROM Test; :) --> Everything works like expected, MSSQL does not bother about "TEST" or "Test". B. Sensitive: Creating a DB explicitely "Case Sensitive" CREATE DATABASE CSDB COLLATE Latin1_General_CS_AI; GO USE CSDB; CREATE TABLE TEST (ID INTEGER); SELECT * FROM TEST; Everyting up to here is working fine. But ... SELECT * FROM Test; ??--> fails with error message "Msg 208, Level 16, State 1, Line 1 Invalid object name 'Test'." ... obviously because the database is created "Sensitive". What's really surprising for me, that the collation sequence now also makes the SQL Language a case sensitive, not the language it self but the objects (table-, field- names and I assume also constraint names). Am I the only one who is surprised by this? Thank you in advance for some high lighting comments.

    Richard DeemingR 1 Reply Last reply
    0
    • L Lost User

      I'm surprised, that the COLLATION used in a DB Create Statement does have influence in executing SQL statements. Following I testet: A. Insensitive: Creating a DB explicitely "Case Insensitive" CREATE DATABASE CIDB COLLATE Latin1_General_CI_AI; GO USE CIDB; CREATE TABLE TEST (ID INTEGER); SELECT * FROM TEST; select * FROM TEST; SELECT * FROM Test; select * FROM Test; :) --> Everything works like expected, MSSQL does not bother about "TEST" or "Test". B. Sensitive: Creating a DB explicitely "Case Sensitive" CREATE DATABASE CSDB COLLATE Latin1_General_CS_AI; GO USE CSDB; CREATE TABLE TEST (ID INTEGER); SELECT * FROM TEST; Everyting up to here is working fine. But ... SELECT * FROM Test; ??--> fails with error message "Msg 208, Level 16, State 1, Line 1 Invalid object name 'Test'." ... obviously because the database is created "Sensitive". What's really surprising for me, that the collation sequence now also makes the SQL Language a case sensitive, not the language it self but the objects (table-, field- names and I assume also constraint names). Am I the only one who is surprised by this? Thank you in advance for some high lighting comments.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      It might seem odd at first glance, but it makes sense when you consider the fact that the names of all the objects within the database are stored in system tables within the database itself. If the default collation for the database is case-sensitive, then the collation for all of the system tables is case-sensitive. So SELECT * FROM sys.tables WHERE name = 'Test' isn't going to match a row where name = 'TEST'. Whilst Microsoft could have chosen to make the system tables always case-insensitive, that would have been a breaking change. Earlier versions of SQL Server didn't allow you to mix collations within a single database, so introducing that change in a later version could have broken existing databases or scripts. There are effectively two "solutions":

      1. Always create your databases using a case-insensitive collation, and use a case-sensitive collation on individual columns;
      2. Always write your scripts using the case of the table and columns names as defined when they were created;

      SQL Server Case Sensitive Collations and DMVs[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      L 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        It might seem odd at first glance, but it makes sense when you consider the fact that the names of all the objects within the database are stored in system tables within the database itself. If the default collation for the database is case-sensitive, then the collation for all of the system tables is case-sensitive. So SELECT * FROM sys.tables WHERE name = 'Test' isn't going to match a row where name = 'TEST'. Whilst Microsoft could have chosen to make the system tables always case-insensitive, that would have been a breaking change. Earlier versions of SQL Server didn't allow you to mix collations within a single database, so introducing that change in a later version could have broken existing databases or scripts. There are effectively two "solutions":

        1. Always create your databases using a case-insensitive collation, and use a case-sensitive collation on individual columns;
        2. Always write your scripts using the case of the table and columns names as defined when they were created;

        SQL Server Case Sensitive Collations and DMVs[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Wow, great! Thank you for your speedy Response. I think I will go with "1.". Little bit more work, but from my Point of view the most transparent way. Thank you again. Bruno

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          It might seem odd at first glance, but it makes sense when you consider the fact that the names of all the objects within the database are stored in system tables within the database itself. If the default collation for the database is case-sensitive, then the collation for all of the system tables is case-sensitive. So SELECT * FROM sys.tables WHERE name = 'Test' isn't going to match a row where name = 'TEST'. Whilst Microsoft could have chosen to make the system tables always case-insensitive, that would have been a breaking change. Earlier versions of SQL Server didn't allow you to mix collations within a single database, so introducing that change in a later version could have broken existing databases or scripts. There are effectively two "solutions":

          1. Always create your databases using a case-insensitive collation, and use a case-sensitive collation on individual columns;
          2. Always write your scripts using the case of the table and columns names as defined when they were created;

          SQL Server Case Sensitive Collations and DMVs[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Quote:

          that would have been a breaking change

          After thinking again and again about it (I'm an old man and need therefor more time), is it not MS who is breaking the SQL Standard with this? Not a big thing, only a thought. Finally, it is like it is (with MSQL) and I have to live with it.

          Richard DeemingR 1 Reply Last reply
          0
          • L Lost User

            Quote:

            that would have been a breaking change

            After thinking again and again about it (I'm an old man and need therefor more time), is it not MS who is breaking the SQL Standard with this? Not a big thing, only a thought. Finally, it is like it is (with MSQL) and I have to live with it.

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            0x01AA wrote:

            is it not MS who is breaking the SQL Standard with this?

            Yes. It looks like SQL92 says that object names should be case-insensitive unless they are quoted. I guess they decided the dangers of making the change outweighed the inconvenience of not following the standards.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            L 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              0x01AA wrote:

              is it not MS who is breaking the SQL Standard with this?

              Yes. It looks like SQL92 says that object names should be case-insensitive unless they are quoted. I guess they decided the dangers of making the change outweighed the inconvenience of not following the standards.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Thanks again to give me some confidence on this

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups