Structured Query Language (SQL) is a standardized language that allows you to perform actions on a database, such as creating projects, reading content, updating content, and deleting entries.
SQL is all that can be used with almost any database support, which allows code written to the database independent of the underlying database.
This tutorial gives an overview of the SQL, which is a prerequisite for understanding JDBC. This tutorial provides enough SQL to be able to create, read, update, and delete data from one database (often called crud operations).
For more information about SQL, you can read our MySQL tutorial.
To create a database:
Copy Code code as follows:
Statement is used to create a new database. The syntax is:
Copy Code code as follows:
sql> CREATE DATABASE database_name;
Example:
The following SQL statement creates an EMP database named:
Copy Code code as follows:
sql> CREATE DATABASE EMP;
To delete a database:
Use the DROP DATABASE statement to delete an existing database. The syntax is:
Copy Code code as follows:
sql> DROP DATABASE database_name;
Note: To create or delete, you should have a database with administrator privileges on the database server. Note that deleting the database will lose all loss of data stored in the database.
To create a table:
The CREATE TABLE statement is used for creating a new table. The syntax is:
Copy Code code as follows:
sql> CREATE TABLE table_name
(
column_name Column_data_type,
column_name Column_data_type,
column_name Column_data_type
...
);
Example:
The following SQL statement creates a four-field named Employees table:
Copy Code code as follows:
sql> CREATE TABLE Employees
(
ID INT not NULL,
Age INT is not NULL,
The VARCHAR (255),
Last VARCHAR (255),
PRIMARY KEY (ID)
);
To delete a table:
The DROP TABLE statement is used to delete an existing table. The syntax is:
Copy Code code as follows:
sql> DROP TABLE table_name;
Example:
The following SQL statement deletes a table named employees:
Copy Code code as follows:
sql> DROP TABLE Employees;
Insert data:
Syntax inserts resemble the following, where Column1, Column2, and so on indicate that the new data appears in the columns:
Copy Code code as follows:
Sql> INSERT into table_name VALUES (column1, Column2, ...);
Example:
Insert the previously created employees database in the following SQL INSERT statement:
Copy Code code as follows:
Sql> INSERT into Employees VALUES (+, ' Zara ', ' Ali ');
SELECT Data:
The SELECT statement is used to retrieve data from the database. The select for this syntax is:
Copy Code code as follows:
Sql> SELECT column_name, column_name, ...
From table_name
WHERE conditions;
The WHERE clause can use comparison operators such as =,!=, <=, >=, and between and like operators.
Example:
The following SQL statement selects age first and last column names where id = 100 from the Employees table:
Copy Code code as follows:
Sql> SELECT A, last, age
From Employees
WHERE id = 100;
The following SQL statement is from the Employees table, where the first column selects age, and the first column contains Zara:
Copy Code code as follows:
Sql> SELECT A, last, age
From Employees
WHERE the '%zara% ';
UPDATE Data:
The UPDATE statement is used to update data. The update syntax is:
Copy Code code as follows:
Sql> UPDATE table_name
SET column_name = value, COLUMN_NAME = value, ...
WHERE conditions;
The WHERE clause can use comparison operators such as =,! =,<,>,<=, and >=, as well as between and like operators.
Example:
The following SQL UPDATE statement changes the age column for employees whose ID is 100:
Copy Code code as follows:
sql> UPDATE Employees SET age=20 WHERE id=100;
DELETE Data:
The DELETE statement is used to delete the data in the table. The syntax delete is:
Copy Code code as follows:
Sql> DELETE from table_name WHERE conditions;
The WHERE clause can use comparison operators such as =,! =,<,>,<=, and >=, as well as between and like operators.
Example:
The following SQL DELETE statement deletes records for employees with ID 100:
Copy Code code as follows:
Sql> DELETE from Employees WHERE id=100;