Defining a Primary Key このトピックを印刷 前のページ 1つ上のレベル 次のページ

ホーム >  データベース構造の探索と変更 > デザインエディター内でのテーブルのデザイン >

Defining a Primary Key

デフォルトでは、 when creating a new table in DatabaseSpy's デザインエディター, a primary key is automatically defined on the first column. This option can, however, be disabled in the デザインエディターオプション, you will then have to define a primary key manually. The instructions below show how to add a primary key in デザインエディター. 更に、 you can always enter and execute the appropriate SQL statement in an SQL エディターウィンドウ.


1.If a table already has a primary key, no primary key may be defined when you show a table in a デザインエディターウィンドウ.
2.The column on which the primary key is defined must not be nullable (that is, the Nullable チェックボックス next to it must be cleared in the table design). If you attempt to create a primary key on a nullable column, a warning ダイアログボックス is displayed.


There are several ways to define a primary key in デザインエディター ウィンドウ:


Select the Make 主キー command from コンテキストメニュー of a column in a table design.


Drag a column from the Columns section and drop it onto the Key section of a table design. To create a primary key on multiple columns, first select multiple columns while holding the Shift key pressed, and then drag them into the Keys section. This menu is also displayed if you click the Create new キー制約 ic_add-column アイコン in the Key section of the table design.


Right-click anywhere into the table design and select 「新規の挿入 | Key | Primary Key をコンテキストメニューから選択します。 または、expand the Keys section, click the Create new キー制約 ic_add-column アイコン at the bottom of the Key column and select Create Primary Key from the menu that pops up.


Regardless of the approach chosen above, the primary key is added to the Key section of the table design.


デフォルトでは、 the name of the new primary key is <generated>, which means its name will be automatically generated by the database. This ensures uniqueness of the name in the database. If necessary, you can override 生成された名前. To do this, enter a descriptive name in place of <generated>, and press Enter.


If you override 生成された名前 with a custom name, the new name must be unique; otherwise, errors will occur when the database change script is executed.


メモ:In MySQL and MariaDB databases, the name of the primary キー制約 is always PRIMARY. In SQLite, custom key names are ignored by the database; therefore, the primary key displayed in the オンラインブラウザー of DatabaseSpy appears with the name <generated>.


To add the primary key on multiple columns, click the Add Column to Key ic_add-column ボタン (at the bottom of the "Columns" column) and choose a column from the ドロップダウンリスト that opens. To remove a column from a primary key, right-click the column (in the "Columns" column), and then select Delete selected objects をコンテキストメニューから選択します。


As always when making changes to the database structure, a change script is generated which must be executed in order to actually create the primary key in the database.


Columns that have a primary key assigned are marked with the primary key ic_primary-key-column アイコン in デザインエディター ウィンドウ and the オンラインブラウザー.

(C) 2019 Altova GmbH