MYSQL 建表时加主外键

1、

create table demo.ChineseCharInfo
(
    ID        int     not null     auto_increment,
    Hanzi     varchar(10)     not null,
    primary key (ID)
)
engine=innodb auto_increment=1 default charset=utf8 collate=utf8_general_ci;
2、
create table demo.ChinesePinyinInfo
(
    ID     int     not null     auto_increment,
    CharID     int     null,
    Pinyin varchar(10)     null,
    Tone tinyint unsigned     null,
    primary key (ID),
    
    -- 方式一:不指定外键名称,数据库自动生成
    foreign key (CharID) references ChineseCharInfo(ID) on delete cascade on update cascade 
    
    -- 方式二:指定外键名称为(FK_Name)
    -- constraint FK_Name foreign key (CharID) references ChineseCharInfo(ID) on delete cascade on update cascade 
)
engine=innodb auto_increment=1 default charset=utf8 collate=utf8_general_ci;

 

摘自:https://www.cnblogs.com/Brambling/p/9263377.html

你可能感兴趣的:(MYSQL 建表时加主外键)