Database schema for simple poll - mysql

I am going to create simple poll for my shop customers. Poll will be display during order process so ID of customer is sufficient protection, poll will be display at first order only.
In poll will be only one question few one choice answers. One of these answers will be "custom". Customer will be able to type own answer.
I have problem how to build my database schema. I prepared this:
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | | |
| customer | int(11) | NO | | | |
| answer | varchar(160) | NO | | | |
+----------+--------------+------+-----+---------+-------+
In answer column will be ID of selected answer or custom text which customer typed to text input.
What do you think about that solution?

Related

MySQL solution for grouped images?

i am looking for someone who can help me to achieve a way to store multiple images as a group in mysql.
Basically a user will upload multiple images for example 3 as a group (1 POST), how can i store them in the database.
This is how i store images right now.
+-------------+------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+-------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| userid | int(11) | NO | | NULL | |
| uploaded_at | datetime | YES | | CURRENT_TIMESTAMP | |
| path | varchar(255) | NO | | NULL | |
| is_grouped | int(11) | YES | | 0 | |
| grouped_to | int(11) | YES | | 0 | |
+-------------+------------------+------+-----+-------------------+----------------+
'is_grouped' ( 0 or 1 ) & 'grouped_to' ( id of main image ) are what i though could work but caused problems while retrieving from mysql database.
Any help would be appreciated.
I don't know what exact problem you are having with your current approach, but I would suggest just using the following definition for the images table:
images (id, user_id, uploaded_at, path)
Then, create a junction table which relates images to their groups:
image_groups (image_id, group_id)
We don't really need the is_grouped column any more, because we can simply check image_groups to see if the image appears in a group and/or if that group has more than one image. Similarly, the grouped_to column has now been made redundant because the junction table stores this information. Note that you might also want to have a groups table, which could store some metadata about each image group, such as the name, time of creation, etc.

How to import the articles into wordpress?

There is a mysql db named articles ,the table name is article ,three fields in it:subject,content,category. There are two values for the field category ,one is c1, other is c2.
There are two categories c1 and c2 in my wordpress.
Now how to import all records in the table article in articles db into my wordpress wp_posts table in mywp (mysql database name for the wordpress).
show columns from wp_posts;
+-----------------------+---------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------------------+------+-----+---------------------+----------------+
| ID | bigint(20) | NO | PRI | NULL | auto_increment |
| post_author | bigint(20) unsigned | NO | MUL | 0 | |
| post_date | datetime | NO | | 0000-00-00 00:00:00 | |
| post_date_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
| post_content | longtext | NO | | NULL | |
| post_title | text | NO | | NULL | |
| post_excerpt | text | NO | | NULL | |
| post_status | varchar(20) | NO | | publish | |
| comment_status | varchar(20) | NO | | open | |
| ping_status | varchar(20) | NO | | open | |
| post_password | varchar(20) | NO | | | |
| post_name | varchar(200) | NO | MUL | | |
| to_ping | text | NO | | NULL | |
| pinged | text | NO | | NULL | |
| post_modified | datetime | NO | | 0000-00-00 00:00:00 | |
| post_modified_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
| post_content_filtered | longtext | NO | | NULL | |
| post_parent | bigint(20) unsigned | NO | MUL | 0 | |
| guid | varchar(255) | NO | | | |
| menu_order | int(11) | NO | | 0 | |
| post_type | varchar(20) | NO | MUL | post | |
| post_mime_type | varchar(100) | NO | | | |
| comment_count | bigint(20) | NO | | 0 | |
+-----------------------+---------------------+------+-----+---------------------+----------------+
There are so many fields in wp_posts, only 3 fields in my source db.
The most important things are :
1.How to set guid when to insert records?
2.After records in the articles db whose category is c1 are inserted into wp_posts table, how to make the records displayed under the category c1 in wordpress?
I think you may use the plugin called Wordpress Importer.
I've done this a few times and I assure you it's more than a simple INSERT into wp_posts. Posts have enough extras that it's just much quicker to use a plugin to do it; namely time consuming is correctly formatted slug, formatted date/time, references in the wp_term_relationships table to note categories, and any meta data you have. Personally, I prefer WP Ultimate CSV Importer Plugin, but there's a number of options out there (several briefly explained here).
Essentially, you'll export your data from your article table into a .csv file, then upload to file using a WP Importer. The one I mentioned looks at the cvs headers and allows you to map them manually to each WordPress-specific field. For example, map your content column to the post_content column.
The main thing to watch for is that you for sure export/import all of the information you have in one go - it's a fair bit more difficult to override existing data (though I believe you can do it), ensure the csv file includes column headers, and backup WordPress before you import anything because it's a mess to manually fix things later.
If you really want to do it manually, your best bet is lots of research and trial/error to see what fields are required. Most of it's pretty straight forward, just time consuming; post_content, post_title are just text fields, author is (probably) the admin's user ID, date/time is just NOW() formatted 0000-00-00 00:00:00, and on. I'd look through the existing plugins and grab their code as a primary point of reference, but then why not just use their plugin since they already did the work and are offering it for free to you.
To answer your last two questions more directly,
The guid is the Globally Unique Identifier. Ideally, it's a unique ID across the internet; specific to your domain and to each post/page/etc on your site. It's mostly used so RSS feeds know what content is new and after it's been created as unique, you should never change it.
You'll need to insert posts into wp_posts then get the ID of the newly inserted post and add it to the wp_term_relations table along with the ID of the category you want which can be found in the wp_terms table.
Here's some more detais on the overall layout of the WordPres database.

