TechView with Kamlesh khaliya

Resources for OCP, Oracle Interviews(kamleshkhaliya@gmail.com)

Wednesday 14 March 2012

Give me the examples for one to one, one to many and many to many relation and how many tables we require for each?


Example (of one to one and one to many)– Suppose we have a table Student containing details like Id, Name, Class, Year, Room_no(primary key in Room Table). We have another table Room containing  details like Room_no, Hostel. So we have primary key and foreign key relation between Student and Room table. If  one room can be allotted to only one student then it will be one to one relation between student and room. If one room can be allocated to more than one student then it will be one to many relation.
Now the question is that if there is a one to one relation then why should not we maintain the room information along with student in student table instead of creating separate Room table for it. Answer is, we can't do this because room is a master having the details of hostel rooms and if we maintain the room along with student then when we will delete the student, the information of corresponding room will be lost.

Example of  many to many -  Suppose we have Emp is the master of employees and Proj is the masters of projects  in an IT company . If one employee can work on any project and one project can be completed by any number of employees then this will be a many to many relation. And to implement this we need to create one additional relation table to maintain the relationship between emp and proj. So to define many to may relation we need three tables.

1 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home