Home >Backend Development >PHP Tutorial >主表和从表怎么建立起来联系呢?

主表和从表怎么建立起来联系呢?

WBOY
WBOYOriginal
2016-06-06 20:18:112874browse

主表的建表语句如下

<code>create table topic(id int primary key not null auto_increment,title varchar(10),content varchar(20));</code>

从表主要用来存储回复的数据 只要有回复者的姓名和内容就行了 那么从表应该怎么建才能和主表关联起来呢? 求指教

回复内容:

主表的建表语句如下

<code>create table topic(id int primary key not null auto_increment,title varchar(10),content varchar(20));</code>

从表主要用来存储回复的数据 只要有回复者的姓名和内容就行了 那么从表应该怎么建才能和主表关联起来呢? 求指教

<code class="SQL">CREATE TABLE reply(
    reply_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    replier VARCHAR,
    content VARCHAR NOT NULL,
    topic_id INT NOT NULL,
    FOREIGN KEY (topic_id) REFERENCES topic(id) ON UPDATE CASCADE
);</code>

用外键保持正确性就好

<code>table reply
    field id auto_increment primary key,
    field topic_id,
    field user_id,
    field content,
    field datetime</code>

兄弟听说过外键么?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn