Home > Software engineering >  Is bridge table population implicit in SQL?
Is bridge table population implicit in SQL?

Time:05-09

Basic question, since I have not written much SQL. When building a link table, which solely has columns that are foreign keys, do you need to insert into the bridge table manually or is the population of this table automatic based on the insertion of values into the 2 other tables?

Example below.

When inserting into student table and class table, the studentclass link table is empty, so I assume I need to also manually enter the values into the link table?

enter image description here

CodePudding user response:

Yes, you need to populate the table yourself. The link table defines the relationship between Student and Class (which students attends which classes). How would the database know which student attends which class if you do not enter the data?

or is the population of this table automatic based on the insertion of values into the 2 other tables?

It is not automatic because even if you insert a class and a student into their respective tables, that does not automatically mean that a student also attends the class. If you did this, every student would attend every class.

CodePudding user response:

sql can not know which rows of the foreign key table(s) you want to link, so oyu have to do it yourself.

A trigger only knows the actual table and data you can select, but the linkin ztable, makes those links that are previously not there, so you can't SELECT that information.

but in your gui, you have the linking information, so it is easy to fill the linking table or bridging table

  • Related