Withdata Software

Store video files in Oracle BLOB

Video files can be stored as Oracle BLOB data.

1. Core Compatible Type & Oracle BLOB Fundamentals

Oracle Database is an enterprise-grade relational database that natively supports the BLOB (Binary Large Object) data type, designed for storing large binary files such as videos. Oracle BLOB has a maximum storage capacity of 4GB per column, fully compatible with all mainstream video formats (AVI, MP4, WMV, MOV, MPEG, FLV, etc.).

Key characteristics of Oracle BLOB for video storage:

2. Key Operational Steps for Video Storage

2.1 Table Creation

Create a dedicated table for video storage with a BLOB column and auxiliary metadata columns (optimize query and management efficiency):

SQL Code for Table Creation:

-- Create dedicated tablespace for video BLOB data (optional but recommended)
CREATE TABLESPACE VIDEO_TBS 
DATAFILE '/u01/app/oracle/oradata/ORCL/video_tbs01.dbf' 
SIZE 10G AUTOEXTEND ON NEXT 1G MAXSIZE UNLIMITED;

-- Create video storage table with BLOB column
CREATE TABLE video_storage (
    id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    video_blob BLOB 
        STORE AS SECUREFILE (TABLESPACE VIDEO_TBS NOLOGGING COMPRESS HIGH),
    file_name VARCHAR2(255) NOT NULL,
    file_format VARCHAR2(50) NOT NULL,
    file_size NUMBER NOT NULL,
    upload_date TIMESTAMP DEFAULT SYSTIMESTAMP
);

2.2 Video File Storage (Write)

SQL Code to Insert Video BLOB Data (using PL/SQL):

-- PL/SQL script to insert video file into Oracle BLOB
DECLARE
    v_blob BLOB;
    v_file UTL_FILE.FILE_TYPE;
    v_buffer RAW(32767);
    v_bytes_read BINARY_INTEGER := 32767;
BEGIN
    -- Initialize empty BLOB
    INSERT INTO video_storage (video_blob, file_name, file_format, file_size)
    VALUES (EMPTY_BLOB(), 'sample_video.mp4', 'MP4', 104857600)
    RETURNING video_blob INTO v_blob;

    -- Open local video file (need DIRECTORY object permission)
    v_file := UTL_FILE.FOPEN('VIDEO_DIR', 'sample_video.mp4', 'rb', 32767);

    -- Read file and write to BLOB
    WHILE v_bytes_read = 32767 LOOP
        UTL_FILE.READ_RAW(v_file, v_buffer, v_bytes_read);
        DBMS_LOB.WRITEAPPEND(v_blob, v_bytes_read, v_buffer);
    END LOOP;

    -- Close file and commit
    UTL_FILE.FCLOSE(v_file);
    COMMIT;
EXCEPTION
    WHEN OTHERS THEN
        UTL_FILE.FCLOSE(v_file);
        RAISE;
END;
/

Use DBBlobEditor (GUI/CLI) to Batch import video files into Oracle BLOB.

2.3 Video File Retrieval (Read)

SQL Code to Extract BLOB Data:

-- Select BLOB data for specific video
SELECT 
    video_blob,
    file_name,
    file_format
FROM 
    video_storage
WHERE 
    id = 1; -- Target video ID

-- PL/SQL script to export BLOB to local file
DECLARE
    v_blob BLOB;
    v_file UTL_FILE.FILE_TYPE;
    v_buffer RAW(32767);
    v_pos INTEGER := 1;
    v_blob_len INTEGER;
BEGIN
    -- Get BLOB data from table
    SELECT video_blob INTO v_blob 
    FROM video_storage 
    WHERE id = 1;

    v_blob_len := DBMS_LOB.GETLENGTH(v_blob);
    v_file := UTL_FILE.FOPEN('VIDEO_DIR', 'exported_video.mp4', 'wb', 32767);

    -- Write BLOB to file
    WHILE v_pos <= v_blob_len LOOP
        DBMS_LOB.READ(v_blob, 32767, v_pos, v_buffer);
        UTL_FILE.WRITE_RAW(v_file, v_buffer);
        v_pos := v_pos + 32767;
    END LOOP;

    -- Close file and commit
    UTL_FILE.FCLOSE(v_file);
    COMMIT;
EXCEPTION
    WHEN OTHERS THEN
        UTL_FILE.FCLOSE(v_file);
        RAISE;
END;
/

Use DBBlobEditor (GUI/CLI) to Batch export Oracle BLOB to VIDEO files.

3. Key Best Practices

4. Common Issues & Solutions


Related Guides