PHP My Admin Not Creating Relations - php

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 )"

Related

I cannot set Foreign key in PHPmyadmin XAMPP

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.

Generic Admin Site in Php / Mysql

i am building a auto / generic client admin panel for Mysql Databases , it only takes a connection string and the system Dynamically creates all the The forms for all the tables with validation and Creates ALL CRUD operations for the tables in the database , i finished all that and did it OOP but the last thing to do is get the tables and Fields Relations Dynamically , so how can i implement that ?
Best Regards,
Look at the MySQL SHOW TABLES... and SHOW COLUMNS... statements.
If you're looking for foreign key constraints you can query INFORMATION_SCHEMA.TABLE_CONSTRAINTS and INFORMATION_SCHEMA.KEY_COLUMN_USAGE.
Both gonna help you out:
SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE table_schema='<database>' AND REFERENCED_COLUMN_NAME IS NOT NULL
Prints you all foreign keys of a specified database.
Can easily be used for any case of working with your foreign keys.
Might also be helpful:
Foreign key constraints missing from SHOW CREATE TABLE output

How do I create a relation between the UserProfile and Location tables?

I am trying to create a database with two tables: Location and UserProfile.
Location contains addresses, and UserProfile contains HowtownLocation and CurrentLocation.
I want Hometownlocation and Currentlocation to contain IDs from Location, and cascade when a user is deleted (assuming more than one user does not share the same address).
One user can have multiple addresses (i.e Hometown and Current), and one address can be used by many users.
I have a sort-of working version, but I still need it to cascade changes... and I want to be able to input data into it in PHPMyAdmin by creating a new UserProfile.
I am using MySQL Workbench, but that is not important.
:)
MySQL Reference helps. The Syntax is:
[CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name, ...)
REFERENCES tbl_name (index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]
reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION
Also make sure that your database is not MyISAM, but InnoDB which is the default in newer MySQL versions as far as i know.

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".

How do I link data on Phpmyadmin

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

Categories