SQL Commands
Introduction
SQL stands for Structured Query Language. SQL commands are the
instructions used to communicate with a database to perform tasks, functions,
and queries with data.
SQL commands can be used to search the database and to do other functions
like creating tables, adding data to tables, modifying data, and dropping tables.
Here is a list of basic SQL commands (sometimes called clauses) you should
know if you are going to work with SQL
DDL (Data Definition Language)
The DDL Commands in Structured Query Language are used to create and
modify the schema of the database and its objects. The syntax of DDL
commands is predefined for describing the data. The commands of Data
Definition Language deal with how the data should exist in the database.
Here are some commands that come under DDL:
o CREATE
o ALTER
o DROP
o TRUNCATE
CREATE Command
CREATE is a DDL command used to create databases, tables, triggers and other
database object
Syntax :
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,.
...]);
Example:
1. CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCH
AR2(100), DOB DATE);
DROP Command
It is used to delete both the structure and record stored in the table.
Syntax
DROP TABLE table_name;
Example
DROP TABLE EMPLOYEE;
ALTER Command
It is used to alter the structure of the database. This change could be
either to modify the characteristics of an existing attribute or probably to
add a new attribute.
Syntax
ALTER TABLE table_name ADD column_name COLUMN-definition;
Example
ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));
TRUNCATE Command
It is used to delete all the rows from the table and free the space containing the
table.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE EMPLOYEE;
Data Manipulation Language (DML)
DML commands are used to modify the database. It is responsible for all form
of changes in the database.The command of DML is not auto-committed that
means it can't permanently save all the changes in the database. They can be
rollback.
Here are some commands that come under DML:
o INSERT
o UPDATE
o DELETE
INSERT Command
The INSERT statement is a SQL query. It is used to insert data into the
row of a table.
Syntax :
INSERT INTO TABLE_NAME
(col1, col2, col3,.... col N)
VALUES (value1, value2, value3, .... valueN);
Example :
INSERT INTO javatpoint (Author, Subject) VALUES ("Sonoo", "DBMS
");
UPDATE Command
This command is used to update or modify the value of a column in the
table.
Syntax:
UPDATE table_name SET [column_name1= value1,...column_nameN = val
ueN] [WHERE CONDITION]
Example :
UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'
DELETE Command
It is used to remove one or more row from a table.
Syntax:
DELETE FROM table_name [WHERE condition];
Example :
DELETE FROM javatpoint
WHERE Author="Sonoo";
Data Control Language (DCL)
DCL commands are used to grant and take back authority from any
database user.
Here are some commands that come under DCL:
o Grant
o Revoke
Grant: It is used to give user access privileges to a database.
Syntax:
Grant select ,insert ,update ,delete on table name to username;
Example :
Grant select ,insert , update , delete , on student to Raj;
Revoke: It is used to take back permissions from the user.
Syntax:
Revoke select ,insert ,update ,delete on tablename to suername;
Example :
Revoke select insert ,update ,delete on student to Raj;
Transaction Control Language (TCL)
TCL commands can only use with DML commands like INSERT, DELETE and
UPDATE only.
These operations are automatically committed in the database that's why they
cannot be used while creating tables or dropping them.
Here are some commands that come under TCL:
o COMMIT
o ROLLBACK
o SAVEPOINT
o
Commit: Commit command is used to save all the transactions to the
database.
Syntax:
COMMIT;
Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
COMMIT;
Rollback: Rollback command is used to undo transactions that have not
already been saved to the database.
Syntax:
ROLLBACK;
Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
ROLLBACK;
Data Query Language (DQL)
DQL is used to fetch the data from the database.
It uses only one command:
o SELECT
SELECT: This is the same as the projection operation of relational
algebra. It is used to select the attribute based on the condition described
by WHERE clause.
Syntax:
SELECT expressions
FROM TABLES
WHERE conditions;
Example:
SELECT emp_name
FROM employee
WHERE age > 20;