Py学习  »  DATABASE

mysql外键错误无法修复

Adam • 4 年前 • 963 次点击  

我是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
 
963 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Ogreucha
Reply   •   1 楼
Ogreucha    4 年前

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)
);