Getting Started
Using This Tutorial
This tutorial contains simple examples of DatabaseLink that give an overview of its functionality and some ideas of how to get started. It uses a lightweight database, HSQLDB, that is installed as part of DatabaseLink. This allows you to try examples in the documentation without having to install your own database. The other DatabaseLink tutorials give detailed reference information.
DatabaseLink provides two styles of interface for working with a database: a command‐line interface, which is more flexible and is useful for using database commands inside programs, and a graphical interface, which is simpler to use. Both interfaces are discussed here.
When you have finished trying these examples, you may wish to restore the example database, by using the "DatabaseLink`DatabaseExamples`" package, as described in "Using the Example Databases".
The Command‐Line Interface
Introduction
The command‐line interface is a powerful and flexible interface that is particularly appropriate if you want to write programs that use database functionality. This section discusses a number of different operations that use a demonstration database.
If you find that the examples in this section do not work as shown, you may need to install or restore the example database with the "DatabaseLink`DatabaseExamples`" package, as described in "Using the Example Databases".
Loading the Package
DatabaseLink is a Wolfram Language add-on application. Before any functions from the package can be used, it must be loaded as follows.

https://wolfram.com/xid/0qdcwnf6ro2j-sk
Connecting to the Database
The details of connecting to a database are described in "Database Connections". The command‐line method uses the function OpenSQLConnection, which returns a handle that can be used to work with a database. The following opens a connection to an included sample database.
If you find that the examples in this section do not work as shown, you may need to install or restore the example database with the "DatabaseLink`DatabaseExamples`" package, as described in "Using the Example Databases".

https://wolfram.com/xid/0qdcwnf6ro2j-w3e

There is also a GUI method to connect to the database that is invoked by executing OpenSQLConnection with no arguments. When this is done, the Connection Tool appears; at this point a connection must be opened or the tool canceled before operations can continue.

https://wolfram.com/xid/0qdcwnf6ro2j-bj9
You can use the Connection Tool to connect to the example database. Further information on how to open a connection to a database is provided in "Database Connections".
Fetching Data
A relational database consists of a set of tables; each table contains data in various categories (typically called columns). Each row of a table contains data values for the different categories. The database application provides functions for managing this data by supporting features such as querying, inserting, updating, or dropping data.
Tables are fundamental to relational databases, and it is important to have a convenient way to list them. You can do this with the SQLTables command as follows.

https://wolfram.com/xid/0qdcwnf6ro2j-mxs

You can see information on the specific columns in a table with the SQLColumns command. An example that provides information on the columns in the SAMPLETABLE1 table follows.

https://wolfram.com/xid/0qdcwnf6ro2j-u2s

You can retrieve the data in the SAMPLETABLE1 table by executing an SQLSelect command as follows.

https://wolfram.com/xid/0qdcwnf6ro2j-gx8

The result of the database query is a Wolfram Language list, which can be used in any Wolfram Language command. In the following example, the last element of each row is plotted.

https://wolfram.com/xid/0qdcwnf6ro2j-s69

The following example retrieves data from the SALES table, but adds column headings and outputs the result in a tabular form.

https://wolfram.com/xid/0qdcwnf6ro2j-mb1

DatabaseLink also allows you to enter raw SQL commands; this might be useful if you are already familiar with SQL and do not want to learn a new language. Here is an example that shows how to obtain all the data in the SALES table.

https://wolfram.com/xid/0qdcwnf6ro2j-jzg

More information on fetching data is available in "Selecting Data".
Inserting Data
You can use the SQLInsert command to insert data in the table. For example, this adds a new row to the SAMPLETABLE1 table.

https://wolfram.com/xid/0qdcwnf6ro2j-k7n

You can see the extra row that has been added.

https://wolfram.com/xid/0qdcwnf6ro2j-nrj

It is also possible to use a raw SQL command to insert more data. Note how the string being inserted, Day7, uses single-quote characters ('). It is also possible to use double-quote characters ("), though these need to be preceded with a Wolfram Language string escape backslash character (\).

https://wolfram.com/xid/0qdcwnf6ro2j-v5s

Another version of raw SQL commands involves using placeholders to represent where the arguments will go and then giving SQLExecute a list argument that contains the actual arguments. This is particularly useful, since it avoids the need to concatenate strings to form the raw command.

https://wolfram.com/xid/0qdcwnf6ro2j-ipc

This shows the data that is currently in the table.

https://wolfram.com/xid/0qdcwnf6ro2j-ofn

More information on inserting data is available in "Inserting Data".
Updating Data
You can use the SQLUpdate command to update data in the table. Often this is combined with a condition, so that only some of the data is modified. For example, this sets all entries of the VALUE column that are greater than 8 to 7.

https://wolfram.com/xid/0qdcwnf6ro2j-v0m
You can see the changes that have been made.

https://wolfram.com/xid/0qdcwnf6ro2j-o1t

It is also possible to use a raw SQL command to update data. This sets all rows for which the VALUE entry is greater than or equal to 6 to 7.

https://wolfram.com/xid/0qdcwnf6ro2j-hz6


https://wolfram.com/xid/0qdcwnf6ro2j-ca5

More information on updating data is available in "Updating Data".
Deleting Data
You can use the SQLDelete command to delete data in the table. Often this is combined with a condition, so that only some of the data is modified. For example, this deletes all rows for which the VALUE entry is 7 or greater.

https://wolfram.com/xid/0qdcwnf6ro2j-o5v

You can see the changes that have been made.

https://wolfram.com/xid/0qdcwnf6ro2j-ogn

It is also possible to use a raw SQL command to delete data. This deletes all entries for which the VALUE entry is greater than 5.7.

https://wolfram.com/xid/0qdcwnf6ro2j-hwm

There is only one row left in the database now.

https://wolfram.com/xid/0qdcwnf6ro2j-f4m

More information on deleting data is available in "Deleting Data".
Batch Commands
If you want to repeat the same command many times, you can do this by providing repeated arguments in a list. Carrying out the same command like this is much faster than doing each command separately.
The following command inserts two rows.

https://wolfram.com/xid/0qdcwnf6ro2j-zf5

This uses a raw SQL command to insert two more rows.

https://wolfram.com/xid/0qdcwnf6ro2j-gr5

The result of the insert commands can be seen as follows.

https://wolfram.com/xid/0qdcwnf6ro2j-f4z

Closing the Connection
When you have finished with the connection, you can close it.

https://wolfram.com/xid/0qdcwnf6ro2j-car
More information on working with connections is provided in "Database Connections". If you have modified the database and want to restore it, you can use the "DatabaseLink`DatabaseExamples`" package, as described in "Using the Example Databases".
The Database Explorer
The Database Explorer is a graphical interface to database functionality. It can be launched by loading DatabaseLink and executing the command DatabaseExplorer[].

https://wolfram.com/xid/0qdcwnf6ro2j-mos

When the Database Explorer opens, you can connect to the different databases that are configured for your system. You can also create new connections. After you connect to a database, you can view the tables and columns, as seen in the following example.
You can then see the data in the database by clicking the Result tab. Here is an example view.
The Database Explorer supports many more features, such as forming more complicated queries, saving queries, and creating reports with the result of a query (saved as a Wolfram Language notebook). These are described in "The Database Explorer".