Joins and Implementation of relationships in sql
November 21, 2012 Leave a comment
We have three types of relationships in SQL.
To implement these relationships
One-to-one: Use foreign key to the referenced table
Example:
Student: sid, fname, lname, addressId
Address: addressId, adress, city, sid
One-to-many: Use foreign key on the “many” side of the relationship linking back to the “one” side.
Example:
Teacher: tid, fname, lname
Classes: cid, cname, tid
Note: Here table ‘Teacher’ is one side and table ‘Classes’ is on many side
Many-to-many: Use junction table.
The junction table stores the primary keys of each table that is involved in the relationship.
Example:
Student: sid, fname, lname
classes: cid, cname, tid
student_classes: cid, sid
Joins
Here is a very good chart of all joins (and its variations) in SQL
Reference: http://stackoverflow.com/questions/38549/difference-between-inner-and-outer-joins
Recent Comments