0% found this document useful (0 votes)
2 views

Practical_Part1-SQL COMMANDS

The document outlines the five types of SQL commands: DDL, DML, DCL, TCL, and DQL, detailing their functions and examples. It explains specific commands under each category, such as CREATE, INSERT, GRANT, COMMIT, and SELECT, along with their syntax and usage. The document serves as a comprehensive guide for understanding and utilizing SQL commands effectively.

Uploaded by

asoala81
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Practical_Part1-SQL COMMANDS

The document outlines the five types of SQL commands: DDL, DML, DCL, TCL, and DQL, detailing their functions and examples. It explains specific commands under each category, such as CREATE, INSERT, GRANT, COMMIT, and SELECT, along with their syntax and usage. The document serves as a comprehensive guide for understanding and utilizing SQL commands effectively.

Uploaded by

asoala81
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Practical Part I

Charmo University
College of Science
Department of Computer Science
2024-2025
Types of SQL Commands

There are five types of SQL commands:


1. Data Definition Language (DDL)
2. Data Manipulation Language (DML)
3. Data Control Language(DCL)
4. Transaction Control Language (TCL)
5. Data Query Language(DQL)
SQL Commands
SQL Commands

DDL DML DCL TCL DQL


❖ CREATE ❖ INSERT ❖ GRANT ❖ COMMIT ❖ SELECT

❖ DROP ❖ UPDATE ❖ ROVOKE ❖ ROLLBACK

❖ ALTER ❖ DELETE ❖ SAVEPOINT

❖ TRUNCATE
1. Data Definition Language (DDL)

DDL changes the structure of the table like creating a table deleting a table altering a
table etc.

All the command of DDL are auto-committed that means it permanently saves all the
changes in the database.

Here are some commands that come under DDL:

A.CREATE

B.DROP

C.ALTER

D.TRUNCATE
A- CREATE It is used to create a new table in
the database.
Syntax:
CREATE TABLE TABLE_NAME (
COLUMN_NAME1 DATATYPES (size),
COLUMN_NAMEN DATATYPES (size), );

Example:
1- Create database
Example: Create database charmo;
2- Create table
Example: Create table student
(
studentID int,
FirstName varchar(255),
LastName varchar(255),
Address varchar(255),
phone No int(255) );
B- DROP: This statement is used to drop an existing database.

1-Drop database
Syntax
DROP DATABASE DatabaseName
Example:
DROP DATABASE Employee

2- Drop table
Syntax
DROP TABLE TableName
Example:
DROP Table Emp;

NOTE :This statement is used to drop an existing table. When you use this statement, complete
information present in the table will be lost.
C- ALTER
This command is used to delete, modify or add constraints
or columns in an existing table.
Add Column

Syntax
ALTER TABLE TableName
ADD ColumnName Datatype;

Example
--ADD Column
ALTER TABLE student
ADD age varchar;
Delete column(drop column)
Syntax

ALTER TABLE TableName


DROP COLUMN ColumnName;
Example

ALTER TABLE student


DROP COLUMN age;
MODIFY DATATYPE OF COLUMN
This statement is used to change the datatype
of an existing column in a table.

Syntax
ALTER TABLE TableName
ALTER COLUMN ColumnName Datatype;

Example
ALTER column age int;
D- TRUNCATE
This command is used to delete the information present in the
table but does not delete the table. So, once you use this
command, your information will be lost, but not the table.

Syntax:

TRUNCATE TABLE table_name;


Example:
❖ TRUNCATE TABLE EMPLOYEE;
❖ Delete from EMPLOYEE where age>30;

insert into [dbo].[ff] (emyName,Number,Id)


values
('aram',99,1),
('ali',100,2),
('lana',25,3);

select *from[dbo].[ EMPLOYEE];

Delete from ff where Number>30;


TRUNCATE TABLE ff;
2. Data Manipulation Language
a. DML commands are used to modify the database. It is responsible for all form of
changes in the database.

b. 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.
A.INSERT

B.UPDATE

C.DELETE

A. INSERT: The INSERT statement is a SQL query. It is used to insert


data into the row of a table.
Syntax:

INSERT INTO TABLE_NAME

(col1s col2s col3s col N)

VALUES (value1s value2s value3s valueN); Or

INSERT INTO TABLE_NAME

VALUES (value1s value2s value3s valueN);

For example:

1. INSERT INTO EMP (EName, Job) VALUES ("SCOTT"s "MANAGER");

1. insert into [dbo].[ff] (emyName,Number,Id)


values
('aram',99,1),
('ali',100,2),
('lana',25,3);
A. UPDATE: This command is used to update or modify the value
B. of a column in the table.

Syntax:

UPDATE table_name
SET column1= values
column2= values
columnN = value
WHERE CONDITION;

