Database Management System Practical File
Database Management System Practical File
Database Management System Practical File
PRACTICAL
file
Semester :5th
1804125
TABLE OF CONTENTS
EXPERIMENT-1
SQL is the standard language for Relational Database System. All the
Relational Database Management Systems (RDMS) like MySQL, MS Access,
Oracle, Sybase, Informix, Postgres and SQL Server use SQL as their standard
database language.
1. The installer asks you to select the installation type, choose the Custom installation type
allows you to step through the SQL Server installation wizard and select the features that you
want to install.
2. Specify the folder for storing the installation files that the installer will download, then click
the Install button.
4. Once the download completes, open the folder that stores the install package and double-
click the SETUP.exe file.
5. The following window displays; select the installation option on the left.
6. Click the first link to launch a wizard to install SQL Server 2017.
7. Specify the edition that you want to install, select Developer edition and click the Next
button.
8. Select the “I accept the license terms.” and click the Next button.
9. Check the “Use Microsoft Update to check for updates (recommended)” to get the security
other important updates for the SQL Server and click the Next button.
10. The installation checks for the prerequisites before installation. If no error found, click the
Next button.
11. Select the features that you want to install. For now, you just need the Database Engine
Services, just check the checkbox and click the Next button to continue.
12. Specify the name and install ID for the instance of the SQL Server and click the Next button.
13. Specify the service account and collation configuration. Just use the default configuration
and click the Next button.
14. Specify the database engine security mode. First, choose Mixed Mode. Next, enter the
password for the SQL Server system administrator (sa) account. Then, re-enter the same
password to confirm it. After that, click the Add Current User button. Finally, click the Next
button.
EXPERIMENT-2
Data Types, Creating Tables, Retrieval of Rows using Select Statement, Conditional
1. Numeric data types such as int, tinyint, bigint, float, real etc.
2. Date and Time data types such as Date, Time, Datetime etc.
3. Character and String data types such as char, varchar, text etc.
4. Unicode character string data types, for example nchar, nvarchar, ntext etc.
5. Binary data types such as binary, varbinary etc.
6. Miscellaneous data types – clob, blob, xml, cursor, table etc.
Syntax:-
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
For example:-
Person ID int,
);
OUTPUT:-
SELECT Syntax:-
SELECT column1, column2, ...
FROM table_name;
For example:-
OUTPUT:-
1 Rahul Gurgaon
2 Sanjay Shimla
3 Deepak Hyderabad
SYNTAX:-
WHERE CONDITION;
For example:-
OUTPUT:-
Alter statement:-
Syntax:-
For example:-
OUTPUT:-
DROP STATEMENT:-
Syntax:-
For example:-
EXPERIMENT-3
Working with null values, matching a Pattern from a Table, Ordering a result of a
query, Aggregate Functions, Grouping the result of a Query, Update and Delete
statement.
If a field in a table is optional, it is possible to insert a new record or update a record without adding a value
to this field. Then, the field will be saved with a NULL value.
Note: A NULL value is different form a zero value or a field that contains spaces. A field with a NULL
value is one that has been left blank during record creation!
IS NULL SYNTAX:-
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
For example:-SELECT Person ID , First Name FROM Persons WHERE First Name IS NOT NULL;
OUTPUT:-
1 Rahul
2 Sanjay
3 Deepak
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
For example:-
SELECT First Name, Pin Code
FROM Persons
WHERE Pin Code IS NULL;
OUTPUT:-
Matching Patterns From a Table:- In SQL, we sometimes need to filter our result set based on some pattern
matching techniques. SQL has a standard pattern matching technique using the 'LIKE' operator. But, it
also supports the regular expression pattern matching for better functionality.
Generally, the REGEXP_LIKE (column_name, 'regex') function is used for pattern matching in SQL.
SQL also supports some operators that work similar to this function, these are: 'REGEXP' and 'RLIKE'
operator. So in this blog, we will learn about all those functions and operators that provide pattern matching
functionality in SQL.
For example:-
SELECT * FROM Persons
OUTPUT:-
The SQL ORDER BY clause is used to sort the data in ascending or descending order,
based on one or more columns. Some databases sort the query results in an ascending order
by default.
Syntax:-
The basic syntax of the ORDER BY clause which would be used to sort the result in an
ascending or descending order is as follows –
SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
6 Komal 22 MP 4500.00
For example:-
Output:-
6 Komal 22 MP 4500.00
AGGREGATE FUNCTIONS:-
An aggregate function performs a calculation on a set of values, and returns a single value.
Except for COUNT(*), aggregate functions ignore null values. Aggregate functions are
• The select list of a SELECT statement (either a subquery or an outer query). A HAVING
clause.
6 Komal 22 MP 4500.00
FOR EXAMPLE:-
OUTPUT:-
AVG(‘ AGE ’)
25.42
For example:-
OUTPUT:-
SUM( ‘SALARY ’)
35000.00
The GROUP BY Statement in SQL is used to arrange identical data into groups with the help of some
functions. i.e if a particular column has same values in different rows then it will arrange these rows in a
group.
Important Points:
• GROUP BY clause is used with the SELECT statement.
• In the query, GROUP BY clause is placed after the WHERE clause.
• In the query, GROUP BY clause is placed before ORDER BY clause if used any.
SYNTAX:-
FROM table_name
WHERE condition
1 HARSH 2000 19
2 DHANRAJ 3000 29
3 ASHISH 1500 19
4 HARSH 3500 19
5 ASHISH 1500 19
FOR EXAMPLE:-
Example:
Group By single column: Group By single column means, to place all the rows with same value of
only that particular column in one group. Consider the query as shown below:
GROUP BY NAME;
OUTPUT:-
NAME SALARY
ASHISH 3000
DHANRAJ 3000
HARSH 5500
The SQL 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 –
Syntax:-
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
1 HARSH 2000 19
2 DHANRAJ 3000 29
3 ASHISH 1500 19
4 HARSH 3500 19
5 ASHISH 1500 19
For example:-
UPDATE EMPLOYEE
WHERE SL NO.=4
OUTPUT:-
1 HARSH 2000 19
2 DHANRAJ 3000 29
3 ASHISH 1500 19
4 HARSH 4000 19
5 ASHISH 1500 19
The SQL 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.
Syntax:-
For example:-
OUTPUT:-
1 HARSH 2000 19
2 DHANRAJ 3000 29
3 ASHISH 1500 19
4 HARSH 4000 19
EXPERIMENT-4
Set operations, Nested Queries, Joins, sequences.
The SQL Set operation is used to combine the two or more SQL SELECT statements.
1. Union
o The SQL Union operation is used to combine the result of two or more SQL SELECT queries.
o In the union operation, all the number of datatype and columns must be same in both the tables on which UNION
operation is being applied.
o The union operation eliminates the duplicate rows from its resultset.
FOR EXAMPLE:-
ID NAME
1 JACK
2 HARRY
3 JACKSON
4 STEPHAN
5 DAVID
OUTPUT:-
NAME
ID
1 JACK
2 HARRY
3 JACKSON
4 STEPHAN
5 DAVID
2. Union All
Union All operation is equal to the Union operation. It returns the set without removing duplication and sorting the data.
Syntax:-
For example:-
OUTPUT:-
ID NAME
1 JACK
2 HARRY
3 JACKSON
3 JACKSON
4 STEPHAN
5 DAVID
3. Intersect
o It is used to combine two SELECT statements. The Intersect operation returns the
common rows from both the SELECT statements.
o In the Intersect operation, the number of datatype and columns must be the same. o
It has no duplicates and it arranges the data in ascending order by default.
Syntax:-
OUTPUT:-
ID NAME
3 JACKSON
4. Minus
o It combines the result of two SELECT statements. Minus operator is used to display the rows which are present in the first
query but absent in the second query.
o It has no duplicates and data arranged in ascending order by default.
Syntax:
For example:-
SELECT * FROM First
MINUS SELECT * FROM
Second;
OUPUT:-
ID NAME
1 JACK
2 HARRY
Nested Query :-
A Subquery is a query within another SQL query and embedded within the WHERE clause.
Important Rule: A subquery can be placed in a number of SQL clauses like WHERE clause, FROM clause, HAVING clause.
o You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along with the operators like =, <, >, >=,
<=, IN, BETWEEN, etc.
o A subquery is a query within another query. The outer query is known as the main query, and the inner query is known as
a subquery.
o Subqueries are on the right side of the comparison operator. o A subquery is enclosed in parentheses.
o In the Subquery, ORDER BY command cannot be used. But GROUP BY command can be used to perform the same
function as ORDER BY command.
SQL subqueries are most frequently used with the Select statement.
Syntax:-
SELECT column_name
FROM table_name
WHERE column_name expression operator
( SELECT column_name from table_name WHERE ... );
1 JOHN 20 2000
2 STEPHAN 26 1500
3 DAVID 27 2000
4 ALINA 29 6500
5 KATHRIN 34 8500
SELECT *
FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE
WHERE SALARY > 4500);
OUTPUT:
4 ALINA 29 6500
5 KATHRIN 34 8500
JOINS:-
As the name shows, JOIN means to combine something. In case of SQL, JOIN means "to combine two or
more tables".
The SQL JOIN clause takes records from two or more tables in a database and combines it together.
ANSI standard SQL defines five types of JOIN :
1. inner join,
2. left outer join,
3. right outer join, 4. full outer join, and
5. cross join.
STAFF TABLE:-
PAYMENT TABLE:-
PAY_ID DATE STAFF_ID AMOUNT
OUTPUT:-
STAFF-ID NAME STAFF_AGE AMOUNT
3 MONTY 25 2500
1 ARYAN 22 3000
4 AMIT 25 3500
(don't care its matched or unmatched) from the both participating tables.
TABLE A:-
A M
1 m
2 n
4 o
TABLE B:-
A N
2 p
3 q
5 r
FOR EXAMPLE:-
SELECT *
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
OUTPUT:-
A M A N
2 n 2 p
1 m - -
4 o - -
- - 3 q
- - 5 r
SEQUENCES:-
Sequences are frequently used in databases because many applications require each row in a table to
contain a unique value and sequences provide an easy way to generate them.
The simplest way in MySQL to use sequences is to define a column as AUTO_INCREMENT and
leave the rest to SQL to take care.
FOR EXAMPLE:-
);
OUTPUT:-
EXPERIMENT-5
Views, Indexes, Database Security and Privileges: Grant and Revoke Commands,
Commit and Rollback Commands.
A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real
tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the data were
coming from one single table.
S ID CLASS
1 11
2 12
3 11
4 12
For Example :-
CREATE View class 11 as
WHERE CLASS=11;
SQL Index
o Indexes are special lookup tables. It is used to retrieve data from the database very fast.
o An Index is used to speed up select queries and where clauses. But it shows down the data input with
insert and update statements. Indexes can be created or dropped without affecting the data.
o An index in a database is just like an index in the back of a book.
o For example: When you reference all pages in a book that discusses a certain topic, you first have to
refer to the index, which alphabetically lists all the topics and then referred to one or more specific
page numbers.
Syntax:-
Syntax:-
Example:-
Syntax:-
For Example:-
GRANT Statement:-
The grant statement enables system administrators to assign privileges and roles to the MySQL user
accounts so that they can use the assigned permission on the database whenever required.
Syntax:-
GRANT privilege_name(s)
ON object
TO user_account_name;
MySQL Revoke Privilege
MySQL provides REVOKE statements to remove privileges from a user account.
REVOKE Statement:-
The revoke statement enables system administrators to revoke privileges and roles to the MySQL user
accounts so that they cannot use the assigned permission on the database in the past.
Syntax:-
The following are the basic syntax of using the REVOKE statement:
REVOKE privilege_name(s)
ON object
FROM user_account_name;
The commands are used to control the transactions, these commands are given below-
COMMIT Command
It is also known as Transactional Command. The command is used by the database to save changes.
Syntax:
COMMIT;
For example:-
1 HARSH 2000 19
2 DHANRAJ 3000 29
3 ASHISH 1500 19
4 HARSH 4000 19
Begin Tran
DELETE FROM EMPLOYEES
WHERE AGE = 29 COMMIT
OUTPUT:-
1 HARSH 2000 19
3 ASHISH 1500 19
4 HARSH 4000 19
ROLLBACK Command:
The ROLLBACK command is the transactional command used to undo transactions that have not already
been saved to the database. Rollback command is used to undo the set of transactions.
Syntax:
ROLLBACK
For example:-
Begin Tran
DELETE FROM EMPLOYEES
WHERE AGE = 29; ROLLBACK
OUTPUT:-
1 HARSH 2000 19
2 DHANRAJ 3000 29
3 ASHISH 1500 19
4 HARSH 4000 19
EXPERIMENT-6
PL/SQL Architecture
The PL/SQL is a case insensitive, a robust procedural extension of the SQL. The SQL
allows its statements to be directly included in the PL/SQL blocks and still looks like a
single language together as they are tightly coupled with each other as there are no APIs
needed to bind them together unlike other programming languages.
1. Header
2. Declaration
3. Execution
4. Exception
For example:-
CREATE TABLE tempp ( item number, square number, CUBE number );
TABLE created.
num*num,
num*num*num);
END;
OUTPUT:-
5 25 125
PL/SQL CODE:-
DECLARE
VAR1 NUMBER;
VAR2 NUMBER;
BEGIN
VAR1:=200;
VAR2:=1;
WHILE (VAR2<=5)
LOOP
DBMS_OUTPUT.PUT_LINE (VAR1*VAR2);
VAR2:=VAR2+1;
END LOOP;
END;
OUTPUT:-
200
400
600
800
1000
Parameters Modes:-
IN mode- Default; Passes a constant value from the calling environment to the procedure
(p_id IN employees.emp_id%type)
IS
Begin
salary=salary*0.10
where emp_id=p_id
End raise_salary;
DECLARE
NUMBER,
BEGIN
FROM employees
END raise_salary;
BEGIN
DBMS_OUTPUT.PUT_LINE
END;
IN OUT Mode- Passes a constant value from the calling environment to the procedure and a
possibly different value from procedure to the calling environment using the same parameter.
A NUMBER := 3;
B NUMBER := 8;
T NUMBER;
BEGIN
T := X;
X := Y;
Y := T;
END MY_SWAP;
BEGIN
12 MY_SWAP(A,B);
15 END;
EXPERIMENT-7
Stored Procedures
A stored procedure or in simple a proc is a named PL/SQL block which performs one or more specific task.
SYNTAX:-
An exception is an error which disrupts the normal flow of program instructions. PL/SQL provides us the
exception block which raises the exception thus helping the programmer to find out the fault and resolve it.
There are two types of exceptions defined in PL/SQL
Syntax :-
WHEN exception THEN
Statement;
Consider the table STUDENT :-
ID NAME MARKS
1 AKSHAY 100
2 PRAVEEN 97
3 JESSIE 99
For example:-
DECLARE
temp varchar(20);
BEGIN
SELECT ID into temp from STUDENT where NAME='RAHUL';
exception
WHEN no_data_found THEN
dbms_output.put_line('ERROR:');
dbms_output.put_line('there is no name as');
dbms_output.put_line(' RAHUL in STUDENT table');
end;
OUTPUT:-
EXPERIMENT-8
TRIGGERS:-
Triggers are stored programs, which are automatically executed or fired when some events
occur.
Triggers can be defined on the table, view, schema, or database with which the event is associated.
CREATING TRIGGERS:-
[OF col_name]
ON table_name
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
6 Komal 22 MP 4500.00
FOR EXAMPLE:-
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff); END;
OUTPUT:-
Trigger created.
RESULT:-
Old salary:
New salary: 7500
Salary difference:
CURSORS:-
A cursor is a pointer to this context area. PL/SQL controls the context area through a cursor. A cursor holds
the rows (one or more) returned by a SQL statement. The set of rows the cursor holds is referred to as the
active set.
• Implicit cursors
• Explicit cursors
For example:-
DECLARE
total_rows number(2);
BEGIN
UPDATE CUSTOMERS
OUTPUT:-
6 Komal 22 MP 5000.00