By Team Clofus Innovations | Mon Jan 17 2022
MySQL is the most popular Open Source Relational SQL database management system. MySQL is one of the best RDBMS being used for developing web-based software applications.
Before we proceed to explain MySQL database system, let’s revise few definitions related to database.
Database: A database is a collection of tables, with related data.
Table: A table is a matrix with data. A table in a database looks like a simple spreadsheet.
Column: One column contains data of one and the same kind, for example the column postcode.
Row: A row is a group of related data, for example the data of one subscription.
Primary Key: A primary key is unique. A key value can not occur twice in one table. With a key, you can find at most one row.
Foreign Key: A foreign key is the linking pin between two tables.
[root@host]# rpm -i MySQL-client-5.0.9-0.i386.rpm
[root@host]# rpm -i MySQL-level-5.0.9-0.i386.rpm
[root@host]# rpm -i MySQL-shared-5.0.9-0.i386.rpm
[root@host]# rpm -i MySQL-bench-5.0.9-0.i386.rpm
Basic Queries are
Creating separate space in MySQL.
create database <DB_Name>;
To use the created Database
use <DB_Name>;
Creating table in Database
create table <TableName>(Column1 Datatype,Column2 Datatype,...);
Insert data in created table
insert into <TableName>
values('string_data',integer_data,....);
To retrieve data from table.
select * from <TableName>; (or) select
<column_name> from <TableName>;
select * from <TableName> where
<column_name>='<user_data'>;
Create view table form existing table.
create view <ViewTableName> as
select <Column1>, <Column1> from <OldTableName>;
To delete data or table
delete table <TableName>;
delete table <TableName> where <ColumnName>="
<userdata>"
Avg
The AVG function calculates the average value of a set of values. It ignores NULL values in the calculation.
select AVG(<ColumnName) from <TableName>;
Count
The COUNT function returns the number of the rows in a table.
select COUNT(*) from <TableName> (or) select
COUNT(<columnName>) from <TableName>;
Sum
The SUM function returns the sum of a set of values.
select SUM(<ColumnName>) from <TableName>;
Min
The MIN function returns the minimum value in a set of values.
select MIN(<ColumnName>) from <TableName>;
Max
The MAX function returns the maximum value in a set of values.
select MAX(<ColumnName>) from <TableName>;
Example:
nodejs with MySQL
var express = require("express");
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'address_book'
});
var app = express();
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} else {
console.log("Error connecting database ... nn");
}
});
app.get("/",function(req,res){
var query="select * from register where
urname='"+req.body.urname+"' and
pw='"+req.body.pw+"'";
connection.query(query, function(err,
rows, fields) {
connection.end();
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.');
});
});
app.get("/",function(req,res){
connection.query('insert into user values('+1+','+sathiya+','+sathiya@gmail.com+','+sathiya2901+')', function(err, rows, fields) {
connection.end();
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.');
});
});
app.listen(3000);