DBMS Exp 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

EXPERIMENT NO.

AIM: Apply DML Commands for the specified system

OBJECTIVES: To perform various DML Operations and executing nested queries


with various clauses.

THEORY:
DML, or Data Manipulation Language, statements are used to manipulate the data present in a
database. The most important DML statements are INSERT, UPDATE & DELETE.
Apart from these commands, there are also other manipulative operators/functions such
as:

➢ Arithmetic, Bitwise, Compound and Comparison Operators


➢ Logical Operators
➢ Aggregate Functions
➢ Special Operators
➢ Set Operations

INSERT

The INSERT statement is used to insert new records in a table.

Syntax:

The INSERT INTO statement can be written in the following two ways:

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

--You need not mention the column names

INSERT INTO table_name VALUES (value1, value2, value3, ...);


UPDATE

The UPDATE statement is used to modify the existing records in a table.

Syntax:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Department of EXTC, SIES GST


DELETE

The DELETE statement is used to delete existing records in a table.

Syntax:

DELETE FROM table_name WHERE condition;


SELECT

The SELECT statement is used to select data from a database and the data returned is stored in
a result table, called the result-set.

The following are the two ways of using this statement:

Syntax:
SELECT column1, column2, ...
FROM table_name;

--(*) is used to select all from the table

SELECT * FROM table_name;

Apart from the individual SELECT keyword, you can use the SELECT keyword with the
following statements:

• DISTINCT
• ORDER BY
• GROUP BY
• HAVING Clause

The ‘SELECT DISTINCT’ Statement


The SELECT DISTINCT statement is used to return only distinct or different values. So, if you
have a table with duplicate values, then you can use this statement to list distinct values.

Syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name;

The ‘ORDER BY’ Statement


The ORDER BY statement is used to sort the desired results in ascending or descending order.
By default, the results would be sorted in ascending order. If you want to sort the records in
descending order, then you have to use the DESC keyword.

Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ...ASC|DESC;
The ‘GROUP BY’ Statement

This statement is used with the aggregate functions to group the result-set by one or more
columns.

Department of EXTC, SIES GST


Syntax:

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
The ‘HAVING’ Clause Statement

Since the WHERE keyword cannot be used with aggregate functions, the HAVING clause was
introduced.

Syntax:

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

ALTER TABLE table_name


DROP CONSTRAINT MyPrimaryKey;

UPDATE Query is used to modify the existing records in a table. You can use the
WHERE clause with the UPDATE query to update the selected rows, otherwise all the
rows would be affected.

The basic syntax of the UPDATE query with a WHERE clause is as follows −

UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

DELETE Query is used to delete the existing records from a table.

You can use the WHERE clause with a DELETE query to delete the selected rows,
otherwise all the records would be deleted.

The basic syntax of the DELETE query with the WHERE clause is as follows −

DELETE FROM table_name


WHERE [condition];

Department of EXTC, SIES GST


Clauses and Operators
1. Group by clause: These are circumstances where we would like to apply the
aggregate functions to a single set of tuples but also to a group of sets of tuples we
would like to specify this wish in SQL using the group by clause. The attributes or
attributes given by the group by clause are used to form groups. Tuples with the same
value on all attributes in the group by clause placed in one group.
Example:.

Select<attribute_name,avg(<attribute_name>)as
<new_attribute_name>l From <table_name>
Group by <attribute_name>

Example: select designation, sum( salary) as total_salary from employee group by


Designation;

2. Having clause: A having clause is like a where clause but only applies only to groups
as a whole whereas the where clause applies to the individual rows. A query can contain
both where clause and a having clause. In that case
a. The where clause is applied first to the individual rows in the tables or table
structures objects in the diagram pane. Only the rows that meet the conditions in the
where clause are grouped.
b. The having clause is then applied to the rows in the result set that are produced
by grouping. Only the groups that meet the having conditions appear in the query
output.
Example:

select dept_no from EMPLOYEE group_by dept_no


having avg (salary) >=all (select avg (salary)
from EMPLOYEE group by dept_no);

Example: The following SQL statement returns TRUE and lists the productnames if it
finds ANY records in the OrderDetails table that quantity = 10:

SELECT ProductName
FROM Products
WHERE ProductID
= ANY (SELECT ProductID FROM OrderDetails WHERE Quantity = 10);

Department of EXTC, SIES GST


IMPLEMENTATION DETAILS
- Simple question based on your application, queries and screen shots for each
type:

Department of EXTC, SIES GST


Group By:

Department of EXTC, SIES GST


Order By:

Department of EXTC, SIES GST


Update:

Department of EXTC, SIES GST


Conclusion: We successfully performed various DML operations on the specified
system.

Name: Aman Chavan


PRN: 120A7016
Branch: SE - ECS

Department of EXTC, SIES GST

You might also like