Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc: how to guarentee case insensitive uniqueness for a string #5382

Open
mattcrdb opened this issue Sep 5, 2019 · 5 comments
Open

Doc: how to guarentee case insensitive uniqueness for a string #5382

mattcrdb opened this issue Sep 5, 2019 · 5 comments
Assignees
Labels
C-doc-improvement no-issue-activity O-external Origin: Issue comes from external users. O-support Internal source: Support P-3 Low priority; nice-to-have T-missing-info

Comments

@mattcrdb
Copy link
Contributor

mattcrdb commented Sep 5, 2019

Matthew Vardi (mattcrdb) commented:

A customer brought up an issue when migrating over to CRDB.

How can he guarantee that an email string column prevents case insensitive duplicates.

One solution is to create a computed column using the lower() function and then applying the unique constraint using alter table.

ALTER TABLE users ADD COLUMN clean_email STRING AS (lower(email)) STORED;
ALTER TABLE users ADD CONSTRAINT unique_clean_email UNIQUE (clean_email).

I have confirmed this works as intended, however, might not be the best solution at scale.

Another solution suggested by @justinj is to use collate. I have not looked into how this works.

Postgres has a citext data type that allows for this functionality, from their documentation: The citext module provides a case-insensitive character string type, citext. Essentially, it internally calls lower when comparing values. Otherwise, it behaves almost exactly like text.

Jira Issue: DOC-321

@jseldess jseldess added A-sql C-doc-improvement O-external Origin: Issue comes from external users. O-support Internal source: Support P-2 Normal priority; secondary task T-missing-info labels Sep 11, 2019
@jseldess
Copy link
Contributor

@bdarnell, @awoods187, @justinj, should this be on the SQL roadmap somewhere?

@bdarnell
Copy link
Contributor

See cockroachdb/cockroach#22463, cockroachdb/cockroach#9682, and #1471.

Note that the definition of "case insensitivity" differs based on language (Turkish is the most notable one that is incompatible with English rules).

@jseldess jseldess removed their assignment Sep 11, 2019
@jseldess
Copy link
Contributor

@rmloveland, assigning to 20.1 milestone, since there's likely enough to handle for 19.2 already. But feel free to change.

@jseldess jseldess added this to the 20.1 milestone Sep 11, 2019
@ericharmeling ericharmeling added P-3 Low priority; nice-to-have and removed P-2 Normal priority; secondary task labels Jan 7, 2021
@jseldess jseldess removed this from the 20.1 milestone May 25, 2021
@jseldess jseldess removed the G-sql label Jan 18, 2022
@github-actions
Copy link

We have marked this issue as stale because it has been inactive for
18 months. If this issue is still relevant, removing the stale label
or adding a comment will keep it active. Otherwise, we'll close it in
10 days to keep the issue queue tidy. Thank you for your contribution
to CockroachDB docs!

Copy link

Richard Loveland (rmloveland) commented:
Ryan Kuo assigning to you since it’s SQL Queries / Migrations (your areas per this doc)

There’s also an interesting internal discussion on collation and migrating from SQL Server that seems related to this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-doc-improvement no-issue-activity O-external Origin: Issue comes from external users. O-support Internal source: Support P-3 Low priority; nice-to-have T-missing-info
Projects
None yet
Development

No branches or pull requests

6 participants