Skip to content

pros-cons-storing-binary-files-database #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Apply permission
USE University;
GRANT SELECT ON Course TO 'gbenga'@'localhost';
Original file line number Diff line number Diff line change
@@ -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;

Original file line number Diff line number Diff line change
@@ -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;

Original file line number Diff line number Diff line change
@@ -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';

Original file line number Diff line number Diff line change
@@ -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;