Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you are trying to achieve is not very clear to me, but it looks very much like you just need to add some two-columns unique constraints.</p> <ul> <li>an <code>email</code> must be unique for one given <code>account_id</code>:</li> </ul> <pre class="lang-sql prettyprint-override"><code>ALTER TABLE your_table ADD UNIQUE (account_id, email); </code></pre> <ul> <li>a <code>phone number</code> must be unique for one given <code>account_id</code>:</li> </ul> <pre class="lang-sql prettyprint-override"><code>ALTER TABLE your_table ADD UNIQUE (account_id, phone); </code></pre> <p>Both indexes may exist at the same time on your table. Either could raise a "duplicate-key violation" error, and would trigger the <code>IGNORE</code> or the <code>ON DUPLICATE</code> clauses of your insertions.</p> <hr> <p>That being said, there is an issue in your structure. You are about to duplicate your customers' details for each <code>account_id</code> they are in business with.</p> <p>You should have a <code>customers</code> table that contains all your customer's contact details (and only that), another <code>accounts</code> table -- your "offices", if I understand it right -- and finally one relation table to model the <a href="http://en.wikipedia.org/wiki/Many-to-many_%28data_model%29" rel="nofollow">n-n relationship</a> between <code>customers</code> and <code>accounts</code>:</p> <pre><code>CREATE TABLE customers_accounts ( customer_id INT NOT NULL, account_id INT NOT NULL, PRIMARY KEY (customer_id, account_id), FOREIGN KEY (customer_id) REFERENCES customers(id) FOREIGN KEY (account_id) REFERENCES accounts(id) ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload