Database Programming and JBDC

Working Through This Course

In order to have a thorough understanding of the course units, you will need to read and understand the contents, practice the steps by designing an Information system of your own, and be committed to learning and implementing your knowledge.
This course is designed to cover approximately seventeen weeks, and it will require your devoted attention.


MODULE ONE

UNIT 1:                                  Introduction to Information Systems
UNIT 2:                                  Introduction to Basic SQL
UNIT 3:                                  SQL Syntax I
UNIT 4:                                  SQL Syntax II
UNIT 5:                                  More SQL Statements
UNIT 6:                                  Database Programming and JBDC

UNIT 6                          Database Programming USING JDBC

Table of Contents
1.0              Introduction
2.0              Objectives
3.0              Main Content

3.1       INTRODUCTION TO DATABASE

3.1.1   Definition of Database
3.1.2    Classification of Database
3.1.3   Database Management Systems
3.1.4    Relational Database Model
3.2      DATABASE OBJECTS AND CONSTRAINTS
3.2.1    Definition of SQL
3.2.2    SQL Statements
3.2.3    Database Objects
3.2.4    Constraints
3.3     DATABASE PROGRAMMING
3.3.1   Database Programming in Java using JDBC
3.3.2   Accessing the database using JDBC step by step
3.3.3   Using JDBC in the real world
4.0              Conclusion
5.0              Summary
6.0              Tutor Marked Assignment
7.0              Further Reading and Other Resources

1.0      INTRODUCTION

In this unit, you will be introduced to the concept of database programming, which involves knowing
what a database is, how to construct a simple database. The unit then teaches you how to query the
database using SQL learnt in previous units then writing programs in form of procedures which carries
out specific assignments on the database. 

2.0              OBJECTIVES

By the end this unit, you should be able to:
•        Explain the term database and Database Management Systems (DBMS) in use
•        Construct entity relationship diagrams
•        Identify the basic database objects
•        Program a database using JDBC
•        Use JDBC in real world applications.

3.0              MAIN CONTENT3.1      INTRODUCTION TO DATABASE3.1.1    DEFINITION OF DATABASE

A databaseis a structured collection of records or data that is stored in a computer system. A database
is an organized body of related information that is organized so that it can be easily accessed, managed
and updated. In one view, databases can be easily classified according to types of content bibliographic,
full-text, numeric and images. The structure is achieved by organizing the data according to a database
model. The model in most common use today is the relational model. Other models such as the
hierarchical model and the network model use a more explicit representation of relationships.
3.1.2    Classification of database
In computing, databases are sometimes classified according to their organizational approach and these
can be described as follows;
•       Relational database: The most prevalent approach is the relational database. A tabular
database in which data is defined so that it can be reorganized and accessed in number of
different ways. 
•       Distributed database: A Distributed database is one that can be dispersed or replicated
among points in a network.
•       Object-oriented programming database: An Object-oriented programming database is
one that is congruent with the data defined in object classes and subclasses. Computer
databases typically contain aggregations of data records or files, such as sales transaction,
product catalogs and inventories, and customer profiles. Typically, a database manager provides
the capabilities of controlling read/write access specifying report generation, and analyzing
usage. Databases and database managers are prevalent in large mainframe systems, but are also
present in smaller distributed workstations and mid-range systems such as the AS/400 and on
personal computers. 
•        Structural Query Language (SQL): This is a language for making interactive queries
from and updating a database such as IBM’s DB2, Microsoft Access and database products
from oracle, Sybase and computer associates.
3.1.3   DATABASE MANAGEMENT SYSTEM
A computer database relies upon software to organize the storage of data. This software is known as a
database management system (DBMS). Database management systems are categorized according to the
database model that they support. The model tends to determine the query languages that are available
to access the database. A great deal of the internal engineering of a DBMS, however, is independent of  

the data model, and is concerned with managing factors such as performance, concurrency, integrity,
and recovery from hardware failures. In these areas there are large differences between products.
3.1.4   RELATIONAL DATABASE MODEL
The relational database model is a logical representation of data that allows relationship among data to
be considered without concern for the physical structure of the data. A relational database uses relations
or two-dimension tables to store information. A table is the basic storage structure of a Relational
Database Model. A table holds all the data necessary about something in the real world, such as
employees, invoices, or customers. For example, one might want to store information about employees
in a company. In relational database, one creates several tables to store different pieces of information
about a company’s employees, such as an employee table, a department table and a salary table.
A relational database:
•        Can be accessed and modified by executing structured SQL statements.
•        Contains a collection of tables with no physical pointers
•        Uses a set of operators
3.2      DATABASE OBJECTS AND CONSTRAINTS
3.2.1   DATABASE OBJECTS
The commonly used database objects are;
•        Table: This is the basic unit of storage; composed of rows
•        View: This logically represents subsets of data from one or more tables
•        Sequence: Generates numeric values
•        Index: This Improves the performance of some queries 
•        Synonym: Gives alternative names to objects
A tableis the basic storage structure of a Relational Database Model. With views,one can present and
hide data from tables. Many applications require the use of unique numbers as primary key values. One
can either build code into the application to handle this requirement or use a  sequence to generate
unique numbers. If the performance of some queries is to be improved, one should consider creating an 

index. Indexes can also be used to enforce uniqueness on a column or a collection of columns. 
Alternative names can be given to database objects by using synonyms.

3.2.3   CONSTRAINTS

