我无法搞清楚这个数据库的外键......
-- --------------------------------------------------------------------------------
-- Drop Table
-- --------------------------------------------------------------------------------
IF OBJECT_ID( 'TJobWorkers' ) IS NOT NULL DROP TABLE TJobWorkers
IF OBJECT_ID( 'TWorkerSkills' ) IS NOT NULL DROP TABLE TWorkerSkills
IF OBJECT_ID( 'TCustomers' ) IS NOT NULL DROP TABLE TCustomers
IF OBJECT_ID( 'TJobMaterials' ) IS NOT NULL DROP TABLE TJobMaterials
IF OBJECT_ID( 'TJobs' ) IS NOT NULL DROP TABLE TJobs
IF OBJECT_ID( 'TMaterials' ) IS NOT NULL DROP TABLE TMaterials
IF OBJECT_ID( 'TSkills' ) IS NOT NULL DROP TABLE TSkills
IF OBJECT_ID( 'TStatus' ) IS NOT NULL DROP TABLE TStatus
IF OBJECT_ID( 'TWorkers' ) IS NOT NULL DROP TABLE TWorkers
IF OBJECT_ID( 'VLaborCosts' ) IS NOT NULL DROP VIEW VLaborCosts
-- --------------------------------------------------------------------------------
-- Step #1.1: Create Tables
-- --------------------------------------------------------------------------------
CREATE TABLE TWorkers
(
intWorkerID INTEGER NOT NULL
,strFirstName VARCHAR(50) NOT NULL
,strLastName VARCHAR(50) NOT NULL
,dteHireDate DATE NOT NULL
,monHourlyRate MONEY NOT NULL
,CONSTRAINT TWorkers_PK PRIMARY KEY( intWorkerID )
)
CREATE TABLE TStatus
(
intJobStatusID INTEGER NOT NULL
,strJobStatus VARCHAR(50) NOT NULL
,CONSTRAINT TStatus_PK PRIMARY KEY( intJobStatusID )
)
CREATE TABLE TSkills
(
intSkillID INTEGER NOT NULL
,strSkillDescription VARCHAR(50) NOT NULL
,CONSTRAINT TSkills_PK PRIMARY KEY( intSkillID )
)
CREATE TABLE TMaterials
(
intMaterialID INTEGER NOT NULL
,strMaterialName VARCHAR(50) NOT NULL
,monMatieralCost MONEY NOT NULL
,CONSTRAINT TMaterials_PK PRIMARY KEY( intMaterialID )
)
CREATE TABLE TJobs
(
intJobID INTEGER NOT NULL
,intJobStatusID INTEGER NOT NULL
,strJobDescription VARCHAR(2000) NOT NULL
,dteStartDate DATE NOT NULL
,dteEndDate DATE NOT NULL
,CONSTRAINT TJobs_PK PRIMARY KEY( intJobID )
)
CREATE TABLE TJobMaterials
(
intMaterialID INTEGER NOT NULL
,intJobID INTEGER NOT NULL
,intMaterialQuantity INTEGER NOT NULL
,CONSTRAINT TJobMaterials_PK PRIMARY KEY( intJobID,intMaterialID )
)
CREATE TABLE TCustomers
(
intCustomerID INTEGER NOT NULL
,intJobID INTEGER NOT NULL
,strFirstName VARCHAR(50) NOT NULL
,strLastName VARCHAR(50) NOT NULL
,strAddress VARCHAR(50) NOT NULL
,strCity VARCHAR(50) NOT NULL
,strState VARCHAR(50) NOT NULL
,strZipCode VARCHAR(50) NOT NULL
,CONSTRAINT TCustomers_PK PRIMARY KEY( intJobID )
)
CREATE TABLE TWorkerSkills
(
intWorkerID INTEGER NOT NULL
,intSkillID INTEGER NOT NULL
,CONSTRAINT TWorkerSkills_PK PRIMARY KEY( intWorkerID,intSkillID )
)
CREATE TABLE TJobWorkers
(
intJobID INTEGER NOT NULL
,intWorkerID INTEGER NOT NULL
,intTotalHoursWorkedByWorker INTEGER NOT NULL
,CONSTRAINT TJobWorkers_PK PRIMARY KEY( intJobID,intWorkerID )
)
-- --------------------------------------------------------------------------------
-- Step #1.2: Create foreign key constraints to enforce referential integrity for all relationships.
-- -----------------------------------------------------------------------------
ALTER TABLE TWorkers ADD CONSTRAINT TWorkers_TWorkerSkills_FK
FOREIGN KEY ( intWorkerID ) REFERENCES TWorkerSkills ( intWorkerID )
ALTER TABLE TStatus ADD CONSTRAINT TStatus_TJobs_FK
FOREIGN KEY ( intJobStatusID ) REFERENCES TJobs ( intJobStatusID )
ALTER TABLE TSkills ADD CONSTRAINT TSkills_TWorkerSkills_FK
FOREIGN KEY ( intSkillID ) REFERENCES TWorkerSkills ( intSkillID )
ALTER TABLE TMaterials ADD CONSTRAINT TMaterials_TJobMaterials_FK
FOREIGN KEY ( intMaterialID ) REFERENCES TJobMaterials ( intMaterialID )
ALTER TABLE TJobs ADD CONSTRAINT TJobs_TJobMaterials_FK
FOREIGN KEY ( intJobID ) REFERENCES TJobMaterials ( intJobID )
ALTER TABLE TCustomers ADD CONSTRAINT TJobs_TCustomers_FK
FOREIGN KEY ( intJobID ) REFERENCES TJobs ( intJobID )
ALTER TABLE TJobs ADD CONSTRAINT TJobs_TJobWorkers_FK
FOREIGN KEY ( intJobID ) REFERENCES TJobWorkers ( intJobID )
-- --------------------------------------------------------------------------------
-- Step #2.1: Populate each table with test data. Make sure that you have sufficient
-- data to test all indicated Updates, Deletes, and Queries.
-- --------------------------------------------------------------------------------
INSERT INTO TWorkers (intWorkerID,strFirstName,strLastName,dteHireDate,monHourlyRate)
VALUES (1,'Mark','Anthony','01/01/2014',15.00)
,(2,'Sam','Bradford','02/01/2014',25.00)
,(3,'Tam','Kim','03/01/2014',10.00)
,(4,'Bill','Tull','04/01/2015',17.00)
,(5,'Mill','Patty','05/01/2015',18.00)
,(6,'Sill','Hill','06/01/2014',13.00)
,(7,'Cam','Newton','07/01/2015',12.00)
,(8,'Pam','Van','08/01/2015',30.00)
,(9,'Fam','Cham','09/01/2014',19.00)
,(10,'Kam','Kimer','10/01/2015',15.00)
INSERT INTO TStatus (intJobStatusID,strJobStatus)
VALUES (1,'Open')
,(2,'In Progress')
,(3,'Complete')
INSERT INTO TSkills (intSkillID,strSkillDescription)
VALUES (1,'Planner')
,(2,'Sander')
,(3,'Painting')
,(4,'Drawing')
,(5,'Hammering')
,(6,'Climbing')
,(8,'Gaming')
,(9,'Talking')
,(10,'Eating')
,(11,'Sleeping')
,(12,'Hitting')
,(13,'Fighting')
,(14,'Rapping')
,(15,'Singing')
,(16,'Learning')
,(17,'Running')
,(18,'Walking')
,(19,'Standing')
,(20,'Nailing')
INSERT INTO TMaterials (intMaterialID,strMaterialName,monMatieralCost)
VALUES (1,'Wood',100)
,(2,'Stone',100)
,(3,'Concrete',50)
,(4,'Diamond',10000)
,(5,'Silver',1000)
,(6,'Iron',1050)
,(7,'Gold',3000)
,(8,'Titanium',400)
,(9,'Trantanium',200)
,(10,'Paint',20)
,(11,'Morter',20)
,(12,'Unobtanium',100000)
INSERT INTO TJobs (intJobID,intJobStatusID,strJobDescription,dteStartDate,dteEndDate)
VALUES (1,1,'Adding a modern Kitchen','01/01/2015','12/01/2015')
,(2,2,'Demolishing old barn','02/01/2015','12/01/2015')
,(3,3,'Adding on a Master closet','03/01/2015','03/30/2015')
,(4,1,'Remodeling bedrooms','04/01/2015','12/01/2015')
,(5,2,'Repaving driveway','05/01/2015','12/01/2015')
,(6,1,'Painting hallway','06/01/2015','09/01/2015')
,(7,2,'Repaving street','05/01/2014','12/01/2014')
,(8,2,'Painting bedroom','06/01/2014','09/01/2014')
INSERT INTO TJobMaterials (intJobID,intMaterialID,intMaterialQuantity)
VALUES ( 1 , 1 , 1 )
,( 2 , 2 , 2 )
,( 3 , 3 , 2 )
,( 4 , 4 , 1 )
,( 5 , 5 , 1 )
,( 6 , 6 , 2 )
,( 7 , 7 , 2 )
,( 8 , 8 , 1 )
,( 1 , 9 , 3 )
,( 2 , 10 , 3 )
,( 3 , 11 , 3 )
,( 4 , 5 , 1 )
,( 5 , 9 , 4 )
,( 6 , 9 , 5 )
,( 7 , 1 , 6 )
,( 8 , 9 , 3 )
,( 1 , 7 , 1 )
,( 2 , 8 , 1 )
,( 3 , 5 , 1 )
,( 4 , 11 , 1 )
,( 5 , 1 , 2 )
,( 1 , 2 , 2 )
,( 2 , 3 , 2 )
,( 3 , 4 , 2 )
INSERT INTO TCustomers(intCustomerID,intJobID,strFirstName,strLastName,strAddress,strCity,strState,strZipCode)
VALUES (1,1,'Emily','Bruns','1111 Main Street','West Chester','Ohio','45242')
,(2,2,'Maylen','Jahn','2222 Main Street','Montgomery','Ohio','45243')
,(3,3,'Brenna','Schlager','3333 Main Street','Indian Hill','Ohio','45244')
,(4,4,'Cara','Roach','4444 Four Rd','Sharonville','Ohio','45245')
,(5,5,'Ryan','Roach','5555 Main Street','Cincinnati','Ohio','45246')
,(6,6,'Tom','Miller','6666 Orleans Ct','Blue Ash','Ohio','45247')
,(6,7,'Tom','Miller','6666 Orleans Ct','Blue Ash','Ohio','45247')
,(6,8,'Tom','Miller','6666 Orleans Ct','Blue Ash','Ohio','45247')
INSERT INTO TWorkerSkills (intWorkerID,intSkillID)
VALUES ( 1 , 1 )
,( 2 , 2 )
,( 3 , 3 )
,( 4 , 4 )
,( 5 , 5 )
,( 6 , 5 )
,( 7 , 1 )
,( 8 , 7 )
,( 8 , 8 )
,( 10 , 9 )
,( 1 , 10 )
,( 2 , 11 )
,( 3 , 12 )
,( 4 , 1 )
,( 5 , 14 )
,( 6 , 15 )
,( 1 , 16 )
,( 2 , 17 )
,( 3 , 18 )
,( 1 , 19 )
,( 2 , 20 )
INSERT INTO TJobWorkers (intJobID,intWorkerID,intTotalHoursWorkedByWorker)
VALUES ( 1 , 1 , 0)
,( 2 , 2 , 60)
,( 3 , 3 , 80)
,( 4 , 4 , 10)
,( 5 , 5 , 100)
,( 6 , 6 , 50)
,( 7 , 7 , 150)
,( 8 , 8 , 10)
,( 3 , 9 , 70)
,( 4 , 10 , 75)
,( 5 , 7 , 120)
,( 6 , 8 , 200)
,( 7 , 9 , 19)
,( 8 , 10 , 199)
,( 7 , 10 , 19)
,( 6 , 10 , 193)
每次运行数据库时,几乎每个外键都会产生这些错误......
引用表'TWorkerSkills'中没有主键或候选键与外键'TWorkers_TWorkerSkills_FK'中的引用列列表匹配。
非常感谢任何帮助/修订/建议。