0% found this document useful (0 votes)
3 views35 pages

Adt Lab Record

The document is a practical record for the Advanced Database Technology Laboratory at C.S.I. Institute of Technology, detailing various database exercises including MongoDB, Cassandra, and OrientDB. It includes aims, procedures, commands, and results for CRUD operations, indexing, and database creation. The record is intended for submission for the Anna University MCA Degree Practical Examination for the academic year 2023-2024.

Uploaded by

stanisyareeni23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views35 pages

Adt Lab Record

The document is a practical record for the Advanced Database Technology Laboratory at C.S.I. Institute of Technology, detailing various database exercises including MongoDB, Cassandra, and OrientDB. It includes aims, procedures, commands, and results for CRUD operations, indexing, and database creation. The record is intended for submission for the Anna University MCA Degree Practical Examination for the academic year 2023-2024.

Uploaded by

stanisyareeni23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

C. S. I.

INSTITUTE OF TECHNOLOGY
T HOVALAI - 629 302
KANYAKUMARI DISTRICT

PRACTICAL RECORD

NAME : ………………………………………………

REG.NUMBER : ………………………………………………

SUBJECT : ADVANCED DATABASE TECHNOLOGY


LABORATORY

SUB.CODE : MC4211
DEPARTMENT OF COMPUTER APPLICATIONS
C.S.I. INSTITUTE OF TECHNOLOGY
THOVALAI - 629 302

CERTIFICATE
Certified that, this is a bonafide record work done
by ………………………………………………. Reg. No ....................................in
the MCA Computer Laboratory for ADVANCED DATABASE TECHNOLOGY

LABORATORY (Sub. Code: MC4211) at C.S.I Institute of Technology, Thovalai


during the year 2023 -2024.

Staff-in-Charge Head of the Department

Submitted for the Anna University MCA Degree Practical Examination held
at C.S.I. INSTITUTE OF TECHNOLOGY, THOVALAI on ………………….

Internal Examiner External Examiner


INDEX

S. No Title Page No. Signature

1 NOSQL Exercises

a. MongoDB – CRUD operations,Indexing,


Sharding, Deployment
b. Cassandra: Table Operations, CRUD
Operations, CQL Types
c. OrientDB Graph database – OrientDB Features

2 MySQL Database Creation, Table Creation, Query

3 MySQL Replication – Distributed Databases

4 Spatial data storage and retrieval in MySQL

5 Temporal data storage and retrieval in MySQL

6 Object storage and retrieval in MySQL

7 XML Databases , XML table creation, XQuery FLWOR


expression
Exp. No : 1 (a)
Date : MongoDB – CRUD operations, Indexing, Sharding, Deployment

AIM:
To perform CRUD operations, Indexing, Sharding, Deployment in MongoDB

PROCEDURE:
1. Start MongoDB
2. Create Collection
3. Inserting document into collection
4. Querying document based on the criteria
5. Updating Document
6. Deleting Document from collection
7. Create index in MongoDB
8. Finding the indexes in a collection
9. Drop indexes in a collection

MongoDB Commands
> show dbs; // To list down all the databases
//Creating a collection user and inserting a document in it.
> db.user.insert({name:"Mala", age:22})
> show dbs
> db.dropDatabase() //To drop Database
> use test
> db.admin.insert({username:"admin",
password: "admin"})

//To show all the documents in the given collection


> db.admin.find()
> show collections //Shows the list of all the collections in the currently selected database

//Creating collection without any parameters


> db.createCollection("students")
> show collections
> db.students.drop() //To drop the collection

//MongoDB insert document using insert()


> db.test.insert(
{
email: "[email protected]",
course: [ { name: "MongoDB", duration: 7 }, { name: "Java", duration: 30 } ]
}
)

//Insert multiple documents in collection


var beginners =
[
{
"StudentId" : 1001,
"StudentName" : "Steve",
"age": 30
},
{
"StudentId" : 1002,
"StudentName" : "Negan",
"age": 42
},
{
"StudentId" : 3333,
"StudentName" : "Rick",
"age": 35
},
];
db.students.insert(beginners);

//Querying all the documents in JSON format


> db.students.find()
> db.students.find().forEach(printjson);
> db.students.find().pretty()

//Query Document based on the criteria


> db.students.find({StudentName : "Steve"}).pretty()

//Find all the students having id less than 3000


db.students.find({"StudentId":{$lt:3000}}).pretty()

//Not Equals Criteria


db.students.find({"StudentId":{$ne:1002}}).pretty()

//MongoDB – Update Document in a Collection


//Updating Document using update() method
Syntax:
db.collection_name.update(criteria, update_data)

> db.got.find().pretty()

> db.got.update({"name":"Jon Snow"},{$set:{"name":"Kit Harington"}})


//Updating Document using save() method
Syntax:
db.collection_name.save( {_id:ObjectId(), new_document} )
> db.got.find().pretty()

> db.got.find({"name": "Kit Harington"}).pretty()


