如何在 SQLite 中创建不区分大小写的唯一列

2021-12-26 00:00:00 pdo sqlite

我一直无法找到这个问题的答案.我正在尝试创建一个具有唯一电子邮件地址列的表.当我这样做时

I haven't been able to find the answer to this. I'm trying to create a table with a unique email address column. And when I do

CREATE TABLE users (
  email TEXT PRIMARY KEY,
  password TEXT NOT NULL CHECK(password<>''),
  UNIQUE (lower(email))
)

使用 PDO 时出现错误:

when using PDO, I get the error:

致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[HY000]:一般错误:1 靠近(":script.php:65 中的语法错误'堆栈跟踪:#0 script.php(65):PDO->exec('CREATE TABLE us...') #1 {main} 在第 65 行的 script.php 中抛出

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1 near "(": syntax error' in script.php:65 Stack trace: #0 script.php(65): PDO->exec('CREATE TABLE us...') #1 {main} thrown in script.php on line 65

第 65 行是 CREATE TABLE 行.如果我取出 UNIQUE,它工作正常.有更好的方法吗?

Line 65 is the CREATE TABLE line. If I take out the UNIQUE, it works fine. Is there a better way of doing it?

推荐答案

COLLATE NOCASE 是你的朋友:

COLLATE NOCASE is your friend:

CREATE TABLE users (
  email TEXT PRIMARY KEY,
  password TEXT NOT NULL CHECK(password<>''),
  UNIQUE (email COLLATE NOCASE)
)

相关文章