您好我正在尝试在Band属性中编写If语句但没有成功T_T。我想要做的是拥有单个字符A,B,C,D,具体取决于owner_category,Sheds和Water_Supply属性。任何帮助都会很棒^^。
CREATE TABLE Rent_Band
(
Plot_ID NUMBER(3),
Band VARCHAR(1)
IF SELECT * FROM Rent_Band WHERE Owner_Category = 'Council' AND WHERE Sheds ='No' THEN Band ='A'
ELSE IF SELECT * FROM Rent_Band WHERE Owner_Category = 'Private' AND WHERE Water_Supply ='Yes' THEN Band ='D'
ELSE IF SELECT * FROM Rent_Band WHERE Owner_Category = 'Council' AND WHERE Water_Supply ='No' AND WHERE Sheds ='Yes' THEN Band ='B'
ELSE IF SELECT * FROM Rent_Band WHERE Owner_Category = 'Private' AND WHERE Water_Supply ='No' AND WHERE Sheds ='No' THEN Band ='B'
ELSE THEN Band = 'C',
Rent_Charge NUMBER(4),
Owner_Category VARCHAR(10) CONSTRAINT Checking_Owner_Category CHECK((Owner_Category='Private') OR (Owner_Category='Council')),
Sheds VARCHAR(3) CHECK((Sheds='Yes') OR (Sheds='No')),
Water_Supply VARCHAR(3) CHECK((Water_Supply='Yes') OR (Water_Supply='No'))
)
答案 0 :(得分:4)
在虚拟列上阅读the documentation:
CREATE TABLE Rent_Band
(
Plot_ID NUMBER(3),
Band VARCHAR(1) AS
(CASE WHEN Owner_Category = 'Council' AND Sheds ='No' THEN 'A'
WHEN Owner_Category = 'Private' AND Water_Supply ='Yes' THEN 'D'
WHEN Owner_Category = 'Council' AND Water_Supply ='No' AND Sheds ='Yes' THEN 'B'
WHEN Owner_Category = 'Private' AND Water_Supply ='No' AND Sheds ='No' THEN 'B'
ELSE 'C' END),
Rent_Charge NUMBER(4),
Owner_Category VARCHAR(10) CONSTRAINT Checking_Owner_Category CHECK((Owner_Category='Private') OR (Owner_Category='Council')),
Sheds VARCHAR(3) CHECK((Sheds='Yes') OR (Sheds='No')),
Water_Supply VARCHAR(3) CHECK((Water_Supply='Yes') OR (Water_Supply='No'))
)
顺便说一句,我强烈建议:
bit
和Yes
使用No
字段而不是Sheds
/ Water_Supply
约束(节省了空间和间接检查限制,并直接转换为大多数应用程序语言中的布尔值)Owner_Category
的查找表而不是字符串约束,原因基本相同