我是SQL和Oracle的新手。我在玩Oracle。我试图限制电话号码,因此它只允许以特定方式插入电话号码。
以下是我的表格:
CREATE TABLE FIRSTTRY(
USERID CHAR(9) NOT NULL,
USERNAME VARCHAR(20) NOT NULL,
EMAILADDRESS VARCHAR(40),
PHONENUMBER VARCHAR(12),
PROFILEPICTURE BLOB,
PRIMARY KEY(USERID)
);
我对这样的电话号码设置了约束:
ALTER TABLE FIRST_TRY
ADD CONSTRAINT FIRSTTRY_PHONENUMBER CHECK
(PHONENUMBER LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'
OR PHONENUMBER LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
OR PHONENUMBER LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]');
我将值插入到表中:
INSERT INTO FIRST_TRY VALUES (1,'ABCD','ABCD@YAHOO.COM','0504562893',NULL);
但它给了我一个错误: 以下是错误:
02290. 00000 - "check constraint (%s.%s) violated"
*Cause: The values being inserted do not satisfy the named check
*Action: do not insert values that violate the constraint.
任何想法,建议?
答案 0 :(得分:1)
ALTER TABLE FIRST_TRY
ADD CONSTRAINT FIRST_TRY_PHONENUMBER
CHECK (regexp_like (PHONENUMBER,'^(\d{3}-\d{3}-?\d{4}|\d{10})$'))
INSERT INTO FIRST_TRY VALUES (1,'ABCD','ABCD@YAHOO.COM','0504562893' ,NULL);
INSERT INTO FIRST_TRY VALUES (2,'ABCD','ABCD@YAHOO.COM','050-4562893' ,NULL);
INSERT INTO FIRST_TRY VALUES (3,'ABCD','ABCD@YAHOO.COM','050-456-2893',NULL);