Firebird 5 のインストール,データベース作成,テーブル定義とレコード挿入(Windows 上)
Firebird 5(リレーショナルデータベース管理システム)のWindows上での導入と基本操作について説明する.wingetコマンドを用いてインストールし,環境変数の設定を行う.ISQL(対話型SQLツール)を使用してデータベースを作成し,テーブル定義とレコード挿入を行う.また,アカウント作成やパスワード変更などの管理タスクもgsecコマンドで実行可能である.データ型やSQL文の具体例も示している.
【目次】
- インストール前に決めておく事項
- Firebird のインストール(Windows 上)
- ISQL の起動と終了
- データベースの作成
- テーブル定義とレコード挿入
- アカウント(ユーザ名とパスワード)作成,パスワード変更
【関連する外部ページ】
- Firebird の URL: https://firebirdsql.org/
- Firebird documentation subproject: http://www.firebirdsql.org/index.php?op=devel&sub=doc
利用条件などは利用者で確認すること.
謝辞:このページで紹介するソフトウェア等の作者に感謝する
インストール前に決めておく事項
- Firebird インストールディレクトリ: c:\firebird50
* 自由に決めてよい.日本語などの全角文字を含まないこと.
Firebird 5 のインストール(Windows 上)
- Windows で,コマンドプロンプトを管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
- 次のコマンドを実行
次のコマンドは,Firebird 5をインストールし,パスを通すものである.
winget install --location "c:\firebird50" FirebirdProject.Firebird.5 powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\firebird50\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
- sysdbaパスワードの設定.
最初に,「cd c:\firebird50\bin」で,Firebirdのインストールディレクトリに移動する. 既定(デフォルト)のsysdbaパスワードは「masterkey」であり,これを使用して,gsec コマンドで,新しいsysdbaパスワードを設定している
cd c:\firebird50 gsec -user sysdba -pass masterkey -mo sysdba -pw 新しいパスワード
ISQL の起動と終了
- ISQL の起動
ISQL の起動は,Windows のメニューで,次のように操作する.
「Firebird 5.0 (x64)」 → 「Firebird ISQL Tool」
あるいは.コマンドプロンプトで,次のように実行する.「sysdba」は,Firebird のデータベース管理者のユーザ名である.これは,自動作成されるものである.
"c:\firebird50\isql.exe" -u sysdba
- ISQL の終了
ISQL の終了は 「exit;」.
exit;
データベースの作成
データベースを作成するために,SQL の create database コマンドを使用する.
- Firebird ISQL の起動
ISQL を起動するには,Windows のメニューで,次のように操作する. 「スタート」 → 「Firebird 5.0 (x64)」 → 「Firebird ISQL Tool」
- データベース生成
utf8の場合
create database 'testdb' default character set utf8;
sjisの場合
create database 'testdb' default character set sjis;
- 「show database;」で,データベースが生成されたことを確認
show database;
- 「exit;」で終了.
exit;
テーブル定義とレコード挿入
- Firebird ISQL の起動
ISQL を起動するには,Windows のメニューで,次のように操作する. 「スタート」 → 「Firebird 5.0 (x64)」 → 「Firebird ISQL Tool」
- 「connect 'testdb';」で,データベースを開く.
connect 'testdb';
- SQL を用いたテーブル定義
Firebird のデータ型 Char: 32767 bytes Varchar: 32767 bytes Smallint: 16 bits Integer: 32 bits BigInt: 64 bits DECIMAL: Float: 32 bits, 3.4x10^-38 to 3.4x10^38, 7 digit precision Double: 64 bits, 1.7x10^-308 to 1.7x10^308, 15 digit precision Timestamp: 64 bits, 1 Jan 100 to 28 Feb 32768. Date: 32 bits, 1 Jan 100. to 29 Feb 32768. Time: 32 bits, 00:00 to 23:59.9999 Blob: 32GB
create table order_records ( id integer primary key not null, yy integer not null CHECK ( yy > 2008 ), mm integer not null CHECK ( mm >= 1 AND mm <= 12 ), dd integer not null CHECK ( dd >= 1 AND dd <= 31 ), customer_name varchar(32) not null, product_name varchar(32) not null, unit_price real not null check ( unit_price > 0 ), qty integer not null check ( qty > 0 ), created_at timestamp not null, updated_at timestamp, check ( ( unit_price * qty ) < 200000 ) );
- SQL を用いたレコード挿入
insert into order_records values( 1, 2024, 7, 26, 'kaneko', 'orange A', 1.2, 10, current_timestamp, NULL ); insert into order_records values( 2, 2024, 7, 26, 'miyamoto', 'Apple M', 2.5, 2, current_timestamp, NULL ); insert into order_records values( 3, 2024, 7, 27, 'kaneko', 'orange B', 1.2, 8, current_timestamp, NULL ); insert into order_records values( 4, 2024, 7, 28, 'miyamoto', 'Apple L', 3, 1, current_timestamp, NULL );
- 確認表示
select * from order_records;
- 更新し確認表示
update order_records set unit_price = 11.2 where id = 1; select * from order_records;
- テーブル一覧の表示
show tables;
- 「exit;」で終了.
exit;
アカウント(ユーザ名とパスワード)作成,パスワード変更
アカウント(ユーザ名とパスワード)作成
"c:\firebird50\gsec" -user sysdba -pass masterkey -add testuser -pw hoge$#34
- Firebird インストールディレクトリ c:\firebird50 の下にある gsec を使う.
- 「-user sysdba -pass masterkey」: Firebird データベース管理者 (sysdba) でログインするという意味.
権限上,Firebird データベース管理者だけがアカウントを作成できるため. 「masterkey」と書いてある箇所は, Firebird データベース管理者 (sysdba)のパスワードに読み替えてください.
- 「-add testuser -pw hoge$#34」: ユーザ名 testuser,パスワード hoge$#34 で,アカウントを作成する.
パスワードの変更
ユーザ testuser について, パスワードを hoge$#34 から newpassに変更したいときの実行例
"c:\firebird50\gsec" -user testuser -pass hoge$#34 -mo testuser -pw newpass
データベース管理者のパスワードの変更
データベース管理者 sysdba のパスワードを hoGE9!#8に設定したいときの実行例
"c:\firebird50\gsec" -user sysdba -pass masterkey -mo sysdba -pw hoGE9!#8
- Firebird インストールディレクトリ c:\firebird50 の下にある gsec を使う.
- 「-user sysdba -pass masterkey」: Firebird データベース管理者でログインするという意味.
- 「-mo sysdba -pw ... : Firebird データベース管理者 (sysdba) のパスワードを変更するという意味.
【まとめ】 Firebird 5の導入から基本操作までを体系的に説明,データベース管理の基礎を示した.