Jump to content

Recommended Posts

Hi All,

 

I am after a bit of advice/suggestions on the following scenario:

 

I have a site that has 3 tables in it's database:

 

Videos

Themes

Customers

 

At present both primary keys of the themes table and customers table are stored in the videos table as foreign keys.  Each video can only be associated to 1 customer and 1 theme.

 

My question is what would be the best way to introduce some separate categories to which a video can be associated to that is not classed as a theme.

 

E.G. each video can only be associated to a single theme or category but not both.

 

Would I need to add another table or can I use the existing ones and add an additional field to them?

 

Any help gratefully received.

Link to comment
https://forums.phpfreaks.com/topic/236542-database-design-question/
Share on other sites

Hi All,

 

thanks for the suggestions.  I have decided to keep it simple and take requinix advice and created a new table called categories and store the primary key in the video table.

 

I have just one other question regarding the structure of a SQL query for this database.  Would someone be able to give me an example of how to list all videos and output the correct theme or category next to it?  I assume I will need to use a JOIN of some sort.  Not sure if this is any help but I am setting the categoryid and themeid field in the video table to 0 depending on which of these 2 the video has been associated to.  E.G If video is associated to a theme then the categoryid in the video table will set to 0 and vice versa.

 

 

why not fenway?

 

Because it goes against the entire principle of proper database design. Do you really think ALTER TABLE .. ADD COLUMN .. is a better alternative to a simple INSERT INTO .. VALUES ..? Not to mention the performance hit you'll take for choosing to use an ALTER TABLE approach since the entire table will lock every time you add a new 'category' leaving thousands of your users waiting for the table to be unlocked.

why not fenway?

 

Because it goes against the entire principle of proper database design. Do you really think ALTER TABLE .. ADD COLUMN .. is a better alternative to a simple INSERT INTO .. VALUES ..? Not to mention the performance hit you'll take for choosing to use an ALTER TABLE approach since the entire table will lock every time you add a new 'category' leaving thousands of your users waiting for the table to be unlocked.

 

noooo that wasnt what i was suggesting at all that a terrible idea...sorry if I was misunderstood

I have just one other question regarding the structure of a SQL query for this database.  Would someone be able to give me an example of how to list all videos and output the correct theme or category next to it?  I assume I will need to use a JOIN of some sort.  Not sure if this is any help but I am setting the categoryid and themeid field in the video table to 0 depending on which of these 2 the video has been associated to.  E.G If video is associated to a theme then the categoryid in the video table will set to 0 and vice versa.

 

You will have to use a couple of LEFT JOINS.

 

SELECT whatever 
FROM Videos LEFT JOIN Themes ON Videos.ThemeID = Themes.ThemID
LEFT JOIN Categories ON Videos.CategoryID = Categories.CategoryID

 

By the way, in the database world, you should assign NULL to a column that is not being used (CategoryID or ThemeID) instead of zero. That would mean that you need to allow NULLS in the table definition.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.