Database schema: Key/Value table or all keys in one record

I guess that this is somewhat of a philosophical question. I need to collect pathology results for a group of patients and store them in a database. In the past I have used a very simple table structure (simplified):
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| ID | bigint(20) | NO | PRI | NULL | |
| Updated | datetime | NO | PRI | NULL | |
| PatientId | varchar(255) | NO | | NULL | |
| Name | varchar(255) | NO | | NULL | |
| Value | varchar(255) | NO | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
More often in schema design I see:
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| ID | bigint(20) | NO | PRI | NULL | |
| PatientId | varchar(255) | NO | | NULL | |
| Ph_Value | varchar(255) | NO | | NULL | |
| K_Value | varchar(255) | NO | | NULL | |
| Ca_Value | varchar(255) | NO | | NULL | |
| Ph_Value_updated | datetime | NO | | NULL | |
| K_Value_updated | datetime | NO | | NULL | |
| Ca_Value_updated | datetime | NO | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
It seems to me that the first design is much more flexible, expandable etc. However, I do wonder about performance hits when the records run to the millions.
The issue with the second is that there may be a couple of hundred fields that need to be recorded on occasions.
I would be really interested to get comments / advice / guidance on this.
You are absolutely right, the first schema is a lot more flexible: you can add new keys on a live database without changing the schema. However, flexibility is usually bought with the time and/or the space. In this case, it's both: you need more space to store all keys for the same row because the ID is replicated N times, and the joins or orderings required to get the fields together would take time.
There is no reason to pay for flexibility unless you need it. If most of your queries need most of the columns, the second result is the most economical. However, if most of your queries ask for a single column, getting the flexibility may be worth spending the CPU time and the database space.
In my opinion, If that name/value pairs won't be changed much so the second option is much better in the terms of space and number of rows.
Also you can have another solution to optimize the first schema , to put the names in another table and just put name_id instead of repeating the same name several times.
The other schema is to have patient table and a table for each value that contains patient_id and value and the table name is the name for that value

How to implement a superclass/subclass structure in MySQL?

