2017-02-20 24 views
-3
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'animal_vaccinations') 
    DROP TABLE animal_vaccinations 

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'animals') 
    DROP TABLE animals 

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'vaccine_codes') 
    DROP TABLE vaccine_codes 

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'intake_codes') 
    DROP TABLE intake_codes 
GO 

--1.2 CREATE TABLE: intake_codes 
create table intake_codes 
(
    intake_code varchar(1) not null, 
    intake_desc varchar(200) not null 
) 

-- 1.3 CREATE TABLE: vaccine_codes 
create table vaccine_codes 
(
    vac_code varchar(50) not null, 
    vac_desc varchar(200) not null 
) 

-- 1.4 CREATE TABLE: animals 
create table animals 
(
    anm_id int identity(3,3) not null, 
    anm_name varchar(100) not null, 
    anm_species char(100) not null, 
    anm_breed varchar(100) not null, 
    anm_age decimal(4,2) not null, 
    anm_gender char(1) default 'F' not null, 
    anm_size char(5) not null, 
    anm_spayorneuter char(1) null, 
    anm_intake_date datetime default getdate() not null, 
    anm_intake_code varchar(1) not null, 
    anm_notes varchar(200) not null 
) 

-- 1.5 CREATE TABLE: animal_vaccinations 
create table animal_vaccinations 
(
    av_anm_id int not null, 
    av_vac_code varchar(50) not null, 
    av_vac_date datetime not null, 
    primary key (av_anm_id, av_vac_code) 
) 

-- Part 2: Add Table Constraints (PKs and Checks ONLY) -- 

-- 2.1 ADD TABLE CONSTRAINTS: animal_vaccinations 
Alter table animal_vaccinations 
add constraint uq_animal_vaccinations Unique (av_anm_id, av_vac_code), 
constraint ck_av_vac_code CHECK (av_vac_code = 'CPV,CDV,CBV,CR,CL,CRV,FHV1,FCV,FPV,FR') 

-- 2.2 ADD TABLE CONSTRAINTS: animals 
Alter table animals 
ADD constraint pk_anm_id PRIMARY KEY (anm_id) 

-- 2.3 ADD TABLE CONSTRAINTS: intake_codes 
Alter table intake_codes 
ADD CONSTRAINT pk_intake_code PRIMARY KEY (intake_code) 

-- 2.4 ADD TABLE CONSTRAINTS: vaccine_codes 
ALTER TABLE vaccine_codes 
ADD constraint pk_vac_code PRIMARY KEY (vac_code) 

-- Part 3: Add Foreign Key Constraints 

-- 3.1 ADD FOREIGN KEY CONSTRAINTS: animal_vaccinations 
ALTER TABLE animal_vaccinations 
ADD constraint fk_av_vac_code FOREIGN KEY (av_vac_code) REFERENCES vaccine_codes(vac_code), 
    constraint fk_av_anm_id FOREIGN KEY (av_anm_id) REFERENCES animals(anm_id) 

-- 3.2 ADD FOREIGN KEY CONSTRAINTS: animals 
ALTER TABLE animals 
ADD constraint fk_anm_intake_code FOREIGN KEY (anm_intake_code) REFERENCES intake_codes(intake_code) 

-- Part 4: Insert Base Data as shown in the assignment. Order is important. Do a table 
-- at a time and place a go statement after each set of inserts. 

-- 4.1 Add Your First Table Rows Here 

insert into animals (anm_name, anm_species, anm_breed, anm_age, anm_gender, anm_size, anm_spayorneuter, anm_intake_date, anm_intake_code, anm_notes) 
values ('Tom', 'Canine', 'Mix.pit/Poodle', 8.80, 'M', 'SM', 'Y', 12/23/16,'C', 'Needs additional water/Hoursebroken'), 
    ('Chi', 'Feline', 'House', 0.80, 'F', 'SM', 'Y', 11/11/16, 'F', 'Very affectionate'), 
     ('Lin', 'Canine', 'Beagle', 2.30, 'M', 'SM', 'N', 1/17/16, 'B', 'Hoursebroken/loves to play ball'), 
     ('Frisky', 'Feline', 'Mix.pit/Poodle', 11.50, 'F', 'Med',' N', 12/2/16, 'B', 'Best in low activity home'), 
     ('Shady', 'Canine', 'House', 4.50, 'F', 'Med', 'Y', 1/16/17, 'C ', 'Null'), 
     ('Sparky', 'Canine', 'Mix.pit/Poodle', 4.10, 'F', 'Lrg', 'N', 1/17/17, 'F', 'Not housebroken/love kids/gentle'), 
     ('Lucy', 'Feline', 'House', 1.10, 'F', 'XL', 'Y', 12/3/16, 'E', 'Null'), 
     ('Blue', 'Canine', 'Lab/Pit.Mixed', 1.20, 'F', 'SM', 'N', 2/4/17, 'B', 'Not housebroken') 

go 

-- 4.2 Add Your Second Table Rows Here 
insert into vaccine_codes(
vac_code, 
vac_desc) 
values ('CPV',' Canine Parvovirus'), 
('CDV',' Canine Distemper'), 
('CBV',' Canine Bordetella'), 
('CR',' Canine Rabies'), 
(' CL',' Canine Lepto'), 
(' CRV',' Canine Rattlesnake Vaccine'), 
(' FHV1',' Feline Herpesvirus'), 
(' FCV',' Feline Calicivirus'), 
(' FPV','Feline Panleukopenia'), 
(' FR',' Feline Rabies') 
go 

-- 4.3 Add Your Third Table Rows Here 
insert into intake_codes (
intake_code, 
intake_desc) 
values ('B',' Stray/At Large'), 
     ('C',' Relinquished by Owner'), 
     ('D',' Owner Intended Euthanasia'), 
     ('E',' Transferred in'), 
     ('F',' Other Intakes') 
go 

エラーメッセージが現れ:のSQL Server 2014 Management Studioを

FOREIGN KEY制約 "fk_anm_intake_code" と競合INSERT文。競合はデータベース "IST359_M003_lwang105"、テーブル "dbo.intake_codes"、列 'intake_code'で発生しました。

誰もがあなたの挿入が外部キー制約違反に失敗しているしてあなたがintake_codesテーブル内の任意のレコードを持っていないよう

+0

を解決する必要があります一致するレコード。どちらのinsert文でエラーが発生しますか?他のテーブルのデータは何ですか? – David

+0

あなたは**ちょうど8時間前にこの質問と全く同じ質問をしました** - **同じ質問を何度も何度も投稿しないでください** –

答えて

0

が見える助けてください。

は、ステップ4.1の前にステップ4.3を行うと、エラーメッセージは、あなたがいないが存在するため、別のテーブルに外部キー制約を持つテーブルにレコードを挿入しようとしていることを意味し、その問題

関連する問題