What is one-to-many relationship data structure?



 One to Many Relationships:

Each record in the Table A is associated with the multiple records in the Table B but each and every record of the table B is associated with only one record of Table A.Foreign key is used in the Table B and primary key is used in the Table A.It is type of relationships which is used for joining work or task. It is used as to store the information in the relational database.Sometimes,Table A is called as the parent table whereas Table B is called as the child table. It is also called as hierarchical relationship. The data should be structured in such way which clearly define the relationships between the entities like primary table and secondary table. Normalization should be applied to avoid the redundant data for an efficient querying system.

One class has high number of students to attend for learning purpose is an example of this.

Primary and Foreign keys:

One side of the relationship must have the primary key which uniquely identifies each record. Many sides of the relationship must have the foreign key which references the primary key of one side. For an example, create the two tables i.e. employees and department table. Indexes should be created on the foreign key column.

PRATICAL QUERIES:

Table departments is created with columns department_id and department_name.Primary key is applied on the department_id.There is no need to mention NOT NULL, if primary key is mentioned after datatype.

create table departments (department_id number primary key,department_name varchar2(20));




desc departments;

Here,department_id is automatically showing NOT NULL, even though NOT NULL is not used at the time of table creation. This is due to primary key is used.

INSERT ALL
INTO departments(department_id ,department_name) VALUES (10,'IT')
INTO departments(department_id ,department_name) VALUES (20,'RETAIL')
INTO departments(department_id ,department_name) VALUES (30,'FINANCE')
select * from dual;

Three records have been inserted into the department table.

It will display all the three records which have been inserted into the departments table.

create table employees (employee_id NUMBER PRIMARY KEY,employee_name varchar2(20),
department_id NUMBER, FOREIGN KEY (department_id) REFERENCES departments(department_id));

Table employees is created with columns employee_id,employee_name and department_id. Primary key is applied on the employee_id.On the department_id,foreign key is used.

INSERT ALL
INTO employees(employee_id ,employee_name,department_id) VALUES (1,'Rahul',10)
INTO employees(employee_id ,employee_name,department_id) VALUES (2,'Rishav',20)
INTO employees(employee_id ,employee_name,department_id) VALUES (3,'Roshan',30)
INTO employees(employee_id ,employee_name,department_id) VALUES (4,'Ganesh',30)
INTO employees(employee_id ,employee_name,department_id) VALUES (5,'Seema',30)
INTO employees(employee_id ,employee_name,department_id) VALUES (6,'Mani',20)
INTO employees(employee_id ,employee_name,department_id) VALUES (7,'Mohan',20)
INTO employees(employee_id ,employee_name,department_id) VALUES (8,'Shiv',10)
INTO employees(employee_id ,employee_name,department_id) VALUES (9,'Radha',10)
select * from dual;

Nine records have been inserted into the employees table with duplicate department_id 10,20 and 30.
select * from employees;




select e.employee_name,d.department_name
FROM employees e 
JOIN departments d
on e.department_id=d.department_id
order by d.department_name;

This query is joining the employees table with the departments table using the department_id as the foreign key and display the list of employee names along with their respective department names.




Comments

Popular posts from this blog

Difference between union and union all in oracle sql plsql

Explain oracle pl sql datatypes interview question and answer for fresher and experienced.

Difference between full join and cross join-oracle sql plsql