IntelliJ SQL-Formatter to indent SET after UPDATE

2 min read 04-10-2024
IntelliJ SQL-Formatter to indent SET after UPDATE


Mastering SQL Formatting in IntelliJ: Indenting SET after UPDATE

For any SQL developer, clean and readable code is essential for maintainability and debugging. IntelliJ IDEA provides a powerful built-in SQL formatter, but sometimes the default settings don't perfectly align with our coding preferences. One common pain point is the default indentation of the SET clause after an UPDATE statement.

The Problem:

By default, IntelliJ indents the SET clause directly under the UPDATE statement, leading to a less visually appealing and potentially confusing layout:

UPDATE employees
SET first_name = 'John',
     last_name = 'Doe';

The Solution:

We can easily adjust IntelliJ's formatting rules to indent the SET clause on a new line, making the SQL statement more readable and consistent with common coding styles. Here's how:

  1. Open IntelliJ IDEA Settings: Go to File > Settings (or IntelliJ IDEA > Preferences on macOS).

  2. Navigate to Editor > Code Style > SQL: In the left pane, expand Editor and navigate to Code Style then SQL.

  3. Find the "UPDATE Statement" section: Scroll down to the "UPDATE Statement" section.

  4. Indentation Settings: Look for the "Indent SET clause" setting. Choose "On a new line" from the dropdown.

  5. Apply and Save: Click Apply and then OK to save the changes.

Enhanced Code:

With the setting adjusted, your SQL code will now look like this:

UPDATE employees
    SET first_name = 'John',
        last_name = 'Doe';

Additional Tips:

  • Customize Other Formatting Rules: The SQL formatter in IntelliJ offers a wide range of customizable options. Explore the various settings to tailor the formatting to your specific preferences.
  • Use Keyboard Shortcuts: Utilize keyboard shortcuts like Ctrl + Alt + L (Windows/Linux) or Cmd + Opt + L (macOS) to quickly format your SQL code after making changes.

Benefits of Consistent Formatting:

  • Improved Readability: A well-formatted SQL statement is easier to understand and analyze.
  • Reduced Errors: Consistent formatting helps to avoid errors and typos.
  • Team Collaboration: A standardized formatting style makes it easier for multiple developers to work on the same codebase.

Conclusion:

By fine-tuning IntelliJ IDEA's SQL formatter, you can ensure your SQL code is consistent, clean, and easy to maintain. This will improve your overall productivity and make working with SQL databases more enjoyable.