2

ユーザーテーブルを考えてみましょう: Username,email1,email2

ここで、このテーブルの 2 つの行に共通の電子メール アドレスがないことを確認したいと思います。これを確実にする最善の方法は何でしょうか?

例: テーブルに "Bill,bill@email.com,bill@gmail.com" という行がある場合、"Billy,billy@yahoo.com,bill@email.com" のような行を挿入しようとするとエラーが発生します。

前もって感謝します、チャンドレッシュ

4

3 に答える 3

4

It looks like you're trying to force a combo between 1:1 and 1:multiple with your data model. Your better bet is to store each Username/Email combo separately. One row for "Username,email1" and another row for "Username,email2". If you have additional "Username"-related fields, those could stay in the core User table, while emails would move to a two-column Email table with a multi-column unique index on (Username,Email).

于 2009-08-10T19:05:16.193 に答える
1

残念ながら、check制約は MySQL の機能ではありません。まあ、そうですが、無視されています。

trigger代わりに , を使用します。

create trigger MyValidation after insert on user for each row
begin
    if (select count(*) from user where 
        email1 = new.email1
        or email1 = new.email2 
        or email2 = new.email1 
        or email2 = new.email2) > 0 then
        --I know that the delete could use this logic, but you can
        --insert other instructions here.

        delete from user where username = new.username

    end if
end
于 2009-08-10T19:02:52.987 に答える
0

ある種のストアド プロシージャを使用できるかもしれませんが、これはデータを挿入する前にチェックできるものかもしれません。

SELECT email1, email2 FROM yourTable 
WHERE email1 LIKE %@email% or email2 LIKE %@email%
于 2009-08-10T19:04:48.940 に答える