Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Index: S.N O. Experiment Date Remark S T. Sign

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

INDEX

S.N Experiment Date Remark T.


o. s Sign
1. To study various DDL commands
CREATE,ALTER,DROP and
TRUNCATE.
2. To apply integrity constraints on
tables like PRIMARY KEY,FOREIGN
KEY,CHECK,NOT NULL,etc.

3. To study various DML command


SELECT,INSERT,UPDATE and
DELETE.

4. WAP to implement built in functions


GROUP BY, HAVING and ORDER
BY.
5. Write the queries to implement the
joints.
6. Write the queries to implement the
sub queries.
7. Write the queries to implement the
set operations.
8. Write the queries to create the views
and queries based on views.
9. Demonstrate the concept of control
structures.
10. Write the queries to implement the
concept of correlated sub queries.
11. Write the queries to implement the
concept of Exception handling.
12. Demonstate the concept of stored
functions and procedures
13. Demonstate the concept of triggers.
EXPERIMENT :-1
AIM:- To study various DDL commands CREATE,ALTER,DROP
and TRUNCATE.
THEORY:-
CREATE: The CREATE TABLE statement is used to create a
new table in a database.
Syntax:-
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
The column parameters specify the names of the columns of
the table.
The datatype parameter specifies the type of data the column can
hold.
ALTER:- ALTER is a DDL command which changes or modifies
the existing structure of the database, and it also changes the
schema of database objects.

We can also add and drop constraints of the table using the
ALTER command.
DROP:-
ALTER is a DDL command which changes or modifies the
existing structure of the database, and it also changes the
schema of database objects.
We can also add and drop constraints of the table using the
ALTER command.
TRUNCATE:- TRUNCATE is another DDL command which deletes
or removes all the records from the table.
EXPERIMENT :-2
AIM:- To apply integrity constraints on tables like PRIMARY
KEY,FOREIGN KEY,CHECK,NOT NULL,etc.
THEORY:-
NOT NULL:- This constraint specifies that the column cannot
have NULL or empty values.

CHECK:- It controls the value in a particular column. It ensures


that the inserted value in a column must be satisfied with the
given condition. In other words, it determines whether the value
associated with the column is valid or not with the given
condition.
PRIMARY KEY:- This constraint is used to identify each record in
a table uniquely. If the column contains primary key constraints,
then it cannot be null or empty. A table may have duplicate
columns, but it can contain only one primary key. It always
contains unique value into a column.

FOREIGN KEY:- This constraint is used to link two tables


together. It is also known as the referencing key. A foreign key
column matches the primary key field of another table. It means
a foreign key field in one table refers to the primary key field of
another table.
EXPERIMENT :-3
AIM:- To study various DML command SELECT,INSERT,UPDATE
and DELETE.
THEORY:-
SELECT : It is the most important data manipulation command
in Structured Query Language. The SELECT command shows the
records of the specified table. It also shows the particular record
of a particular column by using the WHERE clause.
INSERT: It is another most important data manipulation
command in Structured Query Language, which allows users to
insert data in database tables.
Syntax of INSERT Command:

INSERT INTO TABLE_NAME ( column_Name1 , column_Name2 , column_Name3 , .... column_


NameN ) VALUES (value_1, value_2, value_3, .... value_N ) ;

UPDATE: It is another most important data manipulation


command in Structured Query Language, which allows users to
update or modify the existing data in database tables.
Syntax of UPDATE Command

UPDATE Table_name SET [column_name1= value_1, ….., column_nameN = value_N]


WHERE CONDITION;
DELETE: DELETE is a DML command which allows SQL users to
remove single or multiple existing records from the database
tables.This command of DML does not delete the stored data
permanently from the database. We use the WHERE clause with
the DELETE command to select specific rows from the table.

Syntax of DELETE Command

DELETE FROM Table_Name WHERE condition;


EXPERIMENT :-4
AIM:-WAP to implement built in functions GROUP BY, HAVING
and ORDER BY.
THEORY:
The ORDER BY clause allows sorting the output values, i.e.,
sorting the retrieved value by a specific column. Sorting can also
be applied by a column alias that is defined with a clause.

The advantage of ORDER BY is that it can be applied to both


