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..802d1905 --- /dev/null +++ b/sql-queries-10/pros-cons-storing-binary-files-database/storing-binary-files.sql @@ -0,0 +1,21 @@ +-- STORING BINARY FILES +-- 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