创建表时的条件输入

时间:2018-02-03 22:47:15

标签: sql sql-server

我做到了这一点并且有效。

CREATE TABLE Employee (
 Employee_ID varchar2(10) PRIMARY KEY NOT NULL,
 Office_ID varchar2(7) NOT NULL,
 Emp_FirstName varchar2(20) NOT NULL,
 Emp_LastName varchar2(20) NOT NULL,
 Emp_Gender varchar2(1) NOT NULL, 
 Emp_DateOfBirth Date NOT NULL,
 Hire_Date date NOT NULL,
 Emp_CurrentDate date default sysdate,
 Emp_Telephone varchar2(11) NOT NULL,
 Emp_Email varchar2(30) NOT NULL,
 Emp_AddressLine varchar2(50) NOT NULL,
 Emp_PostCode varchar2(8) NOT NULL,
 Emp_Speciality varchar2(20) NOT NULL,
 Emp_Qualification varchar2(20) NOT NULL,
 Emp_AwardingBody varchar2(30) NOT NULL,
 Emp_Salary number(6) NOT NULL,
 Emp_Supervised_By varchar2(10) NOT NULL,
 Employment_History1 varchar2(50),
 Employment_History2 varchar2(50),
 Employment_History3 varchar2(50) ,
 CONSTRAINT fk_staff_office FOREIGN KEY (Office_ID) REFERENCES office (Office_ID), 
 CONSTRAINT Hire_Date_CK check (Hire_Date < Emp_CurrentDate AND (Hire_Date - Emp_DateOfBirth)/365 > 18),
 CONSTRAINT Emp_DateOfBirth_CK check (Emp_DateOfBirth > TO_DATE('1900-01-01', 'YYYY-MM-DD')),
 CONSTRAINT Emp_Salary_CK check (Emp_Salary > 0 AND Emp_Salary < 150000),
 CONSTRAINT Emp_Gender_CK CHECK (Emp_Gender in ('M','F')), 
 CONSTRAINT Emp_Email_CK CHECK ( Emp_Email like '%_@__%._%'),
 CONSTRAINT Emp_Telephone_CK CHECK (regexp_like(Emp_Telephone, '^[0123456789]{11}$') AND Emp_Telephone like '0%'),
 CONSTRAINT Emp_FirstName_CK CHECK (regexp_like(Emp_FirstName, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]{1,20}$')),
 CONSTRAINT Emp_LastName_CK CHECK (regexp_like(Emp_LastName, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]{1,20}$')),
 CONSTRAINT Emp_PostCode_CK CHECK (regexp_like ( Emp_PostCode , '([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\s?[0-9][A-Za-z]{2})')),
 CONSTRAINT Emp_Speciality_CK CHECK (regexp_like(Emp_Speciality, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,20}$')),
 CONSTRAINT Emp_Qualification_CK CHECK (regexp_like(Emp_Qualification, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,20}$')),
 CONSTRAINT Emp_AwardingBody_CK CHECK (regexp_like(Emp_AwardingBody, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,30}$'))
 );

我现在要做的是创建一个约束,使我能够在Emp_Supervised_ByEmployee_ID时自动将属性Emp_Speciality的值设置为CONSTRAINT Emp_Supervised_By_CK check (CASE WHEN Emp_Speciality = 'MANAGER' THEN Emp_Supervised_By = Employee_ID)属性的相同值&#39;管理器&#39 ;.

我尝试这样做,但它不起作用:

expand

是否要求使用CONSTRAINTS NOT TRIGGERS

2 个答案:

答案 0 :(得分:2)

因为您提到了计算列,所以可能会满足您的业务需求,而不是约束。

    CREATE TABLE Employee (
     ...
     Employee_ID varchar2(10) PRIMARY KEY NOT NULL,
     Emp_Speciality varchar2(20) NOT NULL,
     Emp_Supervised_By AS (CASE WHEN Emp_Speciality = 'Manager' THEN Employee_ID ELSE NULL END)
     ....
     );

答案 1 :(得分:0)

我做了这个并且有效:

CREATE TABLE OrderLine ( Invoice_ID varchar2(10) PRIMARY KEY NOT NULL, Item_ID varchar2(10) NOT NULL, Unit_Price number(6,2) NOT NULL, Quantity number(6) NOT NULL, Total_Price number(38,2) AS (CASE WHEN Quantity >= 50 THEN ((Unit_Price*Quantity)-((Unit_Price*Quantity)*0.80)) ELSE Unit_Price*Quantity END) NOT NULL, CONSTRAINT fk_item_invoice FOREIGN KEY(Invoice_ID) REFERENCES Invoice (Invoice_ID), CONSTRAINT fk_item_item FOREIGN KEY(Item_ID) REFERENCES Item (Item_ID), CONSTRAINT Unit_Price_CK CHECK (Unit_Price > 0), CONSTRAINT Quantity_CK CHECK (Quantity > 0), CONSTRAINT Total_Price_CK CHECK (Total_Price > 0) );

CREATE TABLE Invoice ( Invoice_ID varchar2(10) PRIMARY KEY NOT NULL, Office_ID varchar2(7) NOT NULL, Invoice_Date Date NOT NULL, Total_Cost number (9) NOT NULL, CONSTRAINT fk_order_Office FOREIGN KEY(Office_ID) REFERENCES Office (Office_ID), CONSTRAINT Total_Cost_CK CHECK (Total_Cost > 0) );

我想计算TotalCost就像TotalPrice的总和一样。

之前有没有办法使用相同的方法?