0% found this document useful (0 votes)
13 views6 pages

SQL Queries With Example

The document categorizes SQL commands into DDL, DML, DCL, and TCL, explaining their functions and providing examples using a sample company database schema. DDL commands define database structure, DML commands manipulate data, DCL commands control access, and TCL commands manage transactions. It also details the differences between DROP, DELETE, and TRUNCATE commands regarding their effects, use cases, rollback capabilities, and performance.

Uploaded by

Aarthy C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

SQL Queries With Example

The document categorizes SQL commands into DDL, DML, DCL, and TCL, explaining their functions and providing examples using a sample company database schema. DDL commands define database structure, DML commands manipulate data, DCL commands control access, and TCL commands manage transactions. It also details the differences between DROP, DELETE, and TRUNCATE commands regarding their effects, use cases, rollback capabilities, and performance.

Uploaded by

Aarthy C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Let's break down the SQL commands into four categories: DDL (Data Definition

Language), DML (Data Manipulation Language), DCL (Data Control Language), and
TCL (Transaction Control Language). We'll use the same sample database for all the
examples. Let's assume we have the following simple database schema for a company
database with two related tables: employees and departments.

Sample Schema:

 employees table:
o emp_id (INT)
o emp_name (VARCHAR)
o emp_salary (DECIMAL)
o dept_id (INT, FK referencing departments.dept_id)
 departments table:
o dept_id (INT)
o dept_name (VARCHAR)

1. DDL (Data Definition Language)

DDL commands are used to define or modify the structure of a database (e.g., creating,
altering, or deleting tables).

 CREATE: Defines new tables, databases, or views.


 ALTER: Modifies an existing database object.
 DROP: Deletes an existing database object.

Example:

 Creating Tables:

sql
Copy
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(100)
);

CREATE TABLE employees (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(100),
emp_salary DECIMAL(10, 2),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
);

 Altering a Table (e.g., adding a new column):

sql
Copy
ALTER TABLE employees ADD COLUMN emp_email VARCHAR(100);

 Dropping a Table:
sql
Copy
DROP TABLE employees;

2. DML (Data Manipulation Language)

DML commands are used to manipulate data within existing tables. These commands are
primarily for querying, inserting, updating, and deleting data.

 SELECT: Retrieves data from the database.


 INSERT: Adds new records to a table.
 UPDATE: Modifies existing records.
 DELETE: Removes records from a table.

Example:

 Inserting Data:

sql
Copy
INSERT INTO departments (dept_id, dept_name)
VALUES (1, 'HR'), (2, 'Engineering'), (3, 'Sales');

INSERT INTO employees (emp_id, emp_name, emp_salary, dept_id)


VALUES (101, 'John Doe', 50000, 2),
(102, 'Jane Smith', 55000, 1),
(103, 'Mark Johnson', 60000, 3);

 Selecting Data:

sql
Copy
SELECT * FROM employees;

 Updating Data:

sql
Copy
UPDATE employees
SET emp_salary = 65000
WHERE emp_id = 103;

 Deleting Data:

sql
Copy
DELETE FROM employees WHERE emp_id = 101;

3. DCL (Data Control Language)

DCL commands are used to control access to data in the database. They deal with
permissions and access control.

 GRANT: Provides a user with access privileges.


 REVOKE: Removes access privileges from a user.

Example:

 Granting Privileges:

sql
Copy
GRANT SELECT, INSERT ON employees TO user_name;

 Revoking Privileges:

sql
Copy
REVOKE SELECT, INSERT ON employees FROM user_name;

4. TCL (Transaction Control Language)

TCL commands manage the transactions in the database. They ensure that the database
remains in a consistent state.

 COMMIT: Saves the transaction to the database.


 ROLLBACK: Undoes the transaction (returns the database to its previous state).
 SAVEPOINT: Sets a point in a transaction to which you can roll back later.
 SET TRANSACTION: Defines the properties of the transaction.

Example:

 Using Transactions:

sql
Copy
BEGIN;

INSERT INTO employees (emp_id, emp_name, emp_salary, dept_id)


VALUES (104, 'Alice Williams', 70000, 2);

COMMIT; -- Save the changes

-- OR, if something goes wrong:


-- ROLLBACK; -- Undo the changes

 Using Savepoint:

sql
Copy
BEGIN;

SAVEPOINT savepoint1;

INSERT INTO employees (emp_id, emp_name, emp_salary, dept_id)


VALUES (105, 'Bob Brown', 75000, 1);

-- If there's an issue, roll back to savepoint1


ROLLBACK TO SAVEPOINT savepoint1;
Summary of DDL, DML, DCL, and TCL Commands

