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

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

WBOY
Release: 2016-06-06 20:18:11
Original
2874 people have browsed it

主表的建表语句如下

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

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

回复内容:

主表的建表语句如下

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

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

<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>
Copy after login

用外键保持正确性就好

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

兄弟听说过外键么?

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template