I cannot set Foreign key in PHPmyadmin XAMPP - php

When i need to set a field as the foreign key in a table,in PHPMyAdmin. I am not getting it set right.
There is a Parent table called 'user' which has a primary key called 'uid'.I am using the 'uid' in the child table called 'student_register' as foreign key. But while setting it as foreign key constraint through the relation view link in phpmyadmin . i am not able to see the 'user' table in the drop down list to select it and set the 'uid' as foreign key .. I have sent the screen shot to get a clear picture.
the screenshot for the phpmyadmin child table 'student_register'

From the MYSQL User Guide:
If you are facing this issue than you need to follow these basic steps:
Database storage engine must be InnoDB.
Your relational tables must be InnoDB.
Use UNIQUE reference key.
Reference Key can not be NULL.
Datatype of the both columns must be same.
References:
Create Table Foreign Keys in MYSQL
Creating Foreign Key Relationships

You should add index to the column uid in table user in order to appear in the drop down list
Take the structure of table user, then click the last option of the Action - Add index for the column uid.

Related

phpmyadmin replacing table

Can I replace a table in DB through phpmyadmin. Table has two foreign keys and I can truncate it by disabling foreign key checks (or delete it completely), however when I try to reupload it, if I don't enable foreign key checks it doesn't connect to the tables it should (I can't click on foreign ID, usually it takes me to the related table). If I do enable foreign key checks I get an error
#1452 - Cannot add or update a child row: a foreign key constraint fails
Is there a way to somehow replace a table with the same table from the past by keeping the relations alive?
i think the table contains data ... try clearing the table before dropping it.
Actually "you don't need to disable foreign key checks" if you want to delete from (or truncate) only the table(s) where child records are stored. You should be able to delete all and re-insert them later. Please try it this way.
You need to disable the foreign key checks if you want to truncate the tables with primary keys without deleting the child records. (This is not reasonable though.)
The following steps in order should work fine without disabling any constraint. If you don't follow the order, it will fail. You can skip the steps 2 and 3 and use 1 and 4 only.
Delete all child records with foreign key pointing to a primary key.
Delete all master records with primary key.
Re-insert all master records with primary key.
Re-insert all child records with foreign key pointing to a primary key.
Anyway, the error you get sounds like when you try to insert the old data, the matching record is missing in the master table where the foreign key points to.
You can check:
Whether the data in the master tables (the tables with primary key where foreign keys point to) are also deleted or truncated. If so, you need to bring them back before the child records.
Whether the data in the master tables are not the same anymore and the consistency is lost with the old data. (If you are working on a production database, the data might be changing). If so, you need you need to bring the missing records back to the master table.

How to know the name of the unique key generated in mysql table?

I have table named employee in which emp_id is the primary key. After creating the table, I realized that i wanted to add the emp_email column as unique. But while creating a unique key constraint for emp_email column, I created a unique key constraint for emp_name by mistake. Now, i didn't create this unique key by running an ALTER statement, but i used the menu in phpmyadmin and clicked on the unique key icon to create the key.
Now I want to drop the unique key constraint for the emp_name. But to drop it using alter statement, i need to know the name of the unique key that is generated. How can i know the name of the unique key that is generated for emp_name column?
Can I drop the unique key from phpmyadmin menu by clicking on to something ?
I am using XAMPP v3.2.2
Any help will be appreciated .
While in phpmyadmin, select the table, then the structure tab. You should see the Indexes in the lower half, where you can modify/remove existing indexes. It's best to copy your table first (from the operations tab), as a back up, THEN make your edits.

PhpMyAdmin foreign key constraint not functioning

I have watched several tutorials on setting up foreign key constraints on phpMyAdmin now, but I can never seem to make it work.
My aim is to store names in the names table, then have those names as the foreign key of the stats table, where the stats linked with each player will be stored.
The stats table (with index)
(The name table is indexed in the same way as the stats, using the index button)
The setup of the foreign key constraint
No connection seems to have been properly made, since attempting to display the values of statname in PHP yields nothing.
I realise that it's important for the concerning fields to have IDs, which I have created, and in the version of phpMyAdmin I have (4.5.1), ID names don't seem to be displayed in the foreign key constraints section, only the fields those IDs refer to.
I'm pretty new to this, any help and advice would be welcome.
In the player names table, make a field (player_id) which stores the unique id of each player as the primary key which will uniquely identify the player from the table.
Next, in your stats table, take a field (player_id) amd make it as a foreign key which will reference the (player_id) field from the player table to refer the player stats in the stats table.

Should I index my mysql foregin keys

I am using mysql as DB for my Laravel app.
In one table I have 5 fields that are foregin keys, those FK's points to 5 other tables primary keys.
Right now I have only marked them as FK, but do I need to put a index on every FK as well? Or does a FK count as a index?
Thanks in advance,
In mysql when making a FK it automatically indexes that column(s).
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.
You can read more here at dev.mysql

MySQL: How to update foreign key field, and create a relationship, after table values have been set with default values?

I have a teamnews Table:
And another table called team:
The values in teamnews table are predetermined before a user signs into the site.
Lets say when a user(teamName) signs in I want to update the teamID row where NewsID = 1
And create a relationship so that if I eventually delete the user(teamName) the teamID value in the teamNews table is reset to zero.
Is this possible?
Please bare in mind I am using phpMyAdmin, so I am not entirely familiar with advanced SQL terms.
When I try to do this I get and error:
Here's the error:
You need to specify a FOREIGN KEY. You can add it in by running this command:
ALTER TABLE teamnews
ADD CONSTRAINT fk_teamID
FOREIGN KEY (TeamID)
REFERENCES team (teamID)
ON DELETE SET NULL;
This sets up a formal relationship between the tables using the foreign key. The ON DELETE SET NULL is the part that is important to you. This says that whenever any item in the referenced table (team in this instance) is deleted, then all rows in this table that had that team id should set that field to null — exactly what you're looking for.
Be aware that this will only work if you are using the InnoDB database engine (not the MyISAM engine). You can probably change that through phpmyadmin somewhere (I'm not familiar with phpmyadmin so I can't help you on the details).
Also be aware that for this to work, MySQL must actually be able to "SET NULL" -- the field containing the foreign key with this constraint can't be set to "NOT NULL" or it will fail with an error saying to "check data type".

Categories