SQL Commands
SQL Commands
o SQL can perform various tasss lise create a tables add data to tabless drop
the tables modify the tables set permission for users.
o CREATE
o ALTER
o DROP
o TRUNCATE
Syntax:
COLUMN_NAME1 DATATYPES(size)s
COLUMN_NAME2 DATATYPES(size)s
--------------
COLUMN_NAMEN DATATYPES(size)s
);
Example:
EMPNo VARCHAR2(20)s
EName VARCHAR2(20)s
Job VARCHAR2(20)s
DOB DATE
);
b. DROP : This statement is used to drop an existing database. When you use this statement,
complete information present in the database will be lost.
Syntax
DROP DATABASE DatabaseName;
Example
DROP DATABASE Employee;
The ‘DROP TABLE’ Statement
This statement is used to drop an existing table. When you use this statement, complete
information present in the table will be lost.
Syntax
DROP TABLE TableName;
Example
DROP Table Emp;
c. ALTER
This command is used to delete, modify or add constraints or columns in an existing
table.
The ‘ALTER TABLE’ Statement
This statement is used to add, delete, modify columns in an existing table.
The ‘ALTER TABLE’ Statement with ADD/DROP COLUMN
You can use the ALTER TABLE statement with ADD/DROP Column command
according to your need. If you wish to add a column, then you will use the ADD
command, and if you wish to delete a column, then you will use the DROP COLUMN
command.
Syntax
ALTER TABLE TableName ADD ColumnName Datatype;
Syntax:
Example:
o INSERT
o UPDATE
o DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data
into the row of a table.
Syntax:
Or
For example:
Syntax:
For example:
Syntax1:
Syntax1
o Grant
o Revose
Example
Example
o COMMIT
o ROLLBACK
o SAVEPOINT
COMMIT;
Example:
COMMIT;
Syntax:
ROLLBACK;
Example:
ROLLBACK;
Syntax:
SAVEPOINT SAVEPOINT_NAME;
SELECT
This statement is used to select data from a database and the data returned is stored in
a result table, called the result-set.
Syntax
Apart from just using the SELECT keyword individually, you can use the following
keywords with the SELECT statement:
o
o DISTINCT
o ORDER BY
o GROUP BY
o HAVING Clause
o INTO
Example
Example
Select all employees from the 'Emp’ table sorted by EmpNo:
-- Select all employees from the 'Emp table sorted by EmpNo in Descending order:
/* Select all employees from the 'Emp' table sorted bsoEmpNo in Descending order and Ename in
Ascending order: */
Example
To list the number of employees from each city.
Example
To list the number of employees in each city. The employees should be sorted high to low and only
those cites must be included who have more than 5 employees:*/
SELECT COUNT(EmpNo), City FROM Emp GROUP BY City HAVING COUNT(EmpNo) > 2 ORDER BY
COUNT(EmpNo) DESC;
Example
To create a backup of database 'Employee'