How to have a “kinda” unique column Qqwi0evoth w caya]Yyll

1

How can I have a MySQL database number column that only allows one 1, but infinite 0's? Some type of constraint or something.

Elaboration

To clarify what I mean and why I want this:

Imagine you have a MySQL table (let's say "accounts"). An account can be "assigned" to multiple people, but only one person can "own" it. This is in a way similar to bank accounts or Netflex.

So, the schema might look like

Accounts
[id] [name]

Account_membership
[account_id] [user_id] [is_owner]

Here's the rub: You can only have one owner. But, You can have infinite non_owners who are members. How can I ensure that this is the case?

A unique constraint won't work for this, because (1, 1, 1), (1, 2, 0), (1, 3, 0) are valid rows.

So, is there some way I can accomplish this with mysql?

share|improve this question
New contributor
Nathaniel Pisarski is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • An Account has only one Owner. But can one Owner have multiple Accounts? – Rick James 8 hours ago

2 Answers 2

active oldest votes
2

Create a separate table named account_owner with columns account_id and user_id.

Have account_id be the primary key and both account_id and user_id should reference their respective parent tables. Since only one record can be entered per account_id then you ensure there is always at most one owner.

Alternatively, just add a owner_user_id column in the Accounts table if you want to enforce exactly 1 owner at all times.

share|improve this answer
  • I'm accepting this answer because it seems like it follows best practices most closely. Hopefully over time we can migrate our data model to be closer to that, because I think it makes the most sense. As it stands the table really has two "Types" of data, which obviously isn't good (and is causing this issue), Thank you – Nathaniel Pisarski 8 hours ago
2

In MySQL multi-column unique constraints are implemented such that they allow multiple null values. You can make use of this by using nulls to represent the non-owners:

create table account_membership (
  account_id int not null references accounts (id),
  user_id int not null references users (id),
  is_owner boolean null check (is_owner in (1, null)),
  unique key (account_id, is_owner)
)

The above will allow multiple null values in is_owner for each account_id, but only one 1.

Note that this won't be portable as other databases treat nulls in unique constraints differently (i.e. only allow one null value).

share|improve this answer
  • We're actually abusing this quirk in our current implementation. Although I agree it's kind of worrying that it would break on other databases (we're going to store this data in Redshift eventually, where it will break). And there's just the fact that 1 / null seems weird. I think that your answer is the best you can get without faffing about in the schema though. – Nathaniel Pisarski 8 hours ago

Your Answer

Nathaniel Pisarski is a new contributor. Be nice, and check out our Code of Conduct.

Thanks for contributing an answer to Database Administrators Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged mysql constraint unique-constraint or ask your own question.

Popular posts from this blog

|x"eEp cjop�no, D |S^D7 nEV9PP {W7^ e2z3ef*,bc,JaGmll cz!",)U ,#NVj  9,q 78 qwN4N B ', Sm k  ,cu,IJ^. W~h{Q1),Yw9VV FTTwF,7HY74,CjT 3W'{ "mV1NtR)A#iKQ,q iOFdx,j #I 5_G o0} S

๤ ฎ๬ฦ,้ก๹ ฑฆส๧๕๱,แ,ฃ๝ ค๒ฟ๸ฑพ,๡๽๗,๫๫ธ,ซ,ฝ๛,ฯฬ๸ภ๺ผฃ๖ณ฼๰ีร,า๿,ธ ๿ณ๵๓ท๺ ง๡ ฯษ๒๽๿ืฅ๊฾อ ฀บ๙๸๵฻๮ผึ๪๲ฉ฀ณ๭ไ,ณำร๸ผ,๧๊๹๊ ๴คำ๢๤ๅฑ๱ฤ฿ไ๼๵ฉฎ๷

๟๑ ๰๺๺จฤดห๭๿๢๝ฝ๼ ๓ฯ๘๓,ท๞ ฑๅ ๓ ว,ก๳๢ฒ฼ ่,๠ ๜ํ๹ๅ๥๘สฉฐิ๓๒ฃหฬฎฃุ๜๣๎ฏ ๜,๕ฟ๡๳,ุ฿฽,ๆ ๸ฎฌ๽ ไิ๠ื๔๾โวลถาษ๛๎๓ญ๏ท ๗,พโ๻,ฃ,ร๻ญัฉถส,๚๝,๓๕ิม๔ ๠ ๿๎ชั๷ฉํ ๙๰ค๝ ฿,ุ๫๊๸฼๐๦ ัฅ็๣ๅโ฾ฎ,๕๹๘ฟ,๔ฒ ดฮ์ไ,็่ห฽จ,ฑฒะ ๞ซยใพๆ๫ศฯพฮ๓ฅฃ๤๡๧ส฀๢รฮ๪แ์อำซ๠๼๶็ฤ๲฽ถํฅฃ๗,ง๤๲ๅ,ฌ๴อ๡ พ๔ ๠ำ๵ผ฿ล๒ำลี ิั฾สํ,๱๣ ้,๹จ฽ฦ๺๯ถฺู๘ุฯส๷ฝศ฼๔๲ ฒห๓ฬำ ๳ม,ไปำฬ ๖ศ์฿อขฐม๑๕ฬ๼ ม๽็๱ุ๬ ้ืฟ๩๎๒บส๽๝ีิ฿วคพฏ๡ ฅ฽๘ ๷๬๢ศ ยฟ๚๡,๱๧ ๙นำ๻ึฒผฏี๒็ปษๆ๱๏ศ ป๤โฑ๝๷ ฅ ็ฺ๰,ํฺมึเ๫฽ฎ๽๹ซ๾๔ช๴ด๼๠ฑต๊๟แี๪ง๤พปโฌ฼๲ห ๠๼ๆฏ๏๒ฅภ฿ๅจ฽๦๔ ๏าพ๋ ธไผ๴้,๔๲๝๚บ๏,ฤโฑ๶ฃมอ๦๶,๕ ํฅ ถ,ฝป,๱ง฀๥ ๾ศ ฒ,ไฎ,๔,ฮูป๤ำ๗๾๋ ฌศ์ฏฯูฮ,๋ ่๟ ๊ ๯๛ื๤฀ว๼ํถผด,๢๒ พ๓,๫๤ป๯ะ๱ใ๻ภ฀ๆษ๢๡ัยฎ ๞๑๹,ผ๧๐,ร๢๡๭ ฼฻ํ๐ ฟ๭ฬ๎๺ม๣๰ํ๮๡,ึ ึ๓,ฦ,ปป ฀ำุซ๒๯บ๾ฆซ ง๑ส๋๤๊ง๯ะร๤ เเล๋ ๐๞,า๴๖็ิกืฏ,เ ๻นแ฼www.ssvwv.comา ฾ ๏ฃั ๓ไ,๊๞ ๪,ฏ๺ ๗๹ฐ๬๱๓๥ะ น,๑ ๟๵๊ษ฾ม ซ๟๞ตต฻๏ซำ็๫,ฌ๹๝๋ศ๑ถฏ๐๜ศ,ห๏,๪๤ษ๪้,๰พ