Here's a question for connoisseurs: The user enters data about the car, sends it to the server, if you can determine the car brand (its id_car in the cars table) using the entered data, then you simply enter the brand id_car into the referring table (user_cars), if you cannot determine the id_car car brand - then enter all the data entered in the user_cars table. So, in the user_cars table, you cannot create the id_car key and refer to the cars table on it, but so that, if desired, the field is empty. Or can it? If not, then how to solve the problem? I hope the question is more or less clear ...
1 answer
If I understand the question correctly, then you just need to make a link to the cars table. And in the user_cars
table the user_cars
field id_car
to specify the Null property.
Those. You need to perform this query:
ALTER TABLE `user_cars` CHANGE `id_car` `id_car` INT( 11 ) UNSIGNED NULL DEFAULT NULL
Only one note: the id_car
field should be indexed, but not be the primary index (in the user_cars
table). And then I tried to do right now as I wrote. Until the primary index was removed, the NULL field (column of the table) did not want to accept the property. Well, it is in principle, and it is clear why.
- Hmm ... It turned out. What I did: I indexed the id_car field in the user_cars table (in this table, the primary key is id_user_cars (one user can have several machines)), then created a foreign key from user_cars across the id_car field (which can be NULL) in cars on the id_car field, which here is the primary key ... Honestly, I do not understand what it means that I indexed the id_car field in the user_cars table (but indicated that this field is not unique (one user can have two cars of the same brand)) ... You can please explain?) - Nikoole
- @Nikoole, I did not understand you. You have indexed the
id_car
field. Yes, in this table this field should not be unique. You indexed this field then to be able to make a connection. But before that, you must execute the query I wrote above. What exactly is incomprehensible to you? - Dobby007 - It's not clear what this indexing gives ... Well, I just wonder why it is needed. What happens when I index the field. - Nikoole
- The index makes data sampling faster. Indexing is the construction of an index, in search engines all sites are indexed (a reverse index is made there - the words found on the site are remembered: their number, position in the text, etc.), and here the direct index is used. Those. mysql will already know in advance where to look for
id_car
with this value (for example, 5 or 557), and not search through the entire file in search of this value. - Dobby007
|