-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中
INSERT語句衝突與外鍵約束 「fk_anm_intake_code」。衝突發生在數據庫「IST359_M003_lwang105」,表「dbo.intake_codes」,列'intake_code'中。
請人幫忙
錯誤消息意味着你要插入記錄其對另一個表的外鍵約束的表,對其中有沒有匹配記錄。哪個插入語句導致錯誤?另一個表中的數據是什麼? – David
你**只在8小時前問**這個相同的問題 - 請**不要**一遍又一遍地發佈相同的問題 –