First I don't know what a good title for this question
Example
On postid and wordid I can click and it will go to another table.
I want to know what code to make data in phpmyadmin can be click and link to related table?
that are just foreign keys and will let you know which table has this key so that it will be easy for end user (developer) to know yes this field has the relationship with this field of this table.
If you don't use innoDB, you'll have to use the database designer tool in phpmyadmin to set the relations between your tables. After that, you will be able to click on foreign keys as you want.
Please show other table views. postid looks like the Primary key and wordid comes from another table.
This is a basic sql join here: http://en.wikipedia.org/wiki/Join_%28SQL%29
Related
For example, I have Users and Projects tables.
Multiple users can be members of a project. How do I insert multiple users into the members column of the Projects table?
Do I separate by comma like: "John, Alex, Hanna"?
I'm a beginner in MySQL, sorry if this is a dumb question. Thanks!
You defiantly need another table.
It should look like this
Name: UserProjects
Field: UserId
Field ProjectID
Those 2 fields should be primary keys (dual primary id). If you want to go down the route of 'soft deletes' then add a status column which you'll set to 0 if you delete it. Also look up insert on update for mysql.
Trust me this is the way to go. Using a delimiter field or something will only give you problems later down the line.
thanks in advance for any help.
I have a question about foreign keys. I understand the concept of having the data from one table inserted into another for reference. But my question is, how does it get there?
Currently I have two tables and two forms. One form inserts data into table A, the other form inserts into B. Then I use a function to get the id from the last insert into A and insert it into B. Is this the proper way to do this or am I missing something?
There are two possibilities :
You know the primary key before the insertion in table A => Then your technique isn't the right one, since you're retrieving something you already added.
You don't know it (Example: auto-incremented id's) => Then your technique is the right one, and I don't think there is any other better way to achieve what you are asking for.
Note that what I called the primary key is the primary key of the row in table A, and a foreign key for rows in table B.
Short answer, I don't believe you aren't missing anything. There are many ways to accomplish what you are after, but your explanation is probably the most used and straightforward.
Another way is to use a trigger on table A to populate table B after insert (this only works if you do not need any additional user input, like form input to insert into table B).
As you cannot insert two ids at a time, yes it was an proper way.
First inserting the record on primary table, which we knows it.
Secondly, you that last insert id using the mysqli_insert_id() function
Now insert data on foreign table using this primary key.
I am using xamp.
I created a DB using SQL Yog,
I opened my localhost/phpmyadmin/
then selected the newly created database.
I wanted to make relations among tables, for instance there are two tables,USER and USERSTATS, I want to create relation depending on USER_ID, which exists in both table.
I selected create relation option, selected reference key from USERS table, then click on STATS table and selected foreign key, i got a prompt "Create relation", I clicked OK.
Now it was to supposed to be creating relation, but it's not, just a small blank popup window opens in firefox, with link
localhost/phpmyadmin/pmd_general.php?db=MYDBNAME&server=1&token=d9d3ed2661d4cc1d0db47eca1ebee996
But it is not creating the relation.
Please assist me in resolving this issue
Have you created your tables with InnoDB ? InnoDB accepts creation of foreign keys
Did you go through the complete phpmyadmin installation steps? You have to create the phpmyadmin specific tables. Without them you will not be able to see relations or create them.
http://docs.phpmyadmin.net/en/latest/setup.html#phpmyadmin-configuration-storage
mysql doesn't support the foreign key relations, though it accepts that keyword. That's probably the reason phpMyAdmin doesn't allow this.
If you can use InnoDB engine for your table, foreign keys are supported.
From an answer from stackoverflow for why-my-table-doesnt-support-foreign-keys
ALTER TABLE tableName ENGINE = InnoDB;
you need to add an index to the field you want as foreign key. You can do it by going to the table and clicking "Index" or you can do it manually: "ALTER TABLE YourTable ADD INDEX ( YourField )"
Trying to store the terms of a search. I've created a table "Searches" which stores each of the search terms in it's own field (bedrooms, baths, etc). So each row will contain one search.
On the advanced search form, users can select multiple search terms for a single field using an option select. I thought it would be wise to store each of these terms in a unique row of a related table for easy statistics reporting. I thought this way I could quickly report how many times a term is searched for. I also need to have the ability to save and regenerate the search query.
However if none of the terms searched are in the main table, I still need to generate a unique id to link it to the related table. So I would need to insert a blank row to generate the foreign key which I'm reluctant to do.
Is there a better way? I could store the multiple search terms questions in the primary table comma separated but it seems like it would be more difficult to pull them back out and count for statistics etc.
Why do you need to insert a blank row? You don't need to persist any of the records until the time comes to persist all of the records, right?
So as I understand it, your table layout is something like:
Table1
--------
ID
etc.
Table2
--------
ID
Table1ID
etc.
If that's the case, then the order of operations for inserting the data would look like this:
Begin Transaction
Insert into Table1
Get the last inserted ID
Insert into Table2
Commit Transaction
Assuming I understand your UX correctly, this would all happen when the user submits the form.
if i understand you,
it seems like you should have two tables:
search_term
-----------------
term_id
term
and
search
-----------------
search_id
term_id
then you can query search for all the terms and issue the SELECT statement.
I want to create tables and add the data with PHP as follows:
USER TABLES
Table A - ID + Name
Table B - ID + Value A + Value B + IDTable A
I want to put the user details to table B and the same User ID + Name to separate tables. How can I do that? How do I write the SQL in PHPMyAdmin?
Also, I need to make a table where a user can add friends. I don't have a clue how to make this, but if you have any suggestions please let me know. It's for a small social app I'm developing on android.
Thank you.
So this isn't going to be a simple one size fits all answer, it would be best if you understood what you were doing. You are going to need to make sql queries using PHP.
EDIT: I may have misunderstood your question, I apologize. From what I understand you are trying to find a way to link Table A to Table B using the ID tag (so that the ID for a single user extends across multiple tables). The way I would approach this is through the use of a Primary Key. Something like this:
CREATE TABLE TableA(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
PRIMARY KEY (ID)
);
CREATE TABLE TableB(
B_ID INT NOT NULL,
VALUE A ####,
VALUE B ####,
A_ID INT NOT NULL,
PRIMARY KEY(B_ID)
);
Whenever you add a value to TableB also add the primary key ID from TableA that the data belongs to. In the above example #### is NOT sql syntax it represents whatever datatype you want to store in those values as it was not specified in your question.
The question that this leads to is are you trying to allow multiple entries from the same user ID into TableB (i.e. A_ID=5 shows up several times in TableB)? If so this is as far as you can bring your database scheme from what I know. If there will only be one entry in TableB for every entry in TableA you should instead make A_ID the Primary Key in TableB and get rid of B_ID as it will be redundant.
If this does not fully answer your question, please leave a comment below explaining what you still don't understand and what you need help with. If my answer is no where close to answering your question, please edit/reword your question so that we may provide you with better answers.
Cheers and happy coding!