Cross-table constraint

Cross-table constraint

am 09.11.2009 22:55:02 von Sebastiaan van Erk

--------------ms000109050203060401020208
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

I have the following model:

Domain (*)<-(1) Account (*)<-(1) User

That is, each user belongs to exactly 1 account, an account can have
multiple users; each account belongs to a single domain, and a domain
can have multiple accounts.

A user has an email address, which must be unique across the domain
(since it's used to log into that domain: on login you select the domain
and then enter your email and password).

However, the email address need *not* be unique across different
domains. Thus, I want the following combination to be unique:

(Domain.id, User.email)

Is there any way I can do this without duplicating the domain id in the
User table?

Regards,
Sebastiaan

--------------ms000109050203060401020208
Content-Type: application/x-pkcs7-signature; name="smime.p7s"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="smime.p7s"
Content-Description: S/MIME Cryptographic Signature

MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEH AQAAoIIJUTCC
AwMwggJsoAMCAQICEGLYTbNs7ILV8FjWe4hYppAwDQYJKoZIhvcNAQEFBQAw YjELMAkGA1UE
BhMCWkExJTAjBgNVBAoTHFRoYXd0ZSBDb25zdWx0aW5nIChQdHkpIEx0ZC4x LDAqBgNVBAMT
I1RoYXd0ZSBQZXJzb25hbCBGcmVlbWFpbCBJc3N1aW5nIENBMB4XDTA5MDcw MTA1MjIxMVoX
DTEwMDcwMTA1MjIxMVowaDEQMA4GA1UEBBMHdmFuIEVyazETMBEGA1UEKhMK U2ViYXN0aWFh
bjEbMBkGA1UEAxMSU2ViYXN0aWFhbiB2YW4gRXJrMSIwIAYJKoZIhvcNAQkB FhNzZWJzdGVy
QHNlYnN0ZXIuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA xnkA/HlKscOk
7wNt76nwOKIIsm3qlgX3Sf3jNOl+gVBi4Fq3n5BfDR0FBj4pbiQT+Q9s2oTJ coX3/AdZuNga
UH/6+dhcyD2kxgvVK8WK41W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CQYD VQQGEwJaQTEl
MCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkgTHRkLjEsMCoGA1UE AxMjVGhhd3Rl
IFBlcnNvbmFsIEZyZWVtYWlsIElzc3VpbmcgQ0ECEGLYTbNs7ILV8FjWe4hY ppAwgYcGCyqG
SIb3DQEJEAILMXigdjBiMQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3Rl IENvbnN1bHRp
bmcgKFB0eSkgTHRkLjEsMCoGA1UEAxMjVGhhd3RlIFBlcnNvbmFsIEZyZWVt YWlsIElzc3Vp
bmcgQ0ECEGLYTbNs7ILV8FjWe4hYppAwDQYJKoZIhvcNAQEBBQAEggEAVLlE Wt6yVdq0JTtt
KpttTVgLpa0gBCMehcL7fdiSF8s1y133WRa2glGCrNMdHvp8IxwJAu0GGXCh vLd51aRiv4zg
QdGZgRdezE3wuSTEpusNrh9xD0OpOh39NHDoXlP5KIZ9vta+GVXIP8ECat8b Fm+nLzl2z51w
7px3wBYjFTsRp1MGnrhr9uEPjKCTIfB/f/YQlh4vg1neyFposXXL3k/9EPwH oTTgMdIKalEP
HdVbntYmcWUaWN9AN6YvAl3aHVc3O/adXVW7y4xHwf0cYEE2Yb+S0udPnyg0 eOQaeRIS4ey/
AO6SniW5jh2CIaK5XMYXbgw2WMrt2Nmon+/CpAAAAAAAAA==
--------------ms000109050203060401020208--

Re: Cross-table constraint

am 10.11.2009 09:38:16 von Johan De Meersman

I think triggers are your only option, here.

On 11/9/09, Sebastiaan van Erk wrote:
> Hi,
>
> I have the following model:
>
> Domain (*)<-(1) Account (*)<-(1) User
>
> That is, each user belongs to exactly 1 account, an account can have
> multiple users; each account belongs to a single domain, and a domain
> can have multiple accounts.
>
> A user has an email address, which must be unique across the domain
> (since it's used to log into that domain: on login you select the domain
> and then enter your email and password).
>
> However, the email address need *not* be unique across different
> domains. Thus, I want the following combination to be unique:
>
> (Domain.id, User.email)
>
> Is there any way I can do this without duplicating the domain id in the
> User table?
>
> Regards,
> Sebastiaan
>

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org