- Connecting to the sample database
- Creating a database using isql
- Firebird SQL
In the examples
subdirectory of your Firebird installation is a sample database named employee.fdb
. You can use this database to “try your wings”.
If you move the sample database, be sure you move it to a hard disk that is physically attached to your server machine. Shares, mapped drives or (on Unix) mounted SMB (Samba) filesystems will not work. The same rule applies to any databases that you create.
There are two elements to a TCP/IP connection string: the server name and the disk/filesystem path. Its format is as follows:
-
For a Linux server:
servername:/filesystem-path/database-file
Example on a Linux or other Posix server named
serverxyz
:serverxyz:/opt/interbase/examples/employee.fdb
-
For a Windows server:
servername:DriveLetter:\filesystem-path\database-file
Windows example:
serverxyz:C:\Program Files\Firebird\examples\employee.fdb
Connecting to a Firebird database requires the user to authenticate using a user name and a valid password. Any user other than SYSDBA, root
(on Posix systems), or Administrator
(on Windows systems, if Firebird runs as this user) needs also to have permissions to objects inside a database. For simplicity here, we will look at authenticating as SYSDBA using the password masterkey
.
There are several different ways to connect to a database using isql. One way is to start isql in its interactive shell. Go to the bin
subdirectory of your Firebird installation and, at that prompt, type the command isql (on Linux: ./isql) [↵ means “hit Enter”]:
C:\Program Files\Firebird\Firebird_1_5\bin>isql↵Use CONNECT or CREATE DATABASE to specify a databaseSQL>CONNECT "C:\Program Files\Firebird\Firebird_1_5\examples\employee.fdb"↵CON>user 'SYSDBA' password 'masterkey';↵
Important
-
In isql, every SQL statement must end with a semicolon. If you hit Enter and the line doesn't end with a semicolon, isql assumes that the statement continues on the next line and the prompt will change from
SQL>
toCON>
. This enables you to spread long statements over multiple lines. If you hit Enter after your statement and you've forgotten the semicolon, just type it on the empty line after theCON>
prompt and press Enter again. -
If you run Classic Server on Linux, a fast, direct local connection is attempted if the database path does not start with a hostname. This may fail if your Linux login doesn't have sufficient access rights to the database file. In that case, connect to
localhost
:/
<path>. Then the server process (with Firebird 1.5 usually running asfirebird
) will open the file. On the other hand, network-style connections may fail if a user created the database in Classic local mode and the server doesn't have enough access rights. -
If you run Classic Server on Windows, you must specify a hostname (which may be
localhost
) plus a full path, or the connection will fail.
Note
Although single quote symbols are the “norm” for delimiting strings in Firebird, double quote symbols were used with the database path string in the above example. This is sometimes necessary with some of the command-line utilities where the path string contains spaces. Single quotes should work for paths that do not contain spaces.
The quotes around “SYSDBA” and “masterkey” are optional, by the way. Database paths without spaces also don't need to be quoted.
At this point, isql will inform you that you are connected:
DATABASE "C:\Program Files\Firebird\Firebird_1_5\examples\employee.fdb",User: sysdbaSQL>
You can now continue to play about with the employee.fdb
database. The characters isql stand for interactive SQL [utility]. You can use it for querying data, getting information about the metadata, creating database objects, running data definition scripts and much more.
To get back to the command prompt type
SQL>QUIT;↵
For more information about isql, see Using Firebird, Chapter 10: Interactive SQL Utility (isql).
GUI client tools usually take charge of composing the CONNECT string for you, using server, path, user name and password information that you type into prompting fields. Use the elements as described in the preceding topic.
Note
-
It is quite common for such tools to expect the entire server + path as a single string
-
Remember that file names and commands on Linux and other Posix command shells are case-sensitive
There is more than one way to create a database using isql. Here, we will look at one simple way to create a database interactively – although, for your serious database definition work, you should create and maintain your metadata objects using data definition scripts. There is a complete chapter in the Using Firebird manual discussing this topic.
To create a database interactively using the isql command shell, get to a command prompt in Firebird's bin
subdirectory and type isql (Windows) or ./isql (Linux):
C:\Program Files\Firebird\Firebird_1_5\bin>isql↵Use CONNECT or CREATE DATABASE to specify a database
Now, you can create your new database interactively. Let's suppose that you want to create a database named test.fdb
and store it in a directory named data
on your D
drive:
SQL>CREATE DATABASE 'D:\data\test.fdb' page_size 8192↵CON>user 'SYSDBA' password 'masterkey';↵
Important
-
In the CREATE DATABASE statement the quotes around path string, username, and password are mandatory. This is different from the CONNECT statement.
-
If you run Classic Server on Linux and you don't start the database path with a hostname, creation of the database file is attempted with your Linux login as the owner. This may or may not be what you want (think of access rights if you want others to be able to connect). If you prepend
localhost:
to the path, the server process (with Firebird 1.5 usually running asfirebird
) will create and own the file. -
If you run Classic Server on Windows, you must specify a hostname (which may be
localhost
) plus a full path, or the creation will fail.
The database will be created and, after a few moments, the SQL prompt will reappear. You are now connected to the new database and can proceed to create some test objects in it.
To verify that there really is a database there, type in this query:
SQL>SELECT * FROM RDB$RELATIONS;↵
The screen will fill up with a large amount of data! This query selects all of the rows in the system table where Firebird stores the metadata for tables. An “empty” database is not empty – it contains a database which will become populated with metadata as you begin creating objects in your database.
To get back to the command prompt type
SQL>QUIT;↵
For more information about isql, see Using Firebird, Chapter 10: Interactive SQL Utility (isql).
Every database management system has its own idiosyncrasies in the ways it implements SQL. Firebird adheres to the SQL standard more rigorously than any other RDBMS except possibly its “cousin”, InterBase®. Developers migrating from products that are less standards-compliant often wrongly suppose that Firebird is quirky, whereas many of its apparent quirks are not quirky at all.
Firebird accords with the SQL standard by truncating the result (quotient) of an integer/integer calculation to the next lower integer. This can have bizarre results unless you are aware of it.
For example, this calculation is correct in SQL:
1 / 3 = 0
If you are upgrading from a RDBMS which resolves integer/integer division to a float quotient, you will need to alter any affected expressions to use a float or scaled numeric type for either dividend, divisor, or both.
For example, the calculation above could be modified thus in order to produce a non-zero result:
1.000 / 3 = 0.333
Strings in Firebird are delimited by a pair of single quote (apostrophe) symbols – 'I am a string'
– (ASCII code 39, not 96). If you used earlier versions of Firebird's relative, InterBase®, you might recall that double and single quotes were interchangeable as string delimiters. Double quotes cannot be used as string delimiters in Firebird SQL statements.
If you need to use an apostrophe inside a Firebird string, you can “escape” the apostrophe character by preceding it with another apostrophe.
For example, this string will give an error:
'Joe's Emporium'
because the parser encounters the apostrophe and interprets the string as 'Joe'
followed by some unknown keywords.
To make this a legal string, double the apostrophe character:
'Joe''s Emporium'
Notice that this is TWO single quotes, not one double-quote.
The concatenation symbol in SQL is two “pipe” symbols (ASCII 124, in a pair with no space between). In SQL, the “+” symbol is an arithmetic operator and it will cause an error if you attempt to use it for concatenating strings. The following expression prefixes a character column value with the characters “Reported by:
”:
'Reported by: ' || LastName
Take care with concatenations. Be aware that Firebird will raise an error if your expression attempts to concatenate two or more char or varchar columns whose potential combined lengths would exceed the maximum length limit for a char or a varchar (32 Kb).
See also the note below, Expressions involving NULL, about concatenating in expressions involving NULL
.
Before the SQL-92 standard, it was not legal to have object names (identifiers) in a database that duplicated keywords in the language, were case-sensitive or contained spaces. SQL-92 introduced a single new standard to make any of them legal, provided that the identifiers were defined within pairs of double-quote symbols (ASCII 34) and were always referred to using double-quote delimiters.
The purpose of this “gift” was to make it easier to migrate metadata from non-standard RDBMSs to standards-compliant ones. The down-side is that, if you choose to define an identifier in double quotes, its case-sensitivity and the enforced double-quoting will remain mandatory.
Firebird does permit a slight relaxation under a very limited set of conditions. If the identifier which was defined in double-quotes:
-
was defined as all upper-case,
-
is not a keyword, and
-
does not contain any spaces,
...then it can be used in SQL unquoted and case-insensitively. (But as soon as you put double-quotes around it, you must match the case again!)
Warning
Don't get too smart with this! For instance, if you have tables "TESTTABLE" and "TestTable", both defined within double-quotes, and you issue the command:
SQL>select * from TestTable;
...you will get the records from "TESTTABLE", not "TestTable"!
Unless you have a compelling reason to define quoted identifiers, it is usually recommended that you avoid them. Firebird happily accepts a mix of quoted and unquoted identifiers – so there is no problem including that keyword which you inherited from a legacy database, if you need to.
Warning
Some database admin tools enforce double-quoting of all identifiers by default. Try to choose a tool which makes double-quoting optional.
In SQL, NULL
is not a value. It is a condition, or state, of a data item, in which its value is unknown. Because it is unknown, NULL
cannot behave like a value. When you try to perform arithmetic on NULL
, or involve it with values in other expressions, the result of the operation will almost always be NULL
. It is not zero or blank or an “empty string” and it does not behave like any of these values.
So – here are some examples of the types of surprises you will get if you try to perform calculations and comparisons with NULL
:
-
1 + 2 + 3 +
NULL
=NULL
-
not (
NULL
) =NULL
-
'Home ' || 'sweet ' ||
NULL
=NULL
if (a = b) then MyVariable = 'Equal';else MyVariable = 'Not equal';
After executing this code,
MyVariable
will be'Not equal'
if botha
andb
areNULL
. The reason is that'a = b'
yieldsNULL
if at least one of them isNULL
. If the test expression of an “if
” statement isNULL
, it behaves likefalse
: the 'then
' block is skipped, and the 'else
' block executed.Warning
Although the expression may behave like
false
in this case, it's stillNULL
. If you try to invert it usingnot()
, what you get is anotherNULL
- not “true
”.if (a <> b) then MyVariable = 'Not equal';else MyVariable = 'Equal';
Here,
MyVariable
will be'Equal'
ifa
isNULL
andb
isn't, or vice versa. The explanation is analogous to that of the previous example.FirstName || ' ' || LastName
will return
NULL
if eitherFirstName
orLastName
isNULL
.
Tip
Think of NULL
as UNKNOWN
and all these strange results suddenly start to make sense! If the value of Number
is unknown, the outcome of '1 + 2 + 3 + Number
' is also unknown (and therefore NULL
). If the content of MyString
is unknown, then so is 'MyString || YourString
' (even if YourString
is non-NULL
). Etcetera.