These are the tables in my database, I need to create a couple superclass/subclass structures.
The first is where...
Superclass-Crew_Member
Subclasses-Director, Producer, Other_Directing, Other_Production, Art, Camera, Sound, Grip, Electrical, Post.
The second is...
Superclass-Producer
Subclasses-Salaries, Budget
+---------------------+
| Tables_in_film_crew |
+---------------------+
| art |
| budget |
| camera |
| crew_member |
| director |
| electrical |
| equipment |
| grip |
| location |
| manufacturer |
| other_directing |
| other_production |
| post_production |
| producer |
| salaries |
| sound |
+---------------------+
So how exactly would I go about creating those relationships?
Edit:
Maybe I should have clarified some other things too.
Here's what's contained in crew_member (Superclass):
+-------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+-------------------+----------------+
| Member_ID | int(5) | NO | PRI | NULL | auto_increment |
| Member_Name | varchar(25) | YES | | [INSERT EXAMPLE] | |
| DOB | date | YES | | [INSERT EXAMPLE] | |
| Address1 | varchar(25) | YES | | [INSERT EXAMPLE] | |
| Address2 | varchar(25) | YES | | [INSERT EXAMPLE] | |
+-------------+-------------+------+-----+-------------------+----------------+
Meanwhile here's what's contained in Other_Directing (Example Subclass):
+---------------+--------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------+------+-----+---------+----------------+
| O_Director_ID | int(4) | NO | PRI | NULL | auto_increment |
| FAD_ID | int(5) | NO | MUL | NULL | |
| SAD_ID | int(5) | NO | MUL | NULL | |
| SUD_ID | int(5) | NO | MUL | NULL | |
+---------------+--------+------+-----+---------+----------------+
Now all the foreign keys are referring to Member_ID from Crew_Member. All the other tables (except Director and Producer) are created in similar ways.
You could start by following some general rules which have to be taken in consideration when creating a database. Put the information that different groups have in common in 1 table and the specific data in smaller satellite tables.
I would put the generic information about a crew member in the first table:
so we would have an id, name, address and whatever all the members have in common.
Then you create "sub-tables" which relate to the "crew-member" table through the value crew_member_id. In this tables you put only the specific information related to directors, producers etc..
So the fields here might be something like: id, crew_member_id, directed movies, etc..
Even with the superclass producer you should work in the same way. Relate the subtales with the superclass through its primary key to have relations between them.
I would suggest you to read some articles about database designing. It might save your life in the future, cause after a database is made then it becomes much harder to correct mistakes.
http://www.datanamic.com/support/lt-dez005-introduction-db-modeling.html
Yeah this is a really good question, that I was researching as well.
The ideas I came up with were:
1> Have a parent table as the super class with sattelite tables for the attributes for each subclass joined by a foreign key. You could then represent it as a view.
2> Have a parent table as the super class and another single table for all of the extra attributes. This would have to be matched by joined by two foreign keys.
3> One table which holds all of the classes.(Terrible idea)
Theres other ideas but I think the first is the best bet.
Here's more info that suggests the first way.
http://www.tomjewett.com/dbdesign/dbdesign.php?page=subclass.php

Fetching/Inserting huge chunks of data from/to a large table

We have an Online Judge (something similar to SPOJ.pl) where we conduct these 3 hour long contests during the weekends, by the end of which we have close to around 1000 submissions. And we store all these runs on a single table (which includes the submitted code). The present structure of the table is as follows :
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| rid | int(11) | NO | PRI | NULL | auto_increment |
| pid | int(11) | YES | | NULL | |
| tid | int(11) | YES | | NULL | |
| language | tinytext | YES | | NULL | |
| name | tinytext | YES | | NULL | |
| code | longtext | YES | | NULL | |
| time | tinytext | YES | | NULL | |
| result | tinytext | YES | | NULL | |
| error | text | YES | | NULL | |
| access | tinytext | YES | | NULL | |
| submittime | int(11) | YES | | NULL | |
| output | longtext | YES | | NULL | |
+------------+----------+------+-----+---------+----------------+
Now the problem is that, every time we use the ORDER BY clause while querying within this, it ends up sorting the whole table. And in case of more than 1000 rows, each with a considerable amount of data, the time taken is significant. Please note that this is after OPTIMIZE-ing the tables at regular intervals say there have been changes made to the submissions. We do have two options :
Split up the tables after say around 100 entries.
Store the huge chunks of data (the submitted code) as files instead of inserting them as values into the table to reduce the overhead.
Is there another alternative/workaround to this were we can actually maintain the table structure as it is? I could really use some help here. Thanks.
My recommendation would be to do something called vertical partitioning: split the table into multiple tables, with different columns.
In this case, I would have one table that has all the small data: rid, pid, tid, language, name, time, result, access, submittime.
A second table would have: rid, code, error, output.
This way, you can do the sort on the first table and then join in the other fields after the sort. I put code, error, and output together since they sort of seem to go together.