Practical_Part1-SQL COMMANDS
Practical_Part1-SQL COMMANDS
Charmo University
College of Science
Department of Computer Science
2024-2025
Types of SQL Commands
❖ 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.
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
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:
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
For example:
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:
Syntax1
Example2:
Delete all rows from emp table whose Ename is SCOTT
DCL commands are used to grant and take bacs authority from any
database user.
Grant
Revoke
Example
Example
a.COMMIT
b.ROLLBACK
c.SAVEPOINT
Syntax:
ROLLBACK;
Example:
DELETE FROM CUSTOMERS WHERE AGE = 25;
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
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;
ASC|DESC;
Column1, Column2, ... ASC|DESC;
Example
Select all employees from the 'Emp’ table sorted by EmpNo: SELECT *
-- 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:
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
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
Example
To create a backup of database 'Employee' SELECT * INTO EmpNo FROM Emp
The End