How to make a case-insensitive unique column in SQLite(如何在 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)
)
这篇关于如何在 SQLite 中创建不区分大小写的唯一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 SQLite 中创建不区分大小写的唯一列


基础教程推荐
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01