常州建设企业网站,直播秀场网站开发,北京网络公司哪家最好,旅游网站制作模板#x1f308;个人主页: Aileen_0v0 #x1f525;热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法 #x1f4ab;个人格言:“没有罗马,那就自己创造罗马~” #mermaid-svg-g4glZPIY0IKhiTfe {font-family:trebuchet ms,verdana,arial,sans-serif;font-siz…
个人主页: Aileen_0v0 热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法 个人格言:“没有罗马,那就自己创造罗马~” #mermaid-svg-g4glZPIY0IKhiTfe {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-g4glZPIY0IKhiTfe .error-icon{fill:#552222;}#mermaid-svg-g4glZPIY0IKhiTfe .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-g4glZPIY0IKhiTfe .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-g4glZPIY0IKhiTfe .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-g4glZPIY0IKhiTfe .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-g4glZPIY0IKhiTfe .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-g4glZPIY0IKhiTfe .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-g4glZPIY0IKhiTfe .marker{fill:#333333;stroke:#333333;}#mermaid-svg-g4glZPIY0IKhiTfe .marker.cross{stroke:#333333;}#mermaid-svg-g4glZPIY0IKhiTfe svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-g4glZPIY0IKhiTfe .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-g4glZPIY0IKhiTfe .cluster-label text{fill:#333;}#mermaid-svg-g4glZPIY0IKhiTfe .cluster-label span{color:#333;}#mermaid-svg-g4glZPIY0IKhiTfe .label text,#mermaid-svg-g4glZPIY0IKhiTfe span{fill:#333;color:#333;}#mermaid-svg-g4glZPIY0IKhiTfe .node rect,#mermaid-svg-g4glZPIY0IKhiTfe .node circle,#mermaid-svg-g4glZPIY0IKhiTfe .node ellipse,#mermaid-svg-g4glZPIY0IKhiTfe .node polygon,#mermaid-svg-g4glZPIY0IKhiTfe .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-g4glZPIY0IKhiTfe .node .label{text-align:center;}#mermaid-svg-g4glZPIY0IKhiTfe .node.clickable{cursor:pointer;}#mermaid-svg-g4glZPIY0IKhiTfe .arrowheadPath{fill:#333333;}#mermaid-svg-g4glZPIY0IKhiTfe .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-g4glZPIY0IKhiTfe .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-g4glZPIY0IKhiTfe .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-g4glZPIY0IKhiTfe .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-g4glZPIY0IKhiTfe .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-g4glZPIY0IKhiTfe .cluster text{fill:#333;}#mermaid-svg-g4glZPIY0IKhiTfe .cluster span{color:#333;}#mermaid-svg-g4glZPIY0IKhiTfe div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-g4glZPIY0IKhiTfe :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} at first sight 乍一看,初看起来 文章目录 外键约束含义分析 添加外键的语法创建表的时候直接添加外键创建完表以后额外添加 删除外键 外键约束
含义
外键用来让两张表的数据之间建立连接从而保证数据的完整性和一致性。
员工表emp子表
idnameagejobsalaryentrydatemanageriddept_id1金庸66总裁200002000-01-01null52张无忌20项目经理125002005-12-05113杨逍33开发84002000-11-03214韦一笑48开发110002002-02-05215常遇春43开发105002004-09-0731
部门表dept父表
idname1研发部2 市场部3 财务部4销售部5总经办
⚠️注意上面两张表在数据库层面并未建立外键关联所以无法保证数据的一致性和完整性。 1.准备好部门表
-- -------------------约束外键-------------
-- 准备数据
create table dept(id int auto_increment comment ID primary key ,name varchar(50) not null comment 部门名称
)comment 部门表;
insert into dept(id, name) VALUES (1,研发部),(2,市场部),(3,财务部),(4,销售部),(5,总经办);create table emp(id int auto_increment comment ID primary key ,name varchar(50) not null comment 姓名,age int comment 年龄,job varchar(20) comment 职位,salary int comment 薪资,entrydate date comment 入职时间,managerid int comment 直属领导ID,dept_id int comment 部门ID
)comment 员工表;insert into emp (id, name, age, job, salary, entrydate, managerid, dept_id) VALUES(1,金庸,66,总裁,2000,2000-01-01,null,5),(2,张无忌,20,项目经理,12500,2005-12-05,1,1),(3,杨逍,33,开发,8400,2000-11-03,2,1),(4,韦一笑,48,开发,11000,2002-02-05,2,1),(5,常遇春,43,开发,10500,2004-09-07,3,1),(6,小昭,19,程序员鼓励师,6600,2004-10-12,2,1);分析
通过这两张表我们可以看出他们之间存在逻辑关系。但是并未设置物理外键这就无法保证数据的一致性和完整性。 如果我们把1号部门删了但是员工表的数据海关连着1号表这就会导致数据出现不完整性。 添加外键的语法
创建表的时候直接添加外键
create table 表名(字段名 数据类型,...[constraint] [外键名称] foreign key(外键字段名) references 主表(主表列表)
);创建完表以后额外添加
alter table 表名 add constraint 外键名称 foreign key(外键字段名) references 主表(主表列名);再一次删除研发部 ⚠️报错显示我们不能直接删除父表中id为1的记录而子表中还有非常多的记录关联着父表中id为1的记录。 删除外键
-- 删除外键
alter table 表名 drop foreign key 外键名称;-- 删除外键
alter table emp drop foreign key fk_emp_dept_id;删除完外键我们可以发现emp表的蓝色钥匙没有了。