Creating a relationship between a composite key and foreign key - mysql

I'm trying to link a foreign key to a composite key in mysql but im getting an error saying that the structure of the keys arent correct. I've checked the datatype and character length and both are correct.
Is this link even possible? see below for image...
Composite Key:
Foreign Key:
Any help on this would be greatly appreciated.

Not possible to create foreign key from composite key. Should be refer from parent key is primary key and should be not of column in reference key also..as per your share screen not possible to create this.. If you want to achieve this business let me reply will give suggestion to you..

I had my composite key the wrong way round. I switched the 'UniqueCode' to act as the parent key and managed to link fine

Related

MySQL style for creating foreign key

I've searched a bit for this, but I actually haven't found what is the style conception in MySQL for creating a foreign key - in the create table definition or in an alter statement. Thank you.
When to create foreign key:
If at the time of table creation it is clear you that you need foreign key then do at the time of creation, but if you realize later then do it in alter.
Best practices: you can follow below practice, it is not must but you can get benefits-
constraint fk_tableName_colName foreign key (colName) references parent_table(referenced_col_Name) cascading if required
Note: As foreign key name must be unique, so it will help to maintain it.
Points Need to remember:
referenced table in parent table must be indexed (if primary key then no need as it will be indexed).
column in both tables (parent/child) must have same schema.
Have a look at the docs on how to create foreign keys...
http://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html
When a foreign key gets added can be during the initial architecture of the application being built or it can be added later as the application evolves.

PRIMARY KEY and FOREIGN KEY to my table

So im trying to add an primary key and a foreign key to my table, but i just can't seem to get it to work. i've checked the forums already and other places but it didn't answer my problem. So here is my table:
http://puu.sh/mVW7D/5986e08daa.png
im trying to get VeiederID as my foreing key and keep studentnr as primary
but ive tryied to "alter table" and add the foreing key as constraint, but i might be doing it wrong, im very new to mysql. any help is appirciated
Steffen
Two points:
You do not identify the second table. A foreign key works between two tables and you are only showing one.
You do not specify which mysql version you are using.
Regardless: You should read the manual, reference: v 5.7 Foreign Keys
Update
I see from your updated example you are creating 3 tables and you want the rows in Veileder to reference studentinfo. So, the statement in the Veiled table should be
FOREIGN KEY (VeilederID)
REFERENCES studentinfo(VeiederID)
ON DELETE CASCADE
Apologies if the spelling is wrong. One other note, it appears you are trying to make the primary key (VeilederID) map to the foreign key. If I am wrong please comment but I don't know if that would be allow. Typically a table HAS it's own primary and a reference constrain (a.k.a. FOREIGN KEY) as a different column.
think i found it now i dont get an error while im running it this way, but if its right im not sure..
http://puu.sh/mW1Ze/ae333a40d6.png

combination Key

i have a table that have one primary key, and now i want add to this table another primary key, actually i want have two primary keys and i don't know how:(
i know it's a simple and silly question but I will be grateful if any one help me.
There's no "second primary key". The word "primary" shows this clearly...
You can add several unique keys (which can behave like a primary key) or you can define a primary key consisting of several columns.
Hope this helps...

Setting foreign key in phpMyAdmin

I want to make namecon of table "paper" as a foreign key reference to table "journal" to its primary key jname. I am getting confused with this view and not finding any tutorial or link which is explaining to set foreign key with this schema of phpMyAdmin. (Is it new or what)
Please someone let me to figure this out.
First, you need to create an index on the namecon column, click on the Structure tab ("Table structure" sub-heading on newer phpMyAdmin versions), then the Index text for the namecon column. You also need an index on the jname column in your journal table; in most of my databases this is an auto incrementing primary key but all that MySQL requires is that it's an index.
Next, go back to the Relation View (which is a sub-heading of Structure under newer versions). Note that this is in the area for "Foreign key constraints" not "Internal relations." Now you'll be able to select namecon from the "Column" dropdown:
If you want you can give it a name, otherwise MySQL generates one for you.
While making the foreign key, it is also advisable to mark the index key option available int Structure view of the table.
That will make the primary key thing to visible in the relational view of that particular table which can be used for making foreign key.

MySQL foreign key cannot be created (index problems?)

I have a little problem making some foreign keys...
When I try to define the foreign key in MySQL Workbench, I get the following message:
Selected column 'playerName' must be indexed and be of a compatible type for a Foreign Key to be created.
There my problem starts: I'm pretty sure, that the column (towns.playerName) is indexed and it's definitively of the same type ( VARCHAR(255) )...
Indexes of 'towns'
I want to add a Foreign Key from players.name (primary key, not null, unique) to towns.playersName(not null).
So what can I do to get the foreign key created?
It seems i am doing something wrong...
PS: I'm sorry, if there is already a question for this...
EDIT: I just tried again (exactly as I did before several times) and now it works... really strange
Perhaps a bug in MySQL Workbench??
I got the same problem a number of times, but finally found a interesting and useful concept which I missed while learning mysql.
If a column is not a 'key' in the table 1, you cannot add it as foreign key unless it should have an index.
So I make the column a indexed column.
e.g.
CREATE INDEX any_name ON table1 (column1);
finally, I was able to solve my problem.
I believe that the key you are trying to access through the foreign key needs to be a primary key in the other table.
I was getting similar error message. I checked the type of the column in both the tables. It was int in one table whereas varchar in the other table. It should be the same type in both. On using the same type in both the tables, it worked fine.