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..

Tables

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');
commit;


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

CUST_ID          CUSTOMER_NAME
---------------------- ------------------------------------------------------------

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'); 
commit;
 
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.

Comments

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).

@Entity
@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:

@…

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

Iphone & Ipad Tips

Tips for using iPhone & iPad - 1
1.One of the most useful tool of an Iphone or an Ipad is the ability to take a screenshot of your screen. One example of its usefulness is taking a screenshot of an image or article online so it can be view later without needing internet access. To take a ScreenShot of whatever is currently on your screen hold down the home button and the lock button at the same time for about 2 seconds. You should see a flash and a screenshot of your screen will be in your camera roll.

2. Double-clicking the home button brings up the Multitasking bar. From here you can switch between running apps or completely exit out of an app.


3. Creating folders on the home screen can help save some space. It also allows you to group similar apps together, like games. To create folders on the home screen press down on an app until the apps start shaking, then decide what two apps you what in the same folder and drag one of the apps onto the other one. The two apps will merg…