0% found this document useful (0 votes)
25 views3 pages

One To One

Uploaded by

ykid68843
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

One To One

Uploaded by

ykid68843
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction to databases

In this chapter, we introduce the student to the concept of one-to-one relationship


between tables of a database. We explain the concept, follow that up with an example,
and then conclude with a DIY exercise.

One-to-One relationship
A database can consist of multiple tables that are related to each other. One of the
relationships that can exist between tables is a One-to-One relationship. This means
one row in a primary table is related to exactly one row in the secondary table. The
two tables are then connected through a foreign key. So a foreign key is used to
connect rows of different tables. Let us take an example to clarify further the 1-to-1
relationship.

Example

Let’s say we have a student who is doing a certain qualification. A qualification has the
following attributes: id, name, code, and NQF level. A student on the other hand has
an id, name, surname, gender, and age.

From this scenario we can generate two tables, one representing the basic information
of the student (StudentsTBL) and the other keeping information about the
qualification (QualificationsTBL).

StudentsTBL
Field Description Data Type Constraints
ID The student number INTEGER Primary key.
Name Student name. VARCHAR Not null. Maximum size of 20.
Surname Student surname VARCHAR Not null. Maximum size of 20.
Gender Student gender. CHAR Not null. Maximum size is 1.
Age Student age. INTEGER Not null.

QualificationsTBL
Field Description Data Type Constraints
ID Unique qualification id. Primary key.
Name Qualification name. INTEGER Primary key
Code Qualification code VARCHAR Not null. Maximum size of 20.
NQF_Level Qualification NQF level INTEGER Not null.
Student_ID The field that links a INTEGER Foreign key.
student to a qualification.

1
The student id is appearing in both tables. In the StudentsTBL table it serves as a
primary key and in the QualificationsTBL table it serves as a foreign key. As a
foreign key, it connects the two tables.

The relationship existing between the Student and Qualification is a 1-to-1 relationship.
This relationship can be represented nicely in a schema as follows:

Student Qualification

id id
name name
surname code
gender nqf_level
age student_id

The above schema communicates the message that says 1 student is pursuing 1
qualification.

The symbol depicts a primary key.


The symbol depicts a foreign key.

We can then write Java code that will allow us to perform CRUD operations on the
tables generated. Please go through Tutorial 13 for an implementation of a similar
problem.

DIY
Write code for the scenario given in this less. Perform the basic operations (CRUD)
and also add the following functionalities:

• Get the youngest student.


• Get all the qualifications done by female students.

Use the following tables to test your application:

StudentsTBL
Student number Name Surname Gender Age
(Primary key)
111 Tata Mama F 18
222 Tete Meme F 20
333 Titi Mimi M 25
444 Toto Momo M 23
555 Tutu Mumu M 22

2
QualificationsTBL
Primary key Foreign key Name Code NQF level
1 111 Diploma in Computer Science DPRS20 6
2 222 Diploma in Multimedia DPMC20 6
3 333 Diploma in Information Technology DPIT20 6
4 444 Diploma in Informatics DPIF20 6
5 555 Diploma in Computer Systems DPCY20 6
Engineering

Conclusion
In this chapter, we managed to introduce the student to the 1-to-1 relationship. This
relationship simply says 1 row in one table maps exactly to 1 row in another table.

Thank you very much for having taken time to go through the lesson. Enjoy the rest of
the day and God bless you.

You might also like