Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving records in MySQL which does not pre-exist

Let say, I have a pre-defined table called cities, with almost all the cities in my country.

When a user register himself (user table), the column cities_id in the table user stores the city id from the table cities (Foreign Key, reference table cities), something like

CREATE TABLE `cities` (
    `id` int,
    `city_name` varchar(100)
)

CREATE TABLE `user` (
    `id` int,
    `name` varchar(60)
    `****`
    `cities_id` FK
)

The user table stores the city id.

But what if I missed a few cities ... How does the user then save his city name in the user table which does not accept any city name but only IDs.

Can I have one more column city_name right after the cities_id in the table user something like

CREATE TABLE `user` (
    `id` int,
    `name` varchar(60)
    `****`
    `cities_id` FK
    `citiy name` varchar(100)
)

to record the data entered by the user at the time of registration? Can this be done?

like image 878
Daksh B Avatar asked Dec 07 '22 13:12

Daksh B


1 Answers

You can add a type to city table tag, the user can't find their corresponding to the city allows him to type the name of his city, and then you in the city, and will create a corresponding record in the table type marked as a special status (convenient operating personnel check and correction), at the same time to save the record id to the user record

CREATE TABLE `cities` (
    `id` int,
    `city_name` varchar(100),
    `type` int,
)

CREATE TABLE `user` (
    `id` int,
    `name` varchar(60)
    `****`
    `cities_id` FK
)
like image 172
stormbaron Avatar answered Dec 11 '22 11:12

stormbaron