numeric and string columns. String columns are usually sorted
alphabetically.

By default, the ascending (ASC) sorting is applied. For the sorting


to be descending, an additional DESC clause is used.

In SQL, the GROUP BY clause collects data retrieved from


specific groups in a database. Grouping divides all data into
logical sets so that statistical calculations can be performed
separately in each group.

This clause is used to combine the selection results by one or


more columns. After grouping, there will be only one entry for
each value used in the column.

The use of GROUP BY is closely related to the use of aggregate


functions and the HAVING statement. An aggregate function in
SQL is a function that returns a single value over a set of column
values. For example: COUNT (), MIN (), MAX (), AVG (), SUM ().

HAVING is a filtering tool. It indicates the result of aggregate


functions performance. The HAVING clause is used when we
cannot use the WHERE keyword, i.e., with aggregate functions.
While the WHERE clause defines predicate for rows filtering, the
HAVING clause is used after grouping to establish a logical
predicate that filters groups by the values of aggregate functions.
This statement is necessary for checking the values obtained
through aggregate functions from groups of returned rows.
EXPERIMENT :-5
AIM:-To study about various SQL joins INNER join, FULL join,
LEFT join and RIGHT join.
THEORY:
INNER join: The INNER JOIN keyword selects records that have
matching values in both tables.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

The example above displays the name of all the customers who
have placed an order.
LEFT join: The LEFT JOIN keyword returns all records from the
left table (table1), and the matching records from the right table
(table2). The result is 0 records from the right side, if there is no
match.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

The LEFT JOIN keyword returns all records from the left table
(Customer), even if there are no matches in the right table
(Orders).
RIGHT join: The RIGHT JOIN keyword returns all records from
the right table (table2), and the matching records from the left
table (table1). The result is 0 records from the left side, if there is
no match.
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

The RIGHT JOIN keyword returns all records from the right table
(workers), even if there are no matches in the left table (orders).
FULL join: The FULL OUTER JOIN keyword returns all records
when there is a match in left (table1) or right (table2) table
records.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
EXPERIMENT 6
AIM: Write the queries to implement the sub queries.
Theory: A subquery is a SQL query nested inside a larger query.
• A subquery may occur in :
o - A SELECT clause
o - A FROM clause
o - A WHERE clause
• The subquery can be nested inside a SELECT, INSERT,
UPDATE, or DELETE statement or inside another subquery.
• A subquery is usually added within the WHERE Clause of
another SQL SELECT statement.
• You can use the comparison operators, such as >, <, or =.
The comparison operator can also be a multiple-row operator,
such as IN, ANY, or ALL.
• A subquery is also called an inner query or inner select, while
the statement containing a subquery is also called an outer
query or outer select.
• The inner query executes first before its parent query so that
the results of an inner query can be passed to the outer
query.

You can use a subquery in a SELECT, INSERT, DELETE, or


UPDATE statement to perform the following tasks:
• Compare an expression to the result of the query.
• Determine if an expression is included in the results of the
query.
• Check whether the query selects any row.
• The subquery (inner query) executes once before the main
query (outer query) executes.
• The main query (outer query) use the subquery result.

Syntax:
Q. How to find marks of student whose name is Anil and lives in
Delhi?

Q. How to find the marks of student who lives in Noida?


EXPERIMENT 7
AIM: Write the queries to implement the set operations.
Theory:-
The set operators UNION, UNION ALL, INTERSECT, and MINUS
can manipulate the result sets of two queries that specify the same
number of columns in the Projection clause, and that have
compatible data types in the corresponding columns of both
queries.

(The MINUS set operator has EXCEPT as its keyword synonym.


Results that the MINUS and EXCEPT operators return from the
same operands are always identical.)

These operators perform basic set operations of union, intersection,


and difference on the result sets of two queries that are the left and
right operands of the set operators:

• The UNION set operator combines the qualifying rows from


two queries into a single result set that consists of the distinct
rows that either or both of the queries returned. (If you also
include the ALL keyword, the UNION ALL result set can
include duplicate rows.)

• The INTERSECT set operator compares the result sets from


two queries, but returns only the distinct rows that are in the
result sets of both queries.

• The MINUS set operator compares the result sets from two
queries, but returns only the distinct rows in the result set of
the left query that are absent from the result set of the right
query.

• Only the UNION set operator supports the ALL keyword. The
ALL keyword is not valid with the INTERSECT, MINUS, or
EXCEPT set operators, from which only distinct rows are
returned.
• When comparing rows to calculate a set intersection or
difference, two NULL values are considered equal in
INTERSECT and MINUS operations.

UNION:

UNION ALL:
INTERSECT:

EXCEPT/MINUS:
EXPERIMENT 8
AIM: Write the queries to create the views and queries based on
views.
Theory:-
Views in SQL are kind of virtual tables. A view also has rows and
columns as they are in a real table in the database. We can create
a view by selecting fields from one or more tables present in the
database. A View can either have all the rows of a table or specific
rows based on certain condition.

Creating a
view from a
single table.

Creating a view from multiple tables


Only those views are updatable that come from a single table
otherwise we cannot insert, delete or update those views.
Uses of a View: A good database should contain views due to the
given reasons:
1. Restricting data access – Views provide an additional level
of table security by restricting access to a predetermined set
of rows and columns of a table.
2. Hiding data complexity – A view can hide the complexity
that exists in multiple tables join.
3. Simplify commands for the user – Views allow the user to
select information from multiple tables without requiring the
users to actually know how to perform a join.
4. Store complex queries – Views can be used to store
complex queries.
5. Rename Columns – Views can also be used to rename the
columns without affecting the base tables provided the
number of columns in view must match the number of
columns specified in select statement. Thus, renaming helps
to hide the names of the columns of the base tables.
6. Multiple view facility – Different views can be created on
the same table for different users.
EXPERIMENT 9
AIM: Demonstrate the concept of control structures.
Theory:-
MySQL supports the IF, CASE, ITERATE, LEAVE LOOP, WHILE,
and REPEAT constructs for flow control within stored programs. It
also supports RETURN within stored functions.
Many of these constructs contain other statements, as indicated
by the grammar specifications in the following sections. Such
constructs may be nested. For example, an IF statement might
contain a WHILE loop, which itself contains a CASE statement.
MySQL does not support FOR loops.

Simple if statement
Demonstrations using IF control function:-
Demonstrations using CASE function:-
EXPERIMENT 10
AIM: Writ the queries to implement the concept of
correlated subqueries.
Theory:-
Correlated subqueries are used for row-by-row processing. Each
subquery is executed once for every row of the outer query.

A correlated subquery is evaluated once for each row processed


by the parent statement. The parent statement can be
a SELECT, UPDATE, or DELETE statement.
SELECT column1, column2, ....
FROM table1 outer
WHERE column1 operator
(SELECT column1, column2
FROM table2
WHERE expr1 =
outer.expr2);
A correlated subquery is one way of reading every row in a table
and comparing values in each row against related data. It is used
whenever a subquery must return a different result or set of
results for each candidate row considered by the main query. In
other words, you can use a correlated subquery to answer a
multipart question whose answer depends on the value in each
row processed by the parent statement.
EXPERIMENT 11
AIM: Demonstrate the concept of Exception Handling.
Theory:-
While working with stored procedures in MySQL if an exception
or occurs the execution of the procedure terminates abruptly, to
avoid this you need to handle the exceptions in MYSQL.
MySQL provides a handler to handle the exceptions in the stored
procedures. You can handle these exceptions by declaring a
handler using the MySQL DECLARE ... HANDLER Statement.
EXPERIMENT 12
AIM: Demonstrate the concept of Stored Functions and
Procedures.
Theory:-
A stored function is a special kind stored program that returns a
single value. Typically, you use stored functions to encapsulate
common formulas or business rules that are reusable among
SQL statements or stored programs.

Different from a stored procedure, you can use a stored function


in SQL statements wherever an expression is used. This helps
improve the readability and maintainability of the procedural
code.
EXPERIMENT 13
AIM: Demonstrate the concept of Triggers.
Theory:-
Triggers in SQL are special types of stored procedures that are
automatically executed in response to certain events or actions in
a database. They are commonly used to enforce data integrity
rules, audit changes to data, or perform other automated tasks.

You might also like