Dbms Manual 2021
Dbms Manual 2021
TECHNOLOGY
NAMAKKAL – 637 207
NAME : …………………………………………........................
REG. NO : …………………………………………........................
YEAR/SEM : …………………………………………........................
BRANCH : …………………………………………........................
PGP COLLEGE OF ENGINEERING AND
TECHNOLOGY
NAMAKKAL – 637 207
CERTIFICATE
This is to certify that bonafied work done by Mr/Ms ……………………….
(EvenSemester)
To write SQL Statements to create table, add constraints, insert rows, update and delete
rows using Data Definition Language (DDL) and Data Manipulation Language(DML)
RESULT:
Thus the SQL Statements to create table, add constraints, insert rows, update and delete
rows using Data Definition Language (DDL) and Data Manipulation Language(DML)
commands was executed successfully
CREATE A SET OF TABLES, ADD FOREIGN KEY CONSTRAINTS AND
INCORPORATE REFERENTIAL INTEGRITY.
AIM:
To create tables and Execute Data Definition Commands, Data Manipulation Commands to add
foreign key constraints and incorporate referential integrity Statements.
We want to create a set of tables to track orders and their associated products and customers.
We'll create two tables: customers and orders
PROGRAM:
Here's the customers table:
CREATE TABLE customers (
customer_id integer PRIMARY KEY,
first_name varchar(50) NOT NULL,
last_name varchar(50) NOT NULL,
email varchar(50) UNIQUE,
phone_number varchar(20) NOT NULL
);
Here's the orders table:
CREATE TABLE orders (
order_id integer PRIMARY KEY,
order_date date NOT NULL,
customer_id integer NOT NULL,
amount numeric(10, 2) NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
);
OUTPUT:
INSERT INTO customers (customer_id, first_name, last_name, email, phone_number)
VALUES (1, 'John', 'Doe', '[email protected]', '123-456-7890'),
(2, 'Jane', 'Doe', '[email protected]', '987-654-3210'),
(3, 'Bob', 'Smith', '[email protected]', '555-555-1212');
RESULT:
Thus the Tables are created and added with foreign key constraints and incorporates
referential integrity was successfully executed.
QUERY THE DATABLES USING DIFFERENT ‘WHERE’CLAUSE CONDITIONS AND
ALSO IMPLEMENTS AGGREGATE FUNCTIONS
AIM:
Two write a sql program to query the databases using different „where‟ clause conditions
and also implements aggregate functions
LIST OF AGGREGATE FUNCTIONS:
AVG
COUNT
MAX
MIN
STDDEV
SUM
EXAMPLE:
1. AVG:
SQL>select avg(cost) from stores;
2. COUNT:
SQL> select count (client_name) “NO. Of clients:” from client_master;
3. MAX:
SQL> select max (cost) from stores;
4. MIN:
SQL>select min (cost) from stores;
5. STDDEV:
SQL>select stddev(cost) from stores;
6. SUM:
SQL> select sum (cost) from stores;
SQL> create table emp(eid number, ename varchar2(10),age number, salary number);
Table created.
SQL> desc emp;
7
(iii) Find the Maximum age from employee table.
SQL> select max(age) from emp;
MAX(AGE)
44
(iv) Find the Minimum age from employee table.
SQL> select min(age) from emp;
MIN(AGE)
22
(v) Display the Sum of age employee table.
SQL> select sum(age) from emp;
SUM(AGE)
220
22 29 27 29
(ix) Find grouped salaries of employees.(group by clause)
SQL> select salary from emp group by salary;
SALARY
rohan 6000
alex 7000
shane 8000
abhi 8000
tiger 8000
anu 9000
scott 10000
7 rows selected.
ENAME SALARY
scott 10000
anu 9000
Result:
Thus the SQL program to query the database using different „where‟ clause conditions and using
different aggregate functions was created and executed successfully.
QUERY THE DATABASE TABLES AND EXPLORE SUB QUERIES AND SIMPLE
JOIN OPERATIONS.
AIM:
To write Sql queries to query the database tables and explore sub queries and simple join
operations
SUBQUERIES
IN:
SYNTAX:
select * from table1 where in(select attribute from table2);
DESCRIPTION:
The IN connective test for set membership where set is a collection of values produced by a
select clause.
NOT IN:
SYNTAX:
select * from table1 where notin(select attribute from table2);
DESCRIPTION:
The NOTIN connective tests for set membership where set is a collection of values produced by
aselect clause.
ANY/SOME:
SYNTAX:
select * from table1 where attribute > some/any(select attribute from table name(condition));
DESCRIPTION:
The some/any keyword require that atleast one pair compiler, if denotes that the select condition
TRUE if the comparision is TRUE for atlest one of the values that is required.
ALL:
SYNTAX:
select * from table1 where attribute > ALL(select attribute from table where(condition));
DESCRIPTION:
The ALL keyword require that all values from the subselect comply with the comparison
separator.The ALL keyword specifies that the select condition TRUE if the comparision is
TRUEfor everyvalues that the sub query returns.
Examples:
SQL> select * from emp where eno in(select eno from sal);
ENO ENAME DEPT
1 ram cse
2 rajan Ece
4 hari Cse
SQL> select * from emp where eno not in(select eno from sal where dept='cse');
SQL> select * from sal where salary>any(select salary from sal where dept='cse');
ENODEPT SALARY
4eee 8000
5cse 10000
SQL> select * from sal where salary>some(select salary from sal where dept='cse');
SQL> select * from sal where salary>all(select salary from sal where dept='cse');no rows
selected
SQL> select * from sal where salary<all(select salary from sal where dept='cse');ENO DEPT
SALARY
2 ece 1000
SYNTAX:
Select attribute 1, 2, …., from table name1,table name2 where t1=t2 attribute;
DESCRIPITION:
A equi join is a SQL join.
NON EQUI JOIN:
SYNTAX:
Select attribute1, attribute2. ............................. from table 1, table2 where table1 attribute <>
table2 attribute
DESCRIPTION:
A non equi join is a SQL join whose condition is established using all comparison operatorexcept
the equal (=) operations like <=,>=,<,>
SELF JOIN:
Select distinct t1 attributes _name1 t1 attributes name from table name t1 where attribute
name1=t2.attribute_name and t1.attribute_name in<> t2.attribute_name in
DESCRIPTION:
A self join is a type of SQL join which is used to join a table to itself particularly when the used
table has a foreign key the references its own primary key. It compares values with the columnof
a single table.
Example:
SQL> create table eem(eno number(3),ename varchar2(10),dept varchar2(5));Table created.
1 30000 cse
2 50000 it
5 60000 cse
Equi join:
em, sal where eem.eno=sal.eno;
SQL> select eem.eno,ename,sal from e
ENO ENAME SAL
1 Aaa 30000
2 Bbb 50000
Non equi join:
SQL> select eem.eno,ename,sal from eem,sal where eem.eno<>sal.eno;
6 rows selected.
7 rows selected.
RESULT:
Thus the SQL queries to query the database tables and explore sub queries and simple join
operations was written and executed successfully.
QUERY THE DATABASE TABLES AND EXPLORE NATURAL, EQUI AND OUTER
JOINS
NATURAL JOIN:
SYNTAX:
Select attribute1,2……,
From table name1,table name2,
NATURAL JOIN attribute1,attribute2;
DESCRIPITION:
A natural join is a SQL join,which simply joins two table‟s attributes
EQUIJOIN:
SYNTAX:
Select attribute 1, 2, …., from table name1,table name2 where t1=t2 attribute;
DESCRIPITION:
A equi join is a SQL join.
NON EQUI JOIN:
SYNTAX:
Select attribute1, attribute2. ............................. from table 1, table2 where table1 attribute <>
table2 attribute
DESCRIPITION:
A non equi join is a SQL join whose condition is established using all comparison operatorexcept
the equal (=) operations like <=,>=,<,>
INNER JOIN:
SYNTAX:
Select attribute1, attribute 2. ............................. from table 1 left inner join table2 on condition.
DESCRIPTION:
Inner join returns the matching rows from the tables that are being joined.
OUTER JOIN:
1 aaa Cse
2 bbb It
3 ccc Cse
SQL> select * from sal;
1 30000 cse
2 50000 it
5 60000 cse
Equi join:
em, sal where eem.eno=sal.eno;
SQL> select eem.eno,ename,sal from e
ENO ENAME SAL
1 Aaa 30000
2 Bbb 50000
Non equi join:
SQL> select eem.eno,ename,sal from eem,sal where eem.eno<>sal.eno;
8 rows selected.
7 rows selected.
Inner join:
RESULT:
Thus the simple queries for Natural join,Equi join and Outer join operations are used
for retrieving information by joining tables have been verified and executed successfully.
WRITE USER DEFINED FUNCTIONS AND STORED PROCEDURES IN SQL
SQL PROCEDURES:
An Oracle stored procedure is a program stored in an Oracle database. The following are the
advantages of using procedures. Better performance: Oracle stored procedures load once into the
shared pool and remain there unless they become paged out. Subsequent executions of the Oracle
stored procedure are far faster than executions of external code.
Coupling of data with behaviour: DBAs can use naming conventions to couple relational tables
with the behaviors associated with a table by using Oracle stored procedures as "methods". If all
behaviors associated with the employee table are prefixed with the table name--employee.hire,
employee.give_raise, for example--the data dictionary can be queries to list all behaviors
associated with a table (select * from dba_objects where owner = 'EMPLOYEE'), and it's easy to
identify and reuse code via stored procedures.
Isolation of code: Since all SQL is moved out of the external programs and into the Oracle stored
procedures, the application programs become nothing more than calls to Oracle stored
procedures. As such, it becomes very simple to swap out one database and swap in another one.
EXECUTION:
SETTING SERVEROUTPUT ON:
SQL> SET SERVEROUTPUT ON
1. PROCEDURE USING POSITIONAL PARAMETERS
SQL> SET SERVEROUTPUT ON
SQL> CREATE OR REPLACE PROCEDURE PROC1 AS2 BEGIN
3 DBMS_OUTPUT.PUT_LINE ('Hello from procedure...');
4 END;
5/
Hello from procedure...
PL/SQL procedure successfully completed.
99
INPUT:
Enter value for a: 8old 8: a:=&a;
new 8: a:=8;
Enter value for b: 16old 9: b:=&b;
new 9: b:=16;
OUTPUT:
Procedure created.
SQL> set serveroutput on;
SQL> execute pro;
GCD is8
PL/SQL procedure successfully completed.
SYNTAX:
<declaration_section>BEGIN
<executable_section>EXCEPTION
<exception_section>END;
SQL> DECLARE
<VARIABLE DECLARATION>;BEGIN
<EXECUTABLE STATEMENT >;END;
OUTPUT:
C is maximum
PL/SQL procedure successfully completed.
INPUT:
Enter value for number: 2old 5: n:=&number;
new 5: n:=2;
OUTPUT:
entered number is less than 5
PL/SQL procedure successfully completed.
INPUT:
Enter value for a: 21old 7: a:=&a;
new 7: a:=21;
Enter value for b: 12old 8: b:=&b;
new 8: b:=12;
Enter value for b: 45old 9: c:=&b;
new 9: c:=45;
OUTPUT:
C is maximum
PL/SQL procedure successfully completed.
begin endvalue:=&endvalue;n:=1;
for n in 1..endvalueloop
if mod(n,2)=1 then sum1:=sum1+n;end if;
end loop;
dbms_output.put_line('sum ='||sum1);end;
/
INPUT:
Enter value for endvalue: 4 old 6: endvalue:=&endvalue;
new 6: endvalue:=4;
OUTPUT:
sum =4
PL/SQL procedure successfully completed.
OUTPUT:
sum of odd no. bt 1 and4is4
PL/SQL procedure successfully completed.
PL/SQL FUNCTION:
PROCEDURE
1. Start.
2. Create the table with essential attributes.
3. Initialize the Function to carry out the searching procedure.
4. Frame the searching procedure for both positive and negative searching.
5.Execute the Function for both positive and negative result.
6.Stop.
EXECUTION:
SETTING SERVEROUTPUT ON:
SQL> SET SERVEROUTPUT ON
1. IMPLEMENTATION OF FACTORIAL USING FUNCTION
SQL>create function fnfact (n number)return number is
b number;begin b:=1;
for i in 1..nloop b:=b*i; end loop; return b; end;
/ SQL>Declare
n number:=&n;y number; begin y:=fnfact(n);
dbms_output.put_line(y);end;
/
Function created.
Enter value for n: 5
old 2: n number:=&n;new 2: n number:=5; 120
PL/SQL procedure successfully completed.
2. PROGRAM
SQL> create table phonebook (phone_no number (6) primary key,username varchar2(30),doorno
varchar2(10),street varchar2(30),place varchar2(30),pincode char(6));
Table created.
OUTPUT 1:
Vijay,120/5D,bharathi street,NGO colony,629002PL/SQL procedure successfully
completed.SQL> declare
2 address varchar2(100);
3 begin
4 address:=findaddress(23556);
5 dbms_output.put_line(address);
6 end;
7/
OUTPUT2:
Address not found
PL/SQL procedure successfully completed.
RESULT:
Thus SQL Procedures and user defined functions were written and executed successfully.
EXECUTE COMPLEX TRANSACTIONS AND REALIZE DCL AND TCL
COMMANDS
TCL COMMANDS:
COMMANDS OVERVIEW
SAVEPOINT - identify a point in a transaction to which you can later roll back
SAVEPOINT
Syntax: savepoint username;Input:
SQL> savepoint emp;
Output:
savepoint created;
COMMIT
Syntax: commit;
Input:
SQL> commit;
Output:
Commit complete.
ROLLBACK
Syntax:
rollback to savepoint_text_identifier;
Input:
SQL> rollback to emp;
Output:
Rollback complete.
DCL (Data Control Language)
DCL COMMANDS:
DCL provides users with privilege commands the owner of database objects (tables), has the soul
authority ollas them. The owner (data base administrators) can allow other data base users to
access the objects as per their requirement
1. GRANT: The GRANT command allows granting various privileges to other users and
allowing them to perform operations within their privileges.
Example, if a uses is granted as „SELECT‟ privilege then he/she can only view data but cannot
perform any other DML operations on the data base object GRANTED privileges can also be
withdrawn by the DBA at any time
Syntax:
Example:
2. REVOKE: To with draw the privileges that has been GRANTED to a uses, we use the
REVOKE command
Syntax:
SQL>REVOKE PRIVILEGES_NAME ON object-name FROM user_name;
Example:
OUTPUT:
Query OK, 0 rows affected
RESULT:
Thus the complex transactions using DCL and TCL commands are executed successfully.
WRITE SQL TRIGGERS FOR INSERT, DELETE, AND UPDATE OPERATIONS IN A
DATABASE TABLE
AIM
To write a PL/SQL query to create a trigger.
TRIGGERS:
Trigger created.
Trigger created.
S Q1L
00>1upda te ksatundient set regcnsoe=1005 wher e7r0egno=1002;1 row updated.
Trigger created.
3 begin
4 address:=find address(25301);
5 dbms_output.put_line (address);
6 end;
RESULT:
Thus the PL/SQL block for trigger with different controls are verified and executed.
CREATE VIEW AND INDEX FOR DATABASE TABLES WITH A LARGE NUMBER
OF RECORDS
AIM:
To create a view and index for database tables with a large number of records
VIEW:A view is simply the representation of a SQL statement that is stored in memory so
that itcan easily be re-used. It‟s also referred as logical table.
SQL COMMANDS
Creating views:
Syntax:
Create view<view name>;
Description:
This command is used to create view by combining two tables.
Viewing single row of table:
Syntax:
Create view<view name> as select from <table name>;
Description:
This command is used to view a single row from a particular table.
Viewing all columns from a single table:
Syntax:
Create view<view name> as select * from <table name>;
Description :
This is used to create view which displays all columns from a single table.
View specified column from a single table:
Syntax:
Create view<view table name> as select column1,column2 from <tablename>;
Description:
This command is used to create view which displays on a specified from a single table.
View specified column from a muliple table:
Syntax:
Create view<view table name> as select column1,column2,….columnn where „condition‟;
Description:
This is used to create view to display specified columns from multiple tables.
View all column from a muliple table:
Syntax:
Create view<view table name> as select * from <table name> where „condition‟;
Description:
This is used to create view which displays all the columns of a table.
SYNTAX:
CREATE OR REPLACE VIEW <view name > AS < select statement >
INDEXES:
Database system uses indexes to avoid the need for large-table, full-table scans and disk sorts,
which are required when the SQL optimizer cannot find an efficient way to service the SQL
query.
RESULT:
Thus,the view and index for database tables with a large number of records was created
successfully.
CREATE AN XML DATABASE AND VALIDATE IT USING XML SCHEMA
AIM:
To create an XML database and validate it using XML schema.
XML(Extensible Markup Language)-used to describe data and supports information exchange
between computer systems such as websites,databases and third party applications
XML FILE CRAEATION:
CREATE AN XML FILE CALLED "EMPLOYEES.XML
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee>
<id>001</id>
<name>John Doe</name>
<email>[email protected]</email>
<department>Marketing</department>
</employee>
<employee>
<id>002</id>
<name>Jane Smith</name>
<email>[email protected]</email>
<department>Finance</department>
</employee>
<employee>
<id>003</id>
<name>Bob Johnson</name>
<email>[email protected]</email>
<department>IT</department>
</employee>
</employees>
CREATE AN XML SCHEMA CALLED "EMPLOYEES.XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employees">
<xs:complexType>
<xs:sequence>
<xs:element name="employee" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
<xs:element name="department" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML FILE VALIDATION USING XML SCHEMA
XML SCHEMA-describes the structure of the XML document.It is also known as XML
Schema Definition(XSD)
LET'S VALIDATE OUR XML FILE USING THE XML SCHEMA:
<?php
$xml = new DOMDocument();
$xml->load('employees.xml');
if ($xml->schemaValidate('employees.xsd')) {
echo "Validation successful!";
} else {
echo "Validation failed!";
}?>
Output:
Validation successful!
RESULT:
Thus the XML database was created and it is validated by XML schema successfully.
CREATE DOCUMENT, COLUMN AND GRAPH BASED DATA USING NOSQL
DATABASE TOOLS
AIM:
To create a document,column and graph based data using NOSQL Database tools
Document-Based Data
// Insert a blog post into the blogposts collection
db.blogposts.insertOne({
title: "My First Blog Post",
content: "This is my very first blog post!",
author: "John Doe",
publishDate: ISODate("2023-04-07T00:00:00Z")
});
Column based data
// Create the userprofiles column family
db.createCollection("userprofiles", {
capped: false,
autoIndexId: true,
max: 10000
});
// Retrieve all users and their connections from the socialnetwork graph
db.socialnetwork.find();
Result:
Thus ,the document,column and graph based database application was successfully created.
CASE STUDY USING ANY OF THE REAL LIFE DATABASE APPLICATIONS
FROM THE FOLLOWING LIST
AIM:
To case study using the real life database applications
stock.
Orders: contains information about each order, including the order date, the products ordered,
information.
Output:
Products Table
+ + + + +
| ID | Name | Price | Stock |
+ + + + +
| 1 | Apples | 2.50 | 100 |
| 2 | Bananas | 1.50 | 50 |
| 3 | Oranges | 2.00 | 75 |
| 4 | Carrots | 1.00 | 200 |
| 5 | Broccoli | 1.50 | 100 |
| 6 | Milk | 3.50 | 50 |
| 7 | Eggs | 2.00 | 100 |
| 8 | Chicken | 6.00 | 25 |
| 9 | Ground Beef | 5.00 | 30 |
| 10 | Sliced Bread | 2.50 | 50 |
+ + + + +
Orders Table
+ + + + +
| ID | CustomerID | ProductID | Quantity |
+ + + + +
|1 |1 |1 |5 |
|2 |1 |2 | 10 |
|3 |2 | 3, 4, 5 | 5, 10, 3 |
|4 |3 | 6, 7, 8, 9 | 2, 5, 1, 3 |
|5 |2 | 1, 2, 3, 4, 5, 6, 7, 8, 9 | 1, 1, 1, 1, 1, 1, 1, 1, 1 |
+ + + + +
Customers Table
+ + + + +
| ID | FirstName | LastName | ContactNumber |
+ + + + +
| 1 | John | Smith | 555-1234 |
| 2 | Jane | Doe | 555-5678 |
| 3 | Bob | Johnson | 555-9999 |
+ + + + +
II. Society Financial Management
XYZ Society is a housing society that manages the financial transactions of its members. The
society wants to implement a database system to manage its financial data efficiently. The
database will have the following tables
Members: contains information about each member, including their name, address, and contact
information.
Transactions: contains information about each financial transaction, including the transaction
date, amount, and type of transaction (e.g., maintenance fee, electricity bill, water bill, etc.).
Invoices: contains information about each invoice generated, including the invoice date,
agreement ID, the property ID it belongs to, the tenant ID, rental start and end dates, and rental
amount.
Properties Table
+----+---------------+---------------+---------------------+-------------------+
| ID | PropertyName | Location | PropertyType | PropertyStatus |
+----+---------------+---------------+---------------------+-------------------+
| 1 | Phoenix Mall | Mumbai | Shopping Mall | Open |
| 2 | Westend Mall | Pune | Shopping Complex | Closed |
| 3 | Grand Central | Delhi | Shopping Center | Under Renovation |
+----+---------------+---------------+---------------------+-------------------+
Tenants Table
+ + + + + +
| ID | TenantName | ContactNumber | ContactEmail | TenantType |
+ + + + + +
| 1 | Brand Factory | 9123456789 | [email protected] | Retail Clothing |
| 2 | McDonald's | 9987654321 | [email protected] | Food & Beverages |
| 3 | PVR Cinemas | 9123456780 | [email protected] | Entertainment |
+ + + + + +
RentalAgreements Table
+ + + + + + +
| ID | PropertyID | TenantID| Rent | StartDate | EndDate |
+ + + + + + +
|1 |1 |1 | 100000 | 2022-01-01 | 2023-01-01 |
|2 |1 |2 | 50000 | 2022-06-01 | 2023-06-01 |
|3 |2 |3 | 75000 | 2022-04-01 | 2023-04-01 |
+ + + + + + +
V. Star Small and Medium Banking and Finance
Star Small and Medium Banking and Finance is a banking and finance system designed to
manage the banking transactions of small and medium-sized businesses. The system has a
database that stores all the necessary information related to customers, accounts, and
transactions. The database will have the following tables
Customers: contains information about each customer, including their name, contact
account number it belongs to, transaction type, amount, and transaction date.
Customers Table
+ + + + +
| ID | CustomerName | ContactNumber | ContactEmail |
+ + + + +
| 1 | ABC Corporation| 9123456789 | [email protected] |
| 2 | XYZ Company | 9987654321 | [email protected] |
| 3 | PQR Limited | 9123456780 | [email protected] |
+ + + + +
Accounts Table
+ + + + + +
| ID | AccountNumber | CustID | AccountType | Balance |
+ + + + + +
| 1 | 100001 |1 | Current | 500000 |
RESULT:
Thus the case study using real life database application was completed successfully.