Skip to content

2. Firebird Tutorial

Before covering the basics of the SQL language, we will show the reader how to install Firebird SGBD as well as the IB-Expert graphical client.

2.1. Where can I find Firebird?

The main Firebird website is . The downloads page offers the following links (April 2005):

Image

You will need to download the following files:

firebird-win32
SGBD for Windows
firebird-net-provider
a class library for .NET applications that allows access to Firebird without using a driver.
firebird-ODBC-driver
the Firebird ODBC driver

Install these components. SGBD is installed in a folder with contents similar to the following:

The binaries are in the [bin] folder:

fbguard.exe
allows you to start/stop the SGBD
isql.exe
command-line client for managing databases

Note that by default, the administrator of SGBD is named [SYSDBA] and the password is [masterkey]. Menus have been installed in [Démarrer]:

Image

The option [Firebird Guardian] allows you to start/stop the SGBD. After startup, the SGBD icon remains in the Windows taskbar:

To create and manage Firebird databases using the [isql.exe] command-line client, you must read the documentation included with the product in the [doc] folder.

2.2. Firebird documentation

Documentation on Firebird and the SQL language can be found on the Firebird website (January 2006):

Image

Various manuals are available in English:

Firebird 1.5 Quick Start Guide
to get started with FB
Firebird 1.5 Error Codes
To understand the error codes returned by FB

Training manuals for the SQL language are also available:

Image

Data Definition Guide
to learn how to create tables, which data types are supported, ...
Language Reference
the reference guide for learning SQL with Firebird

A quick way to work with Firebird and learn the SQL language is to use a graphical client. One such client is IB-Expert, described in the following section.

2.3. Working with SGBD Firebird using IB- Expert

