Database relationship between two databases - php

I want to create a little blog system. Under each article should be a comment function. I think, I need 2 Databases (1x for the normal articles, 1x for the comments of ech article). Now I dont now how i can create a relationship between boths databases. Here is a picture:
On this picture are the attributes of each database. And how can i contact the databases then? (write & read)

make for every type you want a table. i.e one for articles, writers, categorys etc.
Table articles
+----+-----------+-------+------------+---------+-------------+
| id | writer_id | title | date | message | category_id |
+----+-----------+-------+------------+---------+-------------+
| 1 | 12 | foo | 2015-01-26 | text | 34 |
| 2 | 12 | bar | 2015-01-27 | bar | 32 |
+----+-----------+-------+------------+---------+-------------+
table writer and so on
+-----------+------+
| writer_id | name |
+-----------+------+
| 12 | test |
+-----------+------+
Table comments
+------------+------------+---------+------+
| comment_id | article_id | comment | date |
+------------+------------+---------+------+
and so on
afterwards you can connect them in your sql
SELECT
`articles`.`title`,
`writer`.`name`,
`comments`.`comment`
FROM
`articles`
LEFT JOIN `writer` ON (`writer`.`writer_id` = `articles`.`writer_id`)
LEFT JOIN `comments` ON (`comments`.`article_id` = `articles`.`id`)
Have a look at http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins for a visual explanation of joins.

You need two tables in the same database and you can connect them with a foreign key.
ARTICLE (ID_ARTICLE,WRITER,TITLE,DATE,MESSAGE....)
COMMENT (ID_COMMENT,FK_ARTICLE(to know the article), COMMENT_WRITER ... )

Related

How to identify which table to delete a record when data coming from two tables?

I have two tables where some same kind of information kept. One table has approved information and other one contains pending(waiting for approval) data. I fetch data from both table and display in a same view. So user will see data from both the tables. User can delete those records. But when deleting I've a trouble with finding out which table I should delete.
Assume, table1(Approved info), table2(Pending info)
table1
id | name | description | creator |
-----------------------------------
10 | test1 | N/A | 100 |
11 | test2 | N/A | 100 |
12 | test3 | N/A | 101 |
13 | test4 | N/A | 200 |
table2
id | name | description | creator |
-----------------------------------
10 | test1 | N/A | 105 |
11 | test2 | N/A | 103 |
12 | test3 | N/A | 106 |
13 | test4 | N/A | 202 |
table1 has a record with id of 10; and table2 has a record with id of 10 in that table. Id is the primary key of both tables. Both record will show to user. Let's say user wants to delete the record related to id 12 came from table2. So I want to delete that record from table2. But how can I figure out which table to delete that record. Because I can't use id to figure out the table. I have tried using some kind of data attribute attached with
data coming from table2 to differentiate them. But anyone can change them by inspecting it. So what is the proper way for solve this issue?
On any case, on any system, makes sense to have two to tables with same columns. That should be one of the firsts rules of database design. What's more, you discovered yourself how hard is to maintain a design like that. I see this on legacy systems developed with zero love to the code. In the future this will turn into a snowball. You should change it as soon as possible.
status column
The status of and entity or resource, is classic requirement, usually implemented with one little column which called : status, flag, mode, etc. In your case, it could have these values (#BhaumikPandhi comment):
pending/approved/rejected
id | name | description | creator | status |
--------------------------------------------
10 | test1 | N/A | 100 | pending|
If you are worried to the database optimization, you could use a tinyint with these equivalence in your documentation:
1 = pending
2 = approved
3 = rejected
status table
You could keep your first table called record
id | name | description | creator |
And create another one called record_status with 2 columns, in which record_id is a FK of record table
record_id | status |
Anyway, the status column is the most easy a classic approach to your requirement.

Subcategories in php

I am doing a project in PHP, MySQL. I need to create a database in which there is a category to which there can be many subcategories ( like for a course book I can have physics, computer science, mathematics etc.).
Also there can be some categories which do not have subcategories (say Mythological Books).
The administrator has to add new category or subcategory via a form in PHP page which then updates the database.
How should I design the structure of my table where if admin for following two cases to be incorporated:
Admin only wants a subcategory to be added then upon selecting its parent category, it should be done.
Admin wants to add a new parent category and there exists no subcategory for it.
Please suggest how should I create my table(s).
Create a table called 'categories' with columns 'id', 'name', 'parent'.
Then use it like this:
1, Hardware, null
2, Storage, 1
3, Video, 1
4, Harddisks, 2
...
Here is a simple example with books:
Categories table:
+----+--------------------+--------+
| id | name | parent |
+----+--------------------+--------+
| 1 | Comics | NULL |
| 2 | Programming | NULL |
| 3 | SQL/PHP | 2 |
| 4 | Java | 2 |
| 5 | Marvel | 1 |
| 6 | Mythological Books | NULL |
+----+--------------------+--------+
Books table:
+----+---------------------------------------+----------+
| id | name | category |
+----+---------------------------------------+----------+
| 1 | PHP/MYSQL for dummies | 3 |
| 2 | Iron Man and the prisoners of azkaban | 5 |
+----+---------------------------------------+----------+
If you want to show all the books in the Programming>SQL/PHP category you just simply get them with a select query.
mysql> select * from books where category = 3;
+----+-----------------------+----------+
| id | name | category |
+----+-----------------------+----------+
| 1 | PHP/MYSQL for dummies | 3 |
+----+-----------------------+----------+
1 row in set (0,00 sec)

Mysql join query multiple values

I have a table called facility.
Structure looks as follows:
id | name
---------
1 | Hotel
2 | Hospital
3 | medical shop
I have an other table which is taking data from the above table and keeping multiple values in one column. View looks like below:
id | facilities
---------------
1 | Hospital~~medical shop~~Hotel
2 | Hospital~~Hotel
3 | medical shop~~Hotel
If I want to join these two tables how does the query look like?
I tried this, but it didn't work:
select overview.facilities as facility
from overview join facility on facility.id=overview.facilities;
you can do this with a bit of hackery
select o.facilities as facility
from overview o
join facility f on find_in_set(f.facilities, replace(o.facilities, '~~', ','));
I would highly recommend you change the way you are storing data. currently it is considered un normalized and that quickly becomes a monster to deal with
you should change your table structure to look something more like this
+----------+--------------+
| facility |
+----------+--------------+
| id | name |
+----------+--------------+
| 1 | Hotel |
| 2 | Hospital |
| 3 | medical shop |
+----------+--------------+
+-----------+-------------+
| overview |
+-----------+-------------+
| id | facility_id |
+-----------+-------------+
| 1 | 2 |
| 2 | 3 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 3 |
| 7 | 1 |
+-----------+-------------+
Code Explanation:
basically you are wanting to find the matching facilities in the overview. one handy function MySQL has is FIND_IN_SET() that allows you to find an item in a comma separated string aka find_in_set(25, '11,23,25,26) would return true and that matching row would be returned... you are separating your facilities with the delimiter ~~ which wont work with find_in_set... so I used REPLACE() to change the ~~ to a comma and then used that in the JOIN condition. you can go from here in multiple ways.. for instance lets say you want the facility id's for the overview.. you just add in the select GROUP_CONCAT(f.id) and you have all of the id's... note if you do that you need to add a GROUP BY at the end of your query to tell it how you want the results grouped

PHP & MySQL Tagging system logic

I am a beginner developer and i would like to ask some advice.
I am currently building a platform where people will be allowed to upload images and tag them.
I was reading through some articles with the following structure to store tags
Storing Logic 1
| photo_id | name | tags |
| 1 | some photo | flower, sun. island, beach |
| 2 | some photo2 | hawaii, travle. surf |
Lot of people said this is not such a good idea
So my logic.
I was reading around about Many-to-Many relations and i came up with this logic
Tags table
| tag_id | name |
-----------------------
| 1 | flower |
| 2 | hawaii |
| 3 | surfing |
| 4 | island |
| 5 | travel |
Photos table
| photo_id | name |
---------------------------
| 1 | some photo |
| 2 | some photo2 |
Relation table
| tag_id | photo_id |
---------------------------
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
I have chosen to use Laravel framework to make the development easier
But my problem is with logic 2 and what i am scared of is it will generate a huge load time.
Because there will be no default just user based tags i thought about the following logic.
User uploads the image with tags, before image is saved, check if the actual tags exsit if not save it, than return tags_id and save it to the relation table with photo_id
So i have 2 questions
Which logic is better and why?
If logic 2, is it good the way i thought it up? and should i worry about the load time in the future when lot of tags will be there?
thank you
I would go with the second one. I wouldn't worry about load times. You can easily get the categories with joins.
However, you should add an id column on the relation table so that multiple images can share a category.
In your second example, your relation table should have indexes, so that when you look for all the tags based on a specific photo_id, the answer will be rapidly returned.
See also Foreign Keys
In your relation table, tag_id is a foreign key into your tag table and photo_id is a foreign key into the photo table. Tags may have a relationship to more than 1 photo and a photo may have a relationship to more than one tag.
Similarly the names of your tags (and photos) should also be indexed for rapid searching.

Compare two MySQL tables to filter results

I have a MySQL database with the following structure:
Table1
UniqueID (int) (PrimaryKey & AutoIncrement)
TitleID (varchar)
DescriptionID (varchar)
ContentRating (varchar)
Table2
UID (int) (PrimaryKey & AutoIncrement)
Activity (varchar)
ContentLimit (varchar)
What I want to do is take the value from ContentLimit (in Table2) and compare it against (ContentRating) in Table1, if they match then show all the rows that match. I'm using PHP and MySQL to achieve this.
Below is an Example:
Table1
UniqueID | TitleID | DescriptionID | ContentRating
------------------------------------------------------
1 | Hello | I Am Text | Universal
2 | Again | Yet More Text | Universal
3 | This | Yet More Text | Universal
4 | Is | Yet More Text | Parental Guidance
5 | Some | Yet More Text | Universal
6 | Dummy | Yet More Text | Parental Guidance
7 | Text | Yet More Text | Parental Guidance
8 | I | Yet More Text | Parental Guidance
9 | Think | Yet More Text | Parental Guidance
Table2
UID | Name | Activity | ContentLimit
---------------------------------------------
1 | John Smith | IsActive | Universal
2 | Jane Smith | IsActive | Universal
3 | Felix Tiger | IsActive | Parental Guidance
4 | Spring Load | InActive | Universal
If "Felix Tiger" was logged in then he would be able to see anything submitted with a "Parental Guidance" rating as well as anything with a "Universal" Rating.
But If "Jane Smith" was logged in then she would only be able to view anything submitted with a "Universal" rating
I apologise if I am unclear and will make clear anything that may be mis-read or hard to understand.
Thank you in advance for any help given.
Try this:
SELECT
a.*,
b.*
FROM table1 a
JOIN table2 b ON a.ContentRating = b.ContentLimit

Categories