I have a DB model that uses several tables to hold business logic data; one of the tables includes a user table that hold the UserID. The code reads data from these tables and generates a json string that’s used in the application. To avoid having to redo the queries and recreating the json string, I store the json string in a table call JsonCache that has only 2 columns: UserID and JsonCacheWork. Each user has multiple JsonCacheWork entries (entries add themselves as the result of user interactions).
For the moment, there are no relationships on my database diagram between the User table and the JsonCache table. It seems to work fine.
Why would I need/want to add relationships between the 2 tables and what are the advantags, if any, of keeping my DB diagram as is.
Thanks for the explanation.
You add the relationship between tables to tell the database management system that the records in those two tables are related. DMS can enforce this relationship, which means that you won’t make a mistake of adding a record to JsonCache table with UserId that doesn’t exist.
Also, the foreign key constraint can be used to automatically delete records in one table when a related record in the other table is deleted (cascade deletes). So when you delete a user the DMS will delete his entry in JsonCache for you.
Sql Server is a relational database and relations between tables are key part of database schema. They help maintaining data integrity.