为 phone 或 MYSQL 中的邮政编码创建约束?
Creating a constraint for a phone or zip code in MYSQL?
正在创建以下 table (MySQL 8.0):
create table Customer_Dim(
customer_ID int not null,
FirstName varchar(30) not null,
LastName varchar(30) not null,
Gender char(1) not null CHECK (gender in ('m','f')),
Street1 varchar(100) not null,
Street2 varchar(100) null,
City varchar(30) not null,
StateAbbrev char(2) not null,
ZipCode char(5) not null CHECK (ZipCode LIKE repeat('[0-9]',5)),
PrimaryPhone varchar(10) not null CHECK (PrimaryPhone LIKE repeat('[0-9]',10)),
EmailAddress varchar(50) default null,
constraint custkey Primary Key(customer_ID)
);
但是当我输入以下插入语句时:
insert into Customer_DIM
values (10001, 'Josh', 'Dexter', 'M', '123 E Elm st.', null, 'Denver', 'CO', '80002', '2023459767', 'test@aol.com')
phone 和邮政编码检查失败。据我所知,它应该有效吗?
我认为 regexp
比 repeat
更重要(重复字符)
使用 repeat
会得到这样的结果:
mysql> select repeat('[0-9]', 5);
+---------------------------+
| repeat('[0-9]', 5) |
+---------------------------+
| [0-9][0-9][0-9][0-9][0-9] |
+---------------------------+
因此,请改用 regexp
:
create table Customer_Dim(
customer_ID int not null,
FirstName varchar(30) not null,
LastName varchar(30) not null,
Gender char(1) not null CHECK (gender in ('m','f')),
Street1 varchar(100) not null,
Street2 varchar(100) null,
City varchar(30) not null,
StateAbbrev char(2) not null,
ZipCode char(5) not null CHECK (ZipCode regexp '[0-9]{5}'),
PrimaryPhone varchar(10) not null CHECK (PrimaryPhone regexp'[0-9]{10}'),
EmailAddress varchar(50) default null,
constraint custkey Primary Key(customer_ID)
);
插入相同(好的)示例:
insert into Customer_Dim
values (10001, 'Josh', 'Dexter', 'M', '123 E Elm st.', null, 'Denver', 'CO', '80002', '2023459767', 'test@aol.com')
错误 phone 示例插入:
insert into Customer_Dim
values (10001, 'Josh', 'Dexter', 'M', '123 E Elm st.', null, 'Denver', 'CO', '80002', '202359767', 'test@aol.com')
报错:
Check constraint 'Customer_Dim_chk_3' is violated.
错误的 zip 示例插入:
insert into Customer_Dim
values (10001, 'Josh', 'Dexter', 'M', '123 E Elm st.', null, 'Denver', 'CO', '8002', '2023459767', 'test@aol.com')
报错:
Check constraint 'Customer_Dim_chk_2' is violated.
DB fiddle example 包含以上所有信息。
(包括最后的 repeat
示例)
知道MySQL检查约束在8.0.16版本之前被解析但是被忽略了:
正在创建以下 table (MySQL 8.0):
create table Customer_Dim(
customer_ID int not null,
FirstName varchar(30) not null,
LastName varchar(30) not null,
Gender char(1) not null CHECK (gender in ('m','f')),
Street1 varchar(100) not null,
Street2 varchar(100) null,
City varchar(30) not null,
StateAbbrev char(2) not null,
ZipCode char(5) not null CHECK (ZipCode LIKE repeat('[0-9]',5)),
PrimaryPhone varchar(10) not null CHECK (PrimaryPhone LIKE repeat('[0-9]',10)),
EmailAddress varchar(50) default null,
constraint custkey Primary Key(customer_ID)
);
但是当我输入以下插入语句时:
insert into Customer_DIM
values (10001, 'Josh', 'Dexter', 'M', '123 E Elm st.', null, 'Denver', 'CO', '80002', '2023459767', 'test@aol.com')
phone 和邮政编码检查失败。据我所知,它应该有效吗?
我认为 regexp
比 repeat
更重要(重复字符)
使用 repeat
会得到这样的结果:
mysql> select repeat('[0-9]', 5);
+---------------------------+
| repeat('[0-9]', 5) |
+---------------------------+
| [0-9][0-9][0-9][0-9][0-9] |
+---------------------------+
因此,请改用 regexp
:
create table Customer_Dim(
customer_ID int not null,
FirstName varchar(30) not null,
LastName varchar(30) not null,
Gender char(1) not null CHECK (gender in ('m','f')),
Street1 varchar(100) not null,
Street2 varchar(100) null,
City varchar(30) not null,
StateAbbrev char(2) not null,
ZipCode char(5) not null CHECK (ZipCode regexp '[0-9]{5}'),
PrimaryPhone varchar(10) not null CHECK (PrimaryPhone regexp'[0-9]{10}'),
EmailAddress varchar(50) default null,
constraint custkey Primary Key(customer_ID)
);
插入相同(好的)示例:
insert into Customer_Dim
values (10001, 'Josh', 'Dexter', 'M', '123 E Elm st.', null, 'Denver', 'CO', '80002', '2023459767', 'test@aol.com')
错误 phone 示例插入:
insert into Customer_Dim
values (10001, 'Josh', 'Dexter', 'M', '123 E Elm st.', null, 'Denver', 'CO', '80002', '202359767', 'test@aol.com')
报错:
Check constraint 'Customer_Dim_chk_3' is violated.
错误的 zip 示例插入:
insert into Customer_Dim
values (10001, 'Josh', 'Dexter', 'M', '123 E Elm st.', null, 'Denver', 'CO', '8002', '2023459767', 'test@aol.com')
报错:
Check constraint 'Customer_Dim_chk_2' is violated.
DB fiddle example 包含以上所有信息。
(包括最后的 repeat
示例)
知道MySQL检查约束在8.0.16版本之前被解析但是被忽略了: