简体   繁体   中英

Best practice to insert multiple contacts for single record in mysql

I have a table named booking which has a column contact_number .

What will be the best practice to insert multiple contact numbers for single booking? I have attached an image in which 1 entry has contact numbers separated with commas and other made 2 entries for same booking. Last thing i think of is to create a separate table for contacts. What will be the best way?

There will be an array for multiple contacts, I am coding in codeigniter.

在此处输入图片说明

if I was you I would make a customer table with customer information. If they [ the customer ] have multiple contact numbers you can make a "more information" column (something like this) and when they book again you kown who they are, they will already havea record. It will give you a feel to remember who they are (is somthing like market strategies [maybe?])

There are 3 ways that I can think of:

  1. Using a separate table and having a one to many relationship with the booking

    • best for scalability, indexing, architecture
  2. Using a JSON string to store them in one field

    • not easily searchable, if you plan not to use it somewhere except the booking page, it's better
  3. Using multiple fields like up to 5 (contact number 1, contact number 2..)

    • if you think to assign a limit for that, and you may sometime search for a number

Choose wisely. Think what you (will) need and what you plan to do first, then choose the method.

Create an array and encode with JSON

$contact_number = array(
'mobile' => '+1 32313213',
'fax' => '+1 32432432',
'phone' => '+1 8984234234'
);

$booking_contact = json_encode($contact_number);

Now you can insert $booking_contact into your database. If you want to use it just decode JSON pass true for associative array

//Suppose $row is containing you row result

$booking_contact = json_decode($row['booking_contact'], true);

echo $booking_contact['mobile']; // Output +1 32313213
echo $booking_contact['fax']; // Output +1 32313213
echo $booking_contact['phone']; // Output +1 8984234234

There is an unspoken rule in RDBMS - never ever use commaseparated strings for the purpose of m:n relations - it is simply violating the first normal form.

For more information regarding this matter take a look here

In your case you need :

  • a Table - booking (as you already mentioned)
  • a Table - contact (i assume you already have one as you already have numbers for) a Table
  • a Table - booking_contact with fields ( booking_id , contact_id )

Now if you want all contacts from a booking ID you simply write in Codeigniter style

$query = $this->db
    ->select('*')
    ->from('contact c')
    ->join('booking_contact bc', 'c.contact_id = bc.contact_id', 'left')
    ->join('booking b', 'bc.booking_id = b.booking_id')
    ->where('b.booking_id', 1)
    ->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM