Internet Based Programming: Backend Web Development
Internet Based Programming: Backend Web Development
05/25/2025 2
1
05/25/2025
05/25/2025 3
05/25/2025 4
2
05/25/2025
• Go to MySQL workbench and apply the following query and run it:
• ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password’
Replace 'password with your Database password.
05/25/2025 5
05/25/2025 6
3
05/25/2025
05/25/2025 7
05/25/2025 8
4
05/25/2025
Backend web development- MySQL: CREATE TABLE: Primary Key: Using Alter
Table
• If the table already exists, use the “ALTER TABLE” keyword:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root", MSQL
password: "root", Workbench
database: "mydb"
});
con.connect(function (err) {
if (err) throw err;
console.log("Connected!");
var sql = "ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table altered");
});
});
05/25/2025 9
05/25/2025 10
5
05/25/2025
Backend web development- MySQL: INSERT Data INTO a Table: Multiple Records
• To insert more than one record, make an array containing the values, and insert a question mark in
the sql, which will be replaced by the value array: INSERT INTO customers (name, address) VALUES ?
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO customers (name, address) VALUES ?";
var values = [
['John', 'Highway 71'],
['Peter', 'Lowstreet 4'],
['Amy', 'Apple st 652'],
['Sandy', 'Ocean blvd 2'],
];
con.query(sql, [values], function (err, result) {
if (err) throw err;
console.log("Number of records inserted: " + result.affectedRows);
});
});
05/25/2025 11
Backend web development- MySQL: INSERT Data INTO a Table: The Result
Object
05/25/2025 12
6
05/25/2025
05/25/2025 13
• Selecting Columns: To select only some of the columns in a table, use the "SELECT"
statement followed by the column name.
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "mydb"
});
con.connect(function (err) {
if (err) throw err;
con.query("SELECT name, address FROM customers",
function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
05/25/2025 14
7
05/25/2025
• With “SELECT” query, the result object is an array containing each row as an object.
• You can display a specific record or a specific filed from specific recorded.
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "mydb"
});
con.connect(function (err) {
if (err) throw err;
con.query("SELECT * FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result[0]);
console.log(result[0].address);
});
});
05/25/2025 15
05/25/2025 16
8
05/25/2025
• When selecting records from a table, you can filter the selection by using the "WHERE"
statement.
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "mydb"
});
con.connect(function (err) {
if (err) throw err;
con.query("SELECT * FROM customers WHERE address = 'Highway 71'", function (err, result) {
if (err) throw err;
console.log(result);
});
});
05/25/2025 17
05/25/2025 18
9
05/25/2025
05/25/2025 19
05/25/2025 20
10
05/25/2025
05/25/2025 21
• Use the ORDER BY statement to sort the result in ascending or descending order.
The ORDER BY keyword sorts the result ascending by default.
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "mydb"
});
con.connect(function (err) {
if (err) throw err;
con.query("SELECT * FROM customers ORDER BY name", function (err, result) {
if (err) throw err;
console.log(result);
});
});
05/25/2025 22
11
05/25/2025
• ORDER BY DESC: use the DESC keyword to sort the result in a descending order.
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "mydb"
});
con.connect(function (err) {
if (err) throw err;
con.query("SELECT * FROM customers ORDER BY name DESC", function (err, result) {
if (err) throw err;
console.log(result);
});
});
05/25/2025 23
• You can delete records from an existing table by using the "DELETE FROM" statement:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "mydb"
});
con.connect(function (err) {
if (err) throw err;
var sql = "DELETE FROM customers WHERE address = 'Ocean blvd 2'";
con.query(sql, function (err, result) {
if (err) throw err; The WHERE clause
console.log("Number of records deleted: " + result.affectedRows); specifies which record
});
or records that should
});
be deleted. If you omit
the WHERE clause, all
records will be deleted!
05/25/2025 24
12
05/25/2025
• You can delete an existing table by using the "DROP TABLE" statement:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "mydb"
});
con.connect(function (err) {
if (err) throw err;
var sql = "DROP TABLE customers";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table deleted");
});
});
05/25/2025 25
• If you attempt to delete not exist table you will get the following error:
• So, if the table you want to delete is already deleted, or for any other reason does not exist, you can use the
IF EXISTS keyword to avoid getting an error. var mysql = require('mysql');
var con = mysql.createConnection({
If the table If the table does not host: "localhost",
exist, the result exist, the result object user: "root",
object will look will look like this: password: "root",
like this: database: "mydb"
});
con.connect(function (err) {
if (err) throw err;
var sql = "DROP TABLE IF EXISTS customers";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
05/25/2025 26
13
05/25/2025
• You can update existing records in a table by using the "UPDATE" statement:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
The whole table was database: "mydb"
});
deleted from the
con.connect(function (err) {
previous example so if (err) throw err;
we created table console.log("Connected!");
customers again and var sql = "CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))";
inserted new record con.query(sql, function (err, result) {
to it. if (err) throw err;
console.log("Table created");
});
var sql = "INSERT INTO customers (name, address) VALUES ('Michelle', 'Blue Village 1')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted, ID: " + result.insertId);
});
});
05/25/2025 27
• You can update existing records in a table by using the "UPDATE" statement:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "mydb"
});
con.connect(function (err) {
if (err) throw err;
var sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Blue Village 1'";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result.affectedRows + " record(s) updated");
});
});
Notice the WHERE clause in the UPDATE syntax: The WHERE clause specifies which record or
records that should be updated. If you omit the WHERE clause, all records will be updated!
05/25/2025 28
14
05/25/2025
05/25/2025 29
15