使用SQL Server我試圖做一些非常簡單的事情,但解決方案正在逃避我。如何將邏輯運算符插入到SQL約束中?
我的數據庫在創建時發生錯誤。我真的只是想檢查customer.type
是否爲'Student'
那裏有'@'
那裏對應customer.email
列內。
反之亦然,如果customer.type
是'Faculty'
那他們的customer.email
列以'@d.umn.edu'
結尾。
我已經花了大約半個小時的時間來處理這個問題,而且我愚蠢地無法完成它的工作。
Create table Customer
(
CID int identity(1,1) primary key,
F_name char(25),
M_name char(25),
L_name char(25),
type char(7),
street varchar(50),
city varchar(25),
state char(2),
zip numeric(5),
password varchar(25) not null,
email varchar(25) UNIQUE Not null,
Constraint CK_Customer_type check (type in ('Student','Faculty')),
Constraint CK_Customer_email check((type='Student' AND email like '%@%') OR (type='Faculty' AND email like'%@d.umn.edu'))-- this is throwing an error
)
INSERT INTO Customer (F_name, M_name, L_name, type, street, city, state, zip, password, email)
VALUES
('Jarvis', 'Marvin', 'Vinton', 'Student', '3525 Metz Lane', 'Runnemede St.', 'NJ', 08078, '[email protected]', 'Kohque4Oo'),
('Olivia', 'Audrey', 'Keele', 'Faculty', '2850 Snowbird Ln.', 'Waco', 'NE', 68460, '[email protected]', 'Blackdiamond26')
注意:它不需要在一個約束中。提前致謝。
什麼錯誤?在聲明的最後完成paranthesis之後,這在oracle中起作用。關閉客戶端( –
)也在sqlserver中工作,在sqlfiddle中嘗試。關閉paranthesis.OR(type ='Faculty'和電子郵件like'%@d.umn.edu'))) –
當我嘗試插入數據時,我得到以下錯誤:INSERT語句與CHECK約束「CK_Customer_email」衝突。數據庫「ZZ16AndrewW」發生衝突,表「dbo.Customer」。 該聲明已被終止。 –