From 8dc07622232f222bd2075328c23f2724665cf296 Mon Sep 17 00:00:00 2001 From: oluG4real Date: Mon, 28 Apr 2025 11:21:07 +0100 Subject: [PATCH 1/2] pros-cons-storing-binary-files-database --- .../enhance-security.sql | 3 +++ .../increase-database-size.sql | 7 +++++ .../performance-overhead.sql | 20 ++++++++++++++ .../storing-binary-files.sql | 27 +++++++++++++++++++ .../transactional-integrity.sql | 14 ++++++++++ 5 files changed, 71 insertions(+) create mode 100644 sql-queries-10/pros-cons-storing-binary-files-database/enhance-security.sql create mode 100644 sql-queries-10/pros-cons-storing-binary-files-database/increase-database-size.sql create mode 100644 sql-queries-10/pros-cons-storing-binary-files-database/performance-overhead.sql create mode 100644 sql-queries-10/pros-cons-storing-binary-files-database/storing-binary-files.sql create mode 100644 sql-queries-10/pros-cons-storing-binary-files-database/transactional-integrity.sql diff --git a/sql-queries-10/pros-cons-storing-binary-files-database/enhance-security.sql b/sql-queries-10/pros-cons-storing-binary-files-database/enhance-security.sql new file mode 100644 index 00000000..1eee25f3 --- /dev/null +++ b/sql-queries-10/pros-cons-storing-binary-files-database/enhance-security.sql @@ -0,0 +1,3 @@ +-- Apply permission +USE University; +GRANT SELECT ON Course TO 'gbenga'@'localhost'; \ No newline at end of file diff --git a/sql-queries-10/pros-cons-storing-binary-files-database/increase-database-size.sql b/sql-queries-10/pros-cons-storing-binary-files-database/increase-database-size.sql new file mode 100644 index 00000000..8585ecd5 --- /dev/null +++ b/sql-queries-10/pros-cons-storing-binary-files-database/increase-database-size.sql @@ -0,0 +1,7 @@ +-- Query to calculate the size of each database in MySQL (in megabytes) +SELECT table_schema +AS 'Database', +ROUND(SUM(data_length + index_length) / 1024 / 1024, 6) AS 'Size in MB' +FROM information_schema.TABLES +GROUP BY table_schema; + diff --git a/sql-queries-10/pros-cons-storing-binary-files-database/performance-overhead.sql b/sql-queries-10/pros-cons-storing-binary-files-database/performance-overhead.sql new file mode 100644 index 00000000..903ccf64 --- /dev/null +++ b/sql-queries-10/pros-cons-storing-binary-files-database/performance-overhead.sql @@ -0,0 +1,20 @@ +-- Start timing the operation (precision: microseconds) +SET @start = NOW(6); + +-- Export a textbook (stored as Base64 in DB) to a PDF file +-- 1. FROM_BASE64() decodes the Base64 'textbook' column +-- 2. INTO DUMPFILE writes the binary result to the server's filesystem +-- 3. Restricted to MySQL's secure file directory (e.g., /var/lib/mysql-files/) +SELECT FROM_BASE64(textbook) +INTO DUMPFILE '/var/lib/mysql-files/temp_restored.pdf' +FROM Course +WHERE id = 'ME438'; + +-- End timming +SET @end = NOW(6); + +-- Calculate and display the export duration in milliseconds +-- TIMESTAMPDIFF computes microseconds, divided by 1000 for milliseconds +SELECT CONCAT( + TIMESTAMPDIFF(MICROSECOND, @start, @end)/1000, 'milliseconds') AS file_export_time; + diff --git a/sql-queries-10/pros-cons-storing-binary-files-database/storing-binary-files.sql b/sql-queries-10/pros-cons-storing-binary-files-database/storing-binary-files.sql new file mode 100644 index 00000000..3adbb4a3 --- /dev/null +++ b/sql-queries-10/pros-cons-storing-binary-files-database/storing-binary-files.sql @@ -0,0 +1,27 @@ +-- STORING BINARY FILES +-- View Databases +SHOW Databases; + +-- View Tables +SHOW Tables; + +-- Modify textbook Column in Course Table to accept Binary Files +ALTER TABLE Course MODIFY textbook LONGBLOB; + + +-- INSERTING BINARY FILE INTO DATABASE +-- Checking secure directory MySQL access +SHOW VARIABLES LIKE 'secure_file_priv'; + +-- INSERT SQL-Fundamental.pdf +INSERT INTO Course (id, name, textbook, credits, is_active, department_id) +VALUES ('ME438', 'SQL-Fundamental', + (SELECT TO_BASE64(LOAD_FILE('/var/lib/mysql-files/SQL-Fundamental.pdf'))), 7, 'Yes', 4); + + +-- Retrieving Binary File +SELECT FROM_BASE64(textbook) +INTO DUMPFILE '/var/lib/mysql-files/LinuxCourse_restored.pdf' +FROM Course +WHERE id = 'CS108'; + diff --git a/sql-queries-10/pros-cons-storing-binary-files-database/transactional-integrity.sql b/sql-queries-10/pros-cons-storing-binary-files-database/transactional-integrity.sql new file mode 100644 index 00000000..9c6e6a49 --- /dev/null +++ b/sql-queries-10/pros-cons-storing-binary-files-database/transactional-integrity.sql @@ -0,0 +1,14 @@ +-- Start the transaction +START TRANSACTION; + +-- Insert the pdf binary file into Course Table - textbook column +INSERT INTO Course (id, name, textbook, credits, is_active, department_id) +VALUES ('ME439', 'SQL-Fundamental-2', (SELECT TO_BASE64(LOAD_FILE('/var/lib/mysql-files/LinuxCourse_restored.pdf'))), 10, 'No', 4); + +-- Update Department Table +UPDATE Department +SET course_count = course_count + 1 +WHERE id = 4; + +-- Commit to complete the transaction +COMMIT; \ No newline at end of file From c18aa9c1f399646f9b359462f492b1bc8c5d8b18 Mon Sep 17 00:00:00 2001 From: oluG4real Date: Sat, 3 May 2025 21:31:27 +0100 Subject: [PATCH 2/2] modified storing binary files .sql --- .../storing-binary-files.sql | 6 ------ 1 file changed, 6 deletions(-) diff --git a/sql-queries-10/pros-cons-storing-binary-files-database/storing-binary-files.sql b/sql-queries-10/pros-cons-storing-binary-files-database/storing-binary-files.sql index 3adbb4a3..802d1905 100644 --- a/sql-queries-10/pros-cons-storing-binary-files-database/storing-binary-files.sql +++ b/sql-queries-10/pros-cons-storing-binary-files-database/storing-binary-files.sql @@ -1,10 +1,4 @@ -- STORING BINARY FILES --- View Databases -SHOW Databases; - --- View Tables -SHOW Tables; - -- Modify textbook Column in Course Table to accept Binary Files ALTER TABLE Course MODIFY textbook LONGBLOB;