SQL,外键约束出现错误信息

2024-01-12

 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)

go

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

 go

-- 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)

go


-- 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))

go

-- 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')


go

-- 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)


go

-- 2.4 ADD TABLE CONSTRAINTS: vaccine_codes

ALTER TABLE vaccine_codes
ADD constraint pk_vac_code PRIMARY KEY (vac_code)

go




-- 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)

go

-- 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)

go  
-- 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

出现了错误信息

INSERT 语句与 FOREIGN KEY 约束“fk_anm_intake_code”冲突。冲突发生在数据库“IST359_M003_lwang105”,表“dbo.intake_codes”,列“intake_code”中。

请任何人帮忙


难道你不应该在动物之前插入摄入代码吗?您将与尚不存在的摄入代码相关的内容插入到动物体内

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SQL,外键约束出现错误信息 的相关文章

  • TSQL:无法对 COUNT(*) 执行聚合函数 AVG 来查找一天中最繁忙的时间

    考虑一个保存日志数据的 SQL Server 表 重要的部分是 CREATE TABLE dbo CustomerLog ID int IDENTITY 1 1 NOT NULL CustID int NOT NULL VisitDate
  • xQuery LIKE 运算符?

    有没有办法以与 SQL 相同的方式使用 XQuery 执行 LIKE 操作 我不想构造一些 startswith endswith 和 contains 表达式 我想要实现的目标的示例 for x in user where x first
  • PostgreSQL & regexp_split_to_array + 取消嵌套

    我有这样的绳子 测试1 纽约 X 测试 2 芝加哥 Y 测试 3 宾夕法尼亚州哈里斯堡 Z 我需要的结果是 Column1 Column 2 Column3 Test 1 new york X Test 2 chicago Y Test 3
  • VIEW for 表结合 UNION ALL 的 MySQL 性能

    假设我有 2 张桌子MySQL create table persons id bigint unsigned not null auto increment first name varchar 64 surname varchar 64
  • Solr 增量导入不起作用

    我使用的是solr 4 2 请注意 完全导入有效 但增量导入却无效 增量导入不会给出任何错误 但不会获取任何更改 这是数据配置文件
  • SQL Server查询麻烦,多对多关系

    不知道如何用一行字来表达这个问题 对标题表示歉意 我的数据库中有3个表 例如 Shop Item 商店库存 Shop 和 Item 具有多对多关系 因此 ShopStock 表将它们链接起来 ShopStock 中的字段是 ID ShopI
  • 从两个表中搜索然后删除

    我有两个包含成员数据的表 与 member id 列链接 我需要搜索所有记录email列以 pl 结尾 然后 我需要为此删除两个表中的所有记录 基于 member id 是否可以通过一条 SQL 语句完成此操作 SELECT member
  • 从 SQL Server 2012 查询结果中减去小时数

    我正在 SQL Server 2012 Management Studio 中的警报系统信号自动化平台数据库上运行查询 但遇到了一些问题 我的查询运行得很好 但我无法将结果细化到我想要的水平 我正在选择一些格式为的列DATETIME 我只想
  • 如何将 SQL 结果存入 STRING 变量?

    我正在尝试获取 C 字符串变量或字符串数 组中的 SQL 结果 是否可以 我需要以某种方式使用 SqlDataReader 吗 我对 C 函数和所有功能非常陌生 曾经在 PHP 中工作 所以如果可以的话请给出一个工作示例 如果相关 我已经可
  • 从VBA中的数组批量插入到sql中

    我正在尝试在 Excel 中构建一个按钮 将所选区域上传到 SQL Server 中的表中 第一行将自动视为列标题 这件事该怎么继续下去呢 我想要的是简单和超快的上传 这是我的想法 我将选择选定的区域 然后将其保存为 txt 文件 然后对其
  • 标记个体内事件发生后发生的日期

    我有一组长格式的数据 每人几行 人 id 其中事件 事件 1 应该只发生一次 事件发生后 该人不应再有任何数据 如果事件发生后出现任何记录 我想使用名为 flag flag 1 的新变量创建一个查询 例如 下面标记了 id 5 因为在该人的
  • SQL 更新数据集中的位置

    id1 id2 bool 1 1 F 1 2 F 2 1 F UPDATE table name SET bool T WHERE id1 id2 IN 1 1 2 1 Need work here 所以基本上我想选择条件为 id1 id2
  • 为什么我们不能有多个主键?

    我知道表中不能有超过 1 个主键 但技术原因是什么 直接拉取自SO https stackoverflow com questions 217945 can i have multiple primary keys in a single
  • 混合语言源目录布局

    我们正在运行一个使用多种不同语言的大型项目 Java Python PHP SQL 和 Perl 到目前为止 人们一直在自己的私有存储库中工作 但现在我们希望将整个项目合并到一个存储库中 现在的问题是 目录结构应该是什么样的 我们应该为每种
  • 如何在没有 EF 的 ASP.NET MVC 中使用普通 sql?

    我有一个使用 linq to sql 的类 如何在 ASP NET MVC 3 中使用普通 sql 而不使用 EF 来实现相同的功能 public ActionResult Index var List from c in db OFFIC
  • 按小时拆分日期/时间数据并将日期/时间范围展开为行

    我正在尝试使用 SQL Server 将一系列日期 时间数据扩展为多行 例如 我的数据看起来像 Date StartTime EndTime EmployeeID ShiftType 10 1 2019 8 30 00AM 4 57 00P
  • 在 plsql 中立即执行

    如何从这段代码中得到结果 EXECUTE IMMEDIATE SELECT FROM table name through for loop 通常的方法看起来像这样 for items in select from this table l
  • 返回行位置 - Postgres

    我返回一个带有位置的表 select from select row number over as position from organization result where data1 Hello 返回这个 这是正确的 data1 H
  • 将列的值添加到 LIKE 语句中?

    我有 3 个标签表 标签类别和使用过的标签 我想要获取所有标签的列表以及已使用标签的计数 所使用标签的格式是每个具有标签的文档 ID 的逗号分隔值 我一直在尝试类似的方法 但无法将tags tag 字段的值插入到LIKE 语句中 SELEC
  • 如何打印Oracle中过程的定义?

    oracle中有没有办法查看过程的结构是什么 我正在尝试记录并运行程序 并希望将实际的程序结构存储在我的日志中 您可以查询ALL SOURCE table SELECT text FROM all source WHERE owner lt

随机推荐