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.
Related
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.
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.
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.
I have no idea how to even phrase this correctly, so I hope my title was not too far off.
I know how to add values to a table using PHP and passing values via SQL. However, what I don't know how to do is how to ensure the correct foreign key is used for the particular entry.
I think it's best to describe through example.
Say I have a userbase table that has a usernameID(PK). Now say I have another table called age and that has a column called age and usernameID(FK).
So what I'm saying is that there is a usernameID as a foreign key in the age table. Now, say when I have a form on a website and it asks for an age for a person and that person is logged in, how can I ensure that when I add the age of this particular user, that it gets added for the correct usernameID(FK). I'm going to make the assumption that the usernameID is all ready created, but I juts need to know how to ensure that the data gets connected to the right FK.
Does this make sense what I am saying? I've been searching google for this but have no idea how to describe what I even want!
While creating table define foreign key constraint.
Eg.
CREATE TABLE Age
(
id int NOT NULL,
age int NOT NULL,
username varchar(10),
PRIMARY KEY (id),
FOREIGN KEY (username) REFERENCES User(username)
);
Here, User is the parent table where username column as primary key and in age table as foreign key. If you have used this in your DB for defining FK then on your PHP code it auto show err if trying to put anything other than value allowed.
I hope it will help.
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".