SQL Commands - Booklet
SQL Commands - Booklet
When you use this statement, ALTER TABLE Emp DROP COLUMN MobNo ; a. INSERT: The INSERT statement is a SQL query. It is used to insert data
SQL Commands o All the command of DDL are auto-committed that means it permanently save
complete information present in the database will be lost. into the row of a table.
all the changes in the database.
Syntax Syntax:
o SQL commands are instructions. It is used to communicate with the database. The ‘ALTER TABLE’ Statement with ALTER/MODIFY COLUMN
Here are some commands that come under DDL: DROP DATABASE DatabaseName;
It is also used to perform specifc tassss functionss and queries of data. This statement is used to change the datatype of an existing column in a table. INSERT INTO TABLE_NAME
Example
o SQL can perform various tasss lise create a tables add data to tabless drop o CREATE Syntax (col1s col2s col3s.... col N)
DROP DATABASE Employee;
the tables modify the tables set permission for users. o ALTER ALTER TABLE TableName ADD COLUMN ColumnName Datatype;
VALUES (value1s value2s value3s .... valueN);
The ‘DROP TABLE’ Statement
Example
Types of SQL Commands o DROP
This statement is used to drop an existing table. When you use this statement, complete Or
information present in the table will be lost. --Add a column DOB and change the data type to Date.
o TRUNCATE
There are fve types of SQL commands: DDLs DMLs DCLs TCLs and DQL. INSERT INTO TABLE_NAME
Syntax ALTER TABLE Emp ADD DOB date;
a. CREATE It is used to create a new table in the database.
d. TRUNCATE VALUES (value1s value2s value3s .... valueN);
DROP TABLE TableName;
Syntax: This command is used to delete the information present in the table but does not delete For example:
Example
the table. So, once you use this command, your information will be lost, but not the
CREATE TABLE TABLE_NAME
DROP Table Emp; table. INSERT INTO EMP(ENamesJob) VALUES ("SCOTT"s "MANAGER");
( c. ALTER Syntax: b. UPDATE: This command is used to update or modify the value of a
COLUMN_NAME1 DATATYPES(size)s This command is used to delete, modify or add constraints or columns in an existing column in the table.
table. TRUNCATE TABLE table_name;
COLUMN_NAME2 DATATYPES(size)s Syntax:
The ‘ALTER TABLE’ Statement Example:
-------------- UPDATE table_name SET column1= values column2= values
This statement is used to add, delete, modify columns in an existing table. TRUNCATE TABLE EMPLOYEE; columnN = value WHERE CONDITION;
COLUMN_NAMEN DATATYPES(size)s The ‘ALTER TABLE’ Statement with ADD/DROP COLUMN
2. Data Manipulation Language For example:
); 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 o DML commands are used to modify the database. It is responsible for all form UPDATE Emp SET Ename = 'SMITH' WHERE EmpNo = '1003';
Example: command, and if you wish to delete a column, then you will use the DROP COLUMN
command. of changes in the database. c. DELETE: It is used to remove one or more row from a table.
CREATE TABLE EMP
Syntax o The command of DML is not auto-committed that means it can't permanently
Syntax1:
( ALTER TABLE TableName ADD ColumnName Datatype; save all the changes in the database. They can be rollbacs.
DELETE FROM table_name;
EMPNo VARCHAR2(20)s Here are some commands that come under DML:
Syntax1
EName VARCHAR2(20)s ALTER TABLE TableName DROP COLUMN ColumnName;
o INSERT DELETE FROM table_name WHERE condition;
Job VARCHAR2(20)s
Example
1. Data Definition Language (DDL) --ADD Column MobNo: o UPDATE Example1: Delete all rows from emp table
DOB DATE
o DDL changes the structure of the table lise creating a tables deleting a tables ALTER TABLE Emp ADD MobNo Number(10); o DELETE
DELETE FROM Emp;
);
altering a tables etc. --DROP Column MobNo:
Example2: Delete all rows from emp table whose Ename is SCOTT Syntax: -- To select the number of records to return use: -- Select all employees from the 'Empl’ table sorted by EmpNo and EName: Syntax
DELETE FROM EName WHERE EName="SCOTT"; COMMIT; SELECT * FROM Emp ORDER BY EmpNo, EName;
SELECT TOP 3 * FROM TableName; SELECT * INTO NewTable IN ExternalDB FROM OldTable WHERE
/* Select all employees from the 'Emp' table sorted bsoEmpNo in Descending order and Ename in Condition;
3. Data Control Language Example: Ascending order: */
Example
DELETE FROM CUSTOMERS WHERE AGE = 25; Apart from just using the SELECT keyword individually, you can use the following SELECT * FROM Emp ORDER BY EmpNo ASC, Ename DESC
DCL commands are used to grant and tase bacs authority from any database
keywords with the SELECT statement: To create a backup of database 'Employee'
user. The ‘GROUP BY’ Statement
COMMIT;
o SELECT * INTO EmpNo FROM Emp;
Here are some commands that come under DCL: This ‘GROUP BY’ statement is used with the aggregate functions to group the result-set
b. Rollback: Rollbacs command is used to undo transactions that have not o DISTINCT by one or more columns.
already been saved to the database. o ORDER BY
o Grant o GROUP BY Syntax
Syntax: o HAVING Clause
o Revose o INTO SELECT Column1, Column2,..., ColumnN FROM TableName
ROLLBACK;
a. Grant: It is used to give user access privileges to a database. WHERE Condition GROUP BY ColumnName(s) ORDER BY ColumnName(s);
The ‘SELECT DISTINCT’ Statement
Example:
Example This statement is used to return only different values. Example
DELETE FROM CUSTOMERS WHERE AGE = 25;
GRANT SELECTs UPDATE ON MY_TABLE TO SOME_USERs ANOTHER_USER; Syntax To list the number of employees from each city.
ROLLBACK;
SELECT DISTINCT Column1, Column2, ...ColumnN FROM TableName; SELECT COUNT(EmpNo), City FROM Emp GROUP BY City
b. Revoke: It is used to tase bacs permissions from the user.
c. SAVEPOINT: It is used to roll the transaction bacs to a certain point The ‘HAVING’ Clause
Example without rolling bacs the entire transaction. SELECT DISTINCT MobNo FROM Emp;
The ‘HAVING’ clause is used in SQL because the WHERE keyword cannot be used
Example
REVOKE SELECTs UPDATE ON MY_TABLE FROM USER1s USER2; Syntax: everywhere.
The ‘ORDER BY’ Statement Syntax
SAVEPOINT SAVEPOINT_NAME;
4. Transaction Control Language The ‘ORDER BY’ statement is used to sort the required results in ascending or
descending order. The results are sorted in ascending order by default. Yet, if you wish SELECT ColumnName(s) FROM TableName WHERE Condition GROUP BY
TCL commands can only use with DML commands lise INSERTs DELETE and 5. Data Query Language ColumnName(s) HAVING Condition ORDER BY ColumnName(s);
to get the required results in descending order, you have to use the DESC keyword.
UPDATE only.
DQL is used to fetch the data from the database. Syntax Example
These operations are automatically committed in the database that's why
they cannot be used while creating tables or dropping them. SELECT SELECT Column1, Column2, ...ColumnN FROM TableName 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:*/
This statement is used to select data from a database and the data returned is stored in
Here are some commands that come under TCL: ORDER BY Column1, Column2, ... ASC|DESC;
a result table, called the result-set. SELECT COUNT(EmpNo), City FROM Emp GROUP BY City HAVING COUNT(EmpNo) > 2 ORDER BY
COUNT(EmpNo) DESC;
Syntax Example
o COMMIT
Select all employees from the 'Emp’ table sorted by EmpNo:
o ROLLBACK SELECT Column1, Column2, ...ColumN FROM TableName;
The ‘SELECT INTO’ Statement
SELECT * FROM Emp ORDER BY EmpNo;
o SAVEPOINT --(*) is used to select all from the table The ‘SELECT INTO’ statement is used to copy data from one table to another.
-- Select all employees from the 'Emp table sorted by EmpNo in Descending order:
a. Commit: Commit command is used to save all the transactions to the SELECT * FROM table_name; SELECT * FROM Employee_Info ORDER BY EmpNo DESC;
database.