BackToBasics: Why I Use Lower Case for Data Type Names (Now) (2024)

Table of Contents
Now, admittedly… Summary FAQs

This is a continuation of my #BackToBasics series, where I pledged to write an entry-level post at the beginning of each month. I promised the first Wednesday but, well, you'll see.

For the longest time, I always considered a data type name to be a T-SQL keyword of sorts. I would write code like this where data type names would be forced to upper case to match my chosen convention of upper-case keywords:

DECLARE @x VARCHAR(32), @y INT;SELECT @x = CONVERT(INT, '32');

I have literally thousands of code snippets throughout my blog posts, tips, and forum answers peppered all over the Internet - spread over ten years - where every single instance of a data type name will have been forced to upper case, simply because that had always been my habit. I'm not going to go back and fix those - I mean, I have a life to live, and stuff. But for all my code samples going forward, you'll see a stark difference - all the data type names will be in lower case. Here is an example why - another example of the type of code I would write in the past would use upper case for system data type names when expressed as literals, too. For example:

SELECT system_type_id FROM sys.types WHERE [name] IN (N'INT', N'VARCHAR');

In systems where setup was click click next, this worked no problem, returning 56 and 167 as expected. Then I started dealing with more and more case-sensitive and binary collations, which are sometimes implemented in systems outside of your control. Take this database, for example:

CREATE DATABASE floobmort COLLATE Latin1_General_BIN2; GOUSE floobmort;

Now, run that same query against sys.types. What happens? Empty result set. That comparison tries to compare N'INT' to what's stored in the table, N'int', and because the comparison is done using a binary collation, it returns false. There are other cases the collation difference will cause unexpected errors, for example when we try to inspect the collation used for sys.types.name:

SELECT db = N'floobmort', coll = collation_name FROM floobmort.sys.all_columns WHERE object_id = OBJECT_ID(N'sys.types') AND name = N'name'UNION ALLSELECT 'tempdb', collation_name FROM tempdb.sys.all_columns WHERE object_id = OBJECT_ID(N'sys.types') AND name = N'name';
Msg 451, Level 16, State 1Cannot resolve collation conflict between "SQL_Latin1_General_CP1_CI_AS"and "Latin1_General_BIN2" in UNION ALL operator occurring in SELECT statement column 2.

I'm not trying be meta about metadata ("meta meta"?) or give you nostalgia about the movie Inception, but the way collation name is stored using database-specific collation illustrates precisely why relying on it can be problematic.

We can fix this easily in this case by applying aCOLLATEclause to the firstSELECT(and any collation will do, since we just want readable output; we don't need to worry about changing actual Unicode data or sorting rules as a result):

SELECT db = N'floobmort', coll = collation_name COLLATE SQL_Latin1_General_CP1_CI_AS FROM floobmort.sys.all_columns WHERE object_id = OBJECT_ID(N'sys.types') AND name = N'name'UNION ALLSELECT 'tempdb', collation_name FROM tempdb.sys.all_columns WHERE object_id = OBJECT_ID(N'sys.types') AND name = N'name'; /* RESULTS: floobmort Latin1_General_BIN2 tempdb SQL_Latin1_General_CP1_CI_AS*/

Now we see that they are stored differently, and it gives a better idea why the query above for type names returned an empty result. You will of course want to use a more specific collation clause if you are using a collation and have objects or entities named using characters from those character sets (otherwise they won't look right if that target collation can't display them the same).

Now, admittedly…

...there aren't many real-world scenarios where this can actually lead to problems. Even on a SQL Server instance installed with a binary collation, variable and parameter data types are case insensitive, so you can do this just fine:

CREATE PROCEDURE dbo.what @i INTASBEGIN SET NOCOUNT ON; DECLARE @foo DATETime2(7); SELECT TRY_CONVERT(CHar(21),'blat'); CREATE TABLE #f(a INTeger);ENDGOEXEC dbo.what @i = 4;

There are other, less obvious places where this can break, though. For example, with the new CLR types likegeography, you need to be case sensitive if the database or instance is case sensitive. Try this:

SELECT geography::STGeomFromText('LINESTRING(-10 22, -12 19)', 4326);GOSELECT GEOGRAPHY::STGeomFromText('LINESTRING(-10 22, -12 19)', 4326);

The latter fails on a case-sensitive database or instance with:

Msg 243, Level 16, State 4Type GEOGRAPHY is not a defined system type.

And this is not exclusive to the new CLR types, but rather generic to CLR-related syntax; for example, I can make an old type likecharfail in the same way, if I try to invoke invalid methods:

SELECT CHAR::what;

If the database or instance is case sensitive, you get this error message:

Msg 243, Level 16, State 4Type CHAR is not a defined system type.

On a case insensitive database or instance, it still fails, but with a different message:

Msg 258, Level 15, State 1Cannot call methods on char.

You have no business trying to call methods on that data type, but the point of that example is just to illustrate that the problem is about checking the name of the type, and that happens before any of the other syntax around it is validated.

Also, this can happen if you're not careful about case sensitivity when dealing with alias types (not that you should use these). For example:

CREATE TYPE dbo.mytype FROM VARCHAR(20);GO DECLARE @x dbo.MyType;

Again, this works fine in a case-insensitive database or instance, but if you happen to have a case-sensitive or binary collation in play, you'll see this:

Msg 2715, Level 16, State 3Column, parameter, or variable #1: Cannot find data type dbo.MyType.Parameter or variable '@x' has an invalid data type.

While you can probably enumerate the individual scenarios where this is riskier, the underlying argument I'm trying to make here is one about consistency. I'm a big fan ofalways spelling out date partslikeDAY,MONTH, andMINUTE, instead of using their shorthand, even though there are some cases where ambiguity is not possible. Similarly, here, I feel that striving to always match the exact case that is insys.types- even in situations where it can't break - leads to better habits that can avoid the above problems.

Summary

You should always code defensively and match the case sensitivity of all objects, variables, and parameters. You really never know when code you've written (or that has helped someone else) will get implemented in a case-sensitive or binary collation. I've explained above why I am very careful to match what's insys.types, but this extends to other scenarios, too.

For example, try this in a database using a case-sensitive or binary collation, where you have to be accurate with object names (not just for your own objects, but for system objects, too):

SELECT * FROM SYS.Types;

Result:

Msg 208, Level 16, State 1Invalid object name 'SYS.Types'.

And even worse, the problem extends to parameter and variable names on a case-sensitive or binaryinstance(and probably if you've somehow managed to changemasterto be that way, though I'll confess I haven't been motivated enough to confirm). On such an instance, this code:

DECLARE @foo int;SELECT @FOO;

...fails with this error message:

Msg 137, Level 15, State 2Must declare the scalar variable "@FOO".

This can lead to quite a surprising troubleshooting session if you develop your solution on a case-insensitive instance and then deploy to one that isn't. One thing I've started doing is developing on a BIN2 collation - yes, if I'm sloppy, it may be painful to deal with those error messages during development, but that's way better, IMHO, than hitting them during or after deployment, or when your app or database gets migrated to a different server. And IntelliSense should negate any sloppiness there anyway...

BackToBasics: Why I Use Lower Case for Data Type Names (Now) (2024)

FAQs

Should database names be capitalized? ›

It is legal to write commands in lowercase, but this often leads to subtle bugs with lowercase attribute/column names, so we expect you to use the ALLCAPS convention for any commands or datatypes. Database names (e.g. c9 or imdb ) should be lowercased (no numbers or special characters lie "_" or "-").

Should database tables be lowercase? ›

Only Use Lowercase Letters, Numbers, and Underscores

table. column pattern. Queries are harder to write if you use capital letters in table or column names.

Should SQL be an upper or lower case? ›

All Caps SQL Commands

Show example queries with ALL CAPS, First Letter Caps, and lowercase SQL commands. For readability, all SQL commands should be written in uppercase letters. This allows the reader to identify the keywords in the SQL statement and easily determine what the query is executing.

Does SQL care about capitalization? ›

Keywords in SQL are case-insensitive for the most popular DBMSs.

What is the best practice for database names? ›

The best practice is to avoid spaces and special characters when naming database objects, with the exception of the underscore. The latter can be used (as explained below) to separate words in compound names.

Do you always Capitalise names? ›

(g) Proper names are always capitalized.

What are the rules for database table names? ›

A table represents a collection of entities of the same type, such as customers, orders, or products. Therefore, it makes sense to use a singular noun for the table name, rather than a plural one. This way, you can avoid confusion when writing queries, joining tables, or creating foreign keys.

Are database table names 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. The following statement would not work because it refers to a table both as my_table and as MY_TABLE : mysql> SELECT * FROM my_table WHERE MY_TABLE.

Should variable names be lowercase? ›

The convention for naming variables in Java is to use lowercase letters and to separate words with underscores. This is the convention because it makes the variables more readable.

Why do people write SQL in all caps? ›

The SQL capitalization provides a quick and easy way to visually break up the different parts of the query, helping people differentiate between the SQL parts and the database objects parts of the queries. Joe Celko writes about it in SQL Programming Style: Rule: Uppercase the Reserved Words.

Can I write SQL in lowercase? ›

While the SQL keywords you're referring to are case-insensitive (FROM, SELECT, WHERE, ON, IN, AS, etc.), it is indeed common practice to capitalize these words. Combined with proper spacing and indenting, this practice will make your SQL statements much easier to read and refer back to later on.

Does case matter in SQL column names? ›

The case sensitivity of table and column names depends upon the Database and Operating System. In the PostgreSQL table and column names are sensitive. In MYSQL, table and column names are not case sensitive on windows but they are case sensitive in Linux commands. Column names are not sensitive in MSSQL and MYSQL.

Should SQL table names be lowercase? ›

You can do your table names any way that you like - lowercase, camelCase, under_scores, etc. However, once you have decided on a convention you should stick with it throughout the entire database.

What characters should not be used in SQL? ›

Do not use special characters such as ( ) - & @ * $ | % ~ except for underscore (_). Also do not use Oracle SQL or PL/SQL reserved words, especially in the Oracle name.

Is SQL hard to learn? ›

Learning SQL is generally considered easier than many other programming languages, and mastery of SQL can facilitate learning other programming languages such as Python or JavaScript. Knowledge of SQL can open many professional doors across various industries including finance, social media, and music.

How do you write a database name? ›

Database Naming Convention
  1. Warning! ...
  2. Avoid quotes. ...
  3. Ex: Avoid using names like "FirstName" or "All Employees" .
  4. Lowercase. ...
  5. Ex: Use first_name , not "First_Name" .
  6. Data types are not names. ...
  7. Underscores separate words. ...
  8. Ex: Use word_count or team_member_id , not wordcount or wordCount .

Can you Capitalise a database? ›

Under the current rules of the Financial Accounting Standards number 141 and 142, a data warehouse, as an intangible asset, could be capitalized at its fair value only when a company with an internally built data warehouse is acquired by another corporation.

How do I list a database name? ›

Run the following query to show list of databases: SHOW DATABASES; You can run this statement from MySQL Command Line Client, MySQL Shell, as well as from any GUI tool that supports SQL—for example, dbForge Studio for MySQL. MySQL returns the results in a table with one column—Database.

Should table names be capitalized in MySQL? ›

To avoid problems caused by such differences, it is best to adopt a consistent convention, such as always creating and referring to databases and tables using lowercase names. This convention is recommended for maximum portability and ease of use.

Top Articles
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 5367

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.