DataGrip 2017.3 Help

Creating and editing SQL files

Opening the Files tool window

The Files tool window acts as a file manager for files and folders associated with your project. To open this tool window, do one of the following:

  • Select View | Tool Windows | Files.
  • Press Alt+2.
  • In the lower-left corner of the workspace, point to show tool window bars and select Files.

Attaching a directory to the project

To get optimal coding assistance when editing your SQL files, the corresponding files should be stored in directories defined in DataGrip as your "working directories". To make a directory your working directory, you should "attach" that directory to your project:

  1. Right-click the content pane of the Files tool window and select Attach Directory.
  2. In the dialog that opens select an existing directory or create a new one. For example, to create a new directory in your home directory, click userHome, then click icon newFolder and specify the directory name (e.g. SampleSQLFiles).

Creating an SQL file

  1. In the Files tool window, select the directory in which you want to create a file, and do one of the following:
    • Select New | SQL File from the context menu.
    • Press Alt+Insert and select SQL File.
    • Select New from the File menu, and select SQL File.
  2. In the dialog that opens, specify the file name, e.g. family-ddl. Click OK or press Enter.

    The new file is shown in the Files tool window.


    At the same time, the file opens in the editor.


Specifying an SQL dialect

Each SQL file, normally, is assigned an SQL dialect for better coding assistance. (An SQL dialect is a DBMS-specific version of SQL.)

DataGrip lets you specify the SQL dialect for an individual file, all .sql and .ddl files in a directory, project, or on your computer. In the following example, we'll set the SQL dialect for all the .sql and .ddl in our working directory.

  1. Click the Change dialect to link in the editor. (Alternatively, right-click the editing area, select Change Dialect (<CurrentDialect>), and then select SQL Dialects.)
  2. In the dialog that opens, click add and then select your SampleSQLFiles directory.
  3. Click the SQL Dialect cell to the right of your working directory and select the necessary dialect.
    024 1DBESQLDialects
  4. Agree to apply the selected dialect recursively, i.e. to all the contents of your working folder.

    The result may look something like this:

    024 2DBESQLDialects
  5. Click OK.

Defining a table in an SQL file

Now we are going to define the following table structure in our file:

CREATE TABLE family ( member_id INT NOT NULL, name VARCHAR(50), relation VARCHAR(50) );
  1. For inserting the CREATE TABLE statement, there is a predefined pattern. Press Ctrl+J and select the pattern (tab - new table definition).

    Here is the result:

  2. Type the table name (family).
  3. To switch from family to col, press Tab. Replace col with member_id.
  4. Type the remaining part of the table definition.

Creating a DDL data source

In addition to databases, SQL files containing data definition language statements (DDL statements) can be used as data sources. As a result, the tables, columns and other objects defined in such files become available for referencing in the editor.

The file family-ddl.sql defines a table and therefore can act as a DDL data source. Let us create a data source using that file.

The quickest way to create a DDL data source is by using drag-and-drop:

  • Drag the file family-dll.sql from the Files to the Database tool window. As a result, the corresponding data source is created.

To create DDL data sources, you can also use the Data Sources and Drivers dialog: Ctrl+Shift+Alt+S, new, DDL Data Source, etc.

Defining data in an SQL file

Now we are going to create an SQL file for adding data to the table family. Because family-ddl.sql is defined as a data source, the names of the table and of its columns as well as the info about corresponding data types are now available in the editor.

  1. Create an SQL file (e.g. family-data.sql).

    Let us add this simple INSERT statement into the file:

    INSERT INTO family (member_id, name, relation) VALUES (1, 'Chloe', 'mother');
  2. Add the INSERT statement by selecting the corresponding predefined pattern: Ctrl+J | ins - insert rows into a table.
  3. Select the table name (family).
  4. Select the list of column names (member_id, name, relation).
  5. When specifying the values, press Ctrl+P (View | Parameter Info) to see the information about the value types.
Last modified: 4 April 2018