Command
Description Example
Type
Defines or modifies database CREATE TABLE, ALTER TABLE, DROP
DDL
structure TABLE
DML Manipulates data in the database SELECT, INSERT, UPDATE, DELETE
Controls access to data in the
DCL GRANT, REVOKE
database
Manages transactions and ensures COMMIT, ROLLBACK, SAVEPOINT, SET
TCL
consistency TRANSACTION

These commands together help manage a relational database effectively by structuring it,
manipulating data, controlling access, and ensuring transactions are safely handled.

The SQL commands DROP, DELETE, and TRUNCATE are used to remove data or database
objects, but they behave quite differently in terms of scope, usage, and performance. Here's a
breakdown of each command with examples:

1. DROP Command

The DROP command is used to completely remove a table, database, or other database objects
(such as views, indexes, etc.) from the database. When you use DROP, the table structure and
its data are both deleted permanently.

 Impact: Removes the table definition and the data. The table is no longer available in
the database, and it cannot be recovered unless you have a backup.
 Rollback: You cannot roll back a DROP operation.
 Performance: Fast, as it removes the entire table along with its data.

Example:

sql
Copy
-- Drops the entire "employees" table, including its data and structure
DROP TABLE employees;

2. DELETE Command

The DELETE command is used to remove rows from a table based on a condition. It allows
you to specify which rows to delete (through the WHERE clause), and it removes the data but
keeps the structure of the table intact.

 Impact: Removes specified rows from the table, but the table itself remains in the
database with its structure.
 Rollback: Can be rolled back if used within a transaction (i.e., within a BEGIN and
COMMIT block).
 Performance: Slower than TRUNCATE because each row is deleted individually, and it
may also log each row deletion in the transaction log.
Example:

sql
Copy
-- Deletes records where the employee's salary is less than 50000
DELETE FROM employees WHERE emp_salary < 50000;

 This will delete specific rows that match the condition in the WHERE clause, but the
table structure will remain intact.

3. TRUNCATE Command

The TRUNCATE command is used to remove all rows from a table, but it does not remove the
table structure. Unlike DELETE, it doesn't allow you to specify conditions for row deletion.

 Impact: Removes all rows from the table, but the table structure (including columns,
constraints, etc.) remains. It effectively resets the table, but it does not remove the
table itself.
 Rollback: Cannot be rolled back in many databases (it is non-transactional in some
systems). In others, it may be rollbackable within a transaction.
 Performance: Faster than DELETE because it doesn't log individual row deletions and
doesn't fire triggers. It simply deallocates the data pages used by the table.

Example:

sql
Copy
-- Removes all rows from the "employees" table, but leaves the structure
intact
TRUNCATE TABLE employees;

 This will remove all rows in the employees table, but the table itself remains
available for use.

Key Differences Between DROP, DELETE, and TRUNCATE

Feature DROP DELETE TRUNCATE


Completely removes the Removes specified rows Removes all rows (keeps
Effect
table (data + structure) (keeps table structure) table structure)
When you want to
When you want to When you want to delete
Use case completely remove a
remove specific rows all rows from a table
table
Can use WHERE clause to Not applicable (removes
Where clause Not applicable
specify rows to delete all rows)
Can be rolled back (if May or may not be rolled
Rollback Cannot be rolled back
within a transaction) back (depends on DBMS)
Fires triggers (if any are
Triggers Does not fire triggers Does not fire triggers
defined)
Performance Fast (removes the table Slower (because it Fast (no row-by-row
and its data completely) removes rows one at a operation)
Feature DROP DELETE TRUNCATE
time)
Table
Table is removed Table structure remains Table structure remains
Structure

When to Use Each Command:

 Use DROP when you want to completely remove a table, including all its data,
constraints, and indexes. This is typically done when you no longer need the table or
want to re-create it from scratch.
 Use DELETE when you need to remove specific rows from a table and want to keep the
table structure and constraints intact. It’s useful when you need fine-grained control
over which rows to remove.
 Use TRUNCATE when you want to quickly remove all rows from a table without
deleting the structure and you don't need to worry about triggers or the possibility of
rolling back the operation. It’s efficient for clearing out a table when you want to start
fresh but keep the table available for future use.

Example Scenario

Let's say you have the following table employees:

emp_id emp_name emp_salary dept_id


101 John Doe 50000 2
102 Jane Smith 55000 1
103 Mark Johnson 60000 3

 Using DROP: If you issue DROP TABLE employees;, the entire employees table
(including data and structure) will be removed from the database. This action is
irreversible.
 Using DELETE: If you issue DELETE FROM employees WHERE emp_id = 101;, only
the row for "John Doe" will be deleted, but the employees table will remain with the
other data intact.
 Using TRUNCATE: If you issue TRUNCATE TABLE employees;, all rows will be
removed from the employees table, but the table structure will remain, and you can
add more rows later.

Let me know if you need further clarification on any of these!

You might also like