-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmysql_reference.sql
More file actions
796 lines (659 loc) · 27.1 KB
/
mysql_reference.sql
File metadata and controls
796 lines (659 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
-- --------------------------------------------------------------------------------
-- MySQL Reference and Guide
--
-- ReferenceCollection.com
-- Licensed under CC BY-SA
-- --------------------------------------------------------------------------------
-- Table of Contents
--------------------
-- 1. Introduction
-- 2. Basic Syntax
-- 3. Data Types
-- 4. Database Operations
-- 5. Table Operations
-- 6. Constraints
-- 7. Data Manipulation
-- 8. Querying Data
-- 9. Joins
-- 10. Aggregate Functions
-- 11. Subqueries
-- 12. Indexes
-- 13. Stored Procedures
-- 14. Transactions
-- 15. Views
-- 16. Triggers
-- 17. User Management
-- 18. Backup and Restore
-- 19. Locking
-- 20. Replication
-- 21. Partitioning
-- --------------------------------------------------------------------------------
-- 1. Introduction
-- --------------------------------------------------------------------------------
-- Overview
-----------
-- MySQL is an open-source relational database management system (RDBMS).
-- It uses SQL (Structured Query Language) for database operations.
-- MySQL is widely used for web applications and provides high performance,
-- scalability, and ease of use.
-- History
----------
-- MySQL was originally developed in 1994, It was acquired by Sun Microsystems
-- in 2008, which was then acquired by Oracle Corporation in 2010.
-- MySQL continues to be developed and maintained by Oracle.
-- Architecture
---------------
-- MySQL operates in a client-server architecture. The server manages databases
-- by handling actions such as creating, reading, updating, and deleting data.
-- While the client allows users to interact with the server using SQL commands.
-- Components
-------------
-- Databases: Each database is a collection of related tables.
-- Tables: Structures within a database that hold data in rows and columns.
-- Rows: Individual records in a table.
-- Columns: Fields within a table that define the type of data stored.
-- SQL Commands
---------------
-- SQL commands are divided into several categories:
-- Data Definition Language (DDL): CREATE, ALTER, DROP, TRUNCATE
-- Data Manipulation Language (DML): SELECT, INSERT, UPDATE, DELETE
-- Data Control Language (DCL): GRANT, REVOKE
-- Transaction Control Language (TCL): COMMIT, ROLLBACK, SAVEPOINT
-- --------------------------------------------------------------------------------
-- 2. Basic Syntax
-- --------------------------------------------------------------------------------
-- Comments
-----------
-- There are two types of comments: single-line and multi-line.
-- Single-line comment
# Another single-line comment
/* Multi-line comment */
-- Semicolons
-------------
-- Each SQL statement should end with a semicolon.
SELECT 'Hello, MySQL';
-- Case Sensitivity
-------------------
-- SQL keywords, column names are case-insensitive
-- Database and table names are case-sensitive
CREATE DATABASE ExampleDB;
CREATE DATABASE exampledb; -- This will create a different database
-- --------------------------------------------------------------------------------
-- 3. Data Types
-- --------------------------------------------------------------------------------
-- MySQL supports various data types.
-- Numeric Types
INT -- Whole numbers
DECIMAL(M,D) -- Exact numeric decimal values
FLOAT -- Approximate numeric values
-- String Types
CHAR(M) -- Fixed-length strings
VARCHAR(M) -- Variable-length strings
TEXT -- Large amount of text data
-- Date and Time Types
DATE -- Date values (YYYY-MM-DD)
TIME -- Time values (HH:MM:SS)
DATETIME -- Date and time combination
-- Other Types
BOOLEAN -- True or false values
ENUM -- String object with a value chosen from a list
JSON -- Stores and enables efficient access to JSON
-- --------------------------------------------------------------------------------
-- 4. Database Operations
-- --------------------------------------------------------------------------------
-- In MySQL, data is organized into databases, which contain tables.
-- Listing databases
SHOW DATABASES;
-- Creating a database
CREATE DATABASE IF NOT EXISTS my_database;
-- Dropping a database
DROP DATABASE IF EXISTS my_database;
-- Selecting a database
USE my_database;
-- --------------------------------------------------------------------------------
-- 5. Table Operations
-- --------------------------------------------------------------------------------
-- Tables are composed of columns (fields) and rows (records).
-- Syntax: <column> <datatype> <constraints>
-- Creating a table
CREATE TABLE products (
id INT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(100) NOT NULL,
category VARCHAR(50),
price DECIMAL(8, 2) NOT NULL,
stock_quantity INT,
created_at DATE
);
-- Altering a table (add, modify, drop)
ALTER TABLE products ADD COLUMN discount DECIMAL(5, 2);
ALTER TABLE products MODIFY COLUMN discount DECIMAL(6, 2);
ALTER TABLE products DROP COLUMN discount;
-- Dropping a table
DROP TABLE products;
-- --------------------------------------------------------------------------------
-- 6. Constraints
-- --------------------------------------------------------------------------------
-- MySQL constraints are used to specify rules for the data in a table.
-- They ensure the integrity of data in the database.
--
-- Common MySQL constraints:
-- * AUTO_INCREMENT: Generates a unique sequential value for a column.
-- * PRIMARY KEY: Uniquely identifies each row in a table.
-- * FOREIGN KEY: Establishes a link between two tables.
-- * UNIQUE: Ensures all values in a column are distinct.
-- * NOT NULL: Ensures a column cannot have NULL values.
-- * CHECK: Ensures that all column values satisfy a specific condition.
-- * DEFAULT: Provides a default value, if no value is specified.
CREATE TABLE orders (
order_id INT PRIMARY KEY AUTO_INCREMENT, -- PRIMARY KEY constraint
product_id INT UNIQUE, -- UNIQUE constraint
customer_name VARCHAR(100) NOT NULL, -- NOT NULL constraint
order_date DATE NOT NULL, -- NOT NULL constraint
quantity INT CHECK (quantity >= 1), -- CHECK constraint
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- DEFAULT constraint
FOREIGN KEY (product_id) REFERENCES products(id) -- FOREIGN KEY constraint
);
-- Adding a CHECK constraint
ALTER TABLE orders ADD CONSTRAINT chk_valid_order CHECK (order_date <= CURRENT_DATE);
-- --------------------------------------------------------------------------------
-- 7. Data Manipulation
-- --------------------------------------------------------------------------------
-- Inserting/Updating data in your tables.
-- You can insert either a single row or multiple rows
INSERT INTO products (product_name, category, price, stock_quantity, created_at)
VALUES
('Laptop', 'Electronics', 999.99, 50, '2023-01-15'),
('Smartphone', 'Electronics', 799.99, 150, '2023-02-01'),
('Desk Chair', 'Furniture', 199.99, 75, '2023-03-10');
-- Updating the price of a specific product based on its ID.
UPDATE products SET price = 879.99 WHERE id = 2;
-- Deleting a specific product from the table based on its ID.
DELETE FROM products WHERE id = 3;
-- --------------------------------------------------------------------------------
-- 8. Querying Data
-- --------------------------------------------------------------------------------
-- Retrieving data from your tables.
-- Retrieve all columns for each product in table
SELECT * FROM products;
-- Retrieve only specific columns for each product in table
SELECT product_name, price, stock_quantity FROM products;
-- Retrieve all products with a price greater than 500
SELECT * FROM products WHERE price > 500;
-- Results can be ordered: ascending (ASC) or descending (DESC)
SELECT * FROM products ORDER BY created_at DESC;
-- Retrieve the first 10 products with their names and prices
SELECT product_name, price FROM products LIMIT 10;
-- --------------------------------------------------------------------------------
-- 9. Joins
-- --------------------------------------------------------------------------------
-- Joins are crucial for querying data spread across multiple tables.
-- They are used to combine rows from two or more tables based
-- on a related column between them.
--
-- Main types of MySQL joins:
-- * INNER JOIN: Returns rows with matching values in both tables.
-- * LEFT JOIN : Returns all rows from the left table, and matching
-- rows from the right table. unmatched right rows are NULL.
-- * RIGHT JOIN: Returns all rows from the right table, and matching
-- rows from the left table. unmatched left rows are NULL.
-- * CROSS JOIN: Returns the Cartesian product of two tables, showing all
-- possible row combinations.
-- * SELF JOIN : Joins a table with itself to compare same table rows.
-- INNER JOIN: Retrieve the order details and product name for all orders
SELECT orders.order_id, products.product_name, orders.customer_name, orders.quantity, orders.order_date
FROM orders
INNER JOIN products ON orders.product_id = products.id;
-- LEFT JOIN: Retrieve all products with their order (if available, else NULL)
SELECT products.product_name, orders.order_id, orders.customer_name, orders.quantity, orders.order_date
FROM products
LEFT JOIN orders ON products.id = orders.product_id;
-- RIGHT JOIN: Retrieve all orders with their product (if available, else NULL)
SELECT products.product_name, orders.order_id, orders.customer_name, orders.quantity, orders.order_date
FROM products
RIGHT JOIN orders ON products.id = orders.product_id;
-- --------------------------------------------------------------------------------
-- 10. Aggregate Functions
-- --------------------------------------------------------------------------------
-- Used to perform calculations on a set of values and return a single value.
-- Often used with the GROUP BY clause to group rows that have the same values.
--
-- Some common MySQL aggregate functions:
-- * COUNT(): Returns the number of rows in a set.
-- * SUM(): Returns the sum of a numeric column.
-- * AVG(): Returns the average value of a numeric column.
-- * MIN(): Returns the minimum value of a column.
-- * MAX(): Returns the maximum value of a column.
-- Get the total number of orders
SELECT COUNT(*) AS total_orders FROM orders;
-- Get the total amount of all orders
SELECT SUM(amount) AS total_amount FROM orders;
-- Get the average order amount
SELECT AVG(amount) AS average_amount FROM orders;
-- Get the number of orders per customer
SELECT customer_id, COUNT(*) AS orders_per_customer
FROM orders
GROUP BY customer_id;
-- Get the total amount spent by each customer
SELECT customer_id, SUM(amount) AS total_spent
FROM orders
GROUP BY customer_id;
-- --------------------------------------------------------------------------------
-- 11. Subqueries
-- --------------------------------------------------------------------------------
-- A Subquery is a query nested inside another query. They are useful for
-- breaking down complex queries and can be used in SELECT, WHERE, HAVING,
-- FROM, and EXISTS clauses.
--
-- Types of Subqueries:
-- * Scalar: Returns a single value.
-- * Row: Returns a single row with multiple columns.
-- * Table: Returns multiple rows and columns.
-- * Correlated: References columns from the outer query.
-- Scalar: Retrieves the name of the most expensive product.
SELECT product_name
FROM products
WHERE price = (
SELECT MAX(price)
FROM products
);
-- Row: Retrieve products with the highest price in each category.
SELECT id, product_name, price
FROM products
WHERE (category, price) = (
SELECT category, MAX(price)
FROM products
GROUP BY category
);
-- Table: Retrieves products that have been ordered more than 10 times.
SELECT product_name
FROM products
WHERE id IN (
SELECT product_id
FROM Orders
GROUP BY product_id
HAVING SUM(quantity) > 10
);
-- Correlated: Products with orders in the Last 30 Days
SELECT p.product_name
FROM products p
WHERE EXISTS (
SELECT 1
FROM orders o
WHERE o.product_id = p.id
AND o.order_date > CURDATE() - INTERVAL 30 DAY
);
-- --------------------------------------------------------------------------------
-- 12. Indexes
-- --------------------------------------------------------------------------------
-- Improve query performance by speeding up data retrieval. But may slow
-- down data modification operations such as INSERT, UPDATE, and DELETE.
--
-- Some common MySQL indexes:
-- * INDEX: Speeds up retrieval but doesn't enforce uniqueness.
-- * PRIMARY KEY: Uniquely identifies each row and creates a unique index.
-- * UNIQUE: Ensures distinct values and creates a unique index.
-- * FULLTEXT: For full-text searches.
-- * SPATIAL: For spatial data types.
-- * COMPOSITE: Useful for queries that involve multiple columns.
-- Create an index on the price column.
CREATE INDEX idx_price ON Products (price);
-- Create a full-text index on the description column.
CREATE FULLTEXT INDEX idx_description ON Products (description);
-- Create a composite index on category and price columns.
CREATE INDEX idx_category_price ON Products (category, price);
-- Drop the full-text index on the description column.
DROP INDEX idx_description ON Products;
-- --------------------------------------------------------------------------------
-- 13. Stored Procedures
-- --------------------------------------------------------------------------------
-- A set of SQL statements that can be stored in the database and executed
-- repeatedly. They improve performance by reducing client-server communication.
--
-- Key points:
-- * DELIMITER: Changes the delimiter to allow for multi-line statements.
-- * CREATE PROCEDURE: Used to create a new procedure.
-- * BEGIN and END: Enclose the SQL statements of the procedure.
-- A Procedure that retrieves all orders placed by a specific customer.
DELIMITER $$
CREATE PROCEDURE GetCustomerOrders(
-- Procedures can accept IN, OUT and INOUT parameters.
IN p_customer_name VARCHAR(100),
OUT p_total_orders INT
)
BEGIN
-- Select the order details
SELECT order_id, product_id, order_date, quantity FROM Orders
WHERE customer_name = p_customer_name;
-- Count the total number of orders and store it in the OUT parameter.
SELECT COUNT(*) INTO p_total_orders FROM Orders
WHERE customer_name = p_customer_name;
END $$
DELIMITER ;
-- Calling the Procedure.
SET @total_orders = 0;
CALL GetCustomerOrders('Customer Name', @total_orders);
SELECT @total_orders AS TotalOrders;
-- A Procedure that inserts a new order into the orders table.
DELIMITER //
CREATE PROCEDURE PlaceOrder(
IN p_product_id INT,
IN p_customer_name VARCHAR(100),
IN p_quantity INT
)
BEGIN
DECLARE stock INT;
-- Check the stock availability.
SELECT stock_quantity INTO stock
FROM products
WHERE id = p_product_id;
IF stock >= p_quantity THEN
-- Insert the order if stock is sufficient.
INSERT INTO Orders (product_id, customer_name, order_date, quantity)
VALUES (p_product_id, p_customer_name, CURDATE(), p_quantity);
-- Update the stock quantity.
UPDATE products
SET stock_quantity = stock_quantity - p_quantity
WHERE id = p_product_id;
ELSE
-- Raise an error if there is insufficient stock.
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Insufficient stock for this product';
END IF;
END //
DELIMITER ;
-- Calling the Procedure.
CALL PlaceOrder(1, 'Customer Name', 2);
-- Dropping Stored Procedures.
DROP PROCEDURE IF EXISTS PlaceOrder;
-- --------------------------------------------------------------------------------
-- 14. Transactions
-- --------------------------------------------------------------------------------
-- Transaction is a sequence of one or more SQL operations that are executed
-- as a single unit. If any statement fails, the entire transaction can be
-- rolled back to maintain data integrity.
--
-- Transactions have the following properties (ACID):
-- * Atomicity: Entire transaction succeeds or fails as a whole.
-- * Consistency: Ensures valid state transitions in the database.
-- * Isolation: Transactions run independently of each other.
-- * Durability: Committed transactions persist, even after failures.
--
-- Basic Transaction Commands:
-- * START TRANSACTION / BEGIN: Begins a new transaction.
-- * COMMIT : Saves all changes made in the transaction.
-- * ROLLBACK : Reverts all changes made in the transaction.
-- * SAVEPOINT: Sets a point to which you can roll back.
-- Start a transaction.
START TRANSACTION;
-- Insert a new product.
INSERT INTO products (product_name, category, price, stock_quantity, created_at)
VALUES ('Laptop', 'Electronics', 999.99, 50, '2023-01-15');
-- If something goes wrong, you would roll back the transaction.
ROLLBACK;
-- Commit the transaction, applying all changes.
COMMIT;
-- Another transaction.
START TRANSACTION;
-- Set a savepoint.
SAVEPOINT savepoint_1;
INSERT INTO products (product_name, category, price, stock_quantity, created_at)
VALUES
('Smartphone', 'Electronics', 799.99, 150, '2023-02-01'),
('Desk Chair', 'Furniture', 199.99, 75, '2023-03-10');
-- If needed, roll back to the savepoint.
ROLLBACK TO SAVEPOINT savepoint_1;
-- if everything is correct, commit the transaction.
COMMIT;
-- --------------------------------------------------------------------------------
-- 15. Views
-- --------------------------------------------------------------------------------
-- Views are virtual tables based on the result of an SQL statement.
-- They don't store data and provide a way to simplify complex queries,
-- Improve data security and Ensure a consistent data interface.
-- Create a view to display products that have been ordered.
CREATE VIEW OrderedProducts AS
SELECT
p.id,
p.product_name,
p.category,
COUNT(o.order_id) AS times_ordered
FROM
Products p
JOIN
Orders o ON p.id = o.product_id
GROUP BY
p.id, p.product_name, p.category;
-- Create a view to display orders along with product details.
CREATE VIEW OrderDetails AS
SELECT
o.order_id,
o.customer_name,
o.order_date,
o.quantity,
p.product_name,
p.category,
p.price
FROM
Orders o
JOIN
Products p ON o.product_id = p.id;
-- Selecting the views.
SELECT * FROM OrderedProducts;
SELECT * FROM OrderDetails;
-- Dropping a view.
DROP VIEW OrderDetails;
-- --------------------------------------------------------------------------------
-- 16. Triggers
-- --------------------------------------------------------------------------------
-- Triggers are database objects that are automatically executed when
-- certain events occurs.
--
-- Types of triggers in MySQL:
-- * BEFORE INSERT, AFTER INSERT, BEFORE UPDATE, AFTER UPDATE,
-- BEFORE DELETE, AFTER DELETE
-- This trigger checks if there is enough stock before placing an order.
DELIMITER //
CREATE TRIGGER before_order_insert
BEFORE INSERT ON orders
FOR EACH ROW
BEGIN
DECLARE stock INT;
-- Get the stock quantity of the product being ordered.
SELECT stock_quantity INTO stock FROM Products WHERE id = NEW.product_id;
-- Check if there's enough stock.
IF stock < NEW.quantity THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Insufficient stock for this product';
END IF;
END//
DELIMITER ;
-- This trigger adjusts the stock after an order's quantity is updated.
DELIMITER //
CREATE TRIGGER after_order_update
AFTER UPDATE ON orders
FOR EACH ROW
BEGIN
-- Adjust the stock quantity based on the new order quantity.
UPDATE Products
SET stock_quantity = stock_quantity - (NEW.quantity - OLD.quantity)
WHERE id = NEW.product_id;
END//
DELIMITER ;
-- Dropping a trigger.
DROP TRIGGER after_order_update;
-- --------------------------------------------------------------------------------
-- 17. User Management
-- --------------------------------------------------------------------------------
-- User management involves creating, modifying, and deleting user
-- accounts, as well as granting and revoking privileges.
-- Create a new user with a specified password.
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
-- Grant SELECT, INSERT, and UPDATE privileges on the database to the user.
GRANT SELECT, INSERT, UPDATE ON my_database.* TO 'newuser'@'localhost';
-- Grant all privileges on the database to the user, with the ability to grant.
GRANT ALL PRIVILEGES ON my_database.* TO 'newuser'@'localhost' WITH GRANT OPTION;
-- Revoke INSERT privilege on the database from the user.
REVOKE INSERT ON my_database.* FROM 'newuser'@'localhost';
-- Revoke all privileges and the ability to grant privileges from the user.
REVOKE ALL PRIVILEGES, GRANT OPTION ON my_database.* FROM 'newuser'@'localhost';
-- Create a role and grant SELECT privileges on the database to that role.
CREATE ROLE 'view_role';
GRANT SELECT ON my_database.* TO 'view_role';
-- Assign the role to an existing user.
GRANT 'view_role' TO 'readonly_user'@'localhost';
-- Show the privileges granted to the user.
SHOW GRANTS FOR 'newuser'@'localhost';
-- Change the user's password.
ALTER USER 'newuser'@'localhost' IDENTIFIED BY 'newpassword';
-- Delete the user from the database.
DROP USER 'newuser'@'localhost';
-- --------------------------------------------------------------------------------
-- 18. Backup and Restore
-- --------------------------------------------------------------------------------
-- Regular backups are crucial for data safety and recovery.
-- Backup the entire database to a SQL file.
mysqldump -u username -p my_database > backup_file.sql;
-- Backup specific tables to a SQL file.
mysqldump -u username -p my_database table1 table2 > backup_file.sql;
-- Restore a database from a SQL file.
mysql -u username -p my_database < backup_file.sql;
-- Point-in-Time Recovery:
-- 1. Enable binary logging (in MySQL config my.cnf).
log-bin=mysql-bin
-- 2. List binary logs:
SHOW BINARY LOGS;
-- 3. Apply binary logs up to a specific timestamp:
mysqlbinlog /path/to/mysql-bin.000001 | mysql -u username -p my_database
-- --------------------------------------------------------------------------------
-- 19. Locking
-- --------------------------------------------------------------------------------
-- A Mechanisms used to manage concurrent access to database resources.
-- Preventing conflicts when multiple transactions interact with the same data.
--
-- Main types of locks in MySQL:
-- 1. Table-level locks: Lock an entire table.
-- 2. Row-level locks: Lock specific rows within a table.
-- 3. Advisory locks: Custom, application-defined locks for complex scenarios.
--
-- Lock Types:
-- * Shared (Read) Lock: Allows multiple transactions to read the data,
-- but prevents any transaction from modifying it.
-- * Exclusive (Write) Lock: Allows a transaction to modify data
-- but prevents others from reading or modifying it.
-- Table-level locking: READ (or WRITE)
LOCK TABLES Products READ;
-- Perform read operations on the Products table
-- Release the lock
UNLOCK TABLES;
-- Row-level Locking: FOR UPDATE
START TRANSACTION;
-- This will lock the row where user_id = 1
SELECT * FROM Users WHERE user_id = 1 FOR UPDATE;
-- Perform operations on the locked row
UPDATE Users SET age = 30 WHERE user_id = 1;
-- Commit the transaction, releasing the lock
COMMIT;
-- Row-level Locking: SHARE MODE
START TRANSACTION;
-- Allows reading but prevents other transactions from writing to these rows.
SELECT * FROM Products WHERE category = 'Electronics' LOCK IN SHARE MODE;
-- Releases the shared lock
COMMIT;
-- --------------------------------------------------------------------------------
-- 20. Replication
-- --------------------------------------------------------------------------------
-- Is a process that allows you to automatically copy data from one
-- database server (the master) to one or more database servers (the slaves).
--
-- Key concepts:
-- * Master: Source database server with original data.
-- * Slave: Server receiving data copies from the master.
-- * Binary Log: File recording all changes on the master.
-- * Relay Log: File on the slave with data from the master’s binary log.
--
-- Benefits :
-- High availability by allowing a slave to be promoted to master in case of
-- failure, enables load balancing by distributing read queries across slaves,
-- facilitates backups on a slave, and allows intensive data analysis
-- MASTER:
-- Step 1: Configure the Master Server (my.cnf or my.ini).
[mysqld]
server-id = 1
log-bin = mysql-bin
binlog-do-db = my_database -- specify database to replicate
-- Create a Replication User on the Master.
CREATE USER 'repl_user'@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'%';
FLUSH PRIVILEGES;
-- Lock the tables to prevent changes during the setup.
FLUSH TABLES WITH READ LOCK; -- Lock all tables
-- Get Master Status.
SHOW MASTER STATUS;
UNLOCK TABLES;
-- SLAVE:
-- Configure the Slave Server (my.cnf or my.ini)
[mysqld]
server-id = 2
relay-log = mysql-relay-bin
-- Set Up the Slave.
CHANGE MASTER TO
MASTER_HOST='master_ip_address',
MASTER_USER='repl_user',
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000001', -- File value from 'Step 3'
MASTER_LOG_POS=123; -- Position value from 'Step 3'
-- Start the Slave.
START SLAVE;
-- Verify Replication Status.
SHOW SLAVE STATUS;
-- Stop replication.
STOP SLAVE;
-- Reset replication.
RESET SLAVE;
-- --------------------------------------------------------------------------------
-- 21. Partitioning
-- --------------------------------------------------------------------------------
-- Is a database feature that allows you to divide a large table into smaller,
-- more manageable pieces, yet still treat them as a single table.
--
-- Some common types of MySQL partitioning:
-- * RANGE: Divides the table based on a range of values.
-- * LIST: Divides the table based on a list of values.
-- * HASH: Distributes rows across a predefined number of partitions.
-- * KEY: Similar to HASH but uses MySQL’s internal function.
-- * COMPOSITE: Uses a combination of multiple partitioning methods.
-- RANGE Partitioning
CREATE TABLE products (
id INT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(100) NOT NULL,
category VARCHAR(50),
price DECIMAL(8, 2) NOT NULL,
stock_quantity INT,
created_at DATE
)
PARTITION BY RANGE (YEAR(created_at)) (
PARTITION p0 VALUES LESS THAN (2010),
PARTITION p1 VALUES LESS THAN (2015),
PARTITION p2 VALUES LESS THAN (2020),
PARTITION p3 VALUES LESS THAN (MAXVALUE) -- Handles all dates from 2020 onwards
);
-- Query data from a specific partition
SELECT * FROM products PARTITION (p1);
-- Add a new partition
ALTER TABLE products ADD PARTITION (PARTITION p5 VALUES LESS THAN (2025));
-- Remove a partition
ALTER TABLE products DROP PARTITION p0;
-- LIST Partitioning
CREATE TABLE products (
id INT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(100) NOT NULL,
category VARCHAR(50),
price DECIMAL(8, 2) NOT NULL,
stock_quantity INT,
created_at DATE
)
PARTITION BY LIST (category) (
PARTITION p_electronics VALUES IN ('Electronics'),
PARTITION p_accessories VALUES IN ('Accessories'),
PARTITION p_furniture VALUES IN ('Furniture'),
PARTITION p_other VALUES IN ('Other') -- Handles all other categories
);