Home >>Java JDBC Tutorial >JDBC SQL Syntax

JDBC SQL Syntax

Java Basic SQL Query Syntax

JDBC SQL Syntax is basically the syntax or the structure of the SQL that is used in this context. SQL basically elaborates to the Structured Query Language that is a standardized language whose function is to allow the users or the programmers to execute operations on a database, like creating entries, reading content, updating content, and deleting entries.

SQL is generally known to be supported by almost any database the user will likely to use this has also become popular because this allows the users to write database code independently of the underlying database. This tutorial is to give you an overview of SQL because of the fact that before learning the SQL you cannot proceed to understand the concepts of the JDBC. After the reader or the user have learned this tutorial then they will be able to create, Read, Update, and Delete data from a database that is the basic function of the CRUD operations.

Let's understand these following operations in brief and with examples

1. Create Database

The CREATE DATABASE statement in the SQL is basically used in order to create a new database.

Syntax

SQL> CREATE DATABASE DATABASE_NAME;

Example

SQL> CREATE DATABASE STUDENT;

2. Drop Database

The DROP DATABASE statement in the SQL is basically used in order to delete an existing database. Please note that in order to create or drop a database the programmer should have an administrator privilege on their database server. It is recommended to be extremely careful while deleting a database as it would loss all the data stored in the database.

Syntax

SQL> DROP DATABASE DATABASE_NAME;

Example

SQL> DROP DATABASE STUDENT;

3. Create Table

The CREATE TABLE statement in the SQL is basically used in order to create a new table

Syntax

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

Example

SQL> CREATE TABLE Students
(
   id INT NOT NULL,
   age INT NOT NULL,
   first_name VARCHAR(255),
   last_name VARCHAR(255),
   PRIMARY KEY ( id )
);

4. Drop Table

From the previous SQL statement it is clear that drop means delete something hence, the DROP TABLE statement is basically used when there is a need for deleting an existing table.

Syntax

SQL> DROP TABLE table_name;

Example

SQL> DROP TABLE Students;

5. INSERT Data

This SQL statement is basically used to insert the data. The syntax of the INSERT statement is very similar to a sequential series as it has been depicted below where column1, column2, and the series go on that basically represents the new data that is to be appear in the respective columns:

Syntax

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

Example

SQL> INSERT INTO Students VALUES (100,28, 'sanjeev', 'kumar');

6. SELECT Data

The SELECT statement of the SQL is basically used in order to retrieve data from a database.

Syntax

SQL> SELECT column_name, column_name, ...
     FROM table_name
     WHERE conditions;
	 
	 //OR
SQL> SELECT *
     FROM table_name
     WHERE conditions;	 

Note :The WHERE clause that has been mentioned in the above syntax can be used to make comparison with the help of operators like =,<, >, !=, <=,and >=, as well as the BETWEEN and LIKE operators.

Example

SQL> SELECT first_name, last_name,age 
     FROM Students 
     WHERE id = 100;

7. UPDATE Data

As the name suggests, the UPDATE SQL statement is generally used in order to update the data. Here is the syntax of the UPDATE SQL statement is depicted below for your understanding:

Syntax

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

Example

SQL> UPDATE Students SET age=30 WHERE id=100;

8. DELETE Data

As the name suggests the DELETE SQL statement is generally used to delete the data from tables.

Syntax

SQL> DELETE FROM table_name WHERE conditions;

Example

SQL> DELETE FROM Students WHERE id=100;