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 中创建不区分大小写的唯一列
基础教程推荐
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 在多列上分布任意行 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
