Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For example I have table accounts with id, and table sessions with account_id. The fields are connected. So what is the usability of it?

What I have tried:

To google and to find it here.
Posted
Updated 30-Jul-17 0:00am

1 solution

Those columns are used to link together the tables and their entities. Giving you a specific scenario, just think of the following content,
Account
id | name | ...
1  | ABCD | ...
2  | EFGH | ...

Sessions
id | account_id | ...
1  | 1          | ...
2  | 43         | ...
3  | 3          | ...
4  | 1          | ...
5  | 2          | ...

For example, in the states where you want to show all the sessions linked to a specific account from both the tables, you would use the ID from Account and Account_Id from Sessions table to "JOIN" them together. In SQL JOIN, you specify which records to JOIN based on which criteria — the columns.

Thus, getting something like this,
Account.id | Sessions.id | name | ...
1          | 1           | ABCD | ...
1          | 4           | ABCD | ...

Then further stuff is your own logic to use the data, how? That is all upto you, basically this is done on the tables that have been normalized. You remove a few common anomalies from the tables, and you remove some data redundancies. That step results in more than 1, 2 or even 3 tables sometimes. Thus getting the data back requires you to JOIN the tables. These columns — account.id, sessions.account_id — act as the pivot points for your tables to be JOINed at.

For a better explanation of how to JOIN the tables in SQL, please read the following articles,
Visual Representation of SQL Joins[^]
Types of Join in SQL Server[^]
Database normalization - Wikipedia[^]
 
Share this answer
 
v2
Comments
Richard MacCutchan 30-Jul-17 6:17am    
Session 5 is account 2; think you meant session 4.
Afzaal Ahmad Zeeshan 30-Jul-17 6:41am    
Yes, thank you for pointing the typo.
[no name] 30-Jul-17 6:23am    
Thank you!
Afzaal Ahmad Zeeshan 30-Jul-17 6:41am    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900