2016-04-17 22 views
0

使用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') 

注意:它不需要在一個約束中。提前致謝。

+0

什麼錯誤?在聲明的最後完成paranthesis之後,這在oracle中起作用。關閉客戶端( –

+0

)也在sqlserver中工作,在sqlfiddle中嘗試。關閉paranthesis.OR(type ='Faculty'和電子郵件like'%@d.umn.edu'))) –

+0

當我嘗試插入數據時,我得到以下錯誤:INSERT語句與CHECK約束「CK_Customer_email」衝突。數據庫「ZZ16AndrewW」發生衝突,表「dbo.Customer」。 該聲明已被終止。 –

回答

1

你錯過了一個右括號。

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 
) -- <<-- you are missing the closing parenthesis from create table Customer (

在您的insert語句中,您的密碼和電子郵件地址已顛倒。我換他們在列的列表和它的工作原理是這樣的:

INSERT INTO Customer (F_name, M_name, L_name, type, street, city, state, zip, email, password) 
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') 

(考慮避免明文存儲密碼)

+0

缺少的括號是一個錯字。抱歉。它仍然在拋出一個錯誤。 –

+0

你能夠創建一個SQL小提琴示例。我在這裏創建了一個工作示例:http://sqlfiddle.com/#!3/12b7b – user212514

+0

我嘗試在SQL小提琴上使用MS SQL Server 2014選項。我的創建客戶區塊有效,但是當我嘗試向其中插入數據時,出現錯誤。我剛剛添加了我的插入塊測試學生和教師類型的原始問題。謝謝 –

2
  1. 正如評論前面指出的,通過user212514,你有錯過了結束語。

  2. 其次,在您的插入中,電子郵件ID是最後一個。根據您的值,電子郵件ID將被輸入到密碼字段中,並且密碼正被輸入到電子郵件ID字段中。

你的語句應該是:

INSERT INTO Customer (F_name, M_name, L_name, type, street, city, state, zip, email, password) 
    VALUES 
    ('Jarvis', 'Marvin', 'Vinton', 'Student', '3525 Metz Lane', 'Runnemede St.', 'NJ', 08078, '[email protected]', 'Kohque4Oo') --interchanged email and password. 

INSERT INTO Customer (F_name, M_name, L_name, type, street, city, state, zip, email, password) 
values 
    ('Olivia', 'Audrey', 'Keele', 'Faculty', '2850 Snowbird Ln.', 'Waco', 'NE', 68460, '[email protected]', 'Blackdiamond26') 

有關詳細信息: http://www.w3schools.com/sql/sql_insert.asp