I've got this database with about 26 tables (field names are the same in each table) and i was wondering how simple it would be to do a general search on my website based on a keyword which will search through all tables?
Eg Each table has title, author etc etc so if i had a keyword of hairspray - whats the best way to look for the keyword through all tables..
Preferably not through a join or union due to the amount of tables
Cheers in advance
Its a very bad way, of creating tables.
If they share a common schema they should be one single table, with some additional field to separate or distinguish the data.
If this is not going to be an option for you, you might want to create a temporary table, which will hold all the data from all 26 tables, then query this table for the search.
Related
How to search multiple tables for a keyword regardless of which table or column it would be in?
I am looking for information relating to for example a name or phrase which maybe in one or multiple tables in any column.
I have to create a system to save user's vote for two different type of module: News and Video.
This table should have the same fields:
id
entry_id
vote
user_id
So I tought to add a new field to save also the name of the module (module), in this way I can have just one table in the DB and filter it when needed and create two views for statistic purpose.
I don't really know if the best solution is one table with the new field or is better have two different table.
Let's assume that I have 1000 news and 1000 users and all of them will vote each news I will have 1000000 rows in the table.
Now assume that I have also 1000 videos and also in this case all my users will vote it, other 1000000 rows for an amount of 2000000 rows in a single table.
Do I have any performance problem in this case? And If I will have much more video, news an users?
Operation that I should do:
Insert
Update
Search
If you need more infos please ask
I think the way to answer this question is based on entry_id. The votes are going to be about something and that something is going to reference another table.
So, if you have two separate tables for News and Videos, then you should have two separate votes tables. Neither will have entry_id. One will have news_id and the other video_id.
If you have one table, say Entries for both News and Videos, then have one table.
In other words, I am advising against having one table conditionally reference multiple other tables. It becomes very difficult to express foreign key restraints, for one thing. In addition, join operations are cumbersome to express. Someone else might visit the table and not realize that entry_id can refer to multiple tables, and incorrectly set up queries.
All of these problem can be overcome (and there are situations where one table may be the preferred solution). However, if the original entities are in different tables, then put the votes in different tables.
I am creating a MySQL database in which I have two tables, one for blogs, and one for their locations. What I have done is a field in blogs which is a string of location ids separated by commas: 1,5,7. Then in my PHP script I can explode() that string and get the locations.
The problem comes if I want to look for all blogs of a location using a MySQL query.
Should I create another table for the relationship? Maybe blog 1 location 1, blog 1 location 5, blog 1 location 7. Three rows to represent the former example. That way I would only select the blogs in that location in the query. The other way I would have to select ALL the blogs and check each of them one by one.
What do you think is faster and cleaner?
Thank you!
Different table is cleaner. As a matter of fact, it's the only choice, as it's the very basics of relational databases.
The correct that the best way to do this in a Relational Database setting is the 3rd table, sometimes called a Junction Table, to handle the relationships between Blogs and Locations. What you describe is a many-to-many relationship (or a one-to-many relationship that is going both ways).
Here is a link describing it a little better than me.
A third table to represent the relationships is the only way to go really.
I wondered how a table should be structured when there is going to be more than one value for a certain field. For example, If I have a user who has 10 friends should there be a table that has 10 rows with the user's name and a different friend on each row or should there be one row with the user's name and all the friends put into one? Thanks, the answers will help a lot.
You should make a many-to-many table that links the foreign keys together of two people who are friends. So if Person is the entity you are using, you wouldn't want to constrain the structure by introducing limitations such as 10 friends into a single row.
Fundamentally, what you're trying to do is normalize your data. You should read up a bit on Database Normalization
I am currently using Simple Machines Forum on my website, and users both register AND login using their forum account on my website. The smf_members table contains fields such as:
id_member | member_name | date_registered
What I am trying to do now is extend this to add more custom fields and connectivity on my site. I want to use the id_member field for many things:
For example, I want a user (an entry in this smf_members table) to be able to join a team.
I am going to create a table Teams with the following fields:
ID | Name | Description
and a table TeamMembership with the following fields:
UserID | TeamID | Role
As you can see, this table will link a member and a team, while leaving data specific to a user or team in their respective tables ONLY (successfully preventing redundant data). This sounds good, right?
Well, I don't want those two new tables in the same database as the SMF stuff, because it may get messy. Is it the easiest solution, though? Do you think the easiest solution is to just create my new tables within the same database, with the prefix cst for Custom? I have no idea how to link two databases so if it is too complicated maybe I should just do my cst solution.
I've edited this post and have an additional question.
Thank you for the answers. I have an additional question. Let's say that I wanted to extend new variables to the members, but again, wanted to avoid adding new fields into the SMF forum member table. What is the easiest way about going about this? Like, I want to create a table called UsersExtended and have fields such as:
-ID (this is NOT an auto-increment field, but the value of id_member from the SMF members table)
-Country
Is it easy to create a profile page with this structure and display any relevant data I want from the two tables, in a way, Linking the two so that they act as one big table?
They totally belong in the same database. Use one db and join the tables in your queries.
The only time you want to build a unique database for your application (other than for the application itself) is if you intend to create an API which will serve up that data to other projects in an unbiased manner. That is very common with Solr in which you can churn out blazing fast API's and generally don't belong mixed in with your current MySQL tables.
There is absolutely no harm in having the custom tables in the same database. You cannot define relations between 2 different databases in a relational database system.
If you're using MySQL have a look at this question's accepted answer. Remember you have to prefix your table with database name when querying across databases.
You may try this
SELECT *
from database1.table, database2.table
where database1.table.id=database2.table.id
You should only create two tables within the same database
**smf_members**
id_member | member_name | date_registered
**TeamMembership**
Team_ID | Name | Description | Role | id_member
If any user wants team membership the you can easily manipulate its record using join queries.
Don't know Simple Machines Forum - but I do know that most applications expect to be the only show in town.
I would create a new database = perhaps called "teams" with your own tables, and use NickCL's way of doing cross-database joins to join between the two applications.
You don't mention the specific database engine you're using (I assume it's MySQL), but logically, you can think of databases as namespaces - a way of keeping stuff that belongs together in the same logical place.
Ideally, "people"/"authentication" etc. should be in a separate database from the forums stuff - but as you're working with off-the-shelf code, you don't have that luxury.
In my experience, it's better not to mess with the databases of off-the-shelf software - when you want to upgrade, you have no idea what will happen to your own tables, regardless of their names....