Constraints enforce rules at the table level whenever a row is inserted, updated, or deleted from that
table. The constraint must be satisfied for the operation to succeed. Constraints prevent the deletion of a
table if there are dependencies. The following constraint types are valid;
-          NOT NULL: This specifies that the column cannot contain a null value
-          UNIQUE: This specifies that a column or combination of columns whose values must be unique
for all rows in the table.
-          PRIMARY KEY: This uniquely identifies each row of the table
-          FOREIGN KEY: This establishes and enforces a foreign key relationship between the column
and a column of the referenced table.
-          CHECK: This specifies a condition that must be true.
3.3       DATABASE PROGRAMMING
It is very essential to pay special attention to database programming because Databases are the heart
and soul of many of the recent enterprise applications. For a better performing database a Database
Administrator (DBA) and a specialist database programmer is needed. If database specialists are not
used  during  a  program  development  cycle,  database  often  ends  up  becoming  the  performance
bottleneck. 
An application that does not collate its data in a database is at stake. Programming languages reflect
this trend. That's why most languages provide a robust and flexible library for database access.
Databases can be managed in a programming context by using a database engine called Relational
Database Management Systems. This engine allows an interconnection between programming codes
and the database that keeps the data that is reserved to be used by the codes. Database connections 

within the context of programming are of two forms; 
•         Using a built-in (or an internal) Database Management System: Since most new high level
programming languages come with a built-in DBMS, programmers most times prefer designing the
database and connecting to a database in the programming environment. This reduces the stress the
programmer would have passed through if done otherwise. Some examples of databases that allows
internal database connections are; Java, C#, Visual Basic, etc.
•         Using an external Database Management System: External RDBMS are not resident in the
programming environment, thus an external connection is needed in order to connect to them from a
programming environment.
In this section, one of the commonly used Database Management systems will be considered. It is
called the Java Database Connectivity (JDBC).
3.3.1   Database Programming in Java using JDBC
Java provides the Java Database Connectivity (JDBC) API to access different databases. JDBC, which
is short for Java Database Connectivity, can be defined as "An API for the Java programming language
that defines how a client may access a database." In other words, JDBC specifies the ways and methods
that are needed to access a database, more specifically a relational database such as Oracle, MySQL,
and others. The main aspect of JDBC is that it is a specification and not a product. So different vendors
(here RDBMS vendors) provide their own implementation for the specification. 
For example, the JDBC implementation for Oracle database is provided by Oracle itself and the same is
the case for all others. The implementations provided by the vendors are known as JDBC Drivers. The
most important point to be kept in mind is that JDBC Drivers are installed at client-side and not at
server-side.
JDBC has been with the Java Standard Edition (JSE) from version 1.1. The latest version is 4 and is
being shipped with JSE 6. Regardless of the version, JDBC supports four types of implementations or
drivers. They are:
1. Type I or JDBC-ODBC Bridge
2. Type II or Partly Java Partly Native 
3. Type III or Network Protocol Driver
4. Type IV or Pure Java Driver

The types are defined by how the driver provides communication between the application and the
database server. Here are the details.
A Type I Driver is also called a JDBC-ODBC Bridge. The reason is that in this case JDBC internally
makes calls to the ODBC. It is the ODBC that communicates with the database server. The job of
JDBC is to provide the queries to the ODBC in a form understandable by ODBC and to deliver the
result provided by ODBC to the application in a form that is understandable by the application. This
Driver works mainly with the Windows platform. This is the only type of Driver that is shipped with a
Java installation.
A Type II Driver uses the native API of the target database server to communicate with the server.
Hence it is known as a Native Protocol Driver as well as a Partly Java Partly Native Driver. This Driver
doesn't contain pure Java code as it uses the client-side API provided by the target database server. To
call the client-side API of the database, it uses JNI. However, since it doesn't have the overhead of
calling ODBC, a Type II Driver is faster than a Type I. Also, by using a Type II Driver, one can access
functionalities that are specific to the database server which is being used.
A Type III Driver is also known as Network Protocol Driver. Type III Drivers target the middleware.
The middleware then communicates with the database server. In essence, Type III Drivers are like Type
I with the exception that Type III Drivers are completely written in Java and use the network protocol
of the middleware instead of ODBC API. Type III Drivers are more secure since middleware is in the
picture. In a nutshell, in Type III Drivers the conversion logic is at the middleware level and not at the
client-side.
The Type IV Driveris known as a Pure Java Driver. It is comparable to Type II as it directly interacts
with the database server. Unlike Type II, Type IV doesn't use native API calls. Instead the API has been
written in Java by the vendor. Apart from being a pure Java implementation of database client API, a
Type IV Driver delegates the processing to the database server. That means at client side no processing
related to the database or SQL translation occurs. The only job the client does is connecting to the
server, passing the queries and input to the database server and getting the result back through the same
connection. Due to the delegation of all the processes to the server, the Type IV Driver is also known as
the Thin Client Driver.
The choice of which driver to use depends on the type of application that is being developed. For
example, if the application is web-based, the best option is Type IV as it releases the application server
from being a part of database transactions. In this case the application server would only have to 

provide services to look up the name of the connection pool and maintain the pool. All other datarelated operations would be delegated to the database server. Next we will discuss the steps involved in
using JDBC.
3.3.2   ACCESSING THE DATABASE USING JDBC STEP BY STEP
The best part of using JDBC for database programming is that if one has the required type of driver,
regardless of the database server, the steps to connect and query the database remain more or less the
same. The steps to access database for a typical relational database server include:
1. Loading the driver. 
2. Creating a connection. 
3. Instantiating a statement object. 
4. Retrieving a result set object. 
5. Accessing the data from the result set object. 
All of these steps are the same for any database, be it Oracle or MySQL. The only change comes in the
query to be passed in step four. Here are the details.
Loading the Driver
The driver, regardless of type, can be loaded in one of two ways: using the Class loader, or explicitly
creating  the  instance. The  difference  between  them,  apart  from  how  the  driver  is  instantiated,
is whether the Driver has to be registered explicitly or not.
Using the Class loader
A class can be loaded at runtime using the forName() method of the Class class. The method accepts a
String  having  the  name  of  the  class  to  be  loaded. Also  once  the  class  is  loaded,  calling  the
newInstance() method of Class will create a new object of the loaded class. When the forName()
method is used, the Driver need not be registered explicitly. For example, to instantiate a driver of Type
I using the forName method, the statement would be:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Explicitly creating the instance
The second way to load a driver is to instantiate it explicitly using the new operator. This is similar to
that of creating a new instance of any class. However, when the driver is being explicitly instantiated,  

one will have to register the driver with the runtime environment using the register() method of the
DriverManager class. For example to load Type I Driver, the statements would be:
Driver driver=new sun.jdbc.odbc.JdbcOdbcDriver();
DriverManager.register(driver);
Or the statements can be merged as:
DriverManager.register(new sun.jdbc.odbc.JdbcOdbcDriver());
Once the driver is loaded and registered, the next step is to get a connection.
Creating a Connection
Once the driver has been loaded and registered, the next step is creating a connection with the database
server. The connection is created when one creates an instance of Connection. To get an instance of
Connection,  the  getConnection()  method  of  the  DriverManager  class  has  to  be  called.  In  reality,
Connection is an interface and when getConnection() is called, the DriverManger provides an instance
of the proper implementing class to a reference variable of Connection. There are three forms of the
getConnection() method which are:
 getConnection(String url)- the URL contains all the necessary information including the URL
of the database server, user name and password. 
 getConnection(String url, Properties info) - the URL contains only the URL to the server.
The user name and password are passed as part of the Properties instance. 
 getConnection(String url, String user, String password) - as with the previous form, this
form also contains the URL to the server in the URL parameter. The user name and password
are passed as separate parameters. 
The URL is of the form jdbc:<subprotocol>:subname where subprotocol refers to the protocol used by
the database server and subname refers to the database to which the connection has to be made. For
example, an Oracle subname refers to the tablespace within the database server. So in order to create a
connection using the ODBC data source name or DSN, the statement would be:
Connection connection = DriverManager.getConnection ("jdbc:odbc:test", 
"test1", "test123");
where odbc is the subprotocol and test is the DSN which points to the database to connect to. The next 

step is to create a statement object.
Instantiating a statement object
A statement represents a query to be executed at the database server against a database. In other words,
a statement object is responsible for executing a SQL query as well as retrieving the result of the
executed query. JDBC provides three types of statements based on the type of query to be executed.
They are: Statement, PreparedStatement, and CallableStatement. They are based on the type of query to
be executed.
Statement is the simplest type that represents a simple query. Its object can be instantiated using any of
the following forms of the createStatement() method of the Connection interface:
·createStatement() - 
Returns a Statement object with default concurrency conditions.
·createStatement(int resultSetType, int resultSetConcurrency)-Returns a Statement object with concurrency condition and type of ResultSet according to the values
passed  as  values.  The  most  commonly  used  resulSetTypes  include
ResultSet.TYPE_FORWARD_ONLY (indicating that the data can be read only in forward direction and
once read cannot be moved back to a previous data) and ResultSet.TYPE_SCROLL_SENSITIVE
(indicating that the data can be read in both forward and backward directions, and that the changes
done by any other operation are visible instantly). The commonly used values for resultSetConcurrency
are  CONCUR_READ_ONLY  (indicating  that  ResultSet  may  not  be  updated)  and
CONCUR_UPDATABLE (indicating that object may be updated).
For example, to create a Statement object that would provide a ResultSet object which is scrollable and
updatable, the statement would be
Statement statement = connection.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE); 
PreparedStatement conserves resources. Whenever a query is sent to the database server, it goes
through four steps: parsing the query, compiling the query, linking and executing the query. When a
statement object is used to execute a query all four steps are repeated again and again. This can create
resource hogging. The alternative to it is a PreparedStatement object. If a PreparedStatement object is  

used, the first three steps are performed only once at the start and in successive calls; the values are
then passed to the linked query and it is executed. To create an object of PreparedStatement, any of the
following forms of prepareStatement can be used:
·prepareStatement(String query)-This  form  accepts  a  parameterized  SQL  query  as  a  parameter  and  returns  an  object  of
PreparedStatement. "Select * from user where user_id=?" is an example of a parameterized query.
·prepareStatement(String query, int resultSetType, int resultSetConcurrency)-This form is similar to the first form with the added options of specifying whether ResultSets are
scrollable and updatable or not. The values for the two parameters are the same as those described in
the Statement section.
For example, to create an instance of PreparedStatement which provides an updatable and scrollable
ResultSet, the statements would be:
String query= "Select * from user where user_id=?";
PreparedStatement pStatement = connection.prepareStatement(
query,
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE); 
To call procedures and functions within a database, one can use CallableStatement. However, if the
underlying database doesn't support procedures and functions, then the CallableStatement object won't
work. For example, versions of MySQL database prior to 5.0 didn't support functions and procedures.
To create an object of CallableStatement, use any of the following forms of the prepareCall() method of
Connection:
·prepareCall(String query)-This returns a CallableStatement object that can be used to execute a procedure or function, which is
passed as the query. The query is of the form "{sum(?,?)}" where sum is the function/procedure to be
called.
·prepareCall(String sql, int resultSetType, int resultSetConcurrency)-To get a ResultSet which is both updatable and scrollable, this form can be used. The resultSetType and 

