简体   繁体   中英

MYSQL - Storing lots of text fields in a single row

I'm fairly new to PHP/MYSQL, I have a website which has multiple long forms, and more are added on a regular basis. The data from all the forms is stored in a 'data' MYSQL table.

The way I approached storing the data was to have 200 text columns (as my forms are up to 200 questions long). Not every question needs a text column, about 50% are just single integers or a few word answers, but as they vary in order this was the simplest way I could think of to make it work.

I've now discovered I can't seem to store more than 10 text values in a single row as I get the following error:

Row size too large (> 8126). Changing some columns to TEXT or BLOB or using ROW_FORMAT=DYNAMIC or ROW_FORMAT=COMPRESSED may help. In current row format, BLOB prefix of 768 bytes is stored inline.

Where am I going wrong and what's the best way to store data like this? I know 200 columns seems a lot and I've read this must mean I haven't normalised my data properly, but creating lots of tables doesn't seem to be sensible in this case, as all the data is connected on a single level to the form it came from. Splitting it in to 20 tables with data 1-10 11-20 21-30 just doesn't seem right?

Any help would be much appreciated.

Create a table where each field is a separate row:

CREATE TABLE Question_Fields (
    question_id INT, -- FK to Question table
    field_num INT,
    field_value VARCHAR(1024)
);

You can then join this table with the Question table to get all the text fields for that question.

This is the case in which you should consider NoSQL. You don't have a fixed schema.
Consider using MongoDB Tutorial

// connect
$m = new MongoClient();

// select a database
$db = $m->comedy;

// select a collection (analogous to a relational database's table)
$collection = $db->cartoons;

// add a record
$document = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($document);

// add another record, with a different "shape"
$document = array( "title" => "XKCD", "online" => true );
$collection->insert($document);

// find everything in the collection
$cursor = $collection->find();

// iterate through the results
foreach ($cursor as $document) {
    echo $document["title"] . "\n";
}

您应该将每个问题另存为行,并且在每行中都有列名fe id_field ...这样,您的表就不会有200列,而只有两列(取决于表中的其他列)...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM