Adt Record
Adt Record
INSTITUTE OF TECHNOLOGY
RAJIVGANDHISALAI,KARAPAKKAM,CHENNAI-600097
StudentName :
RegisterNumber :
T.J. INSTITUTE OF TECHNOLOGY
RAJIVGANDHI SALAI,KARAPAKKAM, CHENNAI-600097.
BONAFIDE CERTIFICATE
Certified that this is the bonafide record of work done by
Mr/Ms in Degree course
in the laboratory
during the academic year 2023
Station: Chennai–600097
Date :
MONGODB -Crud
1(A) Operations,Indexing,Sharing
Deployment
1(B) CASSANDRA:Table
Operations,Crud Operations,CQL Types
Step 3: To begin using MongoDB, connect a mongo.exe shell to the running MongoDB instance.
Either:
Copy
"C:\Program Files\MongoDB\Server\4.4\bin\mongo.exe"
1. Create Operations
Create or insert operations add new documents to a collection. If the collection does not currently
exist, insert operations will create the collection.
1
OUTPUT:
2
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])
OUTPUT:
db.inventory.insertMany([
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
3
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
OUTPUT
UPDATE
example uses the db.collection.updateOne() method on the inventory collection to update the
first document where item equals "paper":
db.inventory.updateOne(
{ item: "paper" },
{
4
$set: { "size.uom": "cm", status: "P" },
$currentDate: { lastModified: true }
}
)
OUTPUT
db.inventory.updateMany(
{ "qty": { $lt: 50 } },
{
$set: { "size.uom": "in", status: "P" },
$currentDate: { lastModified: true }
}
5
Replace a Document
The following example replaces the first document from the inventory collection
where item: "paper":
6
db.inventory.replaceOne(
{ item: "paper" },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] })
Delete Documents
The examples on this page use the inventory collection. To populate the inventory collection,
run the following:
db.inventory.insertMany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
] );
OUTPUT
7
Delete All Documents
db.inventory.deleteMany({})
OUTPUT
2) INDEXES
8
Indexes support the efficient execution of queries in MongoDB.
To create an index in the Mongo Shell, use db.collection.createIndex().
db.collection.createIndex( <key and index type specification>, <options> )
The following example creates a single key descending index on the name field:
db.products.createIndex(
{ item: 1, quantity: -1 } ,
{ name: "query for inventory" }
)
OUTPUT
3) SHARDING
9
sh.shardCollection(
"database.collection",
{ "fieldA" : 1, "fieldB" : 1, "fieldC" : "hashed" }
)
OUTPUT
Result:
Thus the program for CURD operation using MONGODB is created successfully.
10
EX.NO. : 1 B) NOSQL EXERCISES
DATE: CASSANDRA: TABLE OPERATIONS, CRUD
OPERATIONS, CQL TYPES
Aim: To create table operation, CURD operation and CQL types using CASSANDRA.
Algorithm:
Step 1: Cassandra is a distributed database from Apache that is highly scalable and designed to
Step 2: Apache Cassandra requires Java 8 to run on a Windows system. Additionally, the Cassandra
Step 4: Apache Cassandra Download page and select the version you would prefer to download.
Step 5: Click the suggested Mirror download link to start the download process.
Step 6: Unzip the compressed tar.gz folder using a compression tool such as 7-Zip or WinZip. In
this example, the compressed folder was unzipped, and the content placed in
Step 7: The system proceeds to start the Cassandra Server by type Cassandra in cmd.
11
Step 8: Access Cassandra cqlsh from Windows CMD
While the initial command prompt is still running open a new command line prompt from
the same bin folder. Enter the following command to access the Cassandra cqlsh bash
shell
Step 9: Large volumes of unstructured data can be an issue for traditional relational
databases. This popular NoSQL database solution is going to allow you to capture
12
Create Table with Simple Primary Key
supp_city text,
supp_email text,
supp_fee int,
supp_name text,
supp_phone int
);
Result:
Thus the program for creating table and CURD operation using CASSANDRA is created
successfully.
13
EX.NO. : 1 C) NOSQL EXERCISES
DATE: HIVE: DATA TYPES, DATABASE OPERATIONS,
PARTITIONING , HiveQL
Aim: To create a program for database, data type, partitioning and HiveQL using HIVE.
Algorithm:
Hive is a data warehouse infrastructure tool to process structured data in Hadoop. It resides on
top of Hadoop to summarize Big Data, and makes querying and analyzing easy.
Step 1: To install Hadoop and Hive in Windows 7. check out the softwares that we need for
Hadoop installation
Step 2: Supported Windows OSs: Hadoop supports Windows Server 2008 and Windows Server
2008 R2, Windows Vista and Windows 7. For installation purposes we are going to make
use of Windows 7 Professional Edition, SP 1.
Step 3: Microsoft Windows SDK: Download and install Microsoft Windows SDK v7.1 to get
the tools, compilers, headers and libraries that are necessary to run Hadoop.
Step 4: Cygwin: Download and install Unix command-line tool Cygwin to run the unix commands
on Windows as per your 32-bit or 64-bit windows. Cygwin is a distribution of popular GNU
and other Open Source tools running on Microsoft Windows.
Step 5: Maven: Download and install Maven 3.1.1.
The installation of Apache Maven is a simple process of extracting the archive and
adding the bin folder with the mvn command to the PATH. Open a new command prompt
and run
“mvn -v” command to verify the installation as below –
14
Step 6 : Protocol Buffers 2.5.0: Download Google’s Protocol Buffers 2.5.0
Download Protocol Buffers 2.5.0 and extract to a folder in C drive. Version should
strictly be 2.5.0 for installing Hive.
Step 8 : Go to My Computer > right-click > Properties > Advanced System settings > System
Properties > Advanced tab > Environment Variables button. Click on ‘New…’ button under
System Variables and add –
15
Variable Value: C:\Program Files\Java\jdk1.7.0_51
Note: Edit the Path environment variable very carefully. Select the whole path and go to
the end to append the new environment variables. Deleting any path variable may lead to
non functioning of some programs.
Adding to PATH:
Add the unpacked distribution’s bin directory to your user PATH environment variable by
opening up the
Step 9 : My Computer > right-click > Properties > Advanced System settings > System
Properties > Advanced tab > Environment Variables button, then adding or selecting the
PATH variable in the ‘System variables’ with the value C:\Program Files\apache-maven-
3.3.9\bin
16
17
Official Apache Hadoop releases do not include Windows binaries, so you have to
download sources and build a Windows package yourself.
Download Hadoop sources tarball hadoop-2.6.4-src.tar.gz and extract to a folder having
short path (say C:\hdp) to avoid runtime problem due to maximum path length limitation
in Windows.
Note: Do not use the Hadoop binary, as it is bereft of Windowsutils.exe and some
Hadoop.dll files. Native IO is mandatory on Windows and without it the Hadoop
installation will not work on Windows. Instead, build from the source code using Maven.
It will download all the required components.
Step 11: For building Hadoop from Native IO Source code,
1. Extract hadoop-2.2.0.tar.gz to a folder (say C:\hdp)
2. Add Environment Variable HADOOP_HOME=“C:\hdp\hadoop-2.6.4-src” and edit
Path variable to add bin directory of HADOOP_HOME, e.g. C:\hdp\hadoop-
2.6.4src\bin.
Step 12: Before moving to the next step make sure you have the following variables set in your
Environment variables window.
JAVA_HOME = C:\hdp\Java\jdk1.7.0_65
PATH = C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program
Files\CMake\bin;C:\protoc-2.5.0-win32;C:\Program Files\apache-maven-
3. 3.9\bin;C:\cygwin64\bin;C:\cygwin64\usr\sbin;%JAVA_HOME%\bin;C:\WINDOWS\
system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\system32\
Wi ndowsPowerShell\v1.0;
18
Note: If the JAVA_HOME environment variable is set improperly, Hadoop will not run. Set
environment variables properly for JDK, Maven, Cygwin and Protocol-buffer. If you still get a
‘JAVA_HOME not set properly’ error, then edit the”C:\hadoop\bin\hadoopenv.cmd” file, locate
‘set JAVA_HOME =’ and provide the JDK path (with no spaces). Step 13: Running Maven
Package
Select Start –> All Programs –> Microsoft Windows SDK v7.1(as an administrator) and
open Windows SDK 7.1 Command Prompt. Change directory to Hadoop source code
folder (C:\hdp\hadoop-2.6.4-src).
Execute maven package with options -Pdist, native-win -DskipTests -Dtar to create
Windows binary tar distribution by executing the below command. mvn package -
Pdist,native-win -DskipTests -Dtar
You will get a long list of commands running for some time while the build process is
running. When build will be successful, you will get the screen like below –
19
If everything goes well in the previous step, then the native distribution
hadoop2.2.0.tar.gz will be created inside C:\hdp\hadoop-dist\target\hadoop-2.6.4
directory.
Extract the newly created Hadoop Windows package to the directory of choice (eg.
C:\hdp\hadoop-2.6.4)
set HADOOP_PREFIX=C:\hdp\hadoop-2.6.4
set HADOOP_CONF_DIR=%HADOOP_PREFIX%\etc\hadoop
set YARN_CONF_DIR=%HADOOP_CONF_DIR%
set PATH=%PATH%;%HADOOP_PREFIX%\bin
<configuration>
<property>
<name>fs.default.name</name>
<value>hdfs://0.0.0.0:19000</value>
</property>
20
</configuration>
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
localhost
Create C:\tmp directory as the default configuration puts HDFS metadata and data files
under \tmp on the current drive.
<configuration>
<property>
<name>mapreduce.job.user.name</name>
21
<value>%USERNAME%</value>
</property>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<property>
<name>yarn.apps.stagingDir</name>
<value>/user/%USERNAME%/staging</value>
</property>
<property>
<name>mapreduce.jobtracker.address</name>
<value>local</value>
</property>
</configuration>
yarn.server.resourcemanager.address
22
0.0.0.0:8020
yarn.server.resourcemanager.application.expiry.interval
60000
yarn.server.nodemanager.address
0.0.0.0:45454
yarn.nodemanager.aux-services
mapreduce_shuffle
yarn.nodemanager.aux-services.mapreduce.shuffle.class
org.apache.hadoop.mapred.ShuffleHandler
yarn.server.nodemanager.remote-app-log-dir
/app-logs
yarn.nodemanager.log-dirs
/dep/logs/userlogs
yarn.server.mapreduce-appmanager.attempt-listener.bindAddress
0.0.0.0
yarn.server.mapreduce-appmanager.client-service.bindAddress
0.0.0.0
23
yarn.log-aggregation-enable
true
yarn.log-aggregation.retain-seconds
-1
yarn.application.classpath
%HADOOP_CONF_DIR%,%HADOOP_COMMON_HOME%/share/hadoop/common/*,%HA
DOOP_COMMON_HOME%/share/hadoop/common/lib/*,%HADOOP_HDFS_HOME%/share/
hadoop/hdfs/*,%HADOOP_HDFS_HOME%/share/hadoop/hdfs/lib/*,%HADOOP_MAPRED_
HOME%/share/hadoop/mapreduce/*,%HADOOP_MAPRED_HOME%/share/hadoop/mapreduc
e/lib/*,%HADOOP_YARN_HOME%/share/hadoop/yarn/*,%HADOOP_YARN_HOME%/shar
e/hadoop/yarn/lib/*
Since Hadoop doesn’t recognize JAVA_HOME from “Environment Variables” (and has problems
with spaces in pathnames),
set JAVA_HOME=C:\hdp\Java\jdk1.7.0_65
c. Initialize Environment Variables by running cmd in “Administrator Mode”, moving to path
C:\hdp\hadoop-2.6.4\etc\hadoop and executing:
C:\hdp\hadoop-2.6.4\etc\hadoop\>hadoop-env.cmd
3. Format the FileSystem – From command prompt, go to path C:\hdp\hadoop-2.6.4\bin\ and run
C:\hdp\hadoop-2.6.4\sbin\>start-dfs.cmd
24
Two separate Command Prompt windows will open automatically to run Namenode and
Datanode
C:\hdp\hadoop-2.6.4\sbin\>start-yarn.cmd
Two command prompts will open, named yarn nodemanager and yarn resourcemanager
after executing the above command.
25
6. Run an example YARN job – Execute the below commands altogether in command prompt
and check the results. It should show you Hadoop license file opened in notepad.
26
27
When the pages open as shown in the above screens, we can conclude that Hadoop is
successfully installed.
CONFIGURING HIVE
Now as installation of hadoop is done, we will progress to install HIVE now.
Pre-requisites for HIVE installation are –
– Java 1.7 (preferred)
– Hadoop 2.x (preferred), 1.x (not supported by Hive 2.0.0 onward)
Step 1: Verifying JAVA Installation
Open command prompt as an administrator and write command as –
> java –version
It should show appropriate version of Java.
Step 2: Verifying Hadoop Installation
> hadoop version
It should show appropriate hadoop version.
Step 3: Downloading Hive
Download any binary for hive from any source available on Internet. You can
use https://redrockdigimark.com/apachemirror/hive/hive-2.1.1/ and download the
binary apache-hive-2.1.1-bin.tar.g z
Step 4: Extracting and Configuring Hive
28
Paste the downloaded HIVE tar file in C drive, e.g, C:\hive\apache-hive-2.1.1-bin and
extract the files there.
Setting Environment variable –
Go to Control Panel > Advanced System Properties > System Properties > Environment
Variables and add a new environment variable named HIVE_HOME. Edit the path to
“C:\hive\apache-hive-2.1.1-bin”
Now in the PATH environment variable, append %HIVE_HOME%\bin and SAVE.
And in CLASSPATH variable, append “C:\hive\apache-hive-2.1.1-bin\lib” and
“C:\hdp\hadoop-2.6.4\share\hadoop\common\lib” paths.
Step 5: Installing MySQL and configuring HIVE
1. Install and start MySQL if you have not already done so.
2. Configure the MySQL Service and Connector
Download mysql-connector-java-5.0.5.jar file and copy the jar file to
%HIVE_HOME%/lib directory.
3. Create the Database and User
Go to MySQL command-line and execute the below commands –
First, Create a metastore_db database in MySQL database using root user –
$ mysql -u root -p Enter password: mysql> CREATE DATABASE
metastore_db;
Create a User [hiveuser] in MySQL database using root user. Let’s take hiveuser as
‘userhive’ and hivepassword as ‘hivepwd’
mysql> CREATE USER 'hiveuser'@'%' IDENTIFIED BY 'hivepassword'; mysql>
GRANT all on *.* to 'hiveuser'@localhost identified by 'hivepassword';
29
Edit hive-site.xml file in %HIVE_HOME%/conf directory and add the
following configurations: <configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost:3306/metastore_db?createDatabaseIfNotExist=true</value
>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>userhive</value>
</property>
30
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>hivepwd</value>
</property>
<property>
<name>hive.metastore.uris</name>
<description>Thrift URI for the remote metastore. Used by metastore client to connect to
remote metastore.</description>
</property>
</configuration>
Step 6: Launch HIVE
On successful installation of Hive, you get to see the following response-
31
Hive console:
hive> create table hivetesting(id string);
MySql console:
There are 2 ways to access metastore_db
mysql -u root -p
Enter password:
mysql> use metastore_db;
mysql> show tables ;
From command prompt,
mysql -u <hiveusername>
-p<hivepassword> <Database name>
E.g. >mysql -u userhive -phivepwd metastore_db
32
Enter password:
mysql> show tables;
On your MySQL database you will see the names of your Hive tables.
Step 7: Verifying HIVE installation
The following sample command is executed to display all the tables:
hive> show tables;
OK
Time taken: 2.798 seconds hive>
33
> RENAME TO PARTITION (Yoj=’1203’);
HiveQL - Select-Where
+------+--------------+-------------+-------------------+--------+
| ID | Name | Salary | Designation | Dept |
+------+--------------+-------------+-------------------+--------+
|1205 | Kranthi | 30000 | Op Admin | Admin |
|1204 | Krian | 40000 | Hr Admin | HR |
|1202 | Manisha | 45000 | Proofreader | PR |
|1201 | Gopal | 45000 | Technical manager | TP |
|1203 | Masthanvali | 40000 | Technical writer | TP |
+------+--------------+-------------+-------------------+--------+
HiveQL - Select-Group By
34
|TP | 3 |
+------+--------------+
HiveQL - Select-Joins
The following query executes JOIN on the CUSTOMER and ORDER tables, and retrieves the
records:
The following query demonstrates LEFT OUTER JOIN between CUSTOMER and ORDER
tables:
35
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | NULL | NULL |
| 6 | Komal | NULL | NULL |
| 7 | Muffy | NULL | NULL |
+----+----------+--------+---------------------+
The following query demonstrates RIGHT OUTER JOIN between the CUSTOMER and
ORDER tables.
hive> SELECT c.ID, c.NAME, o.AMOUNT, o.DATE FROM CUSTOMERS c RIGHT OUTER
JOIN ORDERS o ON (c.ID = o.CUSTOMER_ID);
+------+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+------+----------+--------+---------------------+
|3 | kaushik | 3000 | 2009-10-08 00:00:00 |
|3 | kaushik | 1500 | 2009-10-08 00:00:00 |
|2 | Khilan | 1560 | 2009-11-20 00:00:00 |
|4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
+------+----------+--------+---------------------+
The following query demonstrates FULL OUTER JOIN between CUSTOMER and ORDER
tables:
FROM CUSTOMERS c
FULL OUTER JOIN ORDERS o
ON (c.ID = o.CUSTOMER_ID);
Result:
Thus the program for creating database operations and data type using HIVE is created
successfully.
36
EX.NO. : 1 D) NOSQL EXERCISES
DATE: ORIENTDB GRAPH DATABASE – ORIENTDB
FEATURES
Aim : To create program for orientDB graph database and OrientDB features
Algorithm :
OrientDB is the first multi-model open source NoSQL DBMS that combines the traditional
DBMS features with the power of Graph and the flexibility of Document. It is capable of
bringing high-performance operational into a scalable database.
Download the latest version of OrientDB from this link and unzip it. Now, you have to
navigate into the directory we previously unpacked and run the following command to
start OrientDB Server:
cd orientdb-3.0.4/
cd bin/
./server.sh
37
This will start OrientDB server.
Step 2: If it is the first time starting the server, you will be asked to enter a root password. Once
you have done that, your OrientDB service will be running. That way, you can open the
UI using the link that the server gives you.
Log in with the username root and the password you configured earlier.
38
Step 3: Click on NEW DB and enter “Music” on the database name. Then, click on the CREATE
DATABASE button. Looks nice, but, how can I connect to the DB and manipulate it?
Step 4: Open your favorite IDE capable of supporting Java and Maven, in this example, we will
39
use IntelliJ.
Step 5: To create a new project we have to choose the menu File → New → Project…
Step 6: Select Maven, and now click on Next. Select your GroupId and ArtifactId, and then select
40
Step 7: Everything looks fine over here. Now, let’s click on the ‘Finish’ button.
Step 8: Open the pom.xml file and add the following dependencies, finally enable Java 8 as a
target compiler.
Step 9: Now we will create a simple application with a few steps. To do this, we have to open the
folder src → main → java.
41
Step 10 : Right-click on the folder java → New → Java Class, and write main.
db.close();
orient.close();
Step 12 : Creating the schema
To stick with the music theme (db name), we want to create a graph of singers who have
songs and the year they released that specific song.
42
Vertex
Edge
Step 13: To do this, we create a schema with 3 classes: Singer, Year and Song.
private static void createSchema(ODatabaseSession db) {
singer = db.createVertexClass("Singer");
} if (year == null) {
year = db.createVertexClass("Year");
} if (song == null) {
song = db.createVertexClass("Song");
} if (singer.getProperty("name") == null) {
43
singer.createProperty("name", OType.STRING);
singer.createIndex("Song_name_index", OClass.INDEX_TYPE.NOTUNIQUE,
"name");
} if (year.getProperty("year") == null) {
year.createProperty("year", OType.STRING);
year.createIndex("Song_year_index", OClass.INDEX_TYPE.NOTUNIQUE,
"year");
} if (song.getProperty("title") == null) {
song.createProperty("title", OType.STRING);
song.createIndex("Song_title_index", OClass.INDEX_TYPE.NOTUNIQUE,
"title");
} if (db.getClass("SongBy") == null) {
db.createEdgeClass("SongBy");
} if (db.getClass("ReleaseYear") == null) {
db.createEdgeClass("ReleaseYear");
}}
Step 14 : Now, we have to create one method for each vertex. The way of doing it is by setting
the name of our vertex and the properties.
result.setProperty("name", name);
44
result.setProperty("surname", surname);
result.save();
return result;
result2.setProperty("year", year);
result2.save();
return result2;
result3.setProperty("title", title);
result3.save();
return result3;
It’s time to insert data, so let’s create some vertex and edges in the database.
private static void createSongList(ODatabaseSession db) {
45
OVertex song = createSong(db, "Love me right");
edge1.save();
edge3.save();
edge4.save();}
Step 16 : And maybe a query?
private static void executeAQuery(ODatabaseSession db) {
} rs.close();
46
And that’s all! You’ve made your first Java application. Let’s RUN our project, menu Run→ Run
‘main’.
If we double-click the circle where the Year is, it will expand. So, let’s do that with every vertex
that appears to finally have something like this:
47
Result:
Thus the program for creating OrientDB Graph is created successfully.
48
EX.NO. : 2 MYSQL DATABASE CREATION, TABLE
DATE:
CREATION, QUERY
49
Example: SQL>INSERT INTO student VALUES (1,’Ravi’,’M.Tech’,’Palakol’);
50
1 Creating and Selecting a Database
mysql> CREATE DATABASE menagerie;
mysql> USE menagerie Database changed
2 Creating a Table
mysql> SHOW TABLES;
Empty set (0.00 sec)
mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
species VARCHAR(20), sex CHAR(1), birth DATE, death DATE); mysql>
SHOW TABLES;
+---------------------+
| Tables in menagerie |
+---------------------+
| pet |
+---------------------+
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | NULL | ||
| owner | varchar(20) | YES | NULL | ||
| species | varchar(20) | YES | NULL | ||
| sex | char(1) | YES | NULL | ||
| birth | date | YES | NULL | ||
| death | date | YES | NULL | ||
+---------+-------------+------+-----+---------+-------+
3 Loading Data into a Table
51
(1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),
(3,'C',1.69),(3,'D',1.25),(4,'D',19.95);
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 1 |A | 3.45 |
| 1|B | 3.99 |
| 2 |A | 10.99 |
| 3|B | 1.45 |
| 3|C | 1.69 |
| 3|D | 1.25 |
| 4|D | 19.95 |
+---------+--------+-------+
SELECT article, dealer, price
FROM shop
WHERE price=(SELECT MAX(price) FROM shop);
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0004 | D | 19.95 |
+---------+--------+-------+
SELECT s1.article, s1.dealer, s1.price
FROM shop s1
LEFT JOIN shop s2 ON s1.price < s2.price
WHERE s2.article IS NULL;
52
ORDER BY price DESC
LIMIT 1;
+---------+-------+
| article | price |
+---------+-------+
| 0001 | 3.99 |
| 0002 | 10.99 |
| 0003 | 1.69 |
| 0004 | 19.95 |
+---------+-------+
SELECT article, dealer, price
FROM shop s1
WHERE price=(SELECT MAX(s2.price)
FROM shop s2
WHERE s1.article = s2.article) ORDER BY article;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | C | 1.69 |
| 0004 | D | 19.95 |
+---------+--------+-------+
53
CREATE TABLE person ( id SMALLINT UNSIGNED NOT
NULL AUTO_INCREMENT, name CHAR(60) NOT NULL,
PRIMARY KEY (id)
);
54
+----+---------------------+
| id | name |
+----+---------------------+
| 1 | Antonio Paz |
| 2 | Lilliana Angelovska |
+----+---------------------+
SELECT * FROM shirt;
+----+---------+--------+-------+
| id | style | color | owner |
+----+---------+--------+-------+
| 1 | polo | blue | 1|
| 2 | dress | white | 1|
| 3 | t-shirt | blue | 1|
| 4 | dress | orange | 2|
| 5 | polo | red | 2|
| 6 | dress | blue | 2|
| 7 | t-shirt | white | 2|
+----+---------+--------+-------+
SELECT s.* FROM person p INNER JOIN shirt s
ON s.owner = p.id
WHERE p.name LIKE 'Lilliana%'
AND s.color <> 'white';
+----+-------+--------+-------+
| id | style | color | owner |
+----+-------+--------+-------+
| 4 | dress | orange | 2|
| 5 | polo | red | 2|
| 6 | dress | blue | 2|
+----+-------+--------+--------+
Result :
Thus the database, table and queries are created successfully.
55
EX.NO. : 3
chains of read slaves, backup databases at a remote site or any combination of these. This
post focuses on a simple single master to single slave topology – the more complex
This post also makes the assumption that the 2 MySQL Servers have been installed but that
there is no existing data in the master that needs to be copied to the slave – it’s not complex
Server “black” (192.168.0.31) is to be our master and “blue” (192.168.0.34) the slave.
Algorithm:
Step 1: Edit the configuration files & start the MySQL Servers
The first step in setting up replication involves editing the “my.cnf” file on the servers
that will serve as the master and slave. A default is provided with the MySQL installation
but in case there is already a production MySQL database running on these servers, we
provide local configuration files “master.cnf” and “slave.cnf” that will be used when
At a minimum we’ll want to add two options to the [mysqld] section of the master.cnf file:
unless binary logging is enabled. The server_id variable must be a positive integer value
56
between 1 to 2^32 master.cnf: [mysqld] server-id=1 log-bin=black-bin.log
Note: For the greatest possible durability and consistency in a replication setup using
Next, you’ll need to add the server-id option to the [mysqld] section of the slave’s slave.cnf
file. The server-id value, like the master_id value, must be a positive integer between 1 to
2^32, It is also necessary that the ID of the slave be different from the ID of the master. If
you are setting up multiple slaves, each one must have a unique server-id value that differs
from that of the master and from each of the other slaves.
slave.cnf:
[mysqld]
server-id=2
relay-log-
index=slave
-relay-
bin.index
relay-
log=slave-
relay-bin
datadir=/ho
me/billy/m
57
ysql/slave/d
ata
Now, start the MySQL servers using the service manager or directly from the command line if
Create an account on the master server that the slave server can use to connect. This account
must be given the REPLICATION SLAVE privilege: [billy@black ~]$ mysql -u root --
IDENTIFIED BY 'billy';
We are now ready to initialize replication on the slave; issue a CHANGE MASTER command:
-> MASTER_USER='repl_user',
-> MASTER_PASSWORD='billy',
-> MASTER_LOG_FILE='',
-> MASTER_LOG_POS=4;
Where:
192.168.0.31
58
▪ MASTER_USER: this is the user we granted the REPLICATION SLAVE privilege to in
Now we are ready to perform a basic check to ensure that replication is indeed working. In this
example we insert a row of data into the “simples” table on the master server and then verify that
master> create database clusterdb; master> create table clusterdb.simples (id int not
+-----+
| id |
+-----+
| 1|
59
| 2|
| 3|
| 999 |
+-----+
60
EX.NO. : 4
Algorithm :
Step 1 : The specification describes a set of SQL geometry types, as well as functions on those
MySQL spatial extensions enable the generation, storage, and analysis of geographic features:
MySQL has data types that correspond to OpenGIS classes. Some of these types hold single
geometry values:
● GEOMETRY
● POINT
● LINESTRING
● POLYGON
● MULTIPOINT
● MULTILINESTRING
61
● MULTIPOLYGON
● GEOMETRYCOLLECTION
Geometry Type
Geometry is a word that denotes a geographic feature. Originally the word geometry meant
measurement of the earth. Another meaning comes from cartography, referring to the geometric
features that cartographers use to map the world. It is a noninstantiable class but has a number of
properties, given below are common to all geometry values created from any of the Geometry
subclasses.
Example
Use the CREATE TABLE statement to create a table with a spatial column:
CREATE TABLE geotest (code int(5),descrip varchar(50), g GEOMETRY);
Copy
Sample Output:
Use the ALTER TABLE statement to add or drop a spatial column to or from an existing table:
62
ALTER TABLE geotest ADD pt_loca POINT;
Point Type
Usage of Point
Point Properties
● X-coordinate value.
● Y-coordinate value.
● Point is defined as a zero-dimensional geometry.
● The boundary of a Point is the empty set.
Example
63
+---------------------------------+
1 row in set (0.00 sec)
Curve Type
Curve Properties
LineString Type
Usage of LineString
LineString Properties
Example
64
MySQL> INSERT INTO geotest VALUES (123,"Test Data",GeomFromText(@g)); Query
OK, 1 row affected (0.00 sec)
Surface Type
Surface Properties
Polygon Type
Usage of Polygon
The Polygon objects could represent districts, blocks and so on from a state map.
Polygon Assertions
● The boundary of a Polygon consists of a set of LinearRing objects (that is, LineString
objects that are both simple and closed) that make up its exterior and interior boundaries.
● A Polygon has no rings that cross. The rings in the boundary of a Polygon may intersect
at a Point, but only as a tangent.
● A Polygon has no lines, spikes, or punctures.
● A Polygon has an interior that is a connected point set.
● A Polygon may have holes. The exterior of a Polygon with holes is not connected. Each
hole defines a connected component of the exterior.
65
Example
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)
All the elements in a GeometryCollection must be in the same Spatial Reference System. There
are no other constraints on the elements of a GeometryCollection, although the subclasses of
GeometryCollection described in the following sections may restrict membership. Restrictions
may be based on:
● Element type (for example, a MultiPoint may contain only Point elements)
● Dimension
● Constraints on the degree of spatial overlap between elements
Example
A MultiPoint is a geometry collection composed of Point elements. The points are not connected
or ordered in any way.
Usage of MultiPoint
MultiPoint Properties
66
● A MultiPoint is a zero-dimensional geometry.
● A MultiPoint is simple if no two of its Point values are equal (have identical coordinate
values).
● The boundary of a MultiPoint is the empty set.
MultiCurve Type
MultiCurve Properties
Usage of MultiLineString
Example
67
MySQL> SET @g ='MULTILINESTRING((12 12, 22 22), (19 19, 32 18))'; Query
OK, 0 rows affected (0.00 sec)
MultiSurface Assertions
MultiPolygon Type
MultiPolygon Assertions
MultiPolygon Properties
68
● Each Curve in the boundary of the MultiPolygon is in the boundary of exactly one
Polygon element.
● Every Curve in the boundary of a Polygon element is in the boundary of the
MultiPolygon.
Example
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)
Result :
Thus the spatial data storage and retrieval using MySQL created successfully.
69
EX.NO. : 5
Aim :
To create a temporal data storage and retrieve data in MySQL.
Algorithm:
Step 1: The MySQL TIMESTAMP is a temporal data type that holds the combination of date
and time. The format of a TIMESTAMP is YYYY-MM-DD HH:MM:SS which is fixed at
19 characters.
Step 2: The TIMESTAMP value has a range from '1970-01-01 00:00:01' UTC to '2038-01-19
03:14:07' UTC
Step 3: First, created a new table named test_timestamp that has a TIMESTAMP column: t1;
70
Second, inserts a new row into the categories table without specifying the value for
VALUES ('A');
UPDATE CURRENT_TIMESTAMP;
VALUES('B');
71
Code language: SQL (Structured Query Language) (sql)
The default value of the column created_at is the timestamp when the row was inserted.
UPDATE categories
WHERE id = 2;
Seventh, query data from the categories table to check the update:
SELECT *
FROM categories
WHERE id = 2;
72
Note that if you execute the UPDATE statement to update the same value for the name column,
UPDATE categories
WHERE id = 2;
Result
Thus the program is executed successfully.
73
EX.NO. : 6
Aim :
To create an object storage and retrieve data in MySQL.
Algorithm:
STEP1: Start the program
STEP2: open my sql workbench
STEP3: Create a function hello with (s CHAR(20))
STEP4: SET RETURN CHAR (50)
The function must be set to DETERMINISTIC
STEP5: Return the concat value
STEP6: Returning the created object SELECT
hello('world');
STEP7: Stop the program
Source Code:
CREATE FUNCTION hello (s CHAR(20))
RETURNS CHAR(50) DETERMINISTIC
RETURN CONCAT('Hello, ',s,'!');
SELECT hello('world');
Output:
+----------------+
| hello('world') |
+----------------+
| Hello, world! |
+----------------+
Result :
Thus the program To create an object storage and retrieve data in MySQL executed
successfully.
74
EX.NO. : 7
Aim :
To create XML database, table and XQuery FLOWR expression.
Algorithm :
ooks.xml books.xml
<books>
<book category="JAVA">
<author>Robert</author>
<year>2005</year>
<price>30.00</price>
</book>
75
<book category="DOTNET">
<author>Peter</author>
<year>2011</year>
<price>70.50</price>
</book>
<book category="XML">
<author>Robert</author>
<author>Peter</author>
<year>2013</year>
<price>50.00</price>
</book>
<book category="XML">
<author>Jay Ban</author>
<year>2010</year>
<price>16.50</price>
</book>
</books>
76
Given below is the Xquery expression that is to be executed on the above XML document.
<table><tr><th>Title</th><th>Price</th></tr>
<tr><td>{data($x/title)}</td><td>{data($x/price)}</td></tr>
</table>
</results>
Result
<table>
<tr>
<th>Title</th>
<th>Price</th>
</tr>
<tr>
<td>16.50</td>
</tr>
<tr>
<td>30.00</td>
</tr>
<tr>
77
<td>Learn XQuery in 24 hours</td>
<td>50.00</td>
</tr>
<tr>
<td>70.50</td>
</tr>
</table>
Given below is the Xquery expression that is to be executed on the above XML document.
books.xqy let $books :=
(doc("books.xml")/books/book) return
<table><tr><th>Title</th><th>Price</th></tr>
{
for $x in $books
order by $x/price
return <tr><td>{data($x/title)}</td><td>{data($x/price)}</td></tr> }
</table>
</results>
Result
<table>
<tr>
<th>Title</th>
<th>Price</th>
</tr>
<tr>
<td>Learn XPath in 24 hours</td>
<td>16.50</td>
78
</tr>
<tr>
<td>Learn Java in 24 Hours</td>
<td>30.00</td>
</tr>
<tr>
<td>Learn XQuery in 24 hours</td>
<td>50.00</td>
</tr>
<tr>
<td>Learn .Net in 24 hours</td>
<td>70.50</td>
</tr>
</table>
Result :
Thus the program executed sucessfully.
79
EX.NO. : 8
Algorithm :
Step 1 : To connect an Android app to a MySQL database is configuring the backend.
Step 2 : Click into Webadmin, and log in with the username ‘root’, and the password that you set
Step 3: After logging in, navigate to the top toolbar -> servers -> MySQL Database Server.
Step 4: We are going to want to create a new database, so go ahead and click ‘Create a New
Database’ on the top module. Here we are presented with a fresh database to configure.
Step 5: Click create, and the database will appear under the list of databases. Now click into the
database, and you will be able to navigate into the table that was created as well. Step 6: Click
view data, and then add row. We will leave the id field blank, and make up a name and
a price.
Step 7: Finally, add as many rows as you would like, all of which will be displayed in our
Android app.
80
Configuring the Web API
81
The next step in learning how to connect an Android app to a MySQL database is actually pulling
our data. To do this, we will write a simple PhP script.
Go ahead and open your favorite text or code editor and add the following code:
<?php
// Create connection
$con=mysqli_connect("localhost","username","password","dbname");
// Check connection if
(mysqli_connect_errno())
{ echo "Failed to connect to MySQL: " .
mysqli_connect_error();
}
82
// Encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($con);
?> ake sure on line 4 the parameters match your configuration. Username will be ‘root’,
password will be the MySQL password you set when deploying your VPS, and ‘dbname’ will
be whatever you named your database. Save the file as “stock_service.php”.
This script will create a database connection, select all rows from the table we specified, and then
output any results as a JSON array. It now needs to be added to our web server in order for our
app to access it.
Navigate to the top toolbar again, tools -> File Manager. Using the top toolbar on the page, create
a new directory, and name it ‘api’.
Click into the new ‘api’ directory and upload the ‘stock_service.php’ file. The script can now be
called by its address on the web. If you want to test your script, navigate to
‘http://your_vps_ip_address/api/stock_service.php’, of course replacing ‘your_vps_ip_address’
with the IP of your VPS. If all goes well, you should see something like:
83
This is the JSON array that the script outputs at the very end. Now we just need to build our
Android app which will pull this data and display it in a much more aesthetically pleasing format.
How to Make an Android App to Display the Data Open Android Studio and create a new project.
Name it what you like and select empty activity. The first thing we need to do is allow our app to
access files on the internet.
Navigate to the AndroidManifest.xml file and make sure it has the internet user permission:
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"> <activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
84
Next, let’s configure the UI for our app. Open the activity_main.xml file and add the following
code:
85
android.widget.ArrayAdapter; import
android.widget.ListView; import
android.widget.Toast; import org.json.JSONArray;
import org.json.JSONException; import
org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
ListView listView;
@Override protected
void onPreExecute() {
super.onPreExecute();
}
86
@Override protected void
onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
try { loadIntoListView(s); } catch (JSONException e) {
e.printStackTrace();
}
}
87
JSONArray jsonArray = new JSONArray(json); String[]
stocks = new String[jsonArray.length()]; for (int i = 0; i <
jsonArray.length(); i++) { JSONObject obj =
jsonArray.getJSONObject(i); stocks[i] = obj.getString("name")
+ " " + obj.getString("price");
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, stocks);
listView.setAdapter(arrayAdapter);
}
}
Build and run your app, and you should see all the data from your MySQL database
OUTPUT
Result:
Thus the program for mobile database using mysql is executed successfully.
88