Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the better database table structure for storing a single row of data in MySQL?

So I'm not sure exactly how to title this question. I am creating a database and need to store some default SEO info as follows.

default page title
default keywords
default page description
header code
footer code

There will never be more than 1 entry per field. So the question is do I create a table in the database with columns for each of these data types with the understanding that there will only ever be 1 row of data?

OR do I create a table that has a name column for each of the fields and then a column for the data (text)? With this option I can see that I wont be able to set the data type for each field, instead each would have to be tinytext or varchar.

Here are the 2 database table structures I'm contemplating.

CREATE TABLE `cms_seo` (
`id` int(2) NOT NULL,
`name` VARCHAR(100) NOT NULL,
`data` tinytext NOT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `cms_seo`
(`id`, `name`, `data`)
VALUES
(1, 'Website Keywords', ''),
(2, 'Default Page Title', ''),
(3, 'Default Page Description', ''),
(4, 'Header Code', ''),
(5, 'Footer Code', '');

OR

CREATE TABLE `cms_seo`(
`id` INT(1) NOT NULL AUTO_INCREMENT,
`default_page_title` VARCHAR(500) NOT NULL,
`default_keywords` VARCHAR(1000) NOT NULL,
`default_page_description` TINYTEXT NOT NULL,
`header_code` TINYTEXT NOT NULL,
`footer_code` TINYTEXT NOT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `cms_seo`
(`id`,
`default_page_title`,
`default_keywords`,
`default_page_description`,
`header_code`,
`footer_code`)
VALUES
(NULL, '', '', '', '', '');

Would there be any alternative to storing this data? Such as in a text file? The data will need to be editable through the cms.

like image 347
drooh Avatar asked Oct 14 '25 22:10

drooh


1 Answers

It's a common pattern to store the type of data you describe in a "key/value" format like your design #1. Some advantages include:

  • If your application needs a default for some new property, you can add it simply by INSERTing a new row.
  • There's a practical limit to the width of a row in MySQL. But you can add as many rows as you want.
  • If the list of defaults gets long, you can query for just one row. If you have an index, looking up a single row is more efficient.

Advantages of design #2:

  • You can use MySQL data types to constrain the length or format of each column individually. Design #1 requires you to use a data type that can store any possible value.
  • You store the property names as metadata, instead of as strings. Using metadata for property names is more "correct" relational database design.

I have posted many times in the past discouraging people to use the "key/value" design for data. But it's a legitimate use of that design when you just have one set of values, like the defaults in your case.


Another option, as you have mentioned, would be to store the data in file, instead of a database. See http://php.net/manual/en/function.parse-ini-file.php

Another option is to store the default values in a PHP file Just declare a hash array of them. One advantage of this technique is that a PHP file is converted to bytecode by PHP and then cached.

But since you say you have to be able to edit values through your application, you might find it easier to store it in a database.

like image 159
Bill Karwin Avatar answered Oct 17 '25 11:10

Bill Karwin