使用外键和alter语句创建MySQL关系

时间:2014-04-14 10:45:35

标签: mysql sql foreign-key-relationship

我会尝试具体,因为我觉得我对这个主题的理解也不是很精确。我的问题是理解如何使用外部键创建表之间的关系,我使用alter语句添加。我有这些表。

CREATE TABLE article (
    content TEXT NOT NULL,
    published_on DATE NOT NULL,
    created_on DATE NOT NULL,
);

CREATE TABLE category(
    name VARCHAR(30) NOT NULL,
    date_created DATE NOT NULL,
);

CREATE TABLE user(
    income FLOAT(30, 30) NOT NULL,
    password VARCHAR(30) NOT NULL,
    picture_url TEXT NOT NULL,
);

CREATE TABLE tag(
    description VARCHAR(30) NOT NULL,
    second_priority FLOAT(30, 30) NOT NULL,
);

我必须在那里建立关系:

Tag has a one to one connection to Category
Category has a many to one connection to User
User has a one to many connection to Article

为此,我使用那些陈述:

ALTER TABLE tag ADD CONSTRAINT FOREIGN KEY (tag_id) REFERENCES category (category_id);
ALTER TABLE category ADD CONSTRAINT FOREIGN KEY (user_id) REFERENCES user (user_id);
ALTER TABLE user ADD CONSTRAINT FOREIGN KEY (user_id) REFERENCES article (user_id);

我的问题是第三个失败了。当我切换文章和用户的位置时,约束通过。经过一些挖掘和实验后,我发现我不能约束两个键,其中一个是UNIQUE或PRIMARY KEY,另一个是NOT NULL。到目前为止,我不知道如何继续,有人可以请教我如何创建一对一,多对一,一对多以及这些表之间的多对多关系因为我有点失落我头脑中的一切都是一团糟。

FULL STUFF:

CREATE DATABASE exam_database;
USE exam_database;
CREATE TABLE article (
    content TEXT NOT NULL,
    published_on DATE NOT NULL,
    created_on DATE NOT NULL,
    user_id INT(30) NOT NULL
);

CREATE TABLE category(
    name VARCHAR(30) NOT NULL,
    date_created DATE NOT NULL,
    category_id INT(30) NOT NULL PRIMARY KEY AUTO_INCREMENT UNIQUE,
    user_id INT(30) NOT NULL
);

CREATE TABLE user(
    income FLOAT(30, 30) NOT NULL,
    password VARCHAR(30) NOT NULL,
    picture_url TEXT NOT NULL,
    user_id INT(30) NOT NULL PRIMARY KEY AUTO_INCREMENT UNIQUE
);

CREATE TABLE tag(
    description VARCHAR(30) NOT NULL,
    second_priority FLOAT(30, 30) NOT NULL,
    tag_id INT(30) NOT NULL PRIMARY KEY AUTO_INCREMENT UNIQUE
);

ALTER TABLE tag ADD CONSTRAINT FOREIGN KEY (tag_id) REFERENCES category (category_id);
ALTER TABLE category ADD CONSTRAINT FOREIGN KEY (user_id) REFERENCES user (user_id);
ALTER TABLE user ADD CONSTRAINT FOREIGN KEY (user_id) REFERENCES article (user_id);

1 个答案:

答案 0 :(得分:0)

首先你的结构有一些错误: 缺少PK,为什么使用INT(30)?等等... 您的数据库应如下所示:

CREATE DATABASE exam_database;
USE exam_database;
CREATE TABLE article (
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT UNIQUE,
    user_id INT NOT NULL,
    content TEXT NOT NULL,
    published_on DATE NOT NULL,
    created_on DATE NOT NULL
);

CREATE TABLE category(
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT UNIQUE,
    user_id INT NOT NULL,
    name VARCHAR(30) NOT NULL,
    date_created DATE NOT NULL
);

CREATE TABLE user (
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT UNIQUE    ,
    income FLOAT(30, 30) NOT NULL,
    password VARCHAR(30) NOT NULL,
    picture_url TEXT NOT NULL

);

CREATE TABLE tag (
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT UNIQUE,
    category_id INT NOT NULL,
    description VARCHAR(30) NOT NULL,
    second_priority FLOAT(30, 30) NOT NULL

);

第二,你的FK应该是这样的:

ALTER TABLE tag ADD CONSTRAINT FOREIGN KEY (category_id) REFERENCES category (id);
ALTER TABLE category ADD CONSTRAINT FOREIGN KEY (user_id) REFERENCES user (id);
ALTER TABLE article ADD CONSTRAINT FOREIGN KEY (user_id) REFERENCES  user (id);