Foreign key digunakan untuk menandai suatu tabel terhubung dengan tabel lain dalam konteks tabel parent dan child.sebuah tabel dikatakan child bila memiliki suatu field yang merupakan reference terhadap key di tabel parent.hal ini digunakan untuk menjaga konsistensi dan keterkaitan antar tabel.. Mysql creating table foreign key example. the following example creates a dbdemo database and two tables: categories and products. each category has one or more products and each product belongs to only one category. the cat_id field in the products table is defined as a foreign key with update on cascade and delete on restrict actions.. Sql foreign key constraint. a foreign key is a key used to link two tables together. a foreign key is a field (or collection of fields) in one table that refers to the primary key in another table. the table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table..
Set foreign_key_checks=0; alter table rep_table add foreign key (`rep_company_code`) references `company_table` (`company_code`) on update cascade; set foreign_key_checks=1; allowed that to happen. i'm afraid to try the self referencing one b/c it seems there needs to be some logic in there to work.. Foreign key constraint. a foreign key constraint is a database object that assists in keeping your foreign key data consistent. you create a foreign key constraint to maintain referential integrity. by creating a foreign key constraint, you are telling mysql to enforce certain rules over the data.. So, for example, adding the on update cascade clause to a foreign key definition tells mysql that when a record is updated in the primary table (the table referenced for foreign key checks), all records using that foreign key value in the current table should also be automatically updated with the new values to ensure the consistency of the system..
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.