Cp4152 Database Practice Lab Manual R 2021
Cp4152 Database Practice Lab Manual R 2021
No :1
NOSQL Commands using MONGODB,CASSANDRA,HIVE,ORIENTDB
Date:
A) MONGODB
AIM:
To write NOSQL QUERIES to understand the concept of Open Source Database Management
System such as MongoDB.
PROCEDURE:
Step 1: Start the MongoDB Server (Mongos).
Step 2: Start the Client (Mongod)
Step 3: Perform the MongoDB Curd Operations such as (Create, Update,Read,Delete).
Syntax: To Create/Select a Collection: USE DATABASE_NAME.
Syntax: To Insert: DB.COLLECTION_NAME.INSERT(DOCUMENT).
Syntax: To Update: DB.COLLECTION_NAME.UPDATE(<FILTER>, <UPDATE> )
Syntax: To Display/Search: DB.COLLECTION_NAME.FIND ()
Syntax: To Delete: DB.COLLECTION_NAME.REMOVE (DELETION_CRITERIA)
Step 4: Perform the MongoDB Indexing Operations is used to improve the speed of search operations
instead of searching the whole document.
Syntax: To Create Index: DB.COLLECTION_NAME.CREATE_INDEX({FIELD : VALUE })
Step 5: Perform the MongoDB Sharding Operations
Syntax TO SHARDING DB.COLLECTION_NAME.GETSHARDDISTRIBUTION()
Step 6: Perform the Deployment Operation in MongoDB
Syntax To Deployment: RS.INITIATE() to connect the other Replica machines.
QUERIES WITH EXECUTION:
Create :
Insert:
> db.vysya.insert(
... {
... course: "ADT",
... details: {
... lab: "6 months",
... Trainer: "Natarajan"
... },
... category: "Programming language"
... }
... )
WriteResult({ "nInserted" : 1 })
Update :
Find:
> db.vysya.find()
Delete :
> db.vysya.remove({ })
WriteResult({ "nRemoved" : 1 })
Indexing:
> db.vysya.createIndex({regNo : 1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
Sharding:
Deployment:
> rs.status();
{
"ok" : 0,
"errmsg" : "not running with --replSet",
"code" : 76,
"codeName" : "NoReplicationEnabled"
}
rs.initiate( {
... _id : "rs0",
... members: [
... { _id: 0, host: "mongodb0:27017" },
... { _id: 1, host: "mongodb1:27017" },
... { _id: 2, host: "mongodb2:27017" }
... ]
... })
>exit
>mongo mongodb://mongodb0:27017,mongodb1:27017,mongodb2:27017
>mongo 'mongodb://mongodb0,mongodb1,mongodb2/?replicaSet=rs0'
>rs.printReplicationInfo()
RESULT:
AIM:
To write NOSQL QUERIES to understand the concept of Open Source Database Management System
such as CASSANDRA.
PROCEDURE:
Create KeySpace:
cqlsh>use vysya;
Create Table
cqlsh:vysya>CREATE TABLE VVT(Id int PRIMARY KEY,name text,city text,fees varint);
Alter Table
cqlsh:vysya>ALTER TABLE VVT ADD email text;
View Table
cqlsh:vysya> select * from vvt;
Truncate Data
cqlsh:vysya> TRUNCATE VVT;
cqlsh:vysya> INSERT INTO VVT(Id, fees,name,city)VALUES(1,5000, 'Natarajan S','Namakkal');
cqlsh:vysya> select * from vvt;
AIM:
To write NOSQL QUERIES to understand the concept of Open Source Database Management System
such as HIVE.
PROCEDURE:
Step 1: Start the Hadoop Cluster from sbin Folder (Run start-dfs, start-yarn).
Step 2: Start the derby node using the command(StartNetworkServer -h 0.0.0.0)
Step 3: Then Start the Hive.(Hive command)
Step 4: Hive data types are categorized in numeric types, string types, misc types, and complex types.
Step 5: Syntax to Create a Database.first we have to check weather the DB is Already Exist or Not for that
show database;
Step 6: if Not Exist Create Database Database_Name;
Step 7: Perform Some Table Operations in Hive such as Create, Alter, Drop Table.
Step 8: Finally Partitioning the Hive.
QUERIES WITH EXECUTION:
Static Partitioning
Course string
Time taken: 1.054 seconds, Fetched: 10 row(s)
Dynamic Partitioning
hive> use show;
hive> set hive.exec.dynamic.partition=true;
hive> set hive.exec.dynamic.partition.mode=nonstrict;
hive> create table stud_demo(id int, name string, age int, institute string, course string)
OK
hive> load data local inpath 'd:/student_details' into table stud_demo;
Loading data to table show.stud_demo
Table show.stud_demo status: [numFiles=1, totalSize=152]
hive> insert into student_part partition(course) select id, name, age, institute, course from stud_demo;
-hive >insert student_part > partition(cinto ourse) select id, name, age, institute, course > from studdemo:
e.ry ID = codegyani_20190801062015_d7649030 -£370 -47a2 -a86d -ff402d3e7de7 otal jobs = 3 nothing
Job 1 out of 3 'umber of reduce tasks is set to 0 since there's no reduce operator •tarting Job = job_
1555046592674 0017, Tracking 171,1 = http://ubuntu64server:808: proxy/application1555046592674 0017/
ill Command = /heie/codegyani/hadoop-2.7.1//bin/hadoop job -kil job_15550465• '6740017 Jadoop job
information for Stage-1: number of mappers: 1: number of reducers: 0 '019-08-01 06:21:50,531 Stage-1 map
= 0%, reduce = 0% '019-08-01 06:22:51,598 Stage-1 map = 0%, reduce = 0% '019-08-01 06:23:06,456
Stage-1 map = 100%, reduce = 0%, Cumulative CCU 10.03 MapReduce To cumulative CCU time: 10
seconds 30 cosec nded Job = job1555046592674 0017 Cage-4 is selected by condition resolver. Cage-3 is
filtered out by condition resolver. Cage-5 is filtered out by condition resolver. oving data to:
hdfs://192.168.56.123:8020/user/hive/warehouse/show.db/student. • • . . . • •
RESULT:
AIM:
To write NOSQL QUERIES to understand the concept of Open Source Database Management
System such as OrientDB Graph.
PROCEDURE:
Step3: Choose the Database and enter the username and password for the OrientDB Server.
Step5: Create a Class, Node, Edges and Insert the fields Finally connect the graph display the output.
AIM:
To write a Query for MySQL Database Creation, Table Creation and Some other Queries Execution.
PROCEDURE:
Step 1: Start the MYSQL Server.
Step 2: Open MySQL Command Line Client
Step 3: Write and Execute Queries for Database Creation
Step 4: Write and Execute Queries for Table Creation
Step 5: Verify with Show Command to Check whether the Database and Table Created.
Step 6: Do some other Queries Execution like insert, update, delete a record.
Step7: Close MySQL Command Line Client
Step 8:Stop the MYSQL Server.
QUERIES WITH EXECUTION:
Create DataBase:
Select DataBase
Drop Database:
Create Table:
mysql> CREATE TABLE MCA(id int NOT NULL AUTO_INCREMENT,name varchar(45)
NOT NULL,Dept varchar(35) NOT NULL,age int NOT NULL,PRIMARY KEY (id));
ERROR 1046 (3D000): No database selected
mysql> SHOW DATABASES;
+ -+
| Database |
+ +
| information_schema |
| mysql |
| test |
+ +
3 rows in set (0.00 sec)
Drop Table
mysql> DROP TABLE mca;
Query OK, 0 rows affected (0.00 sec)
AIM:
To apply MYSQL replication technique in Distributed database.
PROCEDURE :
Replication Step 2
4. Copy and paste the code this screen provides into the very bottom of your MySQL config file
(my.ini on Windows) AND add a line that says binlog_format=ROW (this fixes an error when
running an external DB enrollment sync with replication)
5. Restart the MySQL services on the master server, leave PHPMyAdmin Open though
6. Once the service has restarted, click on “Go” on the PHPMyAdmin screen.
7. You will be redirected to the Replication screen which now looks like this
Replication Step 3
8. Lastly, we need to create a replication user so click on the link that says “Add slave replication
user”
9. Create a user and password, set the host to “Any” and click “Go”
10. On the privileges screen ensure the new user has both replication permissions checked and click
“Go”
Replication Step 4
11. That’s all we need to do on the master server, now lets move over to the slave
12. From within PHPMyAdmin on the slave, click the replication tab
13. Then click the link to configure this server as slave replication
14. Copy the line of code that shows your new server id and paste this entire line into the MySQL
config file on your slave database server
15. Stop and start your MySQL service on the slave server
16. Now in PHPMyAdmin enter the username of the replication user you created in step 13
17. Enter the password and the host (the hostname of the master server or its IP Address)
18. If your default port is not 3306 then change it, chances are it uses the default port
19. Click “Go”
Replication Step 5
20. It then takes you back to the replication screen and appears as though it‟s not configured but it
requires a refresh
21. So refresh the page and you wil see it‟s configured but not running
Replication Step 6
Replication Step 7
26. If everything is working then you wil see a message against Slave_IO_State that reads “Waiting
for master to send event”
RESULT:
AIM:
PROCEDURE:
Step 6: In the output screen Select Form Editor output will be displayed.
QUERIES WITH EXECUTION:
CREATE TABLE `test` (`id` INT NOT NULL AUTO_INCREMENT,`geom` GEOMETRY NULL,
PRIMARY KEY (`id`));
Thus the above program for Partial data storage and retrieval in MYSQL executed successfully.
Ex.No :5
TEMPORAL DATA STORAGE AND RETRIEVAL IN MYSQL
Date:
AIM:
PROCEDURE:
Step 3: Create Two Tables with the name of Customers and Orders.
mysql> create table customer(Cust_id int PRIMARY KEY,cust_name text,city text,occupation text);
Query OK, 0 rows affected (0.84 sec)
mysql> create table order1(order_id int PRIMARY KEY,prod_name text,price text);
Query OK, 0 rows affected (0.89 sec)
mysql> show tables;
+----- -+
| Tables_in_vvt |
+----- -+
| customer |
| mca |
| mcadet |
| order1 |
+----- -+
4 rows in set (0.10 sec)
mysql> insert into customer values(1001,'NATARAJAN S','RASIPURAM','ASST_PROF');
Query OK, 1 row affected (0.28 sec)
mysql> insert into customer values(1002,'SUNDAR','SALEM','ASST_PROF');
Query OK, 1 row affected (0.17 sec)
mysql> insert into customer values(1003,'SURESH','ATHUR','ASST_PROF');
Query OK, 1 row affected (0.14 sec)
mysql> insert into customer values(1004,'UDAY','SALEM','ASST_PROF');
Query OK, 1 row affected (0.34 sec)
mysql>
RESULT:
Thus the above program Temporary data storage and retrieval has been executed successfully.
Ex.No :6
OBJECT STORAGE AND RETRIEVAL IN MYSQL
Date:
AIM:
To implement object storage and retrieval in MYSQL using Python programming language.
PROCEDURE:
Step 1: Start the SQL Server.
Step 2: Create a Database “vysya” and Table “mca” in MYSQL
Step 2: In Python import mysql.connector to connect MYSQL with Python
Step 4:
Step 3: Create an object in python and insert that object in table
Step 4: Execute the SQL query.
Step 5: Fetch records from the result.
Step 6: Show the table after you make any changes in the table.
PROGRAM:
import mysql.connector
mydb = mysql.connector.connect(host="localhost",user="root",
password="Vysya@123",database="vysya")
mycursor = mydb.cursor()
sql = "INSERT INTO mca (Name, College) VALUES (%s, %s)"
val = ("Uday", "salem")
mycursor.execute(sql,val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
mycursor.execute("SELECT * FROM mca")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
OUTPUT:
RESULT:
Thus the above program for object creation and retrieval in MYSQL executed successfully using
python programming language
Ex.No :7
XML DATABASE CREATION AND XQUERY FLWOR EXPRESSION
Date:
AIM:
To Create a XML Databases in that create a XML Table and process the XQuery FLOWR
Expressions.
PROCEDURE:
Step1: Create a XML Database in Notepad and save with an Extension filename.xml.
Step4: Create a XML Table in that use XQuery FLWOR expression filter the record.
Program 1: (Book.xml)
<book category="ADT">
<title lang="en">Learn ADT in 24 Hours</title>
<author>Robert</author>
<college>Vysya</college>
<year>2005</year>
<price>30.00</price>
</book>
<book category="DOTNET">
<title lang="en">Learn .Net in 24 hours</title>
<author>Peter</author>
<college>Vysya</college>
<year>2011</year>
<price>40.50</price>
</book>
<book category="XML">
<title lang="en">Learn XQuery in 24 hours</title>
<author>Robert</author>
<author>Peter</author>
<college>Vysya</college>
<year>2013</year>
<price>50.00</price>
</book>
<book category="XML">
<title lang="en">Learn XPath in 24 hours</title>
<author>Jay Ban</author>
<college>Vysya</college>
<year>2010</year>
<price>16.50</price>
</book>
</books>
Program 2(books.xqy)
for $x in doc("books.xml")/books/book
where $x/price>30
return string($x/title)
OUTPUT:
Learn .Net in 24 hours
Learn XQuery in 24 hours
RESULT:
Thus the above program has XML FLOWR Query has been executed successfully.
Ex.No :8
MOBILE DATABASE QUERY PROCESSING USING OPENSOURCE DB
Date:
AIM:
To write a program for Mobile Database Query Processing using open source DB using MongoDB
PROCEDURE:
Step 1: Open Eclipse and Import Java ME SDK 3.0.
Step 2: In Device Management Manually install the Java ME SDK Files.
Step 3: Open a New File JAVAME MIDLET Project, Create a project Name and configure below Emulator
name.
Step 4: To establish a connection between Java ME to MongoDb Download the Jar file mongodb-driver-
3.2.2.jar and Import to Project Library
Step 5: Import the Necessary header files.
Step 6: use MONGOCLIENT to establish connection between MONGODB SERVER TO JAVA ME.
Step 7: create Collection in mongodb console.
Step 8: By using find() display all the records in collections.
Program:
package xyz;
import com.mongodb.*;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
public class Main
{
@SuppressWarnings("deprecation")
public static void main (String args[])
{
MongoClient con = new MongoClient("localhost", 27017);
com.mongodb.DB db= con.getDB("test");
DBCollection col=db.getCollection("SSSIT");
DBCursor cur= col.find();
while(cur.hasNext())
{
System.out.println(cur.next());
}
}
}
OUTPUT:
RESULT: