JDBC SQL Syntax

« Previous Chapter Next Chapter »

Structured Query Language (SQL) is a standardized language that allows you to perform operations on a database, such as creating entries, reading content, updating content, and deleting entries.

SQL is supported by all most any database you will likely use, and it allows you to write database code independently of the underlying database.

This tutorial gives an overview of SQL, which is a pre-requisite to understand JDBC concepts. This tutorial gives you enough SQL to be able to Create, Read, Update, and Delete (often referred to as CRUD operations) data from a database.

For a detailed understanding on SQL, you can read our MySQL Tutorial.

Create Database:

The CREATE DATABASE statement is used for creating a new database. The syntax is:

Example

SQL> CREATE DATABASE DATABASE_NAME;

Example:

The Below SQL statement creates a Database named EMP:

Example

SQL> CREATE DATABASE EMP;

Drop Database:

The DROP DATABASE statement is used for deleting an existing database. The syntax is:

Example

SQL> DROP DATABASE DATABASE_NAME;

Note: To create or drop a database you should have administrator privilege on your database server. Be careful, deleting a database would loss all the data stored in database.

Create Table:

The CREATE TABLE statement is used for creating a new table. The syntax is:

Example

SQL> CREATE TABLE table_name
(
   column_name column_data_type,
   column_name column_data_type,
   column_name column_data_type
   ...
);

Example:

The Below SQL statement creates a table named Friends with four columns:

Example

SQL> CREATE TABLE Friends
(
   id INT NOT NULL,
   age INT NOT NULL,
   first VARCHAR(255),
   last VARCHAR(255),
   PRIMARY KEY ( id )
);

Drop Table:

The DROP TABLE statement is used for deleting an existing table. The syntax is:

Example

SQL> DROP TABLE table_name;

Example:

The Below SQL statement deletes a table named Friends:

Example

SQL> DROP TABLE Friends;

INSERT Data:

The syntax for INSERT looks similar to the following, where column1, column2, and so on represent the new data to appear in the respective columns:

Example

SQL> INSERT INTO table_name VALUES (column1, column2, ...);

Example:

The Below SQL INSERT statement inserts a new row in the Friends database created earlier:

Example

SQL> INSERT INTO Friends VALUES (100, 18, 'Sophia', 'Lomela');

SELECT Data:

The SELECT statement is used to retrieve data from a database. The syntax for SELECT is:

Example

SQL> SELECT column_name, column_name, ...
     FROM table_name
     WHERE conditions;

The WHERE clause can use the comparison operators such as =, !=, <, >, <=,and >=, as well as the BETWEEN and LIKE operators.

Example:

The Below SQL statement selects the age, first and last columns from the Friends table where id column is 100:

Example

SQL> SELECT first, last, age 
     FROM Friends 
     WHERE id = 100;

The Below SQL statement selects the age, first and last columns from the Friends table where first column contains Sophia:

Example

SQL> SELECT first, last, age 
     FROM Friends 
     WHERE first LIKE '%Sophia%';

UPDATE Data:

The UPDATE statement is used to update data. The syntax for UPDATE is:

Example

SQL> UPDATE table_name
     SET column_name = value, column_name = value, ...
     WHERE conditions;

The WHERE clause can use the comparison operators such as =, !=, <, >, <=,and >=, as well as the BETWEEN and LIKE operators.

Example:

The Below SQL UPDATE statement changes the age column of the Friend whose id is 100:

Example

SQL> UPDATE Friends SET age=20 WHERE id=100;

DELETE Data:

The DELETE statement is used to delete data from tables. The syntax for DELETE is:

Example

SQL> DELETE FROM table_name WHERE conditions;

The WHERE clause can use the comparison operators such as =, !=, <, >, <=,and >=, as well as the BETWEEN and LIKE operators.

Example:

The Below SQL DELETE statement delete the record of the Friend whose id is 100:

Example

SQL> DELETE FROM Friends WHERE id=100;


« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!