resultSetConcurrency are same as that used with prepareStatement().
For example, to call a procedure whose name is sum, the statement would be
CallableStatement cStatement = connection.prepareCall(
"{sum(?,?)}");
The next step is to retrieve the ResultSet.
Retrieving the ResultSet object
The rows retrieved by the execution of a SQL query are given back by JDBC in the form of a ResultSet
object. A ResultSet contains all the rows retrieved by a query. To retrieve a ResultSet object, one can
call  the  executeQuery()  method  of  the  Statement  object.  If  the  Statement  object  is  of  the  type
PreparedStatement, then executeQuery() without any argument needs to be called. If it is of the type
Statement, then a SQL query will have to be passed to the method. For example, to retrieve a ResultSet
from a Statement for the query "Select * from user", the code would be
ResultSet result = statement.executeQuery("Select * from user");
The next step is to get data from the ResultSet. 
Accessing the data from the Resultset object
The specialty of ResultSet is that it can be iterated over as a collection and for each iteration, the data
can be accessed as it is an array using an index. A ResultSet object can be iterated over using its next()
method. During each iteration, one row is retrieved from the number of rows returned by the execution
of the SQL query. The columns within the row can be accessed using different forms of the get()
method of the ResultSet object. The forms depend upon the data-type of the column to be accessed
such as getString() if the column type is varchar, getInt() if the column type is int, and so on. Mostly
getString() is used to retrieve data from the columns. The argument that needs to be passed to the
method is either a string containing the column name or the integer value representing the index. The
index starts from 1 and not 0. 
For example, if the user table has a column named "name," then the statements to retrieve the values
for the "name" column would be
while(result.next){
System.out.println(result.getString("name));

}
3.3.3   USING JDBC IN THE REAL WORLD
It is now time to learn how to develop a practical application. An application that implements what has
been explained will be considered in this section;
 GenericDAO- Connects to the database and provides a Statement object. It is generic in the
sense that it accepts a driver name and URL as an argument of constructor. 
 DataOp - Implements database operations. 
 DAOTest- Tests the DAO and DataOp classes. 
So here is the GenericDAO class. It accepts the driver class and URL to connect to as constructor
arguments along with the user name and password.
package jdbctest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class GenericDAO 
{
Connection connection;
Statement statement;
public GenericDAO()
{
connection=null;
statement=null;
}
public GenericDAO(String driverClass,String connectionURL,String user,String password)
{
try
{
Class.forName(driverClass).newInstance();
connection=DriverManager.getConnection(connectionURL,user,password);
statement=connection.createStatement();
}
catch (InstantiationException e)
{
e.printStackTrace();

}
catch (SQLException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
public void setStatement(Statement statement)
{
this.statement = statement;
}
public Statement getStatement()
{
return statement;
}

Next is the DataOp class. It has one method that operates on the user table. This class is not generic.
package jdbctest;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class DataOp 
{
Statement statement;
public DataOp(Statement statement)

{
this.statement=statement;
}
public List getUserList(String user)
{
List list=new ArrayList();
try
{
ResultSet result=statement.executeQuery("Select * from user where user_id='"+user+"'");
while(result.next())
{
list.add(result.getString(1));
}
}
catch (SQLException e)
{
e.printStackTrace();
list=null;
}
return list;
}
}
Last is the class that tests the GenericDAO and DataOp classes. Here we are passing the driver name
corresponding to Type IV of MySQL JDBC driver and the corresponding URL.
package jdbctest;
public class DAOTest 
{
public static void main(String args[])
{
//create instance of DAO class. Here we are using MySQL Type IV Driver
DAO dao=new 
DAO("com.mysql.jdbc.Driver","jdbc:mysql://localhost/test","r

oot","root123");
//Creating instance of DataOp class
DataOp dataOp=new DataOp();
//calling the getUserList method for user whose id is 23
System.out.println(dataOp.getUserList("23"));
}
}
That completes a basic application.
4.0      CONCLUSION
In this unit you have been introduced to the fundamental concepts of Database and Database
management systems. You have also learnt the different types of SQL statements, constraints and
database programming. 
5.0      SUMMARY
What you have learnt in this unit concerns
•        Introduction to Information Systems which refers to a systemof persons, datarecords and
activities that process the data and informationin an organization.
•        The study of information systems originated as a sub-discipline of computer science in an
attempt to understand and rationalize the management of technology within organizations.
•        Areas of application or work which includes:
v     Information Systems Strategy
v     Information Systems Management and 
v     Information Systems Development.
•        Types of Information Systems which Management Information Systems (MIS) or Reporting
Systems, Decision Support Systems, Transaction Information System (TIS) and Expert Systems.
6.0      TUTOR MARKED ASSIGNMENT
•        List and explain the various SQL statements
•        Write a short note on Database Management System and JBDC
7.0      FURTHER READING AND OTHER RESOURCES
SQL – A practical introduction by Akeel I. Din
An Introduction to Database Systems, Eighth Edition, C. J. Date, Addison Wesley, 2004, ISBN:

0-321-19784-4.

MODULE TWO

gives an insight into computer technology and data communications technology which are the specific technologies that collectively sum up into information technology as a whole. Since information comprises of data, this course takes you through the various ways through which data is created and manipulated to produce relevant information needed. It also deals with the various advances in computer hardware, software, and networking technologies which have spurred an evolution in the structure, design, and use of corporate information systems.

Working Through This Course In order to have a thorough understanding of the course units, you will need to read and understand the conten...

More SQL Statements

In order to have a thorough understanding of the course units, you will need to read and understand the contents, practice the steps by designing an Information system of your own, and be committed to learning and implementing your knowledge.
This course is designed to cover approximately seventeen weeks, and it will require your devoted attention.


MODULE ONE

UNIT 1:                                  Introduction to Information Systems
UNIT 2:                                  Introduction to Basic SQL
UNIT 3:                                  SQL Syntax I
UNIT 4:                                  SQL Syntax II
UNIT 5:                                  More SQL Statements
UNIT 6:                                  Database Programming and JBDC

UNIT 5                                  MORE SQL STATEMENTS

Table of Contents
1.0              INTRODUCTION
2.0              OBJECTIVES
3.0      Main Content
3.1      ARITHMETIC OPERATIONS

3.1.1   USING ARITHMETIC OPERATORS
3.1.2   OPERATOR PRECEDENCE
3.1.3   DEFINING A NULL VALUE           
4.0      CONCLUSION
5.0      SUMMARY
6.0      Tutor Marked Assignment
7.0      Further Reading and Other Resources
1.0      INTRODUCTION
Since SQL is a database language that is used for querying and modifying relational databases, this unit
introduces more SQL intructions manipulating database. It presents other statements apart from those
described in Module 2. 
2.0      OBJECTIVES
By the end of this unit, you should be able to 
 Write sql statements 
 Use the ORDER BY statement 
 Use the INSERT INTO statement 
 Update a group of data 
 Delete rows from a table 
3.0      MAIN CONTENT
3.1    ARITHMETIC OPERATIONS
Create expressions with number ad date data by using arithmetic operators. You may need to modify
the way in which data is displayed, perform calculations, or look at what-if scenarios. These are all
possible using arithmetic expressions. An arithmetic expression can contain column names, constant
numeric values and arithmetic operators.
Operator Description

+ Add
- Subtract
* Multiply
/ Divide
3.1.1   USING ARITHMETIC OPERATORS
Let us first extend Table persons to Table employees by adding salary column to it and adding three
more records. For subsequent examples in this unit, the table is also assumed to have more than 5
columns.
P_I
d
LastName FirstName Address City
Salary
1 Akinbode Ola 10, Odeku Str.  Lagos 4800
2 Okafor Chris 23, Princewill Drive Porthacourt 17000
3 Amodu Ali 20, Dauda lane Kaduna 12000
4 Buba Ibrahim 12, Dongorayaro Str. Kastina 9000
5 Ngozi Ebe 10, Felix Str.  Imo 7700
6 Sowale Ayo 63, Atoba road Abeokuta 24000
The example below describes a scenario in which arithmetic operators can be used.
SELECT last_name, salary, salary+300
FROM employees;
This gives

LastName Salary Salary+300
Akinbode 4800 5100
Okafor 17000 17300
Amodu 12000 12300
Buba 9000 9300
Ngozi 7700 8000
Sowale 24000 24300
3.1.2   OPERATOR PRECEDENCE
If an arithmetic expression contains more than one operator, multiplication and division are evaluated
first. If operators within an expression are of the same priority, then evaluation is done from left to
right. Parenthesis can be used to force the expression within parentheses to be evaluated first.
Multiplication and division take priority over addition and subtraction
A query that shows how operator precedence works is shown below;
SELECT last_name, salary, 12*salary+100 
FROM employees
LastName
Salary 12*Salary
+100
Akinbode 4800 57700
Okafor 17000 204100
Amodu 12000 144100
Buba 9000 108100

Ngozi 7700 92500
Sowale 24000 288100
A query that uses brackets to override the operator precedence is shown below;
SELECT last_name, salary, 12*(salary+100) 
FROM employees
LastName
Salary 12*(Salary
+100)
Akinbode 4800 58800
Okafor 17000 205200
Amodu 12000 145200
Buba 9000 109200
Ngozi 7700 93600
Sowale 24000 289200
3.1.3  DEFINING A NULL VALUE
A null is a value that is unavailable, unassigned, unknown, or inapplicable. If a row lacks the data value
for a particular column, that value is said to be null, or to contain a null. Columns of any data type can
contain nulls. However, some constraints, NOT NULL and PRIMARY KEY, prevent nulls from being
used in the column.
A query that shows the null values is shown below;
SELECT last_name, job_id, salary, commission_pct
FROM employees;

LAST_NAME  JOB_ID  SALARY  COMMISSION_PCT 
Akinbode ST_MAN  4800  
Okafor ST_CLERK  17000  
Amodu ST_CLERK  12000  
Buba ST_CLERK  9000  
Ngozi ST_CLERK  7700  
Sowale ST_CLERK  24000
In the COMMISSION_PCT column in the EMPLOYEES table, notice that only a sales manager ( form
the job_id column) can earn a commission.
Null Values in Arithmetic Expressions
Arithmetic expressions containing a null value evaluate to null.
E.g 
SELECT last_name, 12*salary*commission_pct
FROM employees;
LAST_NAME  12*SALARY*COMMISSION_PCT 
Akinbode  
Okafor  
Amodu  
Buba  
Ngozi  
Sowale  

Next UNIT 6:                     Database Programming and JBDC

4.0      CONCLUSION
In this unit, you have learnt how to write basic SQL statements, using operators in SQL, how to use the
SQL ORDER statement, to arrange a group of data. Also the SQL INSERT statement was explained,
including how to update and delete rows in a table.

Next UNIT 6:                     Database Programming and JBDC

5.0              SUMMARY
•        What you have learnt in this unit concerns
•        Writing basic SQL statements 
•        Ordering a group of data using the ORDER BY statement
•        Updating, Inserting and Deleting rows in a table using SQl statements
6.0      FURTHER READING AND OTHER RESOURCES
Introduction to SQL 9i from Oracle University
www.wiki_SQL.com
Database System Concepts, 5th Ed.
SQL Tutorials by James Hoffman, 1997. 
Teach yourself SQL in 21 days, second edition, Macmillian computer publishing
SQL – A practical introduction by Akeel I. Din
An Introduction to Database Systems, Eighth Edition, C. J. Date, Addison Wesley, 2004, ISBN:

0-321-19784-4.

Next UNIT 6:                     Database Programming and JBDC

In order to have a thorough understanding of the course units, you will need to read and understand the contents, practice the steps by desi...

SQL Syntax II

In order to have a thorough understanding of the course units, you will need to read and understand the contents, practice the steps by designing an Information system of your own, and be committed to learning and implementing your knowledge.
This course is designed to cover approximately seventeen weeks, and it will require your devoted attention.


MODULE ONE

UNIT 1:                                  Introduction to Information Systems
UNIT 2:                                  Introduction to Basic SQL
UNIT 3:                                  SQL Syntax I
UNIT 4:                                  SQL Syntax II
UNIT 5:                                  More SQL Statements
UNIT 6:                                  Database Programming and JBDC

UNIT 4                                              SQL Syntax II

Table of Contents
1.0              Introduction
2.0              Objectives
3.0              Main Content
3.1              The ORDER BY Keyword
3.1.1        SQL ORDER BY Syntax
3.1.2        ORDER BY Example
3.2              SQL INSERT INTO Statement
3.2.1   SQL INSERT INTO Syntax
3.2.2   SQL INSERT INTO Example
3.3              SQL UPDATE Statement
3.3.1        SQL UPDATE Syntax
3.3.2   SQL UPDATE Example
3.4              SQL DELETE Statement
3.4.1        SQL DELETE Syntax
3.4.2   SQL DELETE Example
3.4.3   Delete All Rows
3.4      JOINING Tables
4.0              CONCLUSION
5.0              SUMMARY
6.0              Tutor Marked Assignment
7.0              Further Reading and Other Resources

1.0      INTRODUCTION

This unit introduces you to how to write basic SQL programs such as creating tables, selecting a view
from a table and familiarize you with basic SQL operators.

2.0        OBJECTIVES

By the end of this unit, you should be able to:
•        Write simple SQL programs.
•        Familiarize yourself with standard keywords of the SQL programs
•        Understand how to construct a good SQL statement. 
•        Manage and access databases using the create, select, where and the logical
operator.

3.0        MAIN CONTENT

3.1      THE ORDER BY Keyword
3.1.1   ORDER BY Syntax
The ORDER BY keyword is used to sort the result-set. The ORDER BY keyword is used to sort the
result-set by a specified column. The ORDER BY keyword sorts the records in ascending order by  

default. If you want to sort the records in a descending order, you can use the DESC keyword. The
order by clause comes last in a select statement.
The syntax of the order by clause given below;
SELECT  expr
FROM  table
[WHERE  condition(s)]
[ORDER BY{column, expr} [ASC|DESC]          
In the syntax,
ORDER BY     specifies the order in which the retrieved rows are displayed
ASC                  specifies rows in ascending order (this is the default value)
DESC                 order the rows in descending order
With the ORDER BY clause;
•        Numeric values are displayed with the lowest value first e.g 1-999
•        Date values are displayed with the earliest value first e.g 01-JAN-92 before 01-JAN-95
•        Character values are displayed in alphabetical order 
•        Null values are displayed last for ascending sequences and first for descending sequences
3.1.2   ORDER BY EXAMPLE
Sorting in Descending order
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY last_name;
LAST_NAME  JOB_ID  DEPARTMENT_ID  HIRE_DATE 
Akinbode SA_REP  80  21-APR-00 

Amodu SA_REP  20  21-APR-00 
Buba SA_REP  80  24-MAR-00 
Ngozi ST_CLERK  50  08-MAR-00 
Okafor SA_REP  80  23-FEB-00 
Sowale ST_CLERK  50  06-FEB-00 
We also sort using multiple columns.
For example,
SELECT last_name, department_id, salary FROM employees
ORDER BY department_id, salary DESC;
3.2         INSERT STATEMENT
3.2.1       INSERT INTO Syntax
The INSERT INTOstatement is used to insert new records statement is used to insert a new row in a
table.
The syntax of the insert statement is;
INSERT IBTO table [{column, [,column…..])]
VALUES (value [, value….]);

In the syntax,
table  is the name of the table
column  is the name of the column
value       is the corresponding value for the column
3.2.2   INSERT STATEMENT EXAMPLE
INSERTING NEW ROWS
E.G
INSERT INTO departments (department_id, department_name, manager_id, location_id)
VALUES  (170, ‘Public Relations’,100,1700); 
INSERTING ROWS WITH NULL VALUES
Implicit method example
INSERT INTO departments (department_id, department_name) 
Values (30,’Purchasing’);
Explicit Method example
INSERT INTO departments 
Values (100, ‘Finance’, NULL, NULL);
INSERTING SPECIAL VALUES
Example
INSERT  INTO  employees  (employee_id,  first_name,  last_name,  email,  phone_number,  hire_date,
job_id,salary, commission_pct, manager_id, department_id )
Values (7, ‘Adeola’, ‘Chalse’, ‘ade_char’, ‘2348039990985’, SYSDATE, ‘AC_ACCOUNT’, 6900,
NULL, 205, 100);
3.3         UPDATE STATEMENT
3.3.1        UPDATE STATEMENT Syntax

The UPDATE statement is used to update existing records in a table.
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Note:Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record
or records that should be updated. If you omit the WHERE clause, all records will be updated!
3.3.2      SQL UPDATE EXAMPLES
updating rows in a table
UPDATE employees
SET department_id=70
WHERE employee_id = 113;
3.4      SQL DELETE Statement
3.4.1       SQL DELETE SYNTAX
The DELETE statement is used to delete records and rows in a table
DELETE FROM table_name
WHERE some_column=some_value
3.4.2        SQL DELETE EXAMPLES
DELETE * 
FROM employees;
DELETE FROM employees
Where department_id =60;
3.4.3   Delete All Rows

It is possible to delete all rows in a table without deleting the table. This means that the table structure,
attributes, and indexes will be intact:
DELETE FROM table_name
or
DELETE * FROM table_name
Note: Be very careful when deleting records. You cannot undo this statement!
3.5      JOINING TABLES
The Select statement can be used to join two tables together. It can be used to extract part of
Table A and part of Table B to form Table C. For example, assuming studentand studentclassare two
different tables. Let us look at this instruction:-Select student.SID, student.name, studentclass.classname
From sudent, studentclass
Where student.SID = studentclass.SID
This statement shows that SID, name are columns or fields from student table and classname and SID
are also columns from studentclass table.
The fields in the new table to form by this instruction are:-SID                name  classname

Next UNIT 5:                                  More SQL Statements

4.0        CONCLUSION
In this unit you have been introduced to the fundamental Queries of a typical database computing
environment e. g. SQL. You also learnt the specific operators that works for SQL statements. You were
also introduced to various SQL statement syntax necessary in writing simple SQL codes.
5.0        SUMMARY
What you have learned in this unit concerns
•        Structured Query Language (SQL) which is a standard language for accessing and
manipulating databases.

•        The syntax of different SQL statement
•        What SQL statement can be used for
6.0        EXERCISES
Exercise 2.1    Write the SQL statement to delete two rows from student, Name and grade = 56?
Ans:         DELETE FROM student
WHERE Name='Tjessem' AND grade= 56
`                      
Exercise 2.2    What is the syntax to arrange the element of table in Ascending and Descending order.
Ans:         SELECT * FROM Tablename

ORDER BY LastName DESC


Next UNIT 5:                                  More SQL Statements

In order to have a thorough understanding of the course units, you will need to read and understand the contents, practice the steps by desi...

SQL Syntax I

In order to have a thorough understanding of the course units, you will need to read and understand the contents, practice the steps by designing an Information system of your own, and be committed to learning and implementing your knowledge.
This course is designed to cover approximately seventeen weeks, and it will require your devoted attention.


MODULE ONE

UNIT 1:                                  Introduction to Information Systems
UNIT 2:                                  Introduction to Basic SQL
UNIT 3:                                  SQL Syntax I
UNIT 4:                                  SQL Syntax II
UNIT 5:                                  More SQL Statements
UNIT 6:                                  Database Programming and JBDC

UNIT 3                                  SQL Syntax I

Table of Contents
1.0              Introduction
2.0              Objectives
3.0              Main Content
3.1              SQL Create Table Statement
3.1.1        SQL CREATE TABLE Syntax
3.1.2        CREATE TABLE Example
3.2              SQL SELECT Statement
3.2.1        SQL SELECT Syntax
3.2.2         An SQL SELECT Example
3.2.3        Navigation in a Result-set
3.3              The SQL SELECT DISTINCT Statement
3.3.1        SQL SELECT DISTINCT Syntax
3.3.2        SELECT DISTINCT Example
3.4              SQL WHERE Clause
3.4.1        SQL WHERE Syntax
3.4.2        WHERE Clause Example
3.4.3        Quotes Around Text Fields
3.4.4        Operators Allowed in the WHERE Clause 
3.5              SQL AND & OR Operators
3.5.1         AND Operator Example          
3.5.2        OR Operator Example
3.6              Combining AND & OR
4.0              CONCLUSION
5.0              SUMMARY
6.0              Tutor Marked Assignment
7.0              Further Reading and Other Resources
1.0              INTRODUCTION
This unit will introduce you to how write basic SQL programs such as creating tables, selecting a view
from a table and familiarize you with basic SQL operators.
2.0              OBJECTIVES
By the end of this unit, you should be able to:
•        Write simple SQL programs.
•        Familiarize yourself with standard keywords of the SQL programs

•        Understand how to construct a good SQL statement. 
•        Manage and access databases using thecreate, select, where and the logical
operators.
3.0              MAIN CONTENT
3.1      The CREATE TABLE Statement
The CREATE TABLE statement is used to create a table in a database.
3.1.1   SQL CREATE TABLE Syntax
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
)
The data type specifies what type of data the column can hold. For a complete reference of all the data
types available in MS Access, MySQL, and SQL Server visit www.datatyperef.com
3.1.2   CREATE TABLE Example
Now we want to create a table called "Persons" that contains five columns: P_Id, LastName,
FirstName, Address, and City.
We use the following CREATE TABLE statement:
CREATE TABLE Persons
(
P_Id int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),

City varchar(255)
)
The P_Id column is of type int and will hold a number. The LastName, FirstName, Address, and City
columns are of type varchar with a maximum length of 255 characters.
The empty "Persons" table will now look like this:
P
_
I
d
LastName FirstName Address City
The empty table can be filled with data with the INSERT INTO statement.
3.2      The SQL SELECT Statement
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
3.2.1   SQL SELECT Syntax
SELECT column_name(s)
FROM table_name
and
SELECT * FROM table_name
Note: SQL is not case sensitive. SELECT is the same as select.
3.2.2   An SQL SELECT Example
The "Persons" table:
P_Id LastName FirstName Address City
1 Akinbode Ola 10, Odeku Str.  Lagos
2
Okafor Chris 23, Princewill Drive Porthacourt

3 Amodu Ali 20, Dauda lane Kaduna
Now we want to select the content of the columns named "LastName" and "FirstName" from the table
above.
We use the following SELECT statement:
SELECT LastName,FirstName FROM Persons
The result-set will look like this:
LastName FirstName
Akinbode Ola
Okafor Chris
Amodu Ali
SELECT * Example
Now we want to select all the columns from the "Persons" table.
We use the following SELECT statement:
SELECT * FROM Persons
Tip: The asterisk (*) is a quick way of selecting all columns!
The result-set will look like this:
P_Id LastName FirstName Address City
1 Akinbode Ola 10, Odeku Str.  Lagos
2 Okafor Chris 23, Princewill Drive Porthacourt
3 Amodu Ali 20, Dauda lane Kaduna

3.2.3   Navigation in a Result-set
Most database software systems allow navigation in the result-set with programming functions, like:
Move-To-First-Record, Get-Record-Content, Move-To-Next-Record, etc.
3.3      The SQL SELECT DISTINCT Statement
In a table, some of the columns may contain duplicate values. This is not a   problem; however,
sometimes you will want to list only the different (distinct) values in a table.
The DISTINCT keyword can be used to return only distinct (different) values.
3.3.1   SQL SELECT DISTINCT Syntax
SELECT DISTINCT column_name(s)
FROM table_name
3.3.2   SELECT DISTINCT Example
The "PersonsOne" table:
P_Id LastName FirstName Address City
1 Akinbode Ola 10, Odeku Str.  Lagos
2 Okafor Chris
23,  Princewill
Drive
Lagos
3 Amodu Ali 20, Dauda lane Kaduna
Now we want to select only the distinct values from the column named "City" from the table above.
We use the following SELECT statement:
SELECT DISTINCT City FROM Persons
The result-set will look like this:
City
Lagos

Kaduna
3.4      SQL WHERE Clause
The WHERE clause is used to filter records.
The WHERE clause is used to extract only those records that fulfill a specified criterion.
3.4.1        SQL WHERE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
3.4.2   WHERE Clause Example
The "Persons" table:
P_Id LastName FirstName Address City
1 Akinbode Ola 10, Odeku Str.  Lagos
2 Okafor Chris 23, Princewill Drive Porthacourt
3 Amodu Ali 20, Dauda lane Kaduna
Now we want to select only the persons living in the city "Sandnes" from the table above.
We use the following SELECT statement:
SELECT * FROM Persons
WHERE City='Kaduna'

The result-set will look like this:
P_Id LastName FirstName Address City
1 Amodu Ali 20, Dauda lane Kaduna
3.4.3   Quotes Around Text Fields
SQL uses single quotes around text values (most database systems will also accept double quotes).
Although, numeric values should not be enclosed in quotes.
For text values:
This is correct:
SELECT * FROM Persons WHERE FirstName='Chris'
This is wrong:
SELECT * FROM Persons WHERE FirstName=Chris
For numeric values:
This is correct:
SELECT * FROM Persons WHERE Year=1965
This is wrong:
SELECT * FROM Persons WHERE Year='1965'
3.4.4   Operators Allowed in the WHERE Clause
With the WHERE clause, the following operators can be used:
Operator Description
= Equal
<>  Not equal

>  Greater than
<  Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN If you know the exact value you want
to  return  for  at  least  one  of  the
columns
Note: In some versions of SQL the <> operator may be written as !=
3.5.     SQL AND & OR Operators
The AND & OR operators are used to filter records based on more than one condition.
The AND operator displays a record if both the first condition and the second condition is true while
the OR operator displays a record if either the first condition or the second condition is true.
3.5.1   AND Operator Example
The "Persons" table:
P_Id LastName FirstName Address City
1 Akinbode Ola 10, Odeku Str.  Lagos
2 Okafor Chris 23, Princewill Drive Porthacourt
3 Amodu Ali 20, Dauda lane Kaduna
Now we want to select only the persons with the first name equal to "Tove" AND the last name equal to 

"Svendson":
We use the following SELECT statement:
SELECT * FROM Persons
WHERE FirstName='Ola'
AND LastName='Akinbode'
The result-set will look like this:
P_I
d
LastName FirstName Address City
1 Akinbode Ola 10, Odeku Str.  Lagos
3.5.2   OR Operator Example
Now we want to select only the persons with the first name equal to "Tove" OR the first name equal to
"Ola":
We use the following SELECT statement:
SELECT * FROM Persons
WHERE FirstName='Chris'
OR FirstName='Ali'
The result-set will look like this:
P_I
d
LastName FirstName Address City
2 Okafor Chris 23, Princewill Drive Porthacourt
3 Amodu Ali 20, Dauda lane Kaduna
3.5.3   Combining AND & OR
You can also combine AND and OR (use parenthesis to form complex expressions).
Now we want to select only the persons with the last name equal to "Svendson" AND the first name
equal to "Tove" OR to "Ola":
We use the following SELECT statement:

SELECT * FROM Persons WHERE
LastName='Akinbode' OR LastName=’Okafor’
The result-set will look like this:
P_Id LastName FirstName Address City
1 Akinbode Ola 10, Odeku Str.  Lagos
2 Okafor Chris 23, Princewill Drive Porthacourt

Next UNIT 4:                                  SQL Syntax II

4.0              CONCLUSION
In this unit you have been introduced to the fundamental Queries of a typical database computing
environment e. g. SQL. You also learnt the specific operators that works for SQL statements. You were
also introduced to various SQL statements necessary in writing simple SQL codes.
5.0              SUMMARY
The summaries of what you have learnt are:-•        Structured Query Language (SQL) which is a standard language for accessing and
manipulating databases.
•        The syntax of different SQL statement
•        What SQL statement can be used for
•        Not all SQL statement accepts semicolon at the end of it depending on the
platform or server that is used to execute the statement.
6.0              EXERCISES
Exercise 2.1    Create a table called student which will contain the following: student_id, studentname,
dept, level and grade?
Ans: Create Table Student
`                      (Student_id Varchar (25),
Student name Varchar (70),
Dept varchar2 (255),
Level char (12),
Grade number (3))
Exercise2.2     Write the syntax to insert into the table created in exercise 2.1
Ans:  INSERT INTO Student (Student_id, Student name, Dept,
Level, Grade)
VALUES (value1, value2, value3, value4, value5)

7.0              TUTOR MARKED ASSIGNMENT
Create a database performing all the discussed activities in this unit. i. e. create a table, insert into that
table etc.
8.0              FURTHER READING AND OTHER RESOURCES
SQL Tutorials by James Hoffman, 1997. 
Teach yourself SQL in 21 days, second edition, Macmillian computer publishing

SQL – A practical introduction by Akeel I. Din

Next UNIT 4:                                  SQL Syntax II

In order to have a thorough understanding of the course units, you will need to read and understand the contents, practice the steps by desi...