Backend SQL - Getting Started
Backend SQL - Getting Started
Date: 19/03/24
Roll No. and Name: 22BCE305 ROSHNI PANKAJKUMAR RANA
Course Code and Name: 2CS201 FULL STACK WEB DEVELOPMENT
Practical-8:
1. Create database
p8-1.js
host: "localhost",
user: "root",
password: "Roshni123"
});
con.connect(function(err) {
console.log("Connected!");
});
});
Output
2. Create a table
p8-2.js
var mysql = require('mysql2');
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "CREATE TABLE employees (EID INT, Name
VARCHAR(25), Department VARCHAR(25))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
});
Output
3. Insert a record
p8-3.js
var mysql = require('mysql2');
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE database1", function
(err, result) {
if (err) throw err;
console.log("Database created");
});
});
Output
4. Update a record
p8-4.js
var mysql = require('mysql2');
//updating
con.connect(function(err) {
if (err) throw err;
var sql = "UPDATE employees SET Department =
'Sales' WHERE Name = 'Mitalee'";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result.affectedRows + " record(s)
updated");
});
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM employees", function
(err, result, fields) {
if (err) throw err;
console.log("Records after updating");
console.log(result);
});
});
Output