> db.got.save({"_id" : ObjectId("59bd2e73ce524b733f14dd65"), "name":

"Jon Snow", "age": 30})


//MongoDB Delete Document from a Collection
Syntax of remove() method:
db.collection_name.remove(delete_criteria)
> db.students.find().pretty()
> db.students.remove({"StudentId": 3333})
Indexing:
//To create the index on name field in ascending order:
> db.user.insert({name:”Rema”, age:0})
> db.user.createIndex({name:1})
// Drop indexes in a collection
> db.user.dropIndex({name:1})
Output:

> show dbs;


admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
> db.user.insert({name:"Mala", age:22})
WriteResult({ "nInserted" : 1 })
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> db.admin.insert({username:"admin", password:"admin"})
WriteResult({ "nInserted" : 1 })
> db.admin.find()
{ "_id" : ObjectId("605ef4b0c3907340f7f6f092"), "username" : "admin", "password"
: "admin" }
> show collections
admin
> db.createCollection("students")
{ "ok" : 1 }
> show collections
admin
students
> db.admin.drop()
true
> var beginners=
... [
... {
... "StudentId":1001,
... "StudentName":"Steve",
... "age":30
... },
... {"StudentId":1002,
... "StudentName":"Neha",
... "age":20
... },
... ];
> db.students.insert(beginners);

BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.students.find()

