Question
Hi, I need help to decide what order I need to drop my tables. Here's my SQL database with foreign keys established, but I always
Hi, I need help to decide what order I need to drop my tables. Here's my SQL database with foreign keys established, but I always get an error on the DROP Tables.
Here's the background information about this database:
Fixinyerleaks is a residential service company that does Plumbing. They want you to design a database that tracks all jobs for their customers. A Job is a single service call to a customer to fix some sort of plumbing issue. Over the years, a customer can use Fixinyerleaks many times. For each job, the plumber will record their hours and also record all of the materials used for the job.
If you could provide a solution as well as the logic to how this works, would be very appreciated :D
-- --------------------------------------------------------------------------------
-- Drop Tables - Need help on which order to drop the tables!-- --------------------------------------------------------------------------------
-- --------------------------------------------------------------------------------
-- Create Database
-- --------------------------------------------------------------------------------
CREATE TABLE TJobs
(
intJobID INTEGER NOT NULL
,intCustomerID INTEGER NOT NULL
,intStatusID INTEGER NOT NULL
,dtmStartDate DATETIME NOT NULL
,dtmEndDate DATETIME NOT NULL
,strJobDesc VARCHAR(8000) NOT NULL
,CONSTRAINT TJobs_PK PRIMARY KEY ( intJobID )
)
CREATE TABLE TCustomers
(
intCustomerID INTEGER NOT NULL
,strFirstName VARCHAR(255) NOT NULL
,strLastName VARCHAR(255) NOT NULL
,strAddress VARCHAR(255) NOT NULL
,strCity VARCHAR(255) NOT NULL
,intStateID INTEGER NOT NULL
,strZip VARCHAR(255) NOT NULL
,strPhoneNumber VARCHAR(255) NOT NULL
,CONSTRAINT TCustomer_PK PRIMARY KEY ( intCustomerID )
)
CREATE TABLE TStatuses
(
intStatusID INTEGER NOT NULL
,strStatus VARCHAR(255) NOT NULL
,CONSTRAINT TStatuses_PK PRIMARY KEY ( intStatusID )
)
CREATE TABLE TJobMaterials
(
intJobMaterialID INTEGER NOT NULL
,intJobID INTEGER NOT NULL
,intMaterialID INTEGER NOT NULL
,intQuantity INTEGER NOT NULL
,CONSTRAINT TCustomerJobMaterials_PK PRIMARY KEY ( intJobMaterialID )
)
CREATE TABLE TMaterials
(
intMaterialID INTEGER NOT NULL
,strDescription VARCHAR(255) NOT NULL
,monCost MONEY NOT NULL
,intVendorID INTEGER NOT NULL
,CONSTRAINT TMaterials_PK PRIMARY KEY ( intMaterialID )
)
CREATE TABLE TVendors
(
intVendorID INTEGER NOT NULL
,strVendorName VARCHAR(255) NOT NULL
,strAddress VARCHAR(255) NOT NULL
,strCity VARCHAR(255) NOT NULL
,intStateID INTEGER NOT NULL
,strZip VARCHAR(255) NOT NULL
,strPhoneNumber VARCHAR(255) NOT NULL
,CONSTRAINT TVendors_PK PRIMARY KEY ( intVendorID )
)
CREATE TABLE TJobWorkers
(
intJobWorkerID INTEGER NOT NULL
,intJobID INTEGER NOT NULL
,intWorkerID INTEGER NOT NULL
,intHoursWorked INTEGER NOT NULL
,CONSTRAINT TCustomerJobWorkers_PK PRIMARY KEY ( intJobWorkerID )
)
CREATE TABLE TWorkers
(
intWorkerID INTEGER NOT NULL
,strFirstName VARCHAR(255) NOT NULL
,strLastName VARCHAR(255) NOT NULL
,strAddress VARCHAR(255) NOT NULL
,strCity VARCHAR(255) NOT NULL
,intStateID INTEGER NOT NULL
,strZip VARCHAR(255) NOT NULL
,strPhoneNumber VARCHAR(255) NOT NULL
,dtmHireDate DATETIME NOT NULL
,monHourlyRate MONEY NOT NULL
,CONSTRAINT TWorkers_PK PRIMARY KEY ( intWorkerID )
)
CREATE TABLE TWorkerSkills
(
intWorkerSkillID INTEGER NOT NULL
,intWorkerID INTEGER NOT NULL
,intSkillID INTEGER NOT NULL
,CONSTRAINT TWorkerSkills_PK PRIMARY KEY ( intWorkerSkillID )
)
CREATE TABLE TSkills
(
intSkillID INTEGER NOT NULL
,strSkill VARCHAR(255) NOT NULL
,strDescription VARCHAR(255) NOT NULL
,CONSTRAINT TSkills_PK PRIMARY KEY ( intSkillID )
)
CREATE TABLE TStates
(
intStateID INTEGER NOT NULL
,strState VARCHAR(255) NOT NULL
,CONSTRAINT TStates_PK PRIMARY KEY ( intStateID )
)
-- --------------------------------------------------------------------------------
-- Establish Referential Integrity
-- --------------------------------------------------------------------------------
--
-- # Child Parent Column
-- - ----- ------ ---------
-- 1 TJobs TCustomers intCustomerID
-- 2 TJobs TStatuses intStatusID
-- 3 TCustomers TStates intStateID
-- 4 TJobMaterials TJobs intJobID
-- 5 TJobMaterials TMaterials intMaterialID
-- 6 TMaterials TVendors intVendorID
-- 7 TVendors TStates intStateID
-- 8 TJobWorkers TJobs intJobID
-- 9 TJobWorkers TWorkers intWorkerID
-- 10 TWorkers TStates intStateID
-- 11 TWorkerSkills TWorkers intWorkerID
-- 12 TWorkerSkills TSkills intSkillID
-- 1
ALTER TABLE TJobs ADD CONSTRAINT TJobs_TStatuses_FK
FOREIGN KEY ( intCustomerID ) REFERENCES TStatuses ( intCustomerID )
-- 2
ALTER TABLE TJobs ADD CONSTRAINT TJobs_TCustomers_FK
FOREIGN KEY ( intStatusID ) REFERENCES TCustomers ( intStatusID )
-- 3
ALTER TABLE TCustomers ADD CONSTRAINT TCustomers_TStates_FK
FOREIGN KEY ( intStateID ) REFERENCES TStates ( intStateID )
-- 4
ALTER TABLE TJobMaterials ADD CONSTRAINT TJobMaterials_TJobs_FK
FOREIGN KEY ( intJobID ) REFERENCES TJobs ( intJobID )
-- 5
ALTER TABLE TJobMaterials ADD CONSTRAINT TJobMaterials_TMaterials_FK
FOREIGN KEY ( intMaterialID ) REFERENCES TJobs ( intMaterialID )
-- 6
ALTER TABLE TMaterials ADD CONSTRAINT TMaterials_TVendors_FK
FOREIGN KEY ( intVendorID ) REFERENCES TJobs ( intVendorID )
-- 7
ALTER TABLE TVendors ADD CONSTRAINT TVendors_TStates_FK
FOREIGN KEY ( intStateID ) REFERENCES TStates ( intStateID )
-- 8
ALTER TABLE TJobWorkers ADD CONSTRAINT TJobWorkers_TJobs_FK
FOREIGN KEY ( intJobID ) REFERENCES TJobs ( intJobID )
-- 9
ALTER TABLE TJobWorkers ADD CONSTRAINT TJobWorkers_TWorkers_FK
FOREIGN KEY ( intWorkerID ) REFERENCES TJobs ( intWorkerID )
-- 10
ALTER TABLE TWorkers ADD CONSTRAINT TWorkers_TStates_FK
FOREIGN KEY ( intStateID ) REFERENCES TStates ( intStateID )
-- 11
ALTER TABLE TWorkerSkills ADD CONSTRAINT TWorkerskills_TWorkers_FK
FOREIGN KEY ( intWorkerID ) REFERENCES TStates ( intWorkerID )
-- 12
ALTER TABLE TWorkerSkills ADD CONSTRAINT TWorkerskills_TSkills_FK
FOREIGN KEY ( intSkillID ) REFERENCES TStates ( intSkillID )
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started