社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

mysql外键错误无法修复

Adam • 5 年前 • 1872 次点击  

我是my sql新手,从mysql数据库中编写sql脚本,我的大多数表都能工作,但有些表不能工作,mysql错误是尽可能不可描述的。这可能与多个外键有关。

 create table Users (
    id int not null auto_increment,
    type enum('t1', 't2', 't3') not null,
    name char(30) not null,
    phone char(15),
    email char(30),
    username char(30),
    password char(30),
    created timestamp,
    primary key (id)
);

create table Groups (
    id int not null auto_increment,
    user int not null,
    name char(30) not null,
    phone char(15),
    email char(30),
    created timestamp,
    primary key (id),
    foreign key (user) references Users(id)
);

create table Group_Members (
    id int not null auto_increment,
    group int not null,
    user int not null,
    created timestamp,
    primary key (id),
    foreign key (group) references Groups(id),
    foreign key (user) references Users(id)
);
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/48821
 
1872 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Ogreucha
Reply   •   1 楼
Ogreucha    5 年前

Group 是保留字 MySQL . 您必须在查询中使用`引用保留字。

试试这个:

create table Users (
    id int not null auto_increment,
    type enum('t1', 't2', 't3') not null,
    name char(30) not null,
    phone char(15),
    email char(30),
    username char(30),
    password char(30),
    created timestamp,
    primary key (id)
);

create table Groups (
    id int not null auto_increment,
    user int not null,
    name char(30) not null,
    phone char(15),
    email char(30),
    created timestamp,
    primary key (id),
    foreign key (user) references Users(id)
);

create table Group_Members (
    id int not null auto_increment,
    `group` int not null,
    user int not null,
    created timestamp,
    primary key (id),
    foreign key (`group`) references Groups(id),
    foreign key (user) references Users(id)
);