{ "_id" : ObjectId("605ef5b7c3907340f7f6f093"), "StudentId" : 1001, "StudentName


" : "Steve", "age" : 30 }
{ "_id" : ObjectId("605ef5b7c3907340f7f6f094"), "StudentId" : 1002, "StudentName
" : "Neha", "age" : 20 }
> db.students.find().forEach(printjson);
{
"_id" : ObjectId("605ef5b7c3907340f7f6f093"),
"StudentId" : 1001,
"StudentName" : "Steve",
"age" : 30
}
{
"_id" : ObjectId("605ef5b7c3907340f7f6f094"),
"StudentId" : 1002,
"StudentName" : "Neha",
"age" : 20
}
> db.students.find().pretty()
{
"_id" : ObjectId("605ef5b7c3907340f7f6f093"),
"StudentId" : 1001,
"StudentName" : "Steve",
"age" : 30
}
{
"_id" : ObjectId("605ef5b7c3907340f7f6f094"),
"StudentId" : 1002,
"StudentName" : "Neha",
"age" : 20
}
> db.students.find({StudentName :"Steve"}).pretty()
{
"_id" : ObjectId("605ef5b7c3907340f7f6f093"),
"StudentId" : 1001,
"StudentName" : "Steve",
"age" : 30
}
> db.students.find({StudentId :{$lt:3000}}).pretty()
{

"_id" : ObjectId("605ef5b7c3907340f7f6f093"),
"StudentId" : 1001,
"StudentName" : "Steve",
"age" : 30
}
{
"_id" : ObjectId("605ef5b7c3907340f7f6f094"),
"StudentId" : 1002,
"StudentName" : "Neha",
"age" : 20

}
> db.students.update({"StudentName":"Steve"},{$set:{"StudentName":"Mala"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.students.find().pretty()
{
"_id" : ObjectId("605ef5b7c3907340f7f6f093"),
"StudentId" : 1001,
"StudentName" : "Mala",
"age" : 30
}
{
"_id" : ObjectId("605ef5b7c3907340f7f6f094"),
"StudentId" : 1002,
"StudentName" : "Neha",
"age" : 20
}
> db.students.remove({"StudentId":1001})
WriteResult({ "nRemoved" : 1 })
> db.students.find().pretty()
{
"_id" : ObjectId("605ef5b7c3907340f7f6f094"),
"StudentId" : 1002,
"StudentName" : "Neha",
"age" : 20
}
> db.students.createIndex({StudentId:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.students.dropIndex({StudentId:1})
{ "nIndexesWas" : 2, "ok" : 1 }
>

RESULT
Thus the MongoDB CRUD operations, Indexing and Deployment have been verified and
executed successfully.
Exp. No : 1 (b) Cassandra: Table Operations, CRUD Operations,
Date : CQL Types

AIM:
To perform Table Operations, CRUD Operations, CQL Types in Cassandra

PROCEDURE:
1. Start Cassandra Server
2. Create Keyspace
3. Create table
4. Inserting records into the table
5. Create index on the table
6. Perform Read, Update, Delete Operations and Cassandra Query Language Types

Cassandra Commands
//Keyspace is a namespace of tables
CREATE KEYSPACE test
WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
//Use the new keyspace
USE test;
//Create Table
CREATE TABLE user (
user_id int PRIMARY KEY,
first_name text,
last_name text
);

//Insert data:
INSERT INTO user (user_id, first_name, last_name)
VALUES (1, 'Ming', 'Li');
INSERT INTO user (user_id, first_name, last_name)
VALUES (2, 'Hong', 'Zhang');
INSERT INTO user (user_id, first_name, last_name)
VALUES (3, 'Lin', 'Li');
//Fetch data:
SELECT * FROM user;
//Next, fetch users whose last name is 'Li' by creating an index.
CREATE INDEX ON user (last_name);
SELECT * FROM user WHERE last_name = 'Li';
//Updating Records
UPDATE user
SET first_name = 'Hema' WHERE user_id=1;
//Adding a column

ALTER TABLE user


ADD age int;

//Dropping a column
ALTER TABLE user
DROP age;
// Deleting Records
DELETE FROM user WHERE user_id=1;
Output:

cqlsh> CREATE KEYSPACE mca WITH REPLICATION = {'class' : 'SimpleStrategy', 'repl


ication_factor':1};
cqlsh> USE mca;
cqlsh:mca> CREATE TABLE user(user_id int PRIMARY KEY, first_name text, last_name
text);
cqlsh:mca> INSERT INTO user(user_id, first_name, last_name) VALUES(1, 'Ratha', '
M');
cqlsh:mca> INSERT INTO user(user_id, first_name, last_name) VALUES(2, 'Mala', 'K
');
cqlsh:mca> INSERT INTO user(user_id, first_name, last_name) VALUES(3, 'Hema', 'M
');
cqlsh:mca> SELECT * FROM user;
user_id | first_name | last_name
+ +
1 | Ratha | M
2| Mala | K
3| Hema | M

(3 rows)
cqlsh:mca> CREATE INDEX ON user(last_name);
cqlsh:mca> SELECT * FROM user WHERE last_name = 'M';
user_id | first_name | last_name
+ +
1 | Ratha | M
3| Hema | M
(2 rows)
cqlsh:mca> UPDATE user SET first_name='Raja' WHERE user_id=1;
cqlsh:mca> SELECT * FROM user;

user_id | first_name | last_name


+ +
1| Raja | M
2| Mala | K
3| Hema | M

(3 rows)
cqlsh:mca> ALTER TABLE user ADD age int;
cqlsh:mca> ALTER TABLE user DROP age;
cqlsh:mca> DELETE ROM user WHERE user_id=1;
Bad Request: line 1:11 no viable alternative at input 'user'
cqlsh:mca> DELETE FROM user WHERE user_id=1;
cqlsh:mca>

RESULT
Thus the Table Operations, CRUD Operations and CQL types have been verified and executed
successfully in Cassandra.
Exp. No : 1 (c)
OrientDB Graph database – OrientDB Features
Date :

AIM
To perform OrientDB Graph database operations.

PROCEDURE:
1. Starting the OrientDB Console
2. Connecting to Server Instances
3. Create Vertices and Edges
4. Traverse the Graph

OrientDB Commands
Starting the OrientDB Console
In the OrientDB installation directory under bin, there is a file called console.bat for Windows
users.
Connecting to Server Instances
orientdb> CONNECT remote:localhost root my_root_password
To display the list of available Databases
orientdb> LIST DATABASES
To create a brand new local database:
orientdb> create database plocal:../databases/cars admin admin plocal
To create the first graph schema with "Person" and "Car" as 2 new Vertex types and "Owns" as an
Edge type:
orientdb> create class Person extends V
orientdb> create class Car extends V
orientdb> create class Owns extends E
To populate the database with the first Graph elements:
orientdb> create vertex Person set name = 'Luca'
orientdb> create vertex Car set name = 'Ferrari Modena'
orientdb> create edge Owns from (select from Person) to (select from Car)
Now we can traverse vertices. For example, to know the Luca's car, Traverse from Luca
vertex to the outgoing vertices following the "Owns" relationships:
orientdb> select name from ( select expand( out('Owns') ) from Person where name = 'Luca' )
Output:

orientdb> CONNECT remote:localhost root MCA


OK
orientdb {server=remote:localhost}> LIST DATABASES

Found 1 databases:

* demodb

orientdb {server=remote:localhost}> create database plocal:../databases/cars admin admin plocal

Creating database [plocal:../databases/cars] using the storage type [PLOCAL]...


2021-03-31 12:09:39:461 WARNI IMPORTANT! Using default password is unsafe, please change
password for user 'admin' on database 'cars' [OrientDBEmbedded]
Database created successfully.

Current database is: plocal:../databases/cars


orientdb {db=cars}> create class Person extends V

Class created successfully.

orientdb {db=cars}> create class Car extends V

Class created successfully.

orientdb {db=cars}> create class Owns extends E

Class created successfully.

orientdb {db=cars}> create vertex Person set name = 'Luca'

Created vertex '[Person#34:0{name:Luca} v1]' in 0.015000 sec(s).

orientdb {db=cars}> create vertex Car set name = 'Ferrari Modena'

Created vertex '[Car#46:0{name:Ferrari Modena} v1]' in 0.016000 sec(s).

orientdb {db=cars}> create edge Owns from (select from Person) to (select from Car)

+ + + + + +
|# |@RID |@CLASS|out |in |
+ + + + + +
|0 |#58:0|Owns |#34:0|#46:0|
+ + + + + +
Created '1' edges in 0.031000 sec(s).

orientdb {db=cars}> select name from ( select expand( out('Owns') ) from Person where name =
'Luca' )

+ + +
|# |name |
+ + +
|0 |Ferrari Modena|
+ + +

1 item(s) found. Query executed in 0.054 sec(s).


orientdb {db=cars}>

RESULT:

Thus the OrientDB Graph database operations have been verified and executed successfully.
Exp. No : 2
MySQL Database Creation, Table Creation, Query
Date :

AIM:
To perform Database Creation, Table Creation and Querying in MqSQL

PROCEDURE:

1. Start MySQL
2. Create Database
3. Create table into the Database
4. Inserting records into the table
5. Updating records from the table
6. Querying records from the table
7. Deleting records from table
8. Drop the table
9. Drop the database
MySQL Commands
CREATE DATABASE Student; // To create Database
USE Student; //To use the database Student
SHOW DATABASES; //To display all the databases
//To Create the Table
CREATE TABLE student(snum INT(4) PRIMARY KEY, sname varchar(10), major
VARCHAR(10), lev VARCHAR(2), age INT(2));
//To insert records into the table
INSERT INTO student (snum,sname,major,lev,age) values(121,'Agrawal','CSE','SR',21);
INSERT INTO student (snum,sname,major,lev,age) values(119, 'Tony ', 'CSE', 'SR',21);
INSERT INTO student (snum,sname,major,lev,age) values(122, ' Krishna ', 'CSE', 'JR',20);

//To retrieve records from the table


SELECT * FROM student;
SELECT * FROM student WHERE snum = 121;
SELECT * FROM student WHERE sname = "Tony" AND age = 21;
SELECT DISTINCT age FROM student;
SELECT snum, sname, major FROM student ORDER BY snum DESC;
//To add new column into the table
ALTER TABLE student ADD COLUMN place varchar (20);
//To delete the column from the table
ALTER TABLE student DROP COLUMN place;
//To delete the table
DROP TABLE student
Output:
mysql> CREATE DATABASE Student;
Query OK, 1 row affected (0.00 sec)
mysql> USE Student;
Database changed
mysql> SHOW DATABASES;
+ +
| Database |
+ +
| information_schema |
| mysql |
| performance_schema |
| student |
| test |
+ +
5 rows in set (0.00 sec)

mysql> CREATE TABLE student(snum INT(4) PRIMARY KEY, sname varchar(10), major VA
RCHAR(10), lev VARCHAR(2), age INT(2));
Query OK, 0 rows affected (0.08 sec)

mysql> INSERT INTO student (snum,sname,major,lev,age) values(121,'Agrawal','CSE'


,'SR',21);
Query OK, 1 row affected (0.08 sec)

mysql> INSERT INTO student (snum,sname,major,lev,age) values(119, 'Tony ', 'CSE'


, 'SR',21);
Query OK, 1 row affected (0.08 sec)

mysql> INSERT INTO student (snum,sname,major,lev,age) values(122, ' Krishna ', '
CSE', 'JR',20);
Query OK, 1 row affected (0.11 sec)

mysql> SELECT * FROM student;


+ + + + + +
| snum | sname | major | lev | age |
+ + + + + +
| 119 | Tony | CSE | SR | 21 |
| 121 | Agrawal | CSE | SR | 21 |
| 122 | Krishna | CSE | JR | 20 |
+ + + + + +
3 rows in set (0.00 sec)
mysql> SELECT * FROM student WHERE snum = 121;
+ + + + + +
| snum | sname | major | lev | age |
+ + + + + +
| 121 | Agrawal | CSE | SR | 21 |
+ + + + + +
1 row in set (0.00 sec)
mysql> SELECT * FROM student WHERE nsame = " Tony " AND age = 21;
ERROR 1054 (42S22): Unknown column 'nsame' in 'where clause'
mysql> SELECT * FROM student WHERE sname = " Tony " AND age = 21;
Empty set (0.00 sec)

mysql> SELECT * FROM student WHERE sname = "Tony" AND age = 21;
+ + + + + +
| snum | sname | major | lev | age |
+ + + + + +
| 119 | Tony | CSE | SR | 21 |
+ + + + + +
1 row in set (0.00 sec)

mysql> SELECT DISTINCT age FROM student;


+ +
| age |
+------+
| 21 |
| 20 |
+------+
2 rows in set (0.00 sec)

mysql> SELECT snum, sname, major FROM student ORDER BY snum DESC;
+ + + +
| snum | sname | major |
+ + + +
| 122 | Krishna | CSE |
| 121 | Agrawal | CSE |
| 119 | Tony | CSE |
+ + + +
3 rows in set (0.00 sec)

mysql> ALTER TABLE student ADD COLUMN place varchar (20);


Query OK, 3 rows affected (0.22 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> ALTER TABLE student DROP COLUMN place;


Query OK, 3 rows affected (0.18 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> DROP TABLE student;


Query OK, 0 rows affected (0.04 sec)
mysql>

RESULT :
Thus the Database Creation, Table Creation and Querying in MqSQL has been verified and
executed successfully.
Exp. No : 3
Date : Replication – Distributed Databases

AIM:
To perform Replication of Distributed Databases.
PROCEDURE:
Backing Up a Replica Using mysqldump
Stop the replica from processing requests. You can stop replication completely on the replica
using mysqladmin:
shell>mysqladmin stop-slave
Alternatively, you can stop only the replication SQL thread to pause event execution:
shell>mysql-e'STOP SLAVE SQL_THREAD;'
1. Run mysqldump to dump your databases. You may either dump all databases or select databases
to be dumped. For example, to dump all databases:
shell>mysqldump--all-databases>fulldb.dump
2. Once the dump has completed, start replica operations again:
shell>mysqladmin start-slave

Checking Replication Status


The SHOW STATUS statement also provided some information relating specifically to replicas. As of
MySQL version 5.7.5, the following status variables previously monitored using SHOW STATUS were
deprecated and moved to the Performance Schema replication tables:
 Slave_retried_transactions
 Slave_last_heartbeat
 Slave_received_heartbeats
 Slave_heartbeat_period
 Slave_running

mysql>SHOWSLAVESTATUS\G
*************************** 1. row ***************************
• Slave_IO_State: Waiting for master to send event
• Master_Host: source1
• Master_User: root
• Master_Port: 3306
• Connect_Retry: 60
• Master_Log_File: mysql-bin.000004
• Read_Master_Log_Pos: 931
• Relay_Log_File: replica1-relay-bin.000056
• Relay_Log_Pos: 950
• Relay_Master_Log_File: mysql-bin.000004
• Slave_IO_Running: Yes
• Slave_SQL_Running: Yes
• Replicate_Do_DB:
• Replicate_Ignore_DB:
• Replicate_Do_Table:
• Replicate_Ignore_Table:
• Replicate_Wild_Do_Table:
• Replicate_Wild_Ignore_Table:
• Last_Errno: 0
• Last_Error:
• Skip_Counter: 0
• Exec_Master_Log_Pos: 931
• Relay_Log_Space: 1365
• Until_Condition: None
• Until_Log_File:
• Until_Log_Pos: 0
• Master_SSL_Allowed: No
• Master_SSL_CA_File:
• Master_SSL_CA_Path:
• Master_SSL_Cert:
• Master_SSL_Cipher:
• Master_SSL_Key:
• Seconds_Behind_Master: 0
• Master_SSL_Verify_Server_Cert: No
• Last_IO_Errno: 0
• Last_IO_Error:
• Last_SQL_Errno: 0
• Last_SQL_Error:
• Replicate_Ignore_Server_Ids: 0

 On the source, you can check the status of connected replicas using SHOW PROCESSLIST to
examine the list of running processes. Replica connections have Binlog Dump in
the Command field:
mysql>SHOWPROCESSLIST \G;
• *************************** 4. row ***************************
• Id: 10
• User: root
• Host: replica1:58371
• db: NULL
• Command:Binlog Dump
• Time: 777
• State: Has sent all binlog to slave; waiting for binlog to be updated
• Info: NULL

Because it is the replica that drives the replication process, very little information is available in this
report.
For replicas that were started with the --report-host option and are connected to the source, the SHOW
SLAVE HOSTS statement on the source shows basic information about the replicas. The output
includes the ID of the replica server, the value of the --report-host option, the connecting port, and
source ID:
mysql>SHOWSLAVEHOSTS;
Output :

• + + + + + +
• |Server_id| Host | Port |Rpl_recovery_rank|Master_id|
• + + + + + +
• | 10 | replica1 | 3306 | 0| 1|
• + + + + + +
• 1 row in set (0.00 sec)

RESULT
Thus the Replication of Distributed Databases in MySQL has been verified and executed
successfully.
Exp. No : 4
Spatial data storage and retrieval in MySQL
Date :

AIM:
To perform Spatial data storage and retrieval in MySQL.

PROCEDURE:

1. Start MySQL
2. Create Database
3. Create table into the Database
4. Inserting spatial data
5. Retrieving records from the table

MySQL Commands
// CREATE TABLE statement to create a table with a spatial column
CREATE TABLE geotest (code int(5),descrip varchar(50), g GEOMETRY);
describe geotest;
//Point is a geometry which represents a single location in coordinate space.
ALTER TABLE geotest ADD pt_loca POINT;
ALTER TABLE geotest DROP pt_loca ;
//LineString is a Curve with linear interpolation between points.
SET @g = 'LINESTRING(0 0,1 2,2 4)';
INSERT INTO geotest VALUES (123,"Test Data",GeomFromText(@g));
//Polygon is a planar Surface representing a multi sided geometry.
SET @g = 'POLYGON((0 0,8 0,12 9,0 9,0 0),(5 3,4 5,7 9,3 7, 2 5))';
INSERT INTO geotest VALUES (123,"Test Data",GeomFromText(@g));
//GeometryCollection is a geometry that is a collection of one or more geometries of any class.
SET @g ='GEOMETRYCOLLECTION(POINT(3 2),LINESTRING(0 0,1 3,2 5,3 5,4 7))';
INSERT INTO geotest VALUES (123,"Test Data",GeomFromText(@g));
//MultiPoint is a geometry collection composed of Point elements.
SET @g ='MULTIPOINT(0 0, 15 25, 45 65)';
INSERT INTO geotest VALUES (123,"Multipoint",GeomFromText(@g));
// MultiLineString is a MultiCurve geometry collection composed of LineString elements.
SET @g ='MULTILINESTRING((12 12, 22 22), (19 19, 32 18))';
INSERT INTO geotest VALUES (123,"Multistring",GeomFromText(@g));
//MultiPolygon is a MultiSurface object composed of Polygon elements.
SET @g ='MULTIPOLYGON(((0 0,11 0,12 11,0 9,0 0)),((3 5,7 4,4 7,7 7,3 5)))';
INSERT INTO geotest VALUES (123,"Multipolygon",GeomFromText(@g));

select * from geotest;


Output:

mysql> show databases;


+ +
| Database |
+ +
| information_schema |
| mysql |
| performance_schema |
| test |
+ +
4 rows in set (0.07 sec)
mysql> use test;
Database changed
mysql> CREATE TABLE geotest (code int(5),descrip varchar(50), g GEOMETRY);
Query OK, 0 rows affected (0.11 sec)
mysql> describe geotest;
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| code | int(5) | YES | | NULL | |
| descrip | varchar(50) | YES | | NULL | |
|g | geometry | YES | | NULL | |
+ + + + + + +
3 rows in set (0.01 sec)
mysql> ALTER TABLE geotest ADD pt_loca POINT;
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE geotest DROP pt_loca ;
Query OK, 0 rows affected (0.26 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SET @g = 'LINESTRING(0 0,1 2,2 4)';
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO geotest VALUES (123,"Test Data",GeomFromText(@g));
Query OK, 1 row affected (0.08 sec)

mysql> SET @g = 'POLYGON((0 0,8 0,12 9,0 9,0 0),(5 3,4 5,7 9,3 7, 2 5))';
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO geotest VALUES (123,"Test Data",GeomFromText(@g));


Query OK, 1 row affected (0.09 sec)

mysql> SET @g ='GEOMETRYCOLLECTION(POINT(3 2),LINESTRING(0 0,1 3,2 5,3 5,4 7))';


Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO geotest VALUES (123,"Test Data",GeomFromText(@g));
Query OK, 1 row affected (0.09 sec)

mysql> SET @g ='MULTIPOINT(0 0, 15 25, 45 65)';


Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO geotest VALUES (123,"Multipoint",GeomFromText(@g));


Query OK, 1 row affected (0.10 sec)

mysql> SET @g ='MULTILINESTRING((12 12, 22 22), (19 19, 32 18))';


Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO geotest VALUES (123,"Multistring",GeomFromText(@g));


Query OK, 1 row affected (0.08 sec)

mysql> SET @g ='MULTIPOLYGON(((0 0,11 0,12 11,0 9,0 0)),((3 5,7 4,4 7,7 7,3 5)))
';
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO geotest VALUES (123,"Multipolygon",GeomFromText(@g));


Query OK, 1 row affected (0.10 sec)

mysql> select * from geotest;


+ + +

+
| code | descrip |g

|
+ + +

+
| 123 | Test Data | ☺☻ ♥ ≡? @ @
►@
|
| 123 | Test Data | NULL

|
| 123 | Test Data | ☺ ☻ ☺☺ @ @☺☻ ♣
≡? @ @ ¶@ @ ¶@ ►@ ∟@
|
| 123 | Multipoint | ☺♦ ♥ ☺☺ ☺☺ .@ 9@☺
☺ ÇF@ @P@
|
| 123 | Multistring | ☺♣ ☻ ☺☻ ☻ (@ (@ 6@ 6@☺☻
☻ 3@ 3@ @@ 2@
|
| 123 | Multipolygon | ☺♠ ☻ ☺♥ ☺ ♣ &@

(@ &@ "@ ☺♥ ☺ ♣ @ ¶@
∟@ ►@ ►@ ∟@ ∟@ ∟@ @ ¶@ |
+ + +

+
6 rows in set (0.00 sec)

RESULT
Thus the Spatial data storage and retrieval in MySQL has been verified and executed
successfully.
Exp. No : 5
Temporal data storage and retrieval in MySQL
Date :

AIM:
To perform Temporal data storage and retrieval in MySQL

PROCEDURE:
1. Start MySQL
2. Create Database
3. Create table into the Database
4. Inserting temporal data into the table
5. Retrieving records from the table

Temporal Data Types


Type Storage Requires Range
DATE 3 bytes '1000-01-01' to '9999-12-31'
TIME 3 bytes '-838:59:59' to '838:59:59'
DATETIME 8 bytes '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
TIMESTAMP 4 bytes '1970-01-01 00:00:00' to mid-year 2037
YEAR 1 byte 1901 to 2155 (for YEAR(4)), 1970 to 2069 (for YEAR(2))

MySQL Commands
CREATE TABLE ts_test1 ( ts1 TIMESTAMP, data CHAR(30));
DESCRIBE ts_test1;
INSERT INTO ts_test1 (data) VALUES ('original_value');
SELECT * FROM ts_test1;
UPDATE ts_test1 SET data='updated_value';
SELECT * FROM ts_test1;

CREATE TABLE ts_test2 (created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,


data CHAR(30));
INSERT INTO ts_test2 (data) VALUES ('original_value');
SELECT * FROM ts_test2;
UPDATE ts_test2 SET data='updated_value';

CREATE TABLE ts_test (ts TIMESTAMP);


INSERT INTO ts_test (ts) VALUES (NULL);
SELECT * FROM ts_test;
SET time_zone = '+02:00';
SELECT * FROM ts_test;
SET time_zone = '-02:00';
SELECT * FROM ts_test;
SELECT CONVERT_TZ('2008-01-28 13:30:00', '+01:00', '+03:00');
Output:

Enter password:
mysql> show databases;
+ +
| Database |
+ +
| information_schema |
| mysql |
| performance_schema |
| student |
| test |
+ +
5 rows in set (0.05 sec)

mysql> use test;


Database changed
mysql> CREATE TABLE ts_test1 ( ts1 TIMESTAMP, data CHAR(30));
Query OK, 0 rows affected (0.15 sec)

mysql> DESCRIBE ts_test1;


+ + + + + +
---+
| Field | Type | Null | Key | Default | Extra
|
+ + + + + +
---+
| ts1 | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTA
MP |
| data | char(30) | YES | | NULL |
|
+ + + + + +
---+
2 rows in set (0.01 sec)

mysql> INSERT INTO ts_test1 (data) VALUES ('original_value');


Query OK, 1 row affected (0.08 sec)

mysql> SELECT * FROM ts_test1;


+ + +
| ts1 | data |
+ + +
| 2021-03-27 23:37:03 | original_value |
+ + +
1 row in set (0.00 sec)

mysql> UPDATE ts_test1 SET data='updated_value';


Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> SELECT * FROM ts_test1;


+ + +
| ts1 | data |
+ + +
| 2021-03-27 23:37:32 | updated_value |
+ + +
1 row in set (0.00 sec)

mysql> CREATE TABLE ts_test2 (created_time TIMESTAMP DEFAULT


CURRENT_TIMESTAMP, data CHAR(30));
Query OK, 0 rows affected (0.13 sec)

mysql> INSERT INTO ts_test2 (data) VALUES ('original_value');


Query OK, 1 row affected (0.09 sec)

mysql> SELECT * FROM ts_test2;


+ + +
| created_time | data |
+ + +
| 2021-03-27 23:38:02 | original_value |
+ + +
1 row in set (0.00 sec)

mysql> UPDATE ts_test2 SET data='updated_value';


Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> CREATE TABLE ts_test (ts TIMESTAMP);


Query OK, 0 rows affected (0.16 sec)

mysql> INSERT INTO ts_test (ts) VALUES (NULL);


Query OK, 1 row affected (0.07 sec)

mysql> SELECT * FROM ts_test;


+ +
| ts |
+ +
| 2021-03-27 23:38:41 |
+ +
1 row in set (0.00 sec)

mysql> SET time_zone = '+02:00';


Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM ts_test;


+ +
| ts |
+ +

| 2021-03-27 20:08:41 |
+ +
1 row in set (0.00 sec)

mysql> SET time_zone = '-02:00';


Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM ts_test;


+ +
| ts |
+ +
| 2021-03-27 16:08:41 |
+ +
1 row in set (0.00 sec)

mysql> SELECT CONVERT_TZ('2008-01-28 13:30:00', '+01:00', '+03:00');


+ +
| CONVERT_TZ('2008-01-28 13:30:00', '+01:00', '+03:00') |
+ +
| 2008-01-28 15:30:00 |
+ +
1 row in set (0.00 sec)

mysql>

RESULT:
Thus the Temporal data storage and retrieval in MySQL has been verified and executed
successfully.
Exp. No : 6
Date : Object storage and retrieval in MySQL

AIM:
To perform object storage and retrieval in MySQL

PROCEDURE:
1. Start MySQL
2. Create Database
3. Create Table
4. Add JSON object column
5. Storing, Retrieving and Updating the objects
MySQL Commands
1. Create table EMP with ID and Name columns.

CREATE TABLE EMP(empID INT, empName VARCHAR(30));

2. Add a JSON object column empInfo to the EMP table.

ALTER TABLE EMP ADD COLUMN empInfo JSON;

3. Insert 2 records to the EMP table including the JSON object values.

INSERT INTO EMP(empID, empName, empInfo) VALUES(101, 'Kohila',


'{"City":"Nagercoil", "District":"Kanyakumari"}');

INSERT INTO EMP(empID, empName, empInfo) VALUES(102, 'Subin',


'{"City":"Thovalai", "District":"Kanyakumari"}');

4. Display the Keys of the JSON object in EMP Table

SELECT DISTINCT JSON_KEYS(empInfo) FROM EMP;

5. Display only the Distinct information from the JSON Object from the EMP table

SELECT JSON_REMOVE(empInfo, '$.City') FROM EMP;

6. Display the Employee Name and JSON attribute values of EMP records.

SELECT empName AS "Name", empInfo->>"$.City" AS "City", empInfo->>"$.District"


AS "District" FROM EMP;

7. Display the Employee Name and JSON attribute values of EMP records of employees
coming from City "Thovalai".
SELECT empName AS "Name", empInfo->>"$.City" AS "City", empInfo->>"$.District"

AS "District" FROM EMP WHERE empInfo->"$.City" = " Thovalai ";

8. Update JSON Object City value of EMP table where empID is 101
UPDATE EMP SET empInfo = JSON_SET(empInfo, '$.City', 'Kottar') WHERE
empID = 101;
Output:

mysql> show databases;


+ +
| Database |
+ +
| information_schema |
| mysql |
| performance_schema |
| sys |
+ +
4 rows in set (0.00 sec)

mysql> create database test;


Query OK, 1 row affected (0.00 sec)

mysql> use test;


Database changed
mysql> CREATE TABLE EMP(empID INT, empName VARCHAR(30));
Query OK, 0 rows affected (0.03 sec)

mysql> ALTER TABLE EMP ADD COLUMN empInfo JSON;


Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> INSERT INTO EMP(empID, empName, empInfo) VALUES(101, 'Kohila',


'{"City":"Nagercoil", "District":"Kanyakumari"}');
Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO EMP(empID, empName, empInfo) VALUES(102, 'Subin',


'{"City":"Thovalai", "District":"Kanyakumari"}');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT DISTINCT JSON_KEYS(empInfo) FROM EMP;


+ +
| JSON_KEYS(empInfo) |
+ +
| ["City", "District"] |
+ +
1 row in set (0.02 sec)

mysql> SELECT JSON_REMOVE(empInfo, '$.City') FROM EMP;


+ +
| JSON_REMOVE(empInfo, '$.City') |
+ +
| {"District": "Kanyakumari"} |
| {"District": "Kanyakumari"} |
+ +

2 rows in set (0.02 sec)


mysql> SELECT empName AS "Name", empInfo->>"$.City" AS "City", empInfo->>"$.District"
AS "District" FROM EMP;
+ + + +
| Name | City | District |
+ + + +
| Kohila | Nagercoil | Kanyakumari |
| Subin | Thovalai | Kanyakumari |
+ + + +
2 rows in set (0.00 sec)

mysql> SELECT empName AS "Name", empInfo->>"$.City" AS "City", empInfo->>"$.District"


AS "District" FROM EMP WHERE empInfo->"$.City" = " Thovalai ";
+ + + +
| Name | City | District |
+ + + +
| Subin | Thovalai | Kanyakumari |
+ + + +
1 row in set (0.00 sec)

mysql> UPDATE EMP SET empInfo = JSON_SET(empInfo, '$.City', 'Kottar')


-> WHERE empID = 101;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from EMP;


+ + + +
| empID | empName | empInfo |
+ + + +
| 101 | Kohila | {"City": "Kottar", "District": "Kanyakumari"} |
| 102 | Subin | {"City": "Thovalai", "District": "Kanyakumari"} |
+ + + +
2 rows in set (0.00 sec)

RESULT:
Thus the object storage and retrieval in MySQL has been verified and executed successfully.
Exp. No : 7
Date : XML Databases , XML table creation, XQuery FLWOR

AIM:
To perform XML databases, XML table creation and XQuery FLWOR expression

PROCEDURE:
Step 1: Click start programs
Step 2: Select notepad
Step 3: Type the xml codings
Step 4: Save the file with extension .xml
Step 5: Run the file
Step 6: output will be displayed on the window.
Step 7: end

Program:

<?xml version = "1.0"?>


<contact-info>
<contact1>
<name>Kohila</name>
<company>ABC Technologies</company>
<phone>9968532486</phone>
</contact1>
<contact2>
<name>Subin</name>
<company> ABC Technologies </company>
<phone>9968532486</phone>
</contact2>
</contact-info>
Output:

RESULT:
Thus the XML databases, XML table creation and XQuery FLWOR expression operations
have been verified and executed successfully.

You might also like