Creating a comment system with a simple rating system for each comment.
tables : 1.For the comments and it is called comments and it has three columns : id, name, comment
2. for the IP of the user that did the rating and it is called voted_ipand it has three columns id, comment_id, user_ip
The purpose of the voted_ip table is that i need to save the IP address for each rate to validate it that it cannot rate again if it exists.
I created a foreign key from the child table voted_ip in the column comment_id connecting it to the parent table comments in the column id following the steps at this link and this video on how to create a working foreign key except that the child table still do not update after a comment or a rate is inserted.
as follow :
I thought about that there might be another step or I have to do something in the php side of the project. What am I missing?
Data is not inserted in the other table "voted_ip" on insertion in "comment" by itself you have to add it explicitly this constraints are just for checking not for adding data in other table automatically.
Related
I have question about changing value in child table automatically if value of table mother change value.
I have two tables are brand(mother) and cat(child).
I add foreign key is brand_id in table cat related to primary key id in table brand.
If I change brand_name from brand table, I also want brand_name in table cat changed automatically. Is it possible? and how can I do it.
Thank you so much.
Brand:
Cat:
You could create a custom trigger on the brand table which before update would perform the necessary updates on the cat table.
Here is a link to the documentation for MySQL triggers:
https://dev.mysql.com/doc/refman/8.0/en/create-trigger.html
UPDATE:
As commented by #kerbholz, proper data modeling would suggest that the brand name column shouldn't exist in the cat column as it creates duplication of data. If this is purely for your client to see the data, perhaps building them a custom view using properly modeled tables is a better solution.
Link to appropriate documentation:
https://dev.mysql.com/doc/refman/8.0/en/create-view.html
Right now I'm working on a small blog project and I have 2 tables in my database: users and posts. I want to display name of the author of the post so I thought I should make foreign key for user in post table. But what if I would create normal column called for example user_id and just save there id of the author. Then while I would like to display post i could join both tables and display content of post and name of the author.
Does creating foreign key have some adventages?
Either way, you are going to need a "normal" column, such as user_id.
The advantage of making the user_id a foreign key is that then, the database will enforce referential integrity. This means it won't allow you to set on a post a user that does not exist, nor will it allow you to delete a user who has one or more posts (without also deleting the relevant posts).
First of all, i know there are many posts about this subject but the ones i checked didnt help me so i wanted to post my problem.
I am building a website that people can register as user then fill some different forms for me. I have 2 table one is "user" and the other one is "form" table. In user table i have user_id which is auto_incremented and this id connected to "form" table as foreign key. I want to see which user filled forms via user_id. When i try to register filled form to database i get "Cannot add or update a child row: a foreign key constraint fails" message. How can i fix this?
If you have set the user_id on on your form table as a foreign key pointing at the user_id in the user table, you must supply a valid user_id when you insert the form row.
You could set the user_id field on the form table to be allowed to be NULL this will mean it does not have to be set.
you are trying to add some data to your foreign key column that there is no same data in other table .
This mostly happens when there is a data in table and you want to create/ edit/ delete a foreign_key OR
the field that you want to make it foreign_key is not the same format with Table field
solution:
You have to make a
1)backup or copy of you data
2)remove the foreign_keys
3)and Truncate data from table
4)Finally re attach the foreign_keys
5)update your data
I hope it helps
Here is what I want to ask:
I want to make a system to register patients so then they will be able to login. I have 3 type of users though.
admin (no need for registration)
doctor (standard number of doctors, no need for registration)
patient (they will be registered)
I want to keep more info for them than just id, username, password, email.
I am thinking of having more than 1 tables to do this and link them with primary and foreign keys:
1st table
accounts (it will store the login data)
Example:
acc_id(primary key)
acc_password
acc_username
acc_type
2nd table
doctors_extra_info
Example:
acc_id (foreign key)
doc_info_id (primary key)
doc_name
...
...
3rd table
patients_extra_info
Example:
acc_id (foreign key)
pat_info_id (primary key)
pat_name
...
...
4th table
admin_info
Example:
acc_id (foreign key)
admin_id (primary key)
admin_email
a. Which is the best way of doing this?
b. In the part of
registration, how to deal with primary and foreign keys? Two insert
commands in two different tables? [In order to have the same acc_id
in the account table and the extra info table]
c. At the login part,
I need to check the type of user and redirect (header(Location: ..);)
to a page? Is this the right way of doing it?
Any suggestions?
Thank you.
If you're using PHP then when you insert a record you can instantly retrieve the ID created using mysql_insert_id(). You then use this to create other records as your foreign key.
With regards to redirects, I'd simply get the user type from the database and then check the type of user and redirect to page required.
Generally though the tables you have created do not correlate properly. Remember the defining thing about the people using the system is that they are a person, and shouldn't be deined by their job role. They should have a account_type_id linking to another table. Otherwise you have three tables essentially holding the same information.
For example you should have your tables like this
User table
user_id
first_name
last_name
email
account_type_id*
Accounts type table
user_id
account_type_id*
account_type //e.g. patient, doctor, admin etc
This means now that you can easily extend the database with new tables, user access levels, new columns without having to duplicate the same column across three tables and so on. Try reading up on database normalization. A very good video from youtube is http://www.youtube.com/watch?v=fg7r3DgS3rA
I develop a webpage in that i face this problem. please see below.
In user table i have following fields:
Userid
password
Address
phone
Email
In service table i have the following fields:
Ser_name
ser_id
USER_Refid
All these are in same page in php.
When i add values in service table it stores under the same USER_Refid name .
For example the user xxxx is going to add service means the USER_Refid field contain the same name for all services .
My problem is when the user add values in services table and forgot to add the values in user table means i want to delete the values that inserted by that particular user . Please help me to solve this , Thanks in advance.
If your tables are InnoDB, you can a FOREIGN KEY constraint to your service table:
ALTER TABLE service_table ADD CONSTRAINT fk_service_user_refid FOREIGN KEY (USER_refid) REFERENCES user_table (USER_id)
To add the values into services, create a memory table with a session id, populate it with your services, and when the user clicks Save, do the following:
Create an entry for your user in the user_table
Move the entries from the temporary table into the service_table.