PhpStorm DatabasesandSQLEditorinPhpStorm 200116 0026 2492
PhpStorm DatabasesandSQLEditorinPhpStorm 200116 0026 2492
PhpStorm DatabasesandSQLEditorinPhpStorm 200116 0026 2492
Tweet
When writing an application, chances are that we have to work with a database. PhpStorm can help us perform all types of routine database
tasks, such as querying for a record; checking what that column was named again; database development where we have to create the
schema structure; or just developing PHP code against a database. In this tutorial, we will see how.
Working with the Database Tool Window
Setting up a database connection
Refactoring the Database
Creating a Table
Deleting a Table, Index or Column
Adding a Column to an Existing Table
Renaming a Table, Index or Column
Creating Indexes and Foreign Keys
Deleting Indexes and Foreign Keys
Querying, Inserting and Updating data
Using the Table Editor
Using the Database Console
Value View / Single Record Transpose View
Generating a UML database diagram
Database Migration
Exporting Database Tables as SQL Statements
Comparing and Migrating Database Code
Database Code Completion while working in PHP
Coloring Data Sources
Read-Only Data Sources
From the Database tool window, we can connect to various data sources and view database structure; modify it by refactoring tables and
columns; manage data stored in tables by simply editing or adding values; write arbitrary SQL queries with code completion and syntax
highlighting; and run SQL queries from code or the built-in SQL editor. We can generate UML diagrams showing relations between tables, copy
records as INSERT statements, get smart completion when writing joins on tables, and so on.
But first things first: let's set up a database connection.
To connect to a database, we can first pick the type of database. We can connect to Google Cloud SQL, DB2, Derby, H2, HSQLDB, MySQL, an
ODBC connection, Oracle, PostgreSQL, SQL Server (and Microsoft Azure Database), or SQLite an Sybase.
For every database system, different options will be available for configuring the connection. Typically these will be the server, database name,
username and password, but many drivers provide additional options for configuring the connection. PhpStorm does not ship with all database
drivers installed, but it does provide a handy way of downloading them when needed: click the "Download ... driver files" link next to the warning
about missing drivers to download them.
For most drivers, we can just enter the connection details, and PhpStorm will generate the underlying database connection string for us.
For some, we may have to enter a full JDBC connection string. Try searching the Internet for "<driver/database type> jdbc connection
string" if you encounter such driver.
Under the Schemas & Tables tab, we will have to specify which schemas and tables we want to be able to manipulate and generate code
completion information for.
To test the settings, click the Test Connection button, which will tell us if it succeeded (or not) in connecting to the database.
When the connection succeeds, we can save our settings. This will also trigger PhpStorm to download schema information from the database and
display it in the database tool window. We can expand schemas, tables, columns and indexes and get more details on the database structure.
Columns can be edited from the UI and PhpStorm will generate the DDL code for us. However, if we wanted to add additional statements to the
DDL, we can do so by editing them in the SQL preview editor. Completion is available by pressing Ctrl+Space. Let's make our id column auto
increment (in the MySQL dialect).
To delete a table, index or column from our database, we can navigate to it and press Delete (or use the Delete context menu). This will show us
a preview of the SQL statement that is about to be executed. It could be a DROP TABLE, an ALTER TABLE, or something similar that gets
generated.
We can name our index and optionally change the generated DDL before clicking OK to add the index or foreign key to the database.
We can also filter data by adding a filter statement (use Ctrl+Space for completion on columns). Clicking View Query will show the resulting
query.
The Table Editor as well as the Database Console support exporting data to the clipboard or a file. From the toolbar, we can select the desired
output format, such as Comma-Separated Values (CSV), XML or JSON. We can also generate INSERT and UPDATE statements from the data
so we can easily import it into a different database afterwards.
We can enter a query in the console and get syntax highlighting, completion (Ctrl+Space), on-the-fly code analysis, multiple intentions, and
navigation. Completion can even figure out foreign key constraints and suggest JOIN conditions.
To run the query, we select the statement and press Ctrl+Enter. The result set (or query log) is displayed in a separate tool window.
The Table Editor as well as the Database Console support exporting data to the clipboard or a file. From the toolbar, we can select the desired
output format, such as Comma-Separated Values (CSV), XML or JSON. We can also generate INSERT and UPDATE statements from the data
so we can easily import it into a different database afterwards.
When building queries in the Database Console, we can make use of variables and substitute them when running the query. By default, variables
are recognized as strings similar to ?, :variable, @variable, #variable# or $variable$. By using variables, PhpStorm will prompt for the values to
use in the query during execution.
If we press the keyboard shortcut again, we get a transposed view of the selected row. When we work with a table that has many fields and
requires vertical scrolling, the Single Record Transpose View makes it easier to view the data by flipping the column and row axis.
We can export the diagram to a file, add annotations to it, or print it.
If all we need is a quick overview of the database, Ctrl+Alt+U (Alt-CMD-U on Mac OS X) will show the diagram in a popup instead.
Database Migration
Exporting Database Tables as SQL Statements
Often, we want to be able to export databases so we can import them in other databases. For example: copying the schema of a newly created
table on our development machine to a test server, or vice versa.
PhpStorm allows exporting tables as (DDL) SQL statements: using the Copy DDL context menu on one or more tables scripts the tables,
columns, indexes and foreign keys to the clipboard so we can paste them in database console.
If we also need the contents of the various tables, we can use the Save To File | SQL Insert Statements context menu and export the data of
the table(s) as well.
Above, we can see our local database table does not have the country_id column and related primary and foreign keys. Clicking Migrate Left will
generate a script that would bring our local database table to the latest version. Migrate Right would do the opposite.
After selecting (part of) a query, we can press Alt+Enter+* and edit the MySQL fragment, or pick the Run Query in Console entry to do exactly
that: copy the query into a Database Console and run it there.
Colors can be enabled and assigned by using the Color Settings context menu on a data source, schema or object.
When coloring is enabled, we can clearly distinguish the data source from the tab header by its color. Note that PhpStorm also displays the
current database schema in the tab title.
elements from our schema and only provides read access to the data source, even if the user connecting to the database has write privileges.
Tweet