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

EXtraQuery

database management system

Uploaded by

zerin22205101835
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)
11 views

EXtraQuery

database management system

Uploaded by

zerin22205101835
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

DDL (Data Definition Language) and DML (Data Manipulation Language) are two subsets of SQL

(Structured Query Language) used in database management. They serve distinct purposes related to the
structure and content of a database.

DDL (Data Definition Language):

DDL is used to define and manage the structure of a database, including tables, schemas, and other
objects. It is concerned with creating, altering, and deleting database objects such as tables and indexes.

Purpose:

To define or modify the database structure.

It impacts the schema or the overall design of the database.

Common DDL Commands:

CREATE: Used to create new objects like tables, databases, views, etc.

Example:

CREATE TABLE Employees (

EmployeeID INT PRIMARY KEY,

Name VARCHAR(100),

Department VARCHAR(50),

Salary DECIMAL(10, 2)

);

ALTER: Modifies an existing database object (e.g., adding a column to a table).

- Example:

ALTER TABLE Employees ADD Address VARCHAR(255);

DROP: Deletes objects like tables or databases.

Example:

DROP TABLE Employees;


TRUNCATE: Removes all rows from a table but keeps the structure intact.

Example:

TRUNCATE TABLE Employees;

2. DML (Data Manipulation Language):

DML is used to manipulate and manage data within existing database objects. It focuses on CRUD
operations (Create, Read, Update, Delete).

Purpose:

To manage the data stored in tables.

It doesn't alter the structure of the database but only the data it contains.

Common DML Commands:

INSERT: Adds new records to a table.

Example:

INSERT INTO Employees (EmployeeID, Name, Department, Salary)

VALUES (1, 'John Doe', 'IT', 50000);

SELECT: Retrieves data from one or more tables.

Example:

SELECT * FROM Employees;


UPDATE: Modifies existing records in a table.

Example:

UPDATE Employees

SET Salary = 60000

WHERE EmployeeID = 1;

DELETE: Removes records from a table.

Example:

DELETE FROM Employees

WHERE EmployeeID = 1;

Summary of Purpose:

DDL focuses on the structure of the database.

DML focuses on the data within the structure.

Each serves an essential role in database management: DDL for setting up and modifying the schema,
and DML for managing the data stored within that schema.
SQL (Structured Query Language) can be broken down into several key concepts, including relational
algebra, aggregate functions, and set operations. Here's a quick breakdown of each:

### 1. Relational Algebra (Algebra in SQL)

Relational algebra provides a foundation for SQL, using a set of operations to manipulate data in
relational databases. SQL commands essentially translate into these algebraic operations.

Key operations include:

- Selection (σ): Filters rows based on conditions.

SELECT * FROM table WHERE condition;

- Projection (π): Selects specific columns.

SELECT column1, column2 FROM table;

- Union (⋃): Combines the result set of two queries (removes duplicates).

SELECT * FROM table1 UNION SELECT * FROM table2;

- Set Difference (-): Returns rows in one query but not in another.

SELECT * FROM table1 EXCEPT SELECT * FROM table2;

- Cartesian Product (×): Combines every row from one table with every row of another.

SELECT * FROM table1 CROSS JOIN table2;

- Join (⨝): Combines rows from two or more tables based on a related column.
SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;

### 2. Aggregate Functions

Aggregate functions perform a calculation on a set of values and return a single value. They're often
used with the GROUP BY clause.

Common aggregate functions:

- COUNT(): Returns the number of rows.

SELECT COUNT(*) FROM table;

- SUM(): Adds up the values of a numeric column.

SELECT SUM(column) FROM table;

- AVG(): Returns the average of a numeric column.

SELECT AVG(column) FROM table;

- MIN(): Returns the smallest value in a column.

SELECT MIN(column) FROM table;

- MAX(): Returns the largest value in a column.

SELECT MAX(column) FROM table;

### 3. Set Operations


Set operations allow you to combine the results of two or more SELECT queries.

- UNION: Combines results from multiple queries but removes duplicates.

SELECT column1 FROM table1

UNION

SELECT column1 FROM table2;

- UNION ALL: Combines results and keeps duplicates.

SELECT column1 FROM table1

UNION ALL

SELECT column1 FROM table2;

- INTERSECT: Returns rows that are common to both queries.

SELECT column1 FROM table1

INTERSECT

SELECT column1 FROM table2;

- EXCEPT: Returns rows from the first query that are not in the second query.

SELECT column1 FROM table1

EXCEPT

SELECT column1 FROM table2;

These concepts form the backbone of SQL query structure, allowing you to perform complex data
manipulation and analysis.

You might also like