AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Sqlite foreign key and primary key10/4/2023 ![]() ![]() ![]() The above statement produces the following output − While doing so, we add the foreign key constraint on column CUSTOMER_ID reference on column ID of the CUSTOMERS table as shown in the statement below − The following query creates a table with the name CUSTOMERS −įollowing is the output of the above SQL statement − Let us create two tables with the names CUSTOMERS and ORDERS. ![]() REFERENCES referenced_table(referenced_column) It helps to normalize (or organize the data in a database) the data in multiple tables.įollowing is the basic syntax to add Foreign Key constraints on a column of a table in MySQL database − Here the id of the customer is primary key (ID) in the CUSTOMERS table and foreign key in the ORDERS (CUSTOMER_ID) table observe the following diagram − Features of Foreign Keyįollowing is the of features of Foreign Key −Ī Foreign Key is used to reduce the redundancy (or duplicates) in the table. Let’s consider an example scenario, Assume we have two tables namely CUSTOMERS (ID, NAME, AGE, ADDRES, SALARY) and ORDERS (ID, DATE, CUSTOMER_ID, AMOUNT). The table that has the primary key is known as the parent table and the key with the foreign key is known as the child table. The foreign key can reference the unique fields of any table in the database. In SQL, a Foreign Key is a column in one table that matches a Primary Key in another table, allowing the two tables to be connected together.Ī foreign key also maintains referential integrity between two tables, making it impossible to drop the table containing the primary key (preserving the connection between the tables). ![]()
0 Comments
Read More
Leave a Reply. |