To enforce the relationship between rows in the suppliers and supplier_groups table, you use the foreign key constraints. This may leave orphaned rows in the suppliers table. In addition, you may remove a row in the supplier_groups table without deleting or updating the corresponding rows in the suppliers table. In other words, for each row in the suppliers table, there is a corresponding row in the supplier_groups table.Ĭurrently, there is no way to prevent you from adding a row to the suppliers table without a corresponding row in the supplier_groups table. The relationship between supplier_groups and suppliers tables is one-to-many. And each supplier group may have zero or many suppliers. ) Code language: SQL (Structured Query Language) ( sql )Īssuming that each supplier belongs to one and only one supplier group. Let’s start with two tables: suppliers and supplier_groups : CREATE TABLE suppliers ( To enable foreign key constraint: PRAGMA foreign_keys = ON Code language: SQL (Structured Query Language) ( sql ) Introduction to the SQLite foreign key constraints To disable foreign key constraint: PRAGMA foreign_keys = OFF Code language: SQL (Structured Query Language) ( sql ) If the SQLite library is compiled with foreign key constraint support, the application can use the PRAGMA foreign_keys command to enable or disable foreign key constraints at runtime. If the command returns nothing, it means that your SQLite version doesn’t support foreign key constraints. The command returns an integer value: 1: enable, 0: disabled. PRAGMA foreign_keys Code language: SQL (Structured Query Language) ( sql ) To check whether your current version of SQLite supports foreign key constraints or not, you use the following command. The SQLite library must also be compiled with neither SQLITE_OMIT_FOREIGN_KEY nor SQLITE_OMIT_TRIGGER. SQLite has supported foreign key constraint since version 3.6.19. Summary: in this tutorial, you will learn how to use the SQLite foreign key constraint to enforce the relationships between related tables.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |