[MySQL] Foreign keys problem
#1

Hey everyone,i know this isn't the right place for it but here i go:

My goal is to make numbers go 1,2,3,4,5 automatic for every player in mysql. (for a car system). So people told me:create a foreign key. So i did. I have 2 tables:
1st. users with column userid(auto increment and primary)
2nd. vehicles with column vehicleid(auto increment and primary)

So I created a foreign key from the vehicles relation view,and now I can't insert any data into the vehicles table. Need help.Any ideas?
Reply
#2

No ideas... Can you at least provide some code on how are you inserting data into the table? And how did you create foreign keys?
Reply
#3

I don't think code is needed here. I tried using inserting by SQL in phpmyadmin.


I created it by relation view in pma.
Reply
#4

You're probably trying to insert an ID that doesn't exist in the parent table.
Reply
#5

What do you mean?
Reply
#6

A record must exist in the "users" table before you can insert it in the "vehicles" table. Make sense?
Reply
#7

It does, but it does not help, I get this error while inserting into vehicles:
pawn Код:
1452 - Cannot add or update a child row: a foreign key constraint fails (`bebras`.`vehicles`, CONSTRAINT `vehicles_ibfk_1` FOREIGN KEY (`vehicleid`) REFERENCES `users` (`userid`))
Reply
#8

Your database is screwed. You've made a foreign key out of the primary key. This is how your database looks like:

Код:
users
--------
userid PK
username
other
--------

vehicles
--------
vehicleid PK FK
other
--------
This is how it should look like:

Код:
users
--------
userid PK
username
other
--------

vehicles
--------
vehicleid PK
userid FK
other
--------
I can't help you any further, since your goal doesn't make any sense to me whatsoever.
Reply
#9

What should be the index for userid in the vehicles table?

My goal is auto numeration per player
Reply
#10

Well, I still have no idea what you're trying to achieve, but you should learn how to properly use foreign keys.

Some info about foreign keys:
https://sampforum.blast.hk/showthread.php?tid=420363

I also suggest using MySQL Workbench instead of phpmyadmin.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)