zoukankan      html  css  js  c++  java
  • MYSQL 外键

    SET FOREIGN_KEY_CHECKS=0;
    来禁用外键约束.

    SET FOREIGN_KEY_CHECKS=1;
    来启动外键约束.

    在表tt(id)上建立外键,id不是主键 ,bb(bid)也不是主键 ,出错

    mysql> alter table tt add constraint fk_tb foreign key(id) references bb(bid) on delete cascade;
    ERROR 1005 (HY000): Can't create table 'info.#sql-fa8_1d5' (errno: 150)


    创建表bb时建立外键 ,bb(bid)不是主键 tt(id)也不是主键,出错

    mysql> create table bb(bid int,age int, foreign key(bid) references tt(id)on delete cascade);
    ERROR 1005 (HY000): Can't create table 'info.bb' (errno: 150)

    创建表bb时建立外键,bb(bid)是主键 tt(id)不是主键,出错

    mysql> create table bb(bid int primary key,age int, foreign key(bid) references tt(id) on delete cascade);
    ERROR 1005 (HY000): Can't create table 'info.bb' (errno: 150)

    创建表bb时建立外键,bb(bid)是主键 tt(id)是主键,成功

    mysql> create table bb(bid int primary key,age int, foreign key(bid) references tt(id)on delete cascade);
    Query OK, 0 rows affected (0.05 sec)

    查看表结构

    mysql> desc tt;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | name | varchar(16) | YES | MUL | NULL | |
    | info | text | YES | | NULL | |
    | id | int(11) | NO | PRI | NULL | |
    +-------+-------------+------+-----+---------+-------+


    mysql> desc bb;
    +-------+---------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | bid | int(11) | NO | PRI | NULL | |
    | age | int(11) | YES | | NULL | |
    +-------+---------+------+-----+---------+-------+


    结论:互为外键的两个字段必须都是主键

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    建立表product表时cid 不是主键,但是company表的cid是主键,此时外键成功了,cid 的类型是MUL;

    mysql> create table product(pid int unsigned auto_increment, cid int unsigned, name varchar(16),primary key(pid),foreign key(cid) references company(cid));
    Query OK, 0 rows affected (0.05 sec)

    mysql> desc product;
    +-------+------------------+------+-----+---------+----------------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+------------------+------+-----+---------+----------------+
    | pid | int(10) unsigned | NO | PRI | NULL | auto_increment |
    | cid | int(10) unsigned | YES | MUL | NULL | |
    | name | varchar(16) | YES | | NULL | |
    +-------+------------------+------+-----+---------+----------------+
    3 rows in set (0.01 sec)

    mysql> desc company;
    +-------+------------------+------+-----+---------+----------------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+------------------+------+-----+---------+----------------+
    | cid | int(10) unsigned | NO | PRI | NULL | auto_increment |
    | name | varchar(16) | YES | | NULL | |
    +-------+------------------+------+-----+---------+----------------+

    如果建立product表是cid是主键,则外键也会成功,但cid的类型是PRI:

    mysql> alter table product add constraint fk_pro foreign key(cid) references com
    pany(cid) on delete cascade;
    Query OK, 0 rows affected (0.09 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    mysql> desc product;
    +-------+------------------+------+-----+---------+----------------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+------------------+------+-----+---------+----------------+
    | pid | int(10) unsigned | NO | PRI | NULL | auto_increment |
    | cid | int(10) unsigned | NO | PRI | 0 | |
    | name | varchar(16) | YES | | NULL | |
    +-------+------------------+------+-----+---------+----------------+







  • 相关阅读:
    装饰器 无惨固定模式 和 有参装饰器的固定模式
    匿名函数
    字典生成式
    列表生成式
    Centos7安装配置apache-tomcat-8.5.16.tar.gz
    Centos7安装配置jdk-8u151-linux-x64.tar.gz
    Linux CentOS7源码安装配置mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
    VirtualBox新建Centos7虚拟系统
    vmware workstation 10的安装
    redhat linux rpm包安装配置mysql数据库
  • 原文地址:https://www.cnblogs.com/iLoveMyD/p/2420797.html
Copyright © 2011-2022 走看看