11 MySql JDBC
11 MySql JDBC
Introduction to database
A database is simply a structured collection of data. The data relates to each other by nature.
This is why we use the term relational database.
In the relational database, we model data using tables. A table contains columns and rows. It is
like a spreadsheet. A table may relate to anther table using a relationship e.g., one-to-one, one-
to-many relationship etc.
Because we deal with a large amount of data, we need a way to define the databases, tables,
etc., and process data more effectively. In addition, we want to turn the data into information.
And this is where SQL comes to play.
SQL stands for Structured Query Language. SQL is the standardized language used to access
the database.
∑ Data definition language (DDL) contains statements that help you define the database and
its objects e.g., tables, views, triggers, stored procedures, etc.
∑ Data manipulation language (DML) contains statements that allow you to update and
query data.
∑ Data control language (DCL) allows you to grant the permissions to a user to access a
certain data in the database.
MySql
My is the daughter’s name of the MySQL’s co-founder, Monty Widenius. The name of MySQL
is the combination of My and SQL, MySQL.
MySQL is a database management system that allows you to manage relational databases. It is
open source software backed by Oracle.
IPSR Solutions Ltd www.ipsr.edu.in 1
MySql and JDBC
Creating Database
Displaying Databases
USE database_name;
Creating Table
To define a column for the table in the CREATE TABLE statement, you use the following
syntax:
∑ The column_name specifies the name of the column. Each column has a specific data
type and the size e.g.,VARCHAR(255)
∑ The NOT NULL or NULL indicates that the column accepts NULL value or not.
∑ The DEFAULT value is used to specify the default value of the column.
∑ The AUTO_INCREMENT indicates that the value of the column is increased
automatically whenever a new row is inserted into the table. Each table has one and only
one AUTO_INCREMENT column.
If you want to set particular columns of the table as the primary key, you use the following
syntax: PRIMARY KEY (col1,col2,...)
The ALTER TABLE statement allows you to add a column, drop a column, change the data
type of column, add primary key, rename table, and many more.
IPSR Solutions Ltd www.ipsr.edu.in 2
MySql and JDBC
[AFTER column_name];
Rename a table
RENAME TO new_name;
Or
The MySQL TRUNCATE TABLE statement allows you to delete all data in a table. Therefore,
in terms of functionality, The TRUNCATE TABLE statement is like a DELETE statement
without a WHERE clause. However, in some cases, the MySQL TRUNCATE TABLE
statement is more efficient than the DELETE statement.
SELECT statement
The SELECT statement allows you to get the data from tables or views. A table consists of
rows and columns like a spreadsheet. The result of the SELECT statement is called a result set
that is a list of rows, each consisting of the same number of columns.
SELECT
column_1, column_2, ...
FROM
table_1
[INNER | LEFT |RIGHT] JOIN table_2 ON conditions
WHERE
conditions
GROUP BY column_1
HAVING group_conditions
ORDER BY column_1
LIMIT offset, length;
The SELECT statement consists of several clauses as explained in the following list:
∑ SELECT followed by a list of comma-separated columns or an asterisk (*) to indicate
that you want to return all columns.
∑ FROM specifies the table or view where you want to query the data.
∑ JOIN gets data from other tables based on certain join conditions.
IPSR Solutions Ltd www.ipsr.edu.in 4
MySql and JDBC
∑ The percentage ( % ) wildcard allows you to match any string of zero or more characters.
∑ The underscore ( _ ) wildcard allows you to match any single character.
The MySQL allows you to combine the NOT operator with the LIKE operator to find a string
that does not match a specific pattern.
Using LIMIT to Constrain The Number of Rows Returned By SELECT Statement
The LIMIT clause is used in the SELECT statement to constrain the number of rows in a result
set. The LIMIT clause accepts one or two arguments. The values of both arguments must be
zero or positive integers.
SELECT
column1,column2,...
FROM
table
LIMIT offset , count;
∑ The offset specifies the offset of the first row to return. The offset of the first row is 0, not 1.
∑ The count specifies the maximum number of rows to return.
When you use LIMIT with one argument, this argument will be used to specifies the maximum
number of rows to return from the beginning of the result set.
Using MySQL LIMIT to get the highest and lowest values
The LIMIT clause is often used with ORDER BY clause. First, you use the ORDER BY clause
to sort the result set based on certain criteria, and then you use the LIMIT clause to find lowest
or highest values.
Sorting Rows with MySQL ORDER BY
The ORDER BY clause allows you to:
∑ Sort a result set by a single column or multiple columns.
∑ Sort a result set by different columns in ascending or descending order.
SELECT column1, column2,...
FROM tbl
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC],...
IPSR Solutions Ltd www.ipsr.edu.in 7
MySql and JDBC
The ASC stands for ascending and the DESC stands for descending. By default, the ORDER
BY clause sorts the result set in ascending order if you don’t specify ASC or DESC explicitly.
Using MySQL Alias To Make The Queries More Readable
MySQL supports two kinds of aliases which are known as column alias and table alias.
MySQL alias for columns
Sometimes the names of columns are so technical that make the query’s output very difficult to
understand. To give a column a descriptive name, you use a column alias.
SELECT
[column_1 | expression] AS descriptive_name
FROM table_name;
To give a column an alias, you use the AS keyword followed by the alias. If the alias contains
space, you must quote it.
SELECT
CONCAT_WS(', ', lastName, firstname) AS `Full name`
FROM employees;
Because the AS keyword is optional, you can omit it in the statement. Note that you can also
give an expression an alias. In MySQL, you can use the column alias in the ORDER BY,
GROUP BY and HAVING clauses to refer to the column.
MySQL alias for tables
You can use an alias to give a table a different name. You assign a table an alias by using the
AS keyword as the following syntax:
table_name AS table_alias
You often use the table alias in the statement that contains INNER JOIN, LEFT JOIN, self join
clauses, and in subqueries.
∑ First, you have to specify the main table that appears in the FROM clause.
∑ Second, you need to specify the table that you want to join with the main table, which
appears in the INNER JOIN clause. Theoretically, you can join a table with many tables.
However, for better query performance, you should limit the number of tables to join.
∑ Third, you need to specify the join condition or join predicate. The join condition appears
after the keyword ON of the INNER JOIN clause. The join condition is the rule for
matching rows between the main table and the other tables.
SELECT column_list
FROM T1
INNER JOIN T2 ON join_condition1
WHERE where_conditions;
For each row in the T1 table, the MySQL INNER JOIN clause compares it with each row of the
T2 table to check if both of them satisfy the join condition. When the join condition is matched,
it will return the row that combine columns in both T1 and T2 tables. The rows in both T1 and
T2 tables have to be matched based on the join condition. If no match found, the query will
return an empty result set. This logic is also applied if we join more than 2 tables.
If you join multiple tables that have the same column name, you have to use table qualifier to
refer to that column in the SELECT clause to avoid ambiguous column error.
For example, if both T1 and T2 tables have the same column named C in the SELECT clause,
you have to refer to the C column using the table qualifiers as T1.C or T2.C .
IPSR Solutions Ltd www.ipsr.edu.in 9
MySql and JDBC
Parameter Description
IN A parameter whose value is unknown when the SQL statement is created. You bind
values to IN parameters with the setXXX() methods.
OUT A parameter whose value is supplied by the SQL statement it returns. You retrieve
values from the OUT parameters with the getXXX() methods.
INOUT A parameter that provides both input and output values. You bind variables with the
setXXX() methods and retrieve values with the getXXX() methods.