zoukankan      html  css  js  c++  java
  • mysql 的完整性约束 与单表查询

    1 foreign key 外键 建立两张表的联系

    1 创建表时先创建被关联的表 在创建关联表
    create table dep(
       id int primary key,
       name varchar(20) not null,
      descripe varchar(20) not null);
    
    
    
    在创建关联表(emp表)
    
    create table emp(
      id int primary key,
     name varchar(20) not null,
     age int not null,
     dep_id int,
     cinstraint fk_dep foregin key(dep_id) references dep(id));
    
    
    2 插入记录时 先往被关联表中插入记录 再往关联表中插入记录
    
    insert into dep values
    (1,'IT','IT技术有限部门'),
    (2,'销售部','销售部门'),
    (3,'财务部','花钱太多部门');
    
    
    insert into emp values
    (1,'zhangsan',18,1),
    (2,'lisi',19,1),
    (3,'egon',20,2);
    
    
    在关联表中加入 
    on delete cascade  #同步删除
    on update cascade #同步更新
    
    
    修改emp 表
    create table emp(
    id int primary key,
    name varchar(20) not null,
    age int not null,
    dep_id int,
    constraint fk_dep foregin key(dep_id) references dep(id)
    on delete cascade
    on update cascade);
    
  • 相关阅读:
    (5)html表单
    (4)html表格
    (3)HTML ”列表“、图片和超链接
    (1)html开头解说与案例演示
    学习web前端前感
    一、资源合并与压缩
    HTTP协议原理
    图解HTTP总结
    基于TypeScript从零重构axios
    元組
  • 原文地址:https://www.cnblogs.com/mlhz/p/9800865.html
Copyright © 2011-2022 走看看