Search code examples
phpmysqlbigdatalongtext

how to store big text data in two tables with an id link using mysql and php


i have a database with table in which one column is 'project_stmt' which store text data. I have set data type to varchar(1000) but sometimes use input text of project statement is exceed very big like 100K chars, in that case data is not stored to database,

one solution is to use BLOB data type but in this case i have wasted lot of space because 80% of user input for 'project_stmt' will be under 500 chars..... so i want to store user input text for 'project_stmt' to second table with something like assigning data link,,,,how can i do this

CREATE TABLE projects (user_id int(11),
                    project_id int(11) NOT NULL AUTO_INCREMENT,
                    project_title varchar(25) NOT NULL,
                    project_stmt varchar(1000) NOT NULL,
                    PRIMARY KEY (`project_id`),
                    FOREIGN KEY (`user_id`) REFERENCES `user_info` (`user_id`)
                    );

Solution

  • Create a new table.

    CREATE TABLE projects_blob (project_blob_id int(11) NOT NULL AUTO_INCREMENT,
                        project_id int(11),
                        project_stmt blob NOT NULL,
                        PRIMARY KEY (`project_blob_id`),
                        FOREIGN KEY (`project_id`) REFERENCES `projects` (`project_id`)
                        );
    

    When inserting new entries, check the project_stmt lenght and if is > 1000, make an insert in the blob table. When selecting the project_stmt make a left join with this table and instead of projects.project_stmt use

    COALESCE(projects_blob.project_stmt, projects.project_stmt) AS project_stmt
    

    that should do the trick