How to define a foreign key using phpMyAdmin GUI in mySQL - mysql

I am creating a table using the GUI in phpMyAdmin mySQL hosted on an Apache web server (XAMMP).
How do I define a column as a foreign key and where do I insert the reference to the corresponding primary key? There is a drop down option for Primary Key but I don't see any such option for a foreign key.
Thanks

Find the Relation view see picture and there you can define our foreign keys.

Related

Error in foreignkey table

I created the database structure using MySQL workbench. When I do the forward engineer or synchronize model it displays some errors.
When I remove the foreign key or if I connect "INT" as a foreign key to the second table then it's working fine. But I want to use "Varchar(255)". Can't we use "Varchar(255)" as a foreign key? If so please help me to fix this error. I can't do forward engineer to this table.
Yes you can use varchar as foreign key.It is appropriate to add a unique index or a unique constraint to your table. However, your primary key should generally be some "meaningless" value, such as an auto-incremented number or a GUID.

make relation between to different physical databases

I have two databases on two different physical servers. At this time, I want to make relation between some tables in database1 and some tables in database2. I want to know is there anyway for doing that?
Note I have both mysql and oracle databases on my two servers and I can use them. Is it possible to make join between to physical databases with mysql or oracle?
Hamed, check this out.
In oracle it is possible to have a foreign key constraint based on a view. So follow these steps:
first: create a view to a table on the remote database. example:
create view test_view_dblink as
select * from some_table#some_dblink;
second: create a foreign key constraint on the view. example:
alter view test_view_dblink
add constraint test_view_dblink_fk foreign key (column_name)
references table_view_in_your_database(column_name) disable;
the "disable" in the constraint definition is very important.
in my environment this works perfect!
You can also have a primary key:
alter view test_view_dblink
add constraint test_view_dblink_pk primary key (column_name) disable;

Automatic connection of a new table in a existing Mysql database

I have a schema on my db where there are some tables. I have to create a table into this schema and i have to connect it with the others already present on the schema.I make an example:
Tables already present:
SCHOOL(IdSchool,NumStud,IdCountry);
SHOP(IdShop,IdCountry);
New table:
Country(IdCountry,....);
I want to know if there is an automatic mode to connect them (it means not to set the foreign key manually).
I want to know if there is an automatic mode to connect them (it means not to set the foreign key manually).
No.
How is the DBMS to know that Country.IdCountry and SCHOOL.IdCountry are given the same name with the intention to be connected, instead of just accidentally?
You'll have to use ALTER TABLE ... ADD FOREIGN KEY (...) REFERENCES ...1 to explicitly create the foreign key in the existing table.
1 Or ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY (...) REFERENCES ....

Should I be including the FOREIGN KEY when exporting my SQL schema from Workbench?

I export my SQL form MySQL Workbench and noticed there is an option to not including the FOREIGN KEY in the SQL file. Is it better to export with a foreign key or without?
You should include the foreign key if you plan on using the schema for something critical. Without the foreign keys, you will jeopardize the integrity of your data.

Does MySQL Workbench automatically create indexes for foreign keys?

When I create a foreign key in MySQL workbench, a new entry appears on the "Indexes" tab with the exact same same as the foreign key that I just created.
Is this actually the foreign key, showing up on the "Indexes" tab for some reason? Or does MySQL Workbench try to be helpful and create an index for me, knowing that I'm likely to be selecting against that column, and give it (confusingly) the same name as the foreign key?
It's MySQL doing that, not workbench.
And yes, it is being helpful to create an index when you create a foreign key constraint.
Foreign keys in innodb require an index or a prefix of an index with the same fields as the constraint in the same order. It seems MySQL Workbench automatically creates these since they appear in the SQL script exported from MySQL Workbench.
This is helpful but the problem is that it does not recognize the prefix from other indexes so it always creates an index even when it is unnecessary.