The main IB-Expert website is [http://www.ibexpert.com/]. The downloads page offers the following links:

Image

Select the free version version. Once downloaded and installed, you will have a folder similar to the following:

Image

The executable is [ibexpert.exe]. A shortcut is normally available in the [Démarrer] menu:

Image

Once launched, IBExpert displays the following window:

Image

Let’s use option [Database/Create Database] to create a database:

Server
can be [local] or [remote]. Here, our server is on the same machine as [IBExpert]. We therefore choose [local]
Database
Use the [dossier]-style button in the dropdown to select the database file. Firebird stores the entire database in a single file. This is one of its key advantages. You can transfer the database from one machine to another simply by copying the file. The suffix [.gdb] is added automatically.
Username
SYSDBA is the default administrator for current Firebird distributions
Password
masterkey is the password for the administrator SYSDBA in current Firebird distributions
Dialect
the SQL dialect to use
Register Database
if this box is checked, IBExpert will display a link to the database after it has been created

If, when clicking the [OK] creation button, you receive the following warning:

Image

it means you haven’t started Firebird. Start it. A new window will appear:

Image

Charset
Character set to use. Although the screenshot above does not show any information, it is recommended to select the [ISO-8859-1] character set from the drop-down list, which allows the use of accented Latin characters.
Server version
[IBExpert] is capable of handling various SGBD versions derived from Interbase. Select the version version of Firebird that you have installed:

Once this new window is validated by [Register], you get the following result:

Image

To access the created database, simply double-click on its link. IBExpert then displays a tree structure providing access to the database properties:

Image

2.4. Creating a data table

Let’s create a table. Right-click on [Tables] (see window above) and select option [New Table]. This opens the table properties definition window:

Let’s start by naming the table [ARTICLES] using the [1] input field:

Use the [2] input field to define a primary key [ID]:

A field is made a primary key by double-clicking the [PK] (Primary Key) field. Let’s add fields using the button above [3]:

Image

Until we have "compiled" our definition, the table is not created. Use the [Compile] button above to finalize the table definition. IBExpert prepares the SQL queries to generate the table and asks for confirmation:

Image

Interestingly, IBExpert displays the SQL queries it has executed. This allows you to learn both the SQL language and the potentially proprietary SQL dialect used. The [Commit] button validates the current transaction, while [Rollback] cancels it. Here, we accept it using [Commit]. Once this is done, IBExpert adds the created table to our database tree:

Image

By double-clicking on the table, we can access its properties:

Image

The [Constraints] panel allows us to add new integrity constraints to the table. Let’s open it:

Image

We see the primary key constraint we created. We can add other constraints:

  • foreign keys [Foreign Keys]
  • field integrity constraints [Checks]
  • field uniqueness constraints [Uniques]

Let’s specify that:

  • the fields [ID, PRIX, STOCKACTUEL, STOKMINIMUM] must be >0
  • the field [NOM] must be non-empty and unique

Open the [Checks] panel and right-click in its constraint definition area to add a new constraint:

Image

Let’s define the desired constraints:

Image

Note above that the [NOM<>''] constraint uses two apostrophes, not quotation marks. Compile these constraints using the [Compile] button above:

Image

Once again, IBExpert demonstrates its educational value by listing the SQL queries it has executed. Let’s now move on to the [Constraints/Uniques] panel to specify that the name must be unique. This means that the same name cannot appear twice in the table.

Image

Let’s define the constraint:

Image

Then let’s compile it. Once that’s done, open the [DDL] panel (Data Definition Language) for the [ARTICLES] table:

Image

This panel displays the table generation code SQL, including all its constraints. You can save this code in a script to run it later:

SET SQL DIALECT 3;
SET NAMES NONE;
CREATE TABLE ARTICLES (
    ID            INTEGER NOT NULL,
    NOM           VARCHAR(20) NOT NULL,
    PRIX          DOUBLE PRECISION NOT NULL,
    STOCKACTUEL   INTEGER NOT NULL,
    STOCKMINIMUM  INTEGER NOT NULL
);
ALTER TABLE ARTICLES ADD CONSTRAINT CHK_ID check (ID>0);
ALTER TABLE ARTICLES ADD CONSTRAINT CHK_PRIX check (PRIX>0);
ALTER TABLE ARTICLES ADD CONSTRAINT CHK_STOCKACTUEL check (STOCKACTUEL>0);
ALTER TABLE ARTICLES ADD CONSTRAINT CHK_STOCKMINIMUM check (STOCKMINIMUM>0);
ALTER TABLE ARTICLES ADD CONSTRAINT CHK_NOM check (NOM<>'');
ALTER TABLE ARTICLES ADD CONSTRAINT UNQ_NOM UNIQUE (NOM);
ALTER TABLE ARTICLES ADD CONSTRAINT PK_ARTICLES PRIMARY KEY (ID);

2.5. Inserting data into a table

It is now time to enter data into the [ARTICLES] table. To do this, use its [Data] panel:

Image

Data is entered by double-clicking the input fields in each row of the table. A new row is added using the [+] button, and a row is deleted using the [-] button. These operations are performed within a transaction that is committed using the [Commit Transaction] button (see above). Without this commit, the data will be lost.

2.6. The SQL editor for [IB-Expert]

The SQL language (Structured Query Language) allows a user to:

  1. create tables by specifying the type of data they will store and the constraints that this data must satisfy
  1. insert data into them
  2. modify certain data
  3. delete other data
  4. use the content to retrieve information
  5. ...

IBExpert allows a user to perform operations 1 through 4 graphically. We have just seen this. When the database contains many tables, each with hundreds of rows, we need information that is difficult to obtain visually. Suppose, for example, that an online store has thousands of customers per month. All purchases are recorded in a database. After six months, it is discovered that product “X” is defective. The store wants to contact everyone who purchased it so they can return the product for a free exchange. How can the addresses of these buyers be found?

  1. You could manually search through all the tables to find these buyers. That would take a few hours.
  2. We can issue a query SQL that will return a list of these people in a matter of seconds

The SQL language is useful whenever

  • the amount of data in the tables is large
  • there are many tables linked together
  • the information to be retrieved is spread across multiple tables
  • ...

We now present the SQL editor from IBExpert. This editor is accessible via option, [Tools/SQL Editor], or [F12]:

Image

This gives you access to an advanced query editor (SQL) where you can experiment with queries. Let’s enter a query:

Image

We execute the query SQL using the button above. We obtain the following result:

Image

Above, the [Results] tab displays the result table for the SQL [Select] command. To issue a new command SQL, simply return to the [Edit] tab. You will then see the SQL command that was executed.

Image

Several buttons on the toolbar are useful:

  • the [New Query] button allows you to move to a new query SQL:

Image

This brings up a blank editing page:

Image

You can then type a new command SQL:

Image

and execute it:

Image

Let’s return to the [Edit] tab. The various SQL orders issued are stored by [IB-xpert]. The [Previous Query] button allows you to return to a previously issued SQL order:

Image

You are then returned to the previous request:

Image

The [Next Query] button allows you to go to the next SQL order:

Image

You will then see the next order, SQL, in the list of saved orders SQL:

Image

The [Delete Query] button allows you to delete an order SQL from the list of saved orders:

Image

The [Clear Current Query] button clears the editor's contents for the displayed SQL order:

Image

The [Commit] button allows you to permanently save the changes made to the database:

Image

The [RollBack] button allows you to undo the changes made to the database since the last [Commit]. If no [Commit] has been performed since connecting to the database, then the changes made since that connection are undone.

Image

Let’s look at an example. Let’s insert a new row into the table:

Image

The SQL command is executed, but nothing is displayed. We do not know if the insertion took place. To find out, let’s execute the SQL command following [New Query]:

Image

We get the following result:

Image

The row has therefore been successfully inserted. Let’s now examine the table’s contents in another way. Double-click on the table [ARTICLES] in the database explorer:

Image

We get the following table:

Image

The arrow button above allows you to refresh the table. After refreshing, the table above does not change. It appears that the new row has not been inserted. Let’s return to the SQL editor (F12) and then validate the SQL command issued with the [Commit] button:

Image

Once this is done, let’s return to the [ARTICLES] table. We can see that nothing has changed even when using the [Refresh] button:

Image

Above, let’s open the [Fields] tab and then return to the [Data] tab. This time, the inserted row appears correctly:

Image

When the execution of the various SQL commands begins, the editor opens what is called a transaction on the database. The changes made by these SQL commands from the SQL editor will only be visible as long as you remain in the same SQL editor (you can open multiple instances). It is as if the SQL editor were working not on the actual database but on its own copy. In reality, this is not exactly how it works, but this analogy can help us understand the concept of a transaction. All changes made to the copy during a transaction will only be visible in the actual database once they have been committed by a [Commit Transaction]. The current transaction is then completed, and a new transaction begins.

Changes made during a transaction can be rolled back using a command called [Rollback]. Let’s try the following experiment. Start a new transaction (simply run [Commit] on the current transaction) with the following command SQL:

Image

Let’s execute this command, which deletes all rows from table [ARTICLES], and then execute [New Query], the new command SQL:

Image

We get the following result:

Image

All rows have been deleted. Remember that this was done on a copy of the [ARTICLES] table. To verify this, double-click on the [ARTICLES] table below:

Image

and view the [Data] tab:

Image

Even if you use the [Refresh] button or switch to the [Fields] tab and then back to the [Data] tab, the content above remains unchanged. This has been explained. We are in another transaction that is working on its own copy. Now let’s return to the SQL editor (F12) and use the [RollBack] button to undo the line deletions that were made:

Image

We are asked for confirmation:

Image

Let’s confirm. The SQL editor confirms that the changes have been undone:

Image

Let’s rerun the SQL query above to verify. The rows that had been deleted are now back:

Image

Operation [Rollback] has restored the copy on which editor SQL is working to the state it was in at the start of the transaction.