Firebird

  Home  Databases Programming  Firebird


“Firebird Interview Questions and Answers will teach us now that Firebird is an open source relational database management system that runs on GNU/Linux, Windows, and a variety of Unix platforms. The database forked from Borlands open source edition of InterBase in 2000, so learn Firebird or get preparation for the job of Firebird with the help of this Firebird Interview Questions with Answers guide”



44 Firebird Questions And Answers

21⟩ How to extract metadata for the entire database?

It's quite simple, use isql with -x or -a options. Please be careful and test if it works. Some commercial administration tools like to play with system tables directly, and isql isn't always able to understand their hacks.

You can also extract DDL with FlameRobin. Open the properties page for the database and select DDL option at the top.

 121 views

22⟩ How to export data from database and import into another?

If your databases are on-line, i.e. visible to each other via network, then you can use some data pump tool like freeware IB Pump or some of advanced commercial administration tools which have this option integrated.

If your databases are offline, you should first export the data and then import it on the other end. Most admin. tools can do export to CVS,XML or INSERT statements. If efficiency is important, or your have data with BLOB column, you can use the open source FBExport tool.

If you are looking for a way to easily import CSV or XML data into Firebird, take a look at XMLWizard tool. You can also use it to import data from Microsoft Excel or OpenOffice by saving the sheet to .csv format and then importing via XMLWizard

 139 views

23⟩ How to drop all foreign keys in database?

Deleting all foreign keys can be done by querying the system tables and droping them one by one. If you use Firebird 2 or higher, it can be done with a single SQL statement:

set term !! ;

EXECUTE BLOCK RETURNS (stmt VARCHAR(1000)) AS

BEGIN

FOR

select 'alter table '||r.rdb$relation_name ||' drop constraint '||r.rdb$constraint_name||';'

from rdb$relation_constraints r

where (r.rdb$constraint_type='FOREIGN KEY')

into :stmt

DO begin suspend; execute statement :stmt; end

END!!

set term ; !!

If you use Firebird 1.x, you can run the following query to get statements to execute and then copy/paste the result and execute:

select 'ALTER TABLE '||r.rdb$relation_name

||' DROP CONSTRAINT '||r.rdb$constraint_name||';'

from rdb$relation_constraints r

where (r.rdb$constraint_type='FOREIGN KEY')

 152 views

24⟩ How to do replication of Firebird databases?

Firebird does not offer replication out-of-the-box, you need to use some 3rd party tools. Those external tools add specific triggers that log all the changes in database and replicate to other databases.

 164 views

25⟩ How to disconnect the user connection?

Currently there is no easy way of doing it. You can bring database to some of shutdown modes, but it affects all users. If you use Classic you can (with some effort) find the users process by detecting the IP address and open database files of that process and simply kill that process. With Super Server it is not possible as the connection is run in a thread of multithreaded SuperServer process.

There are plans for future versions of Firebird to address this. For example, version 2.1 introduces ability to cancel running queries (by deleting the relevant records from MON$STATEMENTS table).

 133 views

26⟩ How to prevent firebird.log file from filling up the disk partition?

Here are some tips:

a) create a scheduled task or cron job that will truncate or rotate the log file. By rotation, we mean renaming the files in such way that you always have a number of previous logs available. Example:

delete firebird.log.5

rename firebird.log.4 firebird.log.5

rename firebird.log.3 firebird.log.4

rename firebird.log.2 firebird.log.3

rename firebird.log.1 firebird.log.2

rename firebird.log firebird.log.1

This way you'll always have last five logs available, and those too old get deleted. You can also use zip, rar, bzip2 or some other packer to compress the old log files. Since they are plain text, they compress very well.

b) redirect logging to void. For example, on Linux, you can do it by creating a symlink to /dev/null instead of the regular log file:

# cd /opt/firebird

# rm -f firebird.log

# ln -s /dev/null firebird.log

Please note that you really shouldn't be doing this, as you will lose all valuable diagnostic information. It's better to try to find what are the errors, what's causing them and fix the problem at the source.

 144 views

27⟩ How to recreate the index on a Firebird table?

Recreating the index:

ALTER INDEX indexName INACTIVE;

ALTER INDEX indexName ACTIVE;

Please note that it does not work for indices automatically created by the system like the ones for primary key or foreign key. You might want to drop and recreate the constraints in such case.

To recalculate the index statistics (which is enough for most cases), use:

SET STATISTICS INDEX indexName;

 146 views

28⟩ How to pump the data from one database to another?

Many recommend IB Pump or IB Data Pump, but the problem is when you have complex relations between tables. In such cases, it is better to use tool like FBCopy which sorts the tables by dependencies (foreign keys, check constraints) into correct orde

 133 views

29⟩ How to reorder the table columns (fields)?

While the order should not matter to applications (you should always use explicit column names in queries), perhaps it's easier for you when you work with tables in database administration tools. You can move a column to different location using the following SQL statement:

ALTER TABLE table_name ALTER field_name POSITION new_position;

Positions are numbered from one. If you wish to exchange two fields, make sure you run the statement for both of them. It's easy to run tools like FlameRobin to do this (Reorder Fields option at table's properties screen).

 128 views

30⟩ How to repair a corrupt Firebird database?

Here's a short step-by-step walkthrough:

* disconnect users and disable incoming connections to the database

* make a copy of database file (or two copies) and work on that

* use GFIX with -v option to validate the database file

* use GFIX with -v and -f to do full validation

If problem is not too serious, you can try to backup the broken db and restore under a new name:

* use GFIX -mend to prepare corrupt database for backup

* use GBAK -b -g to backup the database. -g disables garbage collection (FAQ #41)

* use GBAK -c to restore backup to a new database.

If you succeed, you have fixed the problem and have a functional database. If not, you can try to create an empty database with the same structure and pump the data to it.

One of the reasons why backup or restore can fail is if some broken database triggers exist, and prevent connection to the database. For example, a database trigger might use some table which has a broken index, etc. To work around this, connect to database with isql tool using -nodbtriggers option and then disable those triggers. You can enable them later when you fix other problems and get a working database again.

 121 views

31⟩ How to select a random record from a table?

There is no such feature in Firebird, but you can use some tricks. The following example requires that you have a unique integer column (primary key is usually used):

SELECT ...field_list...

FROM table t1

WHERE conditions

ORDER BY (t1.int_col + seed)*4294967291-((t1.int_col + seed)*4294967291/49157)*49157;

If you just need one random record, limit the result set using FIRST or ROWS clause. This query will give consistent records for the same seed. If you wish to be completely random, you need to change the seed. You could use the value of int_col from previous run, or simply fetch a new value from a generator (just make sure the same value for seed is used in both places in expression).

 112 views

32⟩ How to specify transaction or query timeout?

In order to keep the server low reasonable, you might want to limit the time a single query can consume. Firebird does not support this directly yet (there are plans for Firebird 3.0).

However, you could periodically query the monitoring tables (Firebird 2.1 and above) to detect and cancel long running queries. You can do:

SELECT * FROM MON$STATEMENTS;

Look for those having MON$STATE set to 1.

Please note that your database needs to be at least ODS 11.1, i.e. created with Firebird 2.1 or above. Older databases won't show you these tables even if you use Firebird 2.1 to access them. To learn more about ODS and how to retrieve it.

 137 views

33⟩ How to stop SuperServer service on Linux using only Firebird tools?

The server is started and stopped by 'fbmgr' executable from 'bin' directory of your Firebird installation. It is called 'ibmgr' in Firebird 1.0. To start the server type:

/opt/firebird/bin/fbmgr -start

To start the server with Guardian (Guardian watches the server and restarts it if it crashes) type:

/opt/firebird/bin/fbmgr -start -forever

To stop a running server, type:

/opt/firebird/bin/fbmgr -shut -user SYSDBA -pass *****

To force a shutdown, type:

/opt/firebird/bin/fbmgr -shut -force -user SYSDBA -pass *****

If you use Firebird 2 or higher, you can also use the regular 'kill' command to shutdown the server, as it handles the signals properly. Make sure you first kill the guardian and then the server (otherwise guardian would restart the server).

 129 views

35⟩ How to tell Firebird to only accept conections from XYZ host or network?

This isn't really a thing you should be configuring in Firebird. There is a RemoteBindAddress setting in firebird.conf which configures on which interfaces/addresses the Firebird listens but that's all. You should really use your system's firewall to set this up.

Beside firewall, if you use Classic on Linux, you can use xinetd or inetd access control files /etc/hosts.allow and /etc/hosts.deny. With xinetd you can also edit the xinetd configuration file for Firebird service, which is in /etc/xinetd.d/firebird and add a line like this:

"only_from = 192.168.0.0/24"

 138 views

36⟩ How to write UDF s in Delphi?

It's quite simple, the only thing you need to remember is that you must always use ib_util_malloc() to allocate memory if your UDF returns string result. The UDF must be declared as FREE_IT, so that Firebird releases the memory after it reads the string.

To use ib_util_malloc(), you need to import it from ib_util.dll into your program - and make sure you use it instead of regular memory alocating functions. Here's a simple example of Delphi UDF:

function ib_util_malloc(l: integer): pointer; cdecl; external 'ib_util.dll';

function ChangeMyString(const p: PChar): PChar; cdecl;

var

s: string;

begin

s := DoSomething(string(p));

Result := ib_util_malloc(Length(s) + 1);

StrPCopy(Result, s);

end;

Declaration in Firebird:

DECLARE EXTERNAL FUNCTION ChangeMyString

CString(255)

RETURNS CString(255) FREE_IT

ENTRY_POINT 'ChangeMyString' MODULE_NAME '......'

 129 views

37⟩ Is it possible to determine clients IP address?

To get it from SQL, you need to use Firebird 2.0 (own address), or Firebird 2.1 (anyone's):

If you use Firebird 2.0 or higher, use the GET_RDB$Context function with ('SYSTEM', 'CLIENT_ADDRESS') parameters.

If you use Firebird 2.1 or higher, you can get address of any client by selecting from the monitoring tables.

With Firebird 1.x you can try to get the information from TCP/IP stack, using netstat or lsof commands from the command-prompt. Just search for Firebird's port (3050 or gds_db).

 115 views

38⟩ Is there a way to automate SQL execution from the command-line, batch job or shell script?

Yes. You can use isql for this. It is located in the 'bin' directory of your Firebird installation. If you wish to try it interactively, run isql and then type:

isql localhost:my_database -user sysdba -pass ******

SQL> input my_script.sql;

SQL> commit;

SQL>

To run it from a batch (.bat) file or a shell script, use -i switch:

isql -i my_script.sql localhost:my_database -user sysdba -pass ******

If you have some DML statements in your script, make sure you put the COMMIT command at the end of the file. Also, make sure the file ends with a newline, as isql executes the commands on the line only after it gets the newline character.

 154 views

39⟩ Is there a way to detect whether fbclient.dll or fbembed.dll is loaded?

There are some ways to detect it:

- check the size of DLL file

- if you are using different versions of Firebird (for example 1.5.4 and 2.0.1, you can query the server version via Services API)

You should understand that fbembed can be used as a regular Firebird client. Checking whether embedded or fbclient is loaded for licensing or similar needs is really not useful. You could use the connection string as guide, but super server can establish direct local connections without localhost prefix.

If you combine all this information, you could get some conclusions:

- if DLL size matches fbembed and connection string doesn't have hostname, you are using embedded

- if DLL size matches fbembed and connection string does have hostname, you are using either super server or classic

- if DDL size matches fbclient and connection string doesn't have hostname, you are using super server via local connection (IPC, XNET)

- if DLL size matches fbclient and connection string does have hostname, you are using either super server or classic

 138 views

40⟩ Is there an example how to configure ExternalFileAccess setting in firebird.conf?

Firebird's config file (firebird.conf) does have descriptions inside that explain everything, but sometimes they are confusing and hard to understand what should you do exactly if you don't have examples. One of such settings is ExternalFileAccess. Some people are even tempted to put Full as it is much easier than trying to guess what's the correct format. Here are the basic settings ('None' to disallow external tables and 'Full' to allow them anywhere) which you probably understood yourself:

ExternalFileAccess = None

ExternalFileAccess = Full

And here are those tricky Restrict settings:

ExternalFileAccess = Restrict C:somedirectory

For multiple directories, use something like this:

ExternalFileAccess = Restrict C:somedirectory;C:someotherdirectory

For Linux users:

ExternalFileAccess = Restrict /some/directory

 157 views