Lecture 3: MySQL DML Operations
INSERT, SELECT, UPDATE, DELETE, JOIN
This work is licensed under CC BY-NC-SA 4.0
© Way-Up 2025
INSERT, SELECT, UPDATE, DELETE
CREATE verb, and modify any
structural information with ALTER. Now we will act on data
INSERT statement
INSERT into facilities(facid, name, membercost, guestcost, initialoutlay, monthlymaintenance)
values (9, 'Swimming Pool', 8, 16, 3000,3000)
UPDATE statement
UPDATE facilities SET membercost=10 WHERE name='Swimming Pool';
SET operator that targets a column, and replace the old valueUPDATE query, always do a select to make like a backup of data before the update
DELETE, as shown below
DELETE from facilities WHERE name='Swimming Pool';
DELETE query, always do a select to make like a backup of data before the update
SELECT verb in combination with FROM operatorFROMWHERE clause=, <,>, <>,
AND, OR) to combine several conditions
SELECT * FROM bookings;
SHOW DATABASES;
USE your_database_name;
SHOW TABLES;
SELECT * FROM facilities
WHERE monthlymaintenance < 1000;
JOIN operator, for example
SELECT * FROM facilities
JOIN bookings on bookings.facid = facilities.facid
WHERE monthlymaintenance < 15;
import mysql.connector
connection = mysql.connector.connect(
host="localhost",
user="root",
password="your_password",
database="your_database"
)
cursor = connection.cursor()
cursor.execute("SELECT * FROM customers")
results = cursor.fetchall()
const mysql = require('mysql2/promise');
const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'your_password',
database: 'your_database'
});
const [rows] = await connection.execute('SELECT * FROM customers');
console.log(rows);
import java.sql.*;
public class MySQLExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/your_database";
String user = "root";
String password = "your_password";
try (Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM customers")) {
while (rs.next()) {
System.out.println(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Using the facilities/bookings database, write queries for:
-- Example solution for #1:
SELECT * FROM facilities
WHERE monthlymaintenance > 100;
Write queries that combine multiple tables:
-- Example solution for #1:
SELECT b.*, m.firstname, m.surname
FROM bookings b
INNER JOIN members m ON b.memid = m.memid;
| Function | Description | Example |
|---|---|---|
| CONCAT() | Concatenate strings | CONCAT(firstname, ' ', lastname) |
| UPPER() / LOWER() | Convert case | UPPER(name) |
| SUBSTRING() | Extract substring | SUBSTRING(text, 1, 10) |
| LENGTH() | String length | LENGTH(description) |
| Function | Description | Example |
|---|---|---|
| NOW() | Current date and time | SELECT NOW() |
| CURDATE() | Current date | SELECT CURDATE() |
| DATE_ADD() | Add interval to date | DATE_ADD(NOW(), INTERVAL 7 DAY) |
| DATEDIFF() | Difference between dates | DATEDIFF(date1, date2) |
| YEAR() / MONTH() / DAY() | Extract date parts | YEAR(order_date) |