![]() ![]() In this case it will reference the TypeId column of the Types table. The REFERENCES Types(TypeId) specifies the column that our foreign key will reference. The FOREIGN KEY(TypeId) part declares Pets.TypeId as the foreign key.Īlthough I didn’t qualify the column name with its table name, we know it’s Pets.TypeId (and not Types.TypeId) because we’re running this in the CREATE TABLE statement for Pets. The part that creates the foreign key is this: FOREIGN KEY(TypeId) REFERENCES Types(TypeId) CREATE TABLE Types(įOREIGN KEY(TypeId) REFERENCES Types(TypeId) We can use the following code to create these two tables. In this example we’ll make it the primary key. While parent keys are usually also the primary key for the table, this is not actually a requirement. In other words, we want to make Pets.TypeId the child key (with a foreign key constraint), and Types.TypeId the parent key (with a primary key constraint). Imagine we want two tables with the following data.Īnd we want the TypeId column of the Pets table to reference the TypeId column of the Types table. Now that we’ve enabled foreign key support, let’s go ahead and create a foreign key. Note that this setting is not required for creating foreign keys, but it is required for enforcing foreign keys. If you open another connection, you’ll need to run the same statement for that connection. This will enable foreign key enforcement for your database connection. To do this, run the following statement: PRAGMA foreign_keys = ON The first thing we should do is to enable foreign key support (if it hasn’t already been done).Īssuming your SQLite library hasn’t been compiled with SQLITE_OMIT_FOREIGN_KEY or SQLITE_OMIT_TRIGGER defined, you will still need to enable foreign key support at runtime. This article provides an example of creating a foreign key when creating a table in SQLite. In the data editor, you can now select several values and navigate to the related data.Īlso, you can watch the Navigate by foreign keys video at for another example.When you create a table in SQLite, you can also create a foreign key in order to establish a relationship with another table. Referencing Rows: rows that are referencing the current object.Īll Related Rows: both referenced and referencing rows. Referenced Rows: rows that are referenced by the current object. In the Go To submenu, you can select to which type of related rows you want to navigate. Right-click a column in a table and select Go To. For a primary key, for example, the template is index template generates the following name for the not unique index: persons_FirstName_LastName_index. ![]() When you create indexes, and primary and foreign key constraints, their default names are generated according to corresponding templates. Productivity tips Modify templates for generated index and key names In the Target Name field, specify the name of the column in the target table. In the Column Name field, specify the name of the column in the child table. In the Columns pane, click the Add button ( ). In the Target Table pane, specify the name of the target table. Right-click a child table and select New | Foreign Key. In the Database tool window ( View | Tool Windows | Database), expand the data source tree until the node of a child table. In the following example, activity.activity_id is a primary key, while visitor.activity_id is a foreign key. If your database contains explicit foreign key relationships, IntelliJ IDEA automatically uses them in auto-completion, data navigation, and diagrams. The table that contains a candidate key is a referenced or target table. ![]() The table that contains a foreign key is a child table. ![]() FOREIGN KEY (activity_id) REFERENCES activity(activity_id) ![]()
0 Comments
Leave a Reply. |