日期:2014-05-18  浏览次数:20654 次

人在国外,急求帮助,谢谢。
和一个德国同学在编数据库,遇到如下问题,弄的焦头烂额的,希望各位大大忙帮一二,不胜感激啊!
 1.首先问个奇怪问题,有时候运行很多遍都是成功的,但是接下来什么都没变,只是又点一遍运行就出错......崩溃了。
 2.下面我们觉得这几个地方有问题:建了三个表顺序如下,属性名是德语,请大家不要介意。这三个表有个相互外键的约束,运行中会给出 FK_Department_Employee 引用了一个无效的 Employee 表,是不是应该把Employee 表放在Department表前?有顺序问题吗?但是这是三个有个循环死锁问题,所以我们也试过在最后用ALter Table Department 
ADD CONSTRAINT 语句来加上外键约束,但是有时还是有错误,请大家耗费下贵眼帮忙看下代码。

CREATE TABLE Department (
 ID int IDENTITY (1, 1) NOT NULL,
 Name nvarchar (20) NOT NULL, 
LeiterID int NOT NULL,
 UebergeordneteAbteilungID int NULL,
 Notiz ntext NULL,
 CONSTRAINT PK_Department PRIMARY KEY CLUSTERED (ID),
 CONSTRAINT FK_Department_Employee FOREIGN KEY (LeiterID) REFERENCES Employee (ID),
 CONSTRAINT FK_Department FOREIGN KEY (UebergeordneteAbteilungID) REFERENCES Department (ID),
 )
 GO

CREATE TABLE Workplace (
 ID int IDENTITY (1, 1) NOT NULL,
 Name nvarchar (20) NOT NULL ,
 ArbeitsplatzStatus nvarchar (15) DEFAULT 'frei' NOT NULL,
 AbteilungsID int NOT NULL,
 Beschreibung ntext NULL,
  Fristende datetime NULL,
 MitarbeiterID int NULL,
 CONSTRAINT PK_Workplace PRIMARY KEY CLUSTERED (ID),
 CONSTRAINT CK_Status CHECK (ArbeitsplatzStatus IN ('frei', 'belegt', 'ausgeschrieben')),
 CONSTRAINT FK_Workplace_Department FOREIGN KEY (AbteilungsID) REFERENCES Department (ID),
 CONSTRAINT FK_Workplace_Employee FOREIGN KEY (MitarbeiterID) REFERENCES Employee (ID),
 
)
 GO

CREATE TABLE Employee (
 ID int IDENTITY (1, 1) NOT NULL,
 Vorname nvarchar (20) NOT NULL,
 Nachname nvarchar (10) NOT NULL,
 Geburtsdatum datetime NULL,
 Email nvarchar (40) NULL,
 Strasse nvarchar (30) NULL,
 PLZ int NULL,
 Ort nvarchar (30) NULL,
 Telefon nvarchar (24) NULL,
 Geschlecht nvarchar (1) DEFAULT 'm' NOT NULL,
 Familienstand nvarchar (15) DEFAULT 'ledig' NOT NULL,
 Notiz ntext NULL,
 --ArbeitsplatzID int NULL,
 Abwesenheitsstatus nvarchar (15) DEFAULT 'anwesend' NOT NULL,
 Gehaltsstufe nvarchar (3) DEFAULT 'I' NOT NULL,
 AbteilungsID int NULL, 
CONSTRAINT PK_Employee PRIMARY KEY CLUSTERED (ID),
 CONSTRAINT CK_EmpGeschlecht CHECK (Geschlecht IN ('w', 'm')),
CONSTRAINT CK_EmpFamilienstand CHECK (Familienstand IN ('ledig', 'verheiratet', 'verwitwet', 'geschieden')),
 CONSTRAINT CK_EmpAbwesenheitsstatus CHECK (Abwesenheitsstatus IN ('anwesend', 'abwesend', 'Urlaub', 'Dienstreise', 'krank')),
 CONSTRAINT CK_EmpGehaltsstufe CHECK (Gehaltsstufe IN ('I', 'II', 'III', 'IV')),
 CONSTRAINT CK_EmpGebutsdatum CHECK (Geburtsdatum < getdate()-18),
 --CONSTRAINT FK_Employee_Workplace FOREIGN KEY (ArbeitsplatzID) REFERENCES Workplace (ID),
 CONSTRAINT FK_Employee_Department FOREIGN KEY (AbteilungsID) REFERENCES Department (ID),

3.另外的一个错误好像是关于联合主键的,在另一个表上建了联合主键,显示错误说这不是一个约束...代码如下:

CREATE TABLE ObjectToQualification(
 Typ int NOT NULL,
 ObjectID int NOT NULL,
 QualifikationsID int NOT NULL,
 QualifikationslevelID int NOT NULL,
 CONSTRAINT PK_ObjectToQualification PRIMARY KEY CLUSTERED (Typ,ObjectID,QualifikationsID),
 CONSTRAINT FK_ObjectToQualification_Qualification FOREIGN KEY (QualifikationsID) REFERENCES Qualification (ID),
 CONSTRAINT FK_ObjectToQualification_QualificationLevel FOREIGN KEY (QualifikationslevelID) REFERENCES QualificationLevel (ID),
 )
 
GO

以上就是主要问题,感谢大家抽出宝贵时间,再次感谢!
 

------解决方案--------------------
实在不行的话,不要约束,用触发器吧