To enforce referential integrity and prevent orphaned records. It tells the database what it needs to know to make it impossible to store data that is not valid according to your business rules. If you make it impossible to store invalid data, then your data will be valid. Valid data makes everybody happy.
If each EMPLOYEE
must have a MANAGER
, then tell the database that and it won't let you create an EMPLOYEE
record without a MANAGER
reference.
If a MANAGER
needs to be deleted but you still have an EMPLOYEE
that lists that id as a MANAGER
, the database won't let you delete the MANAGER
(or can be told to also delete the EMPLOYEE
with cascaded deletes).
If the key of a MANAGER
needs to change, a cascaded update rule on a foreign key will automatically update any associated EMPLOYEE
records automatically.
If we don't connect what will be happen?
@DeanKuga raises an important point. Most large customers of major applications will want or need direct database access. This may be to use third party reporting applications, mass data manipulation your front-end doesn't provide, or imports or exports to other systems they own.
Here's something a lot of developers don't think about: Even though you're only licencing the application to your customers and you will still own the application, the customer owns the data. It's their data in every sense. Your rights as an independent vendor end with copyrights and patents on the program code. If the customer wants to connect to the RDBMS they have a license for on the server they own to read the data they own, that is their right and they absolutely will do that. That is not reverse engineering. Indeed, this capability is one of the primary and express purposes of using a general RDBMS and it's one reason why customers love them. They can always get their data out. Customers do not want giant information silos that prevent them from moving data to where they need it. Whatever the application is that you sold them, it almost certainly will not do everything that the business needs. They will have multiple applications and they all need to communicate to work together.