For example:

UPDATE Emp
SET Ename = 'SMITH'
WHERE EmpNo = '1003';

UPDATE ff
SET Number= 88
WHERE Id=4;
A. DELETE: It is used to remove one or more row from a table.

Syntax1:

DELETE FROM table_name;

Syntax1

DELETE FROM table_name WHERE condition;


Example:
DELETE FROM ff
WHERE emyName='dana';//delete only one row.
Example1:
Delete all rows from emp table

DELETE FROM Emp;


DELETE FROM ff; //delete all rows data.

Example2:
Delete all rows from emp table whose Ename is SCOTT

DELETE FROM EName WHERE EName="SCOTT";


DELETE FROM ff
WHERE emyName='dana';//delete one row only.
3. Data Control Language

DCL commands are used to grant and take bacs authority from any
database user.
Grant

Revoke

a. Grant: It is used to give user access privileges to a database.

Example

GRANT SELECTs UPDATE ON MY_TABLE TO SOME_USERs ANOTHER_USER;

a. Revoke: It is used to take bacs permissions from the user.

Example

REVOKE SELECTs UPDATE ON MY_TABLE FROM USER1s USER2;


4. Transaction Control Language
TCL commands can only use with DML commands like INSERTs DELETE and UPDATE

These operations are automatically committed in the database that's why


they cannot be used while creating tables or dropping them.

a.COMMIT

b.ROLLBACK

c.SAVEPOINT

a. Commit: Commit command is used to save all the transactions to


the database.
Syntax:
COMMIT;
Example:
DELETE FROM CUSTOMERS WHERE AGE =25;

COMMIT; // save data when we record it.

a. 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; //undo to delete rows (records) that not saved by commit.


a. SAVEPOINT: It is used to roll the transaction bacs
to a certain point without rolling bacs the entire
transaction.

Syntax:

SAVEPOINT SAVEPOINT_NAME;
5. Data Query Language
DQL is used to fetch the data from the database.

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

SELECT
Column1,
Column2, ...ColumN,
FROM TableName;
--(*) is used to select all from the table

SELECT * FROM table_name;

-- To select the number of records to return use:

SELECT TOP 3 * FROM TableName;

Apart from just using the SELECT keyword individually, you can use the
following keywords with the SELECT statement:
1. DISTINCT
2. ORDER BY
3. GROUP BY
4. HAVING Clause
5. INTO
1- ‘SELECT DISTINCT’ Statement
This statement is used to return only different values.
Syntax

SELECT DISTINCT
Column1, Column2, ...ColumnN FROM TableName;

SELECT DISTINCT MobNo FROM Emp;


Example
2- ‘ORDER BY’ Statement
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 to get the required results in descending order, you have to
use the DESC keyword.
Syntax

SELECT Column1, Column2, ...ColumnN FROM

TableName ORDER BY Column1, Column2, ...

ASC|DESC;
Column1, Column2, ... ASC|DESC;
Example

Select all employees from the 'Emp’ table sorted by EmpNo: SELECT *

FROM Emp ORDER BY EmpNo;

-- Select all employees from the 'Emp table sorted by EmpNo in Descending order:
SELECT * FROM Employee Info ORDER BY EmpNo DESC;

-- Select all employees from the 'Empl’ table sorted by EmpNo and EName:

SELECT * FROM Emp ORDER BY EmpNo, EName;


/* Select all employees from the 'Emp' table sorted bsoEmpNo in
Descending order and Ename in Ascending order: */
SELECT * FROM Emp ORDER BY EmpNo ASC, Ename DESC
3- ‘GROUP BY’ Statement
This ‘GROUP BY’ statement is used with the aggregate functions to group the
result-set by one or more columns.
Syntax

SELECT Column1, Column2,..., ColumnN FROM TableName

WHERE Condition GROUP BY ColumnName(s) ORDER BY


ColumnName(s);

Example
To list the number of employees from each city.
SELECT COUNT(EmpNo), City FROM Emp GROUP BY City
4- ‘HAVING’ Clause
The ‘HAVING’ clause is used in SQL because the WHERE keyword cannot
be used everywhere.
Syntax

SELECT ColumnName(s) FROM TableName WHERE Condition


GROUP BY ColumnName(s) HAVING Condition ORDER BY
ColumnName(s);

Example
To list the number of employees in each city. The employees should be sorted high to low and
only those cities 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;
5- ‘SELECT INTO’ Statement
The ‘SELECT INTO’ statement is used to copy data from one table
to another.
Syntax

SELECT * INTO NewTable IN ExternalDB FROM


OldTable WHERE Condition;

Example
To create a backup of database 'Employee' SELECT * INTO EmpNo FROM Emp
The End

You might also like