0% found this document useful (0 votes)
4 views15 pages

Internet Based Programming: Backend Web Development

The document provides a comprehensive guide on backend web development using MySQL, covering installation, establishing connections, and various SQL commands such as creating databases and tables, inserting data, and querying records. It includes code snippets for practical implementation and troubleshooting tips for common errors. The content is structured to facilitate learning for users looking to manage and manipulate database information effectively.
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)
4 views15 pages

Internet Based Programming: Backend Web Development

The document provides a comprehensive guide on backend web development using MySQL, covering installation, establishing connections, and various SQL commands such as creating databases and tables, inserting data, and querying records. It includes code snippets for practical implementation and troubleshooting tips for common errors. The content is structured to facilitate learning for users looking to manage and manipulate database information effectively.
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/ 15

05/25/2025

Internet Based Programming


Backend web development: MySQL (introduction and installing, establishing a connection,
create database, create table, primary key, primary key using alter table, insert data into a
table, insert multiple records, select from, where statement, order by statement, delete from
statement, drop table statement, drop table if exists statement, update statement)

Asst. Prof. Dr. İnan KAZANCI


ikazanci@bandirma.edu.tr

Backend web development- MySQL: Introduction and Installation

• One of the most popular databases is


MySQL.
• MySQL is a database management system
that allows you to add, access, and analyze
data in a database across a network.
• You need to download and install MySQL
from the following link:
https://dev.mysql.com/downloads/installer/.
• Refer to document: “Lecture- 14- MySQL_
Installation-guide.pdf”

05/25/2025 2

1
05/25/2025

Backend web development- MySQL: Introduction and Installation

• Open MSQL Workbench:


• Select Local instance of MySQL
connection.
• Enter Your Database password:

05/25/2025 3

Backend web development- MySQL: Establishing a Connection

• Then you have to install MySQL package by:

• Then you need to create connection to the Database:


var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost", index.js: creates
user: "root", connection with
password: "root" the Dataset on the
});
con.connect(function (err) { localhost
if (err) throw err;
console.log("Connected!");
});

05/25/2025 4

2
05/25/2025

Backend web development- MySQL: Establishing a Connection

• If you get the following error:

• 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

Backend web development- MySQL: CREATE DATABASE

• To create a database schema in MySQL, use the "CREATE DATABASE" statement:


var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost", MSQL
user: "root",
Workbench
password: "root"
});
con.connect(function (err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE mydb", function (err, result) {
if (err) throw err;
console.log("Database created");
});
});

05/25/2025 6

3
05/25/2025

Backend web development- MySQL: CREATE TABLE

• To create a table in MySQL, use the "CREATE TABLE" statement.


 Make sure you define the name of the database when you create the connection.
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost", MSQL
user: "root", Workbench
password: "root",
database: "mydb"
});
con.connect(function (err) {
if (err) throw err;
console.log("Connected!");
var sql = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
});

05/25/2025 7

Backend web development- MySQL: CREATE TABLE: Primary Key


• Primary key: when creating a table, you should also create a column with a unique key for each
record.
 This can be done by defining a column as "INT AUTO_INCREMENT PRIMARY KEY" which will insert a unique number for
each record. Starting at 1, and increased by one for each record.
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root", MSQL
password: "root",
database: "mydb" Workbench
});
con.connect(function (err) {
if (err) throw err;
console.log("Connected!");
var sql = "CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255), address VARCHAR(255))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
});

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

Backend web development- MySQL: INSERT Data INTO a Table

• To fill a table in MySQL, use the "INSERT INTO" statement.


var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
MSQL
user: "root",
password: "root", Workbench
database: "mydb"
});
con.connect(function (err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO customers (name, address) VALUES
('Company Inc', 'Highway 37')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});

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

• The Result Object: when executing a query, a result object is returned.


• The result object contains information about how the query (INSERT) affected the table (e.g., the number of
affected rows, the numbers of records caused warning, message from MySQL server, for tables with an
auto increment id field the id of the row you just inserted, etc. )
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 = "INSERT INTO customers (name, address) VALUES ('Michelle', 'Blue Village 1')"; Note: To be able to
con.query(sql, function (err, result) { get the inserted
if (err) throw err;
console.log("1 record inserted, ID: " + result.insertId);
id, only one row can
}); be inserted.
});

05/25/2025 12

6
05/25/2025

Backend web development- MySQL: SELECT FROM: Select All Table

• To select data from a table in MySQL, use the "SELECT" statement.


SELECT * will return all columns

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);
});
});

05/25/2025 13

Backend web development- MySQL: SELECT FROM: Selecting Columns

• 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

Backend web development- MySQL: SELECT FROM: The Result Object

• 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

Backend web development- MySQL: SELECT FROM: The Fields


Object
• The third parameter of the callback function (Fields) is an array containing information
about each field (column) in the result.

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(fields);
console.log(fields[2].name);
});
});

05/25/2025 16

8
05/25/2025

Backend web development- MySQL: WHERE Statement

• 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

Backend web development- MySQL: WHERE Statement: Wildcard


Characters
• You can select the records that starts, includes, or ends with a given letter or phrase by using '%’.

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 LIKE 'Highway%'", function (err, result) {
if (err) throw err;
console.log(result);
});
});

05/25/2025 18

9
05/25/2025

Backend web development- MySQL: WHERE Statement: Escaping


Query Values
• When query values are variables provided by the user, you should escape the values. The MySQL
module has methods to escape query 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;
var adr = 'Lowstreet 4';
//Escape the address value:
var sql = 'SELECT * FROM customers WHERE address = ' + mysql.escape(adr);
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});

05/25/2025 19

Backend web development- MySQL: WHERE Statement: Escaping


Query Values
• It is also possible to use a ? as a placeholder for the values you want to escape. In this case, the
variable is sent as the second parameter in the query() method:
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 adr = 'Lowstreet 4';
//Escape the address value:
var sql = 'SELECT * FROM customers WHERE address = ?';
//Send an array with value(s) to replace the escaped values:
con.query(sql, [adr], function (err, result) {
if (err) throw err;
console.log(result);
});
});

05/25/2025 20

10
05/25/2025

Backend web development- MySQL: WHERE Statement: Escaping


Query Values
• If you have multiple placeholders (?), the array contains multiple values, in that 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;
var name = 'Peter';
var adr = 'Apple st 652';
//Escape the name and the address values:
var sql = 'SELECT * FROM customers WHERE name = ? OR address = ?';
//Send an array with value(s) to replace the escaped values:
con.query(sql, [name, adr], function (err, result) {
if (err) throw err;
console.log(result);
});
});

05/25/2025 21

Backend web development- MySQL: ORDER BY Statement

• 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

Backend web development- MySQL: ORDER BY Statement

• 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

Backend web development- MySQL: DELETE FROM Statement

• 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

Backend web development- MySQL: DROP TABLE Statement

• 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

Backend web development- MySQL: DROP TABLE IF EXISTS Statement

• 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

Backend web development- MySQL: UPDATE Statement

• 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

Backend web development- MySQL: UPDATE Statement

• 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy