Professional Documents
Culture Documents
DBMS Lab - CSE
DBMS Lab - CSE
Student Name
Register Number
THANGAVELU ENGINEERING COLLEGE
RAJIV GANDHI SALAI, KARAPAKKAM, CHENNAI-600097.
BONAFIDE CERTIFICATE
Certified that this is the Bonafide record of work done by the Mr/Ms
in the Degree course in the
Laboratory during the academic
year
NATURAL JOIN,
5 EQUI JOIN AND
OUTER JOIN
6 PROCEDURE AND
FUNCTIONS
10 XML DATABASE
CREATION AND
VALIDATION
11 CREATING
DOCUMENT,
COLUMNS & GRAPH
USING NOSQL
12 SIMPLE GUI
APPLICATION USING
DATABASE
13 CASE STUDY OF
BANK
MANAGEMENT
SYSTEM
EX.NO: 1 DDL and DML commands
AIM:
PROCEDURE:
Step 1: Start
Step 2: Create a database and use it for basic operations.
Step 3: Create a table with necessary attributes and execute DDL and DML
commands.
Step 4: Display the result.
Step 5: Stop
RESULT :
SQL> CREATE TABLE EMP (EMPNO NUMBER (4), ENAME VARCHAR2 (10),
DESIGNATIN VARCHAR2 (10), SALARY NUMBER (8,2));
Table created.
Name Type
Null?
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
EMPNO NUMBER(6)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
Table altered.
SQL> DESC EMP;
REMOVE / DROP :-
SQL> ALTER TABLE EMP DROP COLUMN DOJ;
Table altered.
Output :-
UNIQUE CONSTRAINTS :-
SQL> CREATE TABLE ShirtBrands(Id INTEGER, BrandName VARCHAR(40) UNIQUE, Size
VARCHAR(30));
SQL> INSERT INTO ShirtBrands(Id, BrandName, Size) VALUES(1, 'Pantaloons', 38), (2,
'Cantabil', 40);
SQL> INSERT INTO ShirtBrands(Id, BrandName, Size) VALUES(1, 'Raymond', 38), (2, 'Cant
abil', 40);
Output
CHECK CONSTRAINT :-
CHECK (expr)
SQL> CREATE TABLE Persons ( ID int NOT NULL,Name varchar(45) NOT NULL, Age in t CHECK
(Age>=18) );
SQL> INSERT INTO Persons(Id, Name, Age) VALUES (1,'Robert', 28), (2, 'Joseph', 35), (3,
'Peter', 40);
Output :-
In the below output, we can see that the first INSERT query executes successfully, but the
second statement fails and gives an error that says: CHECK constraint is violated for key Age.
PRIMARY KEY CONSTRAINT:-
CREATE TABLE Persons ( ID int NOT NULL PRIMARY KEY, Name varchar(45) NOT NULL,
Age int, City varchar(25));
Output
EX.NO:2 FOREIGN KEY AND REFERENTIAL INTEGRITY CONSTRAINT
AIM :-
To create a set of tables and add foreign key and referential integrity constraints.
PROCEDURE :-
Step 1:Start
Step 2:Create Table Department and Employee with necessary attributes.
Step 3:Add Foreign Key constraints in department table by altering it.
Step 4:Check referential integrity constraints by perform any operation.
Step 5: Stop .
RESULT :
Name NVARCHAR(50)
);
EMPLOYEES :-
);
ADDING THE FOREIGN KEY CONSTRANT :-
OUTPUT:
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
(`db2`.`employees`, CONSTRAINT `employees_ibfk_1` FOREIGN KEY (`DepartmentID`)
REFERENCES `department` (`Id`))
EX.NO: 3 QUERIES WITH WHERE CLAUSE AND AGGREATE
FUNCTIONS.
AIM :-
To write queries using WHERE clause and Aggreate Functions.
PROCEDURE
Step 1: Start .
Step 2: Write queries using different WHERE Clause .
RESULT :-
Thus the queries were executed successfully
3. MySQL WHERE Clause :-
Output:
Consider our database has a table named employees, having the following data. Now, we
are going to understand this function with various examples:
SQL> SELECT SUM(working_hours) AS "Total working hours" FROM employees;
Output:
Consider our database has a table named employees, having the following data. Now, we
are going to understand this function with various examples:
Output:
We will get the result as below:
EX.NO:4 SIMPLE JOIN AND SUB QUERIES
AIM:-
To execute and verify the SQL commands for Simple JOIN and sub queries.
PROCEDURE :-
STEP 1: Start
STEP 2: Create the table with its essential attributes.
STEP 3: Insert attribute values into the table
STEP 4: Execute Commands for JOIN operation and extract information from the table. STEP
5: Execute Commands for Sub queries operation.
STEP 6: Stop
RESULT :-
Thus the queries were executed successfully
4. MYSQL INNER JOIN (SIMPLE JOIN) :-
Consider two tables "officers" and "students", having the following data.
Output :-
MYSQL SUBQUERY :-
SQL>SELECT emp_name, city, income FROM employees WHERE emp_id IN (SELECT e mp_id
FROM employees);
Syntax:
SQL>CREATE TABLE balance ( id INT AUTO_INCREMENT PRIMARY KEY, account int, balance
FLOAT(10, 2) );
AIM :-
To write a query to perform natural join ,equi join and outer join.
PROCEDURE :-
Step 1: Start
Step 2:Create table with necessary attributes .
Step 3: Perform natural join,equi join and outer join operations with queries Step 4: Stop
RESULT :-
Thus the queries were executed successfully
5. NATURAL JOIN:
SQL> SELECT cust. customer_name, bal.balance FROM customer AS cust NATURAL JOI N
balance AS bal;
Syntax:
SELECT columns FROM table1 RIGHT [OUTER] JOIN table2 ON table1.column = table2.co
lumn;
Consider two tables "officers" and "students", having the following data.
SQL>SELECT officers.officer_name, officers.address, students.course_name,
students.studen t_name FROM officers RIGHT JOIN students ON officers.officer_id =
students.student_id;
Output
EQUI JOIN :-
SQL> SELECT cust. customer_name, bal.balance FROM customer AS cust, balance AS bal
WHERE cust.account = bal.account_num;
EX.NO:6 PROCEDURE AND FUNCTIONS
AIM : -
To write a SQL block to display the student name, marks whose average mark is above 60%.
ALGORITHM
STEP 1: Start
STEP 2: Create a table with table name stud_exam .
STEP 3: Insert the values into the table and Calculate total and average of each student .
STEP 4: Execute the procedure function the student who get above 60%.
FUNCTIONS :-
AIM :-
To write a Functional procedure to search an address from the given
database.
ALGORITHM:
STEP 1: Start
STEP 4: Frame the searching procedure for both positive and negative searching.
STEP 5: Execute the Function for both positive and negative result .
STEP 6: Stop
RESULT :-
Thus the queries were executed successfully .
6. SETTING SERVEROUTPUT ON:
PROGRAM:
Output
Procedure created.
Table created
1 row created.
1 row created.
1 row created.
SQL> select * from student;
priya 78 88 77 60 89
surya 99 77 69 81 99
suryapriya 100 90 97 89 91
SQL> declare
ave number(5,2);
tot number(3);
cursor c_mark is select*from student where mark1>=40 and mark2>=40 and
mark3>=40 and mark4>=40 and mark5>=40;
begin
dbms_output.put_line('regno name mark1 mark2 mark3 mark4 mark4 mark5 total average');
dbms_output.put_line(' ');
for student in c_mark
loop
tot:=student.mark1+student.mark2+student.mark3+student.mark4+student.mark5;
ave:=tot/5;
dbms_output.put_line(student.regno||rpad(student.name,15)
||rpad(student.mark1,6)||rpad(student.mark2,6)||rpad(student.mark3,6)
||rpad(student.mark4,6)||rpad(student.mark5,6)||rpad(tot,8)||rpad(ave,5));
end loop;
end;
/
OUTPUT
Table created.
1 row created.
1 row created.
3 begin
4 address:=findaddress(20312);
5 dbms_output.put_line(address);
6 end;
7 /
OUTPUT :-
Vijay,120/5D,bharathi street,NGO colony,629002
7.DCL COMMANDS :-
EX.NO:7 DCL AND TCL COMMANDS
AIM :-
PROCEDURE :-
Step 1: Start
Step 5: Stop.
RESULT :-
Thus the queries were executed successfully .
GRANT :-
[WITH GRANTOPTION];
MySQL> GRANT SELECT ON employee TO
REVOKE:-
REVOKE privilege_nameONobject_nameFROM{user_name|PUBLIC|rol_name}
MySQL> REVOKE SELECT ON employee FROM user1;
Command Successfully Completed
1 row created.
SQL> SELECT * FROM EMP;
ROLL BACK:-
COMMIT :-
SQL> COMMIT;
Commit complete.
8.PROGRAM
EX.NO:8 CREATION OF DATABASE TRIGGERS
AIM :-
To create database triggers using PL/SQL code
PROCEDURE :-
RESULT :-
Thus the queries were executed successfully .
SQL>create table poo(rno number(5),name varchar2(10));
Table created.
RNO NAME
------ ----------
1 kala
2 priya
SQL>create or replace trigger pool before insert on poo for each row
2 declare
3 rno poo.rno%type
4 cursor c is select rno from poo;
5 begin
6 open c;
7 loop;
8 fetch c into rno;
9 if:new.rno=rno then
10 raise_application_error(-20005,‟rno already exist‟);
11 end if;
12 exit when c%NOTFOUND
13 end loop;
14 close c;
15 end;
16 /
Trigger created.
OUTPUT
SQL>insert into poo values(01,‟kala‟)
Insert into poo values (01,‟kala‟)
*
ERROR at line1:
ORA-20005:rno already exist
ORA-06512:”SECONDCSEA.POOL”,line 9
ORA-04088:error during execution at trigger “SECONDCSEA.POOL”
EX.NO:9 VIEWS AND INDEX
AIM :-
To execute and verify the SQL commands for Views and Indexes.
PROCEDURE :-
STEP 1: Start
STEP 2: Create the table with its essential attributes.
STEP 3: Insert attribute values into the table.
RESULT :-
Thus views and indexes created successfull
9.CREATION OF TABLE:-
SQL> CREATE TABLE EMPLOYEE (
EMPLOYEE_NAMEVARCHAR2(10),
EMPLOYEE_NONUMBER(8),
DEPT_NAME VARCHAR2(10),
Table created.
TABLE DESCRIPTION:-
SQL> DESC EMPLOYEE;
DEPT_NO NUMBER(5)
DATE_OF_JOIN DATE
CREATION OF VIEW:-
SQL> CREATE VIEW EMPVIEW AS SELECT
EMPLOYEE_NAME,EMPLOYEE_NO,DEPT_NAME,DEPT_NO,DATE_OF_JOIN FROM
EMPLOYEE;
view created.
DESCRIPTION OF VIEW:-
SQL> DESC EMPVIEW;
NAME NULL? TYPE
EMPLOYEE_NAME VARCHAR2(10)
EMPLOYEE_NO NUMBER(8)
DEPT_NAME VARCHAR2(10)
DEPT_NO NUMBER(5)
DISPLAY VIEW:-
SQL> SELECT * FROM EMPVIEW;
EMPLOYEE_N EMPLOYEE_NO DEPT_NAME DEPT_NO
1 ROW CREATED.
SQL> SELECT * FROM EMPVIEW;
EMPLOYEE_N EMPLOYEE_NO DEPT_NAME DEPT_NO
RAVI 124 ECE 89
VIJAY 345 CSE 21
RAJ 98 IT 22
GIRI 100 CSE 67
SRI 120 CSE 67
SQL> SELECT * FROM EMPLOYEE;
DELETION OF VIEW:
DELETE STATEMENT
SQL> DELETE FROM EMPVIEW WHERE EMPLOYEE_NAME='SRI';
RAJ 98 IT 22
GIRI 100 CSE 67
UPDATE STATEMENT :
SQL> UPDATE EMPKAVIVIEW SET EMPLOYEE_NAME='KAVI' WHERE
EMPLOYEE_NAME='RAVI';
1 ROW UPDATED.
SQL> SELECT * FROM EMPKAVIVIEW;
EMPLOYEE_N EMPLOYEE_NO DEPT_NAME DEPT_NO
KAVI 124 ECE 89
VIJAY 345 CSE 21
RAJ 98 IT 22
GIRI 100 CSE 67
DROP A VIEW:
USE indexes;
Database changed
SQL>CREATE TABLE
(7, 'David', 'Williams', 'STU901', 90000), (8, 'Sarah', 'Johnson', 'VWX234', 95000),
(9, 'James', 'Brown', 'YZA567', 100000);
Query OK, 9 rows affected (0.010 sec)
Records: 9 Duplicates: 0 Warnings: 0
+++++++++++
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows |
filtered
| 1 | SIMPLE | employees | NULL | ref | salary | salary | 5 | const | 1 | 100.00 |
+++++++++++
1 row in set, 1 warning (0.00 sec)
EX.NO:10 XML DATABASE CREATION AND VALIDATION
AIM :-
To create a XML database file and Validate the Schema
ALGORITHM :-
Step 1 : Start
Step 6 : Stop
RESULT :-
Thus the queries were executed successfully .
10. CREATE TABLE:
CREATE TABLE person (
person_id INT NOT NULL PRIMARY KEY,
<personperson_id="2"fname="Sajon"lname="Rondela"/>
<personperson_id="3"><fname>Likame</fname><lname>Örrtmons</lname></person>
<personperson_id="4"><fname>Slar</fname><lname>Manlanth</lname></person>
<person><fieldname="person_id">5</field><fieldname="fname">Stoma</field>
<fieldname="lname">Milu</field></person>
<person><fieldname="person_id">6</field><fieldname="fname">Nirtam</field>
<fieldname="lname">Sklöd</field></person>
<personperson_id="7"><fname>Sungam</fname><lname>Dulbåd</lname></person>
<personperson_id="8"fname="Sraref"lname="Encmelt"/>
</list>
LOAD XML LOCAL INFILE 'c:/db/person.xml' //this is ths location of the xml data file
INTO TABLE person
ROWS IDENTIFIED BY '<person>';
OUTPUT :
AIM :-
To Create Document,column and Graph using NOSQL Tools.
ALGORITHM :-
Step 1: Start
RESULT :-
Thus the queries were executed successfully.
11.Create database in MongoDB:
>Install Mongodb shell
>Connect with localhost
>Connection string:
mongodb://localhost:27017
OUTPUT :
OUTPUT:
Create document in MongoDB
mydbnew>db.details.insertOne({"website":"mywebsite"})
OUTPUT :
SQL>Db.details.find()
OUTPUT :
EX.NO:12 SIMPLE GUI APPLICATION USING DATABASE
AIM :-
To develop a program in python to implement the GUI based application
ALGORITHM :
Step 1: Start
Step 2: Import necessary files to perform database operations
Step 3: Design Login Screen with User Name and Password fields.
RESULT :-
Thus the queries were executed successfully .
12.CREATING CHART USING SAMPLE DATA:-
PROCEDURE:
Step 1: Log into MongoDB Atlas.
To access the MongoDB Charts application, you must be logged into Atlas
Step 2: Select your desired Atlas project, or create a new project.
If you have an Atlas Project with clusters containing data you wish to visualize,
Step 3: Select the project from the Context dropdown in the left navigation pane.
Step 4: Create an Atlas cluster. The MongoDB Charts application makes it easy to connect
OUTPUT :
PROGRAM :-
import tkinter as tk
import mysql.connector
from tkinter import *
def submitact():
user = Username.get()
passw = password.get()
print(f"The name entered by you is {user} {passw}")
logintodb(user, passw)
def logintodb(user, passw):
# If password is enetered by the
# user
if passw:
db = mysql.connector.connect(host ="localhost", user = user,
password = passw, db ="College")
cursor = db.cursor()
# If no password is enetered by the
# user
else:
db = mysql.connector.connect(host ="localhost", user = user,
db ="College")
cursor = db.cursor()
# A Table in the database
savequery = "select * from STUDENT"
try:
cursor.execute(savequery)
myresult = cursor.fetchall()
# Printing the result of the
# query
for x in myresult:
print(x)
print("Query Executed successfully")
except:
db.rollback()
print("Error occurred")
root = tk.Tk()
root.geometry("300x300")
root.title("DBMS Login Page")
root.mainloop()
OUTPUT :
13. ER diagram of Bank Management System :-
• Bank Entity : Attributes of Bank Entity are Bank Name, Code and Address.
This bank ER diagram illustrates key information about bank, including entities such as
branches, customers, accounts, and loans. It allows us to understand the relationships
between entities.
Atomicity :-
This property ensures that either all the operations of a transaction reflect in database or
none.
The logic here is simple, transaction is a single unit, it can’t execute partially. Either it
executes completely or it doesn’t, there shouldn’t be a partial execution.
Let’s say first operation passed successfully while second failed, in this case A’s balance
would be 300$ while B would be having 700$ instead of 800$. This is unacceptable in a
banking system. Either the transaction should fail without executing any of the operation or
it should process both the operations. The Atomicity property ensures that.
There are two key operations are involved in a transaction to maintain the atomicity of the
transaction.
Abort: If there is a failure in the transaction, abort the execution and rollback the changes
made by the transaction. Commit: If transaction executes successfully, commit the changes
to the database.
Consistency :-
Database must be in consistent state before and after the execution of the transaction. This
ensures that there are no errors in the database at any point of time. Application
programmer is responsible for maintaining the consistency of the database.
Example:
A transferring 1000 dollars to B. A’s initial balance is 2000 and B’s initial balance is 5000.
Before the transaction:
Total of A+B = 2000 + 5000 = 7000$
The data is consitendct before and after the execution of the transaction so this example
maintains the consistency property of the database.
Isolation:-
A transaction shouldn’t interfere with the execution of another transaction. To preserve the
consistency of database, the execution of transaction should take place in isolation (that
means no other transaction should run concurrently when there is a transaction already
running).
For example account A is having a balance of 400$ and it is transferring 100$ to account B &
C both. So we have two transactions here. Let’s say these transactions run concurrently and
both the transactions read 400$ balance, in that case the final balance of A would be 300$
instead of 200$. This is wrong.
If the transaction were to run in isolation then the second transaction would have read the
correct balance 300$ (before debiting 100$) once the first transaction went successful.
Durability:-
Once a transaction completes successfully, the changes it has made into the database
should be permanent even if there is a system failure. The recovery-management
component of database systems ensures the durability of transaction.
STORED PROCEDURE :-
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT
statements.
SET NOCOUNT ON;
WHERE AccountID = @AccountID AND [Date] BETWEEN @StartDate AND @EndDate END
EXEC bank.GetTransactions
@AccountID = 100000,
@StartDate = '4/1/2007',
@EndDate = '4/30/2007'