r/SQL • u/AssistanceClean948 • Sep 16 '26
SQLite Best way to create email field.
right now my users table like this:
CREATE TABLE
IF NOT EXISTS Users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE COLLATE NOCASE,
passwordHash TEXT NOT NULL,
createdAt TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updatedAt TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
CHECK(length(trim(name)) >= 1),
CHECK(length(trim(email)) >= 1),
CHECK(length(trim(passwordHash)) >= 1)
) STRICT;
as far as I know email names are case-insensitive. But not sure about for all email providers. Also I guess COALLATE NOCASE only works for ASCII. so what if user email contains non-ASCII characters? even could it contain?
3
Upvotes
18
u/gumnos Sep 16 '26
Fortunately, "everybody knows what an email address is", right? 😆
While you can search for emails with case-insensitive comparisons, you should always store exactly what the user provides and use that case-preserved address when sending because the local part (to the left of the "@") is case-sensitive by RFC mandate.
Many email-providers opt to treat the local portion as case-insensitive, but the RFCs require intermediaries (such as you/your-app between the user entering the address and their MTA receiving the address/message) to preserve case.
The domain (the part to the right of the "@") is case-insensitive and can be normalized if you want, though be aware that it's valid to have an "@" in the local portion (when quoted; see the above quiz-link), so it takes extra care if you plan to normalize the domain-name.
In full RFC compliance, non-ASCII characters in the domain-name should be translated to Punycode, and any non-7-bit-ASCII characters in the local portion should be encoded.
10
u/Imaginary__Bar Sep 16 '26
Two people can share an email address so you don't want that to be unique.
Also, email address should be case-insensitive but you should accept user-input as-is, then format the email address when it's used (eg, on output).
So when someone enters John.Smith@example.com you can output/match on john.smith@example.com
You can check for a valid email address in your app.
You should also accept non-roman character sets.
(I'd be tempted to store the email address in two parts; the "name" portion and the "domain" portion, but that's personal choice)
2
u/Old-Biscotti3842 Sep 17 '26
Your hunch about NOCASE is right, and the docs are more specific than "ASCII only". It folds the 26 upper case ASCII letters, and SQLite says it doesn't attempt full UTF case folding on purpose, due to the size of the tables that would require. lower() is the same story, ASCII unless you've built with ICU. So Ivan@example.com and ivan@example.com collide the way you want, while Özkan@example.com and ÖZKAN@example.com don't. Whether you should be accepting the second kind at all is the separate question gumnos answered.
The other thing worth untangling: storing what the user typed and your COLLATE NOCASE aren't in conflict. A collation only affects comparison, the bytes on disk stay exactly what was inserted, so the column-level COLLATE gets you both at once, original case preserved and the second signup still rejected by the unique index. That part you've already got right.
If you want the non-ASCII case handled properly, fold it in your app and keep the result in a second column, email_norm or whatever, with the UNIQUE moved onto that one. A generated column won't get you there, the generator would be lower() again. You can register your own deterministic function and use that instead, but then the file only opens properly inside your app, which is a bad trade for a users table.
One more, since email is your only external identity. Unique address isn't unique person: Gmail ignores dots in the local part and everything after a +, so john.smith+shop@gmail.com and johnsmith@gmail.com are one inbox, and most other providers don't do either of those things. Don't try to normalize that in the database. It's per-provider policy rather than a property of addresses, and stripping dots for, say, a Fastmail address would merge two different people. If you need it, it belongs in a signup-time check against a list of providers you know behave that way.
And a freebie since you're doing length checks already: there's a ceiling too, 254 characters. That's what's left of RFC 5321's 256 byte forward path once the angle brackets are in there.
-1
u/Blomminator Sep 16 '26
Email addresses are insensitive, unless very very niche custom weird setup somewhere.
Yes, non-ASCII is possible. So if that is entered, it's not working correctly. Haven't tested it, but the unique should not work.
What are you trying to solve?