.NET Tools
Essential productivity kit for .NET and game developers
Find your perfect database development style with Rider
Rider contains many features for styling code so that it’s easy to read, and therefore easy to understand and maintain that code.
This post is part of a series around finding a coding style that fits you like a glove, and how Rider can help:
- Find your perfect coding style using Rider
- Find your perfect C# style using Rider
- Find your perfect database development style using Rider
- Find your perfect web development style using Rider
In this post, we’ll look at how you can create a style that works for all your database programming. For the purposes of this post, we’ll focus on Microsoft SQL server, though the styles can be applied to any query language.
Database maintenance and query languages have their own way of doing things that is different than general purpose languages. To start, most query languages are declarative and are often considered to be a DSL (domain specific language). This means how we read and write SQL can be a bit different than how we’d write a procedural, object-oriented, functional, or other type of language.
Make the case for great SQL style
SQL languages tend to be case-insensitive. But database developers and DBAs have traditionally distinguished keywords, types, identifiers, and object names with specific capitalizations. This makes SQL immensely more readable, as your eyes can easily pick out the main points of what’s going on in a block of code.
A popular notation is to capitalize keywords, however, object name style and capitalization can vary from team to team. Customizing capitalization in Rider is easily done in the Preferences/Options | Editor | Code Style | [SQL Language] | Case.
Related to casing and capitalization in SQL is the notion of quoted identifiers. Quoting an identifier is important when you have a database structure that requires fully qualified naming syntax. This is often caused from a database that contains objects with modern names, such as a table name with spaces – for example, an [Order Details] table.
Other times, databases use multiple schemas that need to be distinguished with a fully-qualified name. Rather than typing in quotes for identifiers in SQL statements, configure the identifiers alongside the capitalization in Preferences/Options.
Define your DDL’s style
DDL (Data Definition Language) is a specific subset of SQL commands that are meant for creating and managing database objects such as databases, tables, stored procedures, triggers, views, logins, and more. Styles for DDL tend to be similar to query command styles.
Rider contains a tab in the Code Style settings targeting DDL commands for all SQL dialects it supports. DDL code isn’t just for creating objects though – it can be used as supplemental documentation to the code base. A quick skim through some DDL can reveal what constraints a table contains, object access security, or even whether client code should access a stored procedure or view rather than direct table access. That is, if the DDL is easy to read. Easy to read means the code is styled and formatted well.
Stylish SQL expressions
Querying data often involves imperative code expressions, such as branching and switching statements. Without it, data would come in a raw, tabular format that requires manipulation in the app’s code instead.
Fortunately, Rider contains several style preferences for expressions in SQL languages, including spacing options around and inside of parentheses, and the alignment of keywords such as WHEN
, THEN
, and ELSE
. Alignment is a huge part of style, and quite important for readability in SQL. Just use Ctrl+Alt+L | Cmd-Alt-L to reformat existing code.
That’s why Rider has so many options regarding alignment and wrapping. Specific statements such as INSERT
, UPDATE
, and SELECT
all have entire sections dedicated to their syntax and style.
Rider allows you to customize all the settings necessary to style your SQL code just perfectly.
Download Rider today and let us know what styles you love to use.