| Understanding Relational Database with myNet.Dbase | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
What is Database Relationship? We are living in the world that all connected with information. Database is where information warehousing. Database Relationship is the relation between databases and tables that define the way how we organize and process data and information. It is so important that a small error from bad design can lead to misinformation, data integrity . And thus your business who depends on that database. On the other hand, if designed properly, it will help your business a lot in term of processing, collecting and sharing data and information. In real life, your tables now are not stand alone, but they have the relationship with other tables. So that from one table you can relate data from other tables. There are 3 database relationships: One-to-One, One-to-Many, Many-to-Many. I don't need Database Relationship now. I design my own way? It is ok if you design stand alone simple database. But if you don't setup the relationship between Customer table and Invoice table, you will not be able to request information from one to another. Such as: - What is the Customer ID (Name, Address, Contact..) of this Invoice 1002? - How many _acme_invoices that the Customer XYZ Inc has purchased? What are they? Who make up the Database Relationship? You are the one who makes up the database relationship based on these factors: • Common sense. Such as the relationship between Customers and Invoices table. It is always true that a Customer buys many things and thus has many invoices. But an Invoice, number 11235 for example, belongs only one customer. • The logistic collecting information of your business. For example the relationship between Projects and Employees table. In this case, it is Many-to-Many. But for other businesses, they require each Project can be assigned to many Employees, and one Employee can be assigned to one Project at a time. So the relationship now is not Many-to-Many, but One-to-Many. NOTE: Relational Database is only supported by database that is built in this feature. Can I use myNet.Dbase within my home or office computers, VPN, Intranet, then sharing and collaborating without on the Net? • Yes, definitely! You can get it running in 5 minutes with the installation. 1) Business may not need website, but it needs database. You install myNet.Office on one PC in your home, or in office. See myNet.Office installation instruction. That is it. DONE in 5 minutes. Now all computers within Home or Office can use myNet.Office by accessing its I.P address. See how below. 2) Check myNet.Office computer's I.P address by: - Start -> Run -> Cmd -> DOS screen -> type: > ipconfig /all -> Will give the I.P address of computer that installed myNet.Office, such as: 192.168.0.101. • Run myNet.Dbase computer. From any computer in your home, in office, in VPN, or Intranet that link together by router, you access to myNet.Office by: - Open any PC's Web browser -> Type: http://192.168.0.101, the I.P Address of myNet.Office's PC -> Goto its site -> Login as Admin -> Now you can Access, Work and Collaborate myNet.Dbase with others within your Home, Office, Intranet. You don't even need Networking all local computers. You don't need to be on the Net to use myNet.Dbase. Of course, you can always Publish your database online any time. • If your business has locations separated far away, let say one is in Toronto, one is in Downtown or Vancouver. You don't have VPS, Network, or anything that links them. Then Self-Host is the best option to use myNet.Dbase. Use myNet.Page creates an internal company website. No publish on the Net. No body outside can see. Any location, any where on earth, employees can access the company web database securely login through the Internet using static I.P address (get it from your local Internet provider) or Domain name if you have one. myNet.Dbase is a Client/Server, Relationship, and Web Database. • So what is a Client/Server, Relationship, and Web Database? Not all databases are relational, and not all relational databases are built on the client/server paradigm. But most of the time you'll want a relational database server, so it's important to clarify the distinction. myNet.Dbase is a true Client/Server Database. A Java Technology and heavy duty database server.
myNet.Dbase is a true Relational Database . Not all database applications are relational database.
myNet.Dbase is a Web Database . It is:
How is myNet.Dbase dealing with Database Relationship?
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The Relational Tables is _acme_employees and _acme_workoffice is One-to-One. | ||
| CREATE TABLE _acme_employees ( EID int not null auto_increment primary key, /* Primary Key of _acme_employees */ WID int, /* Foreign Key */ NAME varchar(20), PHOTO varchar(20), ADDRESS varchar(200), PHONE varchar(20), EMAIL varchar(50) /* ..more fields ..*/ ); DBR Formula: _acme_employees (1) -> WID -> (1)-> _acme_workoffice |
CREATE TABLE
_acme_workoffice ( WID int not null auto_increment primary key, /* Primary Key of _acme_workoffice*/ EID int , /* Foreign Key */ TYPE enum ("Tech support", "Marketing", "Accounting", "Software Developer", "Manager" ) /* ..more fields ..*/ ); DBR Formula: _acme_workoffice (1) -> EID -> (1)-> _acme_employees |
|
| The Setup: 2 steps: 1) Put the Primary Key ( EID ) of one table (_acme_employees) to become the Foreign Key of the related table _acme_workoffice, and vice versa. 2) Use [myNet.Dbase] -> [Database Relationship Manager] tool to establish the relationship between them. That is it. DONE in 5 minutes. NOTE: You can use EID as Primary Key for _acme_workoffice. There is no need to create WID primary key. |
||
Figure 1B shows an example of a one-to-many relationship between DEPARTMENT and PROJECT. In this diagram, DEPARTMENT is considered the parent entity while PROJECT is the child. Reading from left to right, the diagram represents departments may be responsible for many projects. The optionality of the relationship reflects the "business rule" that not all departments in the organization will be responsible for managing projects. Reading from right to left, the diagram tells us that every project must be the responsibility of exactly one department.
| The Relational Tables: | ||
| CREATE TABLE
_acme_department ( DID int not null auto_increment primary key, /* Department ID = PM Key */ DEP_NAME enum ("Engineering", "Human Resources", "Marketing", "Accounting", "Software Development") /* ..more fields ..*/ ); DBR Formula: Department (1) -> DID -> (m)-> _acme_project Which means: One Department has managed many Projects. |
CREATE TABLE
_acme_project ( PID int not null auto_increment primary key, DID int, /* Foreign Key */ PRJ_CODE varchar(20), PRJ_COST double, PRJ_FUNDING double, PRJ_DESCRIPTION varchar(20), PROJECT_START_DATE date, PROJECT_END_DATE date, SUBJECT PRJ_TYPE enum ("Tech Support", "Marketing", "Accounting", "Software Developer"), DESCRIPTION text /* ..more fields ..*/ ); DBR Formula: _acme_project (1) -> DID -> (1)-> Department Which means: One Projects is managed by One Department only. |
|
| The Setup to establish the
One-to-Many relationship between
_acme_department and
_acme_project 2 steps: 1) Put the Primary Key ( DID ) of main table (_acme_department) to become the Foreign Key of the related table _acme_project. 2) Use [myNet.Dbase] -> [Database Relationship Manager] tool to establish the relationship between them. That is it. DONE in 5 minutes. |
||
| CREATE TABLE _acme_Customers ( CustomerID int not null auto_increment primary key, /*Primary Key*/ F_Name varchar (20), L_Name varchar (20) /* ..more fields ..*/ ); DBR Formula: _acme_Customer (1) -> _acme_CustomerID -> (m)-> Invoice |
CREATE TABLE Invoice ( InvoiceID int not null auto_increment primary key, /*Primary Key*/ CustomerID int, /* Foreign Key */ Cust_Name varchar (50), Amount double, /* ..more fields ..*/ ); DBR Formula: Invoice (1) -> _acme_CustomerID -> (1)-> _acme_Customer |
|
| The Setup to establish the One-to-Many
relationship between _acme_Customers and
Invoice 2 steps: 1) Put the Primary Key CustomerID of main table _acme_Customers to become the Foreign Key of the related table Invoice. ( No vice versa) 2) Use myNet.Dbase [Database Relationship Manager] tool to establish the relationship between them. That is it. DONE in 5 minutes. |
||
![]() |
![]() One-to-Many Relationship ( 1 - ∞ ). NOTE: The Products and Orders is Many-to-Many. Thus Order Details is the associate table used to break up Many-to-Many into 2 of One-to-Many. |
Figure 1C shows a many-to-many relationship between EMPLOYEE and PROJECT. An employee may be assigned to many projects; each project must have many employee Note that the association between EMPLOYEE and PROJECT is optional because, at a given time, an employee may not be assigned to a project. However, the relationship between PROJECT and EMPLOYEE is mandatory because a project must have at least two employees assigned. Many-To-Many relationships can be used in the initial drafting of the model but eventually must be transformed into two one-to-many relationships. The transformation is required because many-to-many relationships cannot be represented by the relational model. The process for resolving many-to-many relationships is discussed in the next section.
Many-to-many relationships cannot be used in the data model because they cannot be represented by the relational model. Because it creates redundancy and duplication. John Smith - the CEO, and John Smith - the Employee, missed up identity, could happen if two tables are Many-to-Many relationship. Therefore, Many-to-Many relationships must be resolved early in the modeling process. The strategy for resolving Many-to-Many relationship is to replace the relationship with an association entity and then relate the two original entities to the association entity. This strategy is demonstrated below Figure 6.1 (a) shows the many-to-many relationship:
Employees may be assigned to many projects.
Each project must have assigned to it more than one employee.
In addition to the implementation problem, this relationship presents other problems. Suppose we wanted to record information about employee assignments such as who assigned them, the start date of the assignment, and the finish date for the assignment. Given the present relationship, these attributes could not be represented in either EMPLOYEE or PROJECT without repeating information. The first step is to convert the relationship assigned to to a new entity we will call ASSIGNMENT. Then the original entities, EMPLOYEE and PROJECT, are related to this new entity preserving the cardinality and optionality of the original relationships. The solution is shown in Figure 1B.
Figure 2: Resolution of a Many-To-Many Relationship
![]() |
![]() |
| CREATE TABLE
_acme_employees ( EID int not null auto_increment primary key, NAME varchar(20), PHOTO varchar(20), ADDRESS varchar(200), PHONE varchar(20), EMAIL varchar(50) /* ..more fields ..*/ ); DBR Formula: _acme_employees (1) -> EID -> (m) _acme_assigment Which means: One Employee is Assigned to -> Many Projects. |
CREATE TABLE
_acme_assigment ( PID_EID int not null auto_increment primary key, EID int, /* Primary key of _acme_employees */ PID int /* Primary of _acme_project */ /* ..more fields ..*/ ); • _acme_assigment is the association table (entity) between Employees and Projects. |
CREATE TABLE
_acme_project ( PID int not null auto_increment primary key, PRJ_CODE varchar(20), PRJ_COST double, PRJ_FUNDING double, PRJ_DESCRIPTION varchar(20), PROJECT_START_DATE date, PROJECT_END_DATE date, SUBJECT PRJ_TYPE enum ("Tech Support", "Marketing", "Accounting", "Software Developer"), DESCRIPTION text /* ..more fields ..*/ ); DBR Formula: _acme_project (1) -> PID -> (m) _acme_assigment Which means: One Project is Assigned to -> Many Employees. |
||
| The Setup: 2 steps: 1) Create the association table _acme_assigment. This table must include EID - the Primary Key of _acme_employees, and PID - the primary key of _acme_project, plus has its own table primary key is: PID_EID. 2) Use [myNet.Dbase] -> [Database Relationship Manager] tool to establish the relationship between _acme_employees with _acme_assigment, and _acme_employees with _acme_assigment. That is it. DONE in 5 minutes. |
||||
Create the Relational Control View and
Calculate The View with
myNet.Dbase
myNet.Dbase
allows you to create a selected Relational View of fields, that
may interest you and calculate its values. For example, when
viewing the information of _acme_invoices from Customer ABC Inc, instead
of viewing 35 pieces of data of _acme_invoices, you can select to view
only data Cust_Name,
Cust_Address, GST_Amount, PST_Amount, Sub_Total, Cust_Phone only. And
they you calculate the Total of Sub_Total, total of
GST_Amount, total of PST_Amount of all the _acme_invoices that the
Customer ABC Inc has purchased. You can select View of Relation by
Embedded View or Popup View.
• Embedded View is the
static
View
displayed inside the page.
• Popup View is the
dynamic View
displayed from a popup window outside the page.
Let's Get Started Designing Database... Use this as an example for
future design.
Let's get started building database. Here are 5 steps that
we recommend for users, who have no database
administration skills. The requirement to do these steps is:
To
understand How to CREATE a Table using simple
SQL script.,
What tables do you need? List them down.
Of course you know why
you need them. You always can expand, add more tables in the
future. At the present, you need tables for Employees, Project,
Department, Workoffice, Customers, Invoices, Products.
CREATE
TABLE simple syntax first plus CREATE the PRIMARY KEY for each table .
Use an ID number by increment is the easiest way to make a
key, because it is unique and automatic every time you add a new record.
Don't worry about
what table attributes (or fields, columns) you add them.. later.
The write the SQL Syntax English-like expression like this:
Create Table <tablename> ( Key_name type );
(see the table). In this step, you begin to write SQL table expression
in the definition table below.
Example:
CREATE TABLE
_acme_customers (
CustomerID
int not null auto_increment primary key,
/* Add more fields later*/
);
•
How To Choose a Primary Key
In this case, it is chosen as: int not null auto_increment primary key
means int = Integer type,
not null = Key should always has value,
auto_increment means that every time you add a new record, the number
key will automatically increases to plus 1, and it is chosen as a
primary Key. Thus it is unique and
simple. If you make up an integer and assigned to the key, you can't
keep track which number has been used. If you use Name ( even many
experienced db designers used to ) as a key. Then it is
possible you miss up the identity of John Smith - the CEO, and
John Smith - the Employee. So you should choose Primary Key as a Number
referred to a String.
NOTE: Database programmers like to choose Name in
their database as you often see because the customer Name John
Smith is better reference than put CustomerID of John Smith,
which is 543325. So that users can easily understand and refer.
But if it is designed relational database, then just one click on
CustomerID, you can find out much more information about that
customer. Not only customer Name, but also that customer's Address,
Phone, Company Name...
Define the Relationship between Tables. And Do Primary Key
Assignments.
A table can have many different relationships.
For example, table Employees may have
1-to-1 with Workoffice table, and 1-to-Many with Department table...
But there are only 3 relationships:
1-to-1, 1-to-Many, Many-to-Many.
1 - to -1 = Table primary: TP (1)
-> (Relational_Key)
rk -> (1) TR :Table Relation => 1 instance (record of TP) related by rk given only 1 P
instance. And vice versus holds true.
1 -to - Many = TP (1) -> rk -> (m) TR => 1 record of
TP related by rk given many TP records. BUT 1 record of TR related by rk given 1 TP record.
Many -to - Many = TP (m) -> rk -> (m)
TR => Many records of TP related by
rk gives many TP records.
And many records of TR related by rk gives
many TP records. In our case:
- Table Employees is having Many-to-Many with table Projects because
each Employee work on many different Projects. And one Project can be
assigned to many Employees.
- Table Department is having 1-to-Many relationship with Employees
because each Department has many Employees. And one Employee is
belonging to only 1 Department.
- Table Workoffice is having 1-to-1 relationship with table Employees
because each Workoffice has one Employees. And one Employee is having
only one Workoffice.
Here are the rules:
1 - to -1: Use the same PK (Primary
Key) of TP (Table primary) for TR (Table relation).
For example, if _acme_employees, with PK = EID, has the relation 1-to-1 with
_acme_workoffice then => You should use EID as PK for _acme_workoffice
too.
1 - to -Many: Put the PK (Primary
Key) of TP (Table primary), used as Foreign Key (FK) into TR (Table
relation). For example, if _acme_customers, with PK =
CustomerID,
has the relation 1-to-Many with _acme_invoices then => You should put
CustomerID as FK in _acme_invoices too.
Many - to
-Many: Introduce a middle (association) table. Then
divide Many-to-Many into two of 1-to-Many and 1-to-Many with the
associated table.
Example:
- Students and Teachers are Many-to-Many relation ship.
Because a Student can have many Teachers. And a Teacher has many
Students.
- Invoice and Product are Many-to-Many relationship. Because
a Invoice has many ordered Products. And a
Product can be on many different Invoices.
Translate into code:
| CREATE TABLE Customers ( CustomerID int not null auto_increment primary key, /* more fields later*/ ) Customers (1) -> InvoiceID -> (m) Products. |
CREATE TABLE Invoice ( InvoiceID int not null auto_increment primary key, CustomerID int , /*Foreign Key*/ /* more fields later*/ ) Invoice (1) -> InvoiceID -> (m) Inv_Prod. |
CREATE TABLE Inv_Prod ( InvProdID int not null auto_increment primary key, InvoiceID int, /*Foreign Key*/ ProductID int, /*Foreign Key*/ /* more fields later*/ ) The associate table. |
CREATE TABLE Products ( ProductID int not null auto_increment primary key, /* more fields later*/ ) Products (1) -> ProductID -> (m) Inv_Prod. |
After this step, you should have the definition
table below like this:
| Table Primary | Relationship and Key | Table Relation | Analyze |
|
CREATE TABLE
_acme_customers ( CustomerID int not null auto_increment primary key, /* more fields later*/ ); |
1-to-Many _acme_customers (1) -> CustomerID -> (m) _acme_invoices |
CREATE TABLE
_acme_invoices ( InvoiceID int not null auto_increment primary key, CustomerID int, /* more fields later*/ ); |
- One Customer has purchased many
Invoices. But One Invoice has only one Customer. • How? Just use common sense right? So this is: One-to-Many Relationship. - Who is One: _acme_customers - Who is Many: _acme_invoices |
| CREATE TABLE
_acme_employees ( EID int not null auto_increment primary key, /* more fields later*/ ); |
1-to-Many EID _acme_employees (1) -> EID -> (m) _acme_assigment |
CREATE TABLE
_acme_assigment ( PID_EID int not null auto_increment primary key, EID int, /* PK of _acme_employees */ PID int /* Primary of _acme_project */ /* Expand table add more fields... */ ); |
To divide Many-to-Many relationship of Employees and Projects, we introduce association table _acme_assigment in order to create two of 1-to-Many relations. _acme_assigment has 2 foreign keys EID and PID includes. |
| CREATE TABLE
_acme_projects ( PID int not null auto_increment primary key, /* more fields later*/ ); |
1-to-Many PID _acme_projects (1) -> PID -> (m) _acme_assigment |
CREATE TABLE
_acme_assigment ( PID_EID int not null auto_increment primary key, EID int, /* PK of _acme_employees */ PID int /* Primary of _acme_project */ /* Add more fields... */ ); |
_acme_projects
is relating to _acme_employees
with Many-to-Many. So table _acme_assigment is introduced in order to divide the relationship Many-to-Many into 1-to-Many with _acme_assigment |
| CREATE TABLE
_acme_department ( DID int not null auto_increment primary key, DEP_NAME enum ("Engineering", "Human Resources", "Marketing", "Accounting", "Software Development") /* Expand table add more fields... */ ); |
1-to-Many DID _acme_department (1) -> DID -> (m) _acme_projects |
CREATE TABLE
_acme_projects ( PID int not null auto_increment primary key, DID int, /* PK of _acme_department */ /* Add more fields... */ ); |
• One Department has managed many Projects. But one Project belongs to only one Department. Thus it is 1-to-Many. |
| CREATE TABLE
_acme_workoffice ( EID int not null auto_increment primary key, /* used same key with _acme_employees*/ /* more fields later*/ ); |
1-to-1 EID |
CREATE TABLE
_acme_employees ( EID int not null auto_increment primary key, /* more fields later*/ ) |
• One work office (computer used, or office, or a cubic) has occupied by only one Employee. And one Employee occupied 1 office. |
| CREATE TABLE
_acme_employees ( EID int not null auto_increment primary key, NAME varchar(20), PHOTO varchar(20), ADDRESS varchar(200), PHONE varchar(20), EMAIL varchar(50) /* Expand table add more fields... */ ); |
| CREATE TABLE
_acme_projects ( PID int not null auto_increment primary key, DID int, /* for Relation as 1-to-Many with Department*/ PRJ_CODE varchar(20), PRJ_COST double, PRJ_FUNDING double, PRJ_DESCRIPTION varchar(20), PROJECT_START_DATE date, PROJECT_END_DATE date, SUBJECT PRJ_TYPE enum ("Tech Support", "Marketing", "Accounting", "Software Developer"), DESCRIPTION text /* Expand table add more fields... */ ); |
| CREATE TABLE
_acme_assigment ( PID_EID int not null auto_increment primary key, EID int, /* Primary key of _acme_employees. Used as Foreign Key */ PID int /* Primary of _acme_project. Used as Foreign Key */ /* Expand table add more fields... */ ); |
| CREATE TABLE
_acme_department ( DID int not null auto_increment primary key, DEP_NAME enum ("Engineering", "Human Resources", "Marketing", "Accounting", "Software Development") /* Expand table add more fields... */ ); |
| CREATE TABLE _acme_workoffice ( EID int not null auto_increment primary key, /* used same key with _acme_employees*/ TYPE enum ("Tech support", "Marketing", "Accounting", "Software Developer", "Manager" ) /* Expand table add more fields... */ ); |

The Entity-Relationship diagram provides a pictorial representation of
the major data objects, the entities, and the relationships between them.
Once the basic diagram is completed, the next step is Design Tables
that represented by these relationship, and Code
Implementation.
We are living the the world governed by data and information.
Database is the most important property of your business.
Business can't run without them. Garbage in.
Garbage out. Therefore, if you are in the position of
Management, or dbase Designer, you should take it seriously.
Additional Resources:
We hope this article will give you some
information about the background, the design, the code, and how to do with
relational database. You can find more information from these links.
- The Relational Database
Normalization ( Some background theory of Relational
Algebra and Relational Calculus )
-
IBM Database Relationship
-
Design Your Own Database from Dr Lorna Scammell, University
Database adviser, Newcastle University.
-
Database Journal for Developer - Introduction to Database Relationship