Skip to main content

Databases - Basics

In the previous post (Databases Introduction)  we looked at different types of databases and simple concepts of tables. In this section, we will look at some basics of databases- tables, inner joins and outer joins. The syntax of SQL used here is for Oracle. However, it is very similar in all other databases such as MySQL, MS SQLServer, Postgres, etc..


We will create two tables by using the SQL statements below: 
  1. create table customer (cust_id number(4,0) not null, customer_name varchar2(60) not null);
  2. create table invoice (invoice_id number(4) not null, cust_id number(4) not null , invoice_total number(10,2) default 0.00, paid_ind char(1));
In the first SQL we create the table called Customer with the column names cust_id and customer_name. Observe that we do not want to store null values in each of the columns in the customer table.

In the second SQL we create the table invoice, which has its columns and also the cust_id column from the customer table. This is how we delegate that a specific invoice is associated to a specific customer.

Observe that one customer can have many invoices and hence we have a one-many relationship between the customer and invoice tables. This implies that a customer may not have any invoices i.e. a one-zero relationship between the customer and invoice tables.

We can make the above create SQLs complex by specifying the tablespace name (we are using the default tablespace assigned to the user you are logging on) and adding foreign key constraints to the cust_id in the invoice table to the customer table. This will eliminate the input of data into the invoice table for which a cust_id is non-existent in the customer table.

A simple way to input data into the tables is to use the insert SQL statement which is given below:

insert into customer values (100, 'Paradigm Solutions');
insert into customer values (102, 'Siemens Pvt Ltd');
insert into customer values (104, 'British Telecom');

Now when we do a simple select from the customer table we get:

---------------------- ------------------------------------------------------------

100                   Paradigm Solutions

102                   Siemens Pvt Ltd

104                  British Telecom

Now here are the SQL statements for inserting records into the invoice table.
insert into invoice values ( 8442, 102, 5430.00, 'Y');
insert into invoice values ( 8997, 102, 5430.00, 'Y');
insert into invoice values ( 8775, 102, 5430.00, 'N');
insert into invoice values (9876,100,2345.78,'N') ; 
insert into invoice values (9886,100,345.00,'N'); 
As an exercise, we leave to the reader to retrieve the records inserted into the table invoice.

Inner and Outer Joins

To retrieve customers and invoices for which there are invoices, we do an inner join  where  the SQL is constructed by joining the column cust_id from the table customer and invoice:
select c.cust_id,  customer_name, i.invoice_id,  i.invoice_total, i.paid_ind
from customer c, invoice i where  c.cust_id= i.cust_id;;
gives results as:
CUST_ID                CUSTOMER_NAME                       INVOICE_ID  INVOICE_TOTAL  PAID_IND
---------------- --------------------------------------------------  ---------------------- ----------------------    --------  
100                        Paradigm Solutions                                 9876                     2345.78           N
100                       Paradigm Solutions                                  9886                    345                   N  
102                      Siemens Pvt Ltd                                           8775                    5430                 N 
102                     Siemens Pvt Ltd                                            8997                    5430                 Y 
102                    Siemens Pvt Ltd                                             8442                    5430                 Y 

We can make our SQL criteria (the sql after the where keyword) complex by requesting only those which have not been paid by modifying as below:

where  c.cust_id= i.cust_id and i.paid_ind ='N'

This will return those invoices which have not paid for the customers who have invoices.

Observe that we have used aliases for table names customer, invoiced as c and i respectively. This eliminates any ambiguous column names like cust_id being specified in the SQL. This, as a result, also eliminates typing the complete table name when referring the columns in the criteria or in the select.

The above was an example of inner join where we get the records when they exist in both tables. However, if you remember that we do not get the third customer - British Telecom from the table customer as it has no invoices associated with it.  In order to obtain all the customer names and their invoices, whether they exist or not, we will write an outer join as shown:

select c.cust_id, customer_name, i.invoice_id  from customer c, invoice i where  c.cust_id= i.cust_id (+);

returns the results as shown below:
CUST_ID                         CUSTOMER_NAME                     INVOICE_ID
---------------------- ---------------------------------------------------- ----------------------

100                                Paradigm Solutions                                         9876
100                                Paradigm Solutions                                         9886
102                               Siemens Pvt Ltd                                                 8775
102                              Siemens Pvt Ltd                                                  8997
102                              Siemens Pvt Ltd                                                  8442
104                              British Telecom

Observe that in the outer join SQL we specify in the join (+) which is equivalent to writing as:

select c.cust_id, customer_name, i.invoice_id  from customer c left outer join  invoice i on i.cust_id= c.cust_id ;

The latter method is more readable and specifies whether it is left or right outer join while the (+) indicates how the tables are already joined and the right , left outer is dependent on which side of the = sign the (+) is assigned.

Nowadays in Object Oriented Programming (OOP), we can understand both of these joins specifically when using Object Relation Mapping (ORM) tools like Hibernate, topLink etc. Most of the time we are using an outer join to get the most relevant information and hence in Hibernate (of which we will see in future articles) by default it uses outer join.


Popular posts from this blog

Hibernate-Secondary Table Annotation..

When using Hibernate with annotation, SecondaryTable needs to be used carefully while using the Discriminator. We assume that there is a table called Inventory which is the parent table that has common elements including Id, createDate, modifiedDate, SKU#, and productType.
The other tables, which are children of the Inventory table, are TV_Inventory, DVD_inventory etc.. In this example, we consider TV_Inventory which inherits from Inventory (in terms of objects).

@Table(name = "INVENTORY")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "PRODUCT_TYPE", discriminatorType = DiscriminatorType.STRING, length = 20)
@SequenceGenerator(name = "InventorySequence", sequenceName = "INV_SEQ", allocationSize = 1)

public abstract class Inventory {
 // getters and setters with other annotations for the columns and relevant methods.

Consider the child object associated with the table TV_INVENTORY. The outline is shown below:


Single Sign On - SSO

Single Sign On (SSO) is the ability of the user to login into his/her system or network and have access to all the applications which he is authorized to under the Lightweight Directory Access Protocol (LDAP). Another good definition is given on this page .A website with many links to SSO is defined here.

  What is Authentication?
         Authentication is the process of verifying or validating the user and password against some security processes.

   What is Authorization?
         Authorization is granting access (roles) to each user for a specific application.

Consider a company of say ten employees which contains an accounting system and a human resources system. Each user is authenticated and authorized on the company network to access public folders, intra-net, and possibly webmail.

However, only the HR department is authorized to access the HR system. Similarly, the Accounting department is authorized to access the accounting system. There is also a possibility that certai…

4 Circles releases Word Frequency..

4 Circles released its application 'Word Frequency v1.1'.

This application counts the number of words in your essay and tells you the frequency of each word. This application is simple to use and helpful for bloggers, writers, journalists, students and speech writers. The application has 1 box for the text to be copied and pasted (left hand side) while clicking the button will count the total number of words in the essay.

This application is written in JDK 1.6, and runs on JRE 1.6.x or above. The user needs to copy the text and place it in the text box and click on the button to get the frequency in the grid. Please download the software here:
Word Frequency