How can I associate one record with another in the same table?

核能气质少年 提交于 2019-12-05 20:51:49

You need two tables--one you have already:

Cables
  ID autoincrement primary key
  ...

The Cables table should just describe the properties of the cables, and should know nothing of how a cable connects to other cables.

The second table should be a list of possible connections between pairs of cables and optionally descriptive information about the connections:

Connections
  Cable1ID long not null constraint Connections_Cable1ID references Cables (ID) on delete cascade
  Cable2ID long not null constraint Connections_Cable2ID references Cables (ID) on delete cascade
  ConnectionDesc varchar(100)

This kind of table is known as a junction table, or a mapping table. It is used to implement a many-to-many relationship. Normally the relationship is between two different tables (e.g. students and courses), but it works just as well for relating two records within the same table.

This design will let you join the Cables, Connections, and Cables (again) tables in a single query to get the report you need.

The alternative is to have one connectors table, and then two foreign keys to it in the cables table.

 CREATE TABLE connector (
     id INT PRIMARY KEY,
     ...
 );

 CREATE TABLE cable (
     primary_connector_id INT NOT NULL REFERENCES connector(id),
     secondary_connector_id INT NOT NULL REFERENCES connector(id),
     ...
 );

Create a foreign key in the table which you want to connect to other table.

table one is

+---------------+------------+------+-----+---------+----------------+
| Field         | Type       | Null | Key | Default | Extra          |
+---------------+------------+------+-----+---------+----------------+
| user_id       | int(11)    | NO   | PRI | NULL    | auto_increment |
| name          | varchar(5) | NO   |     | NULL    |                |
| user_group_id | int(11)    | NO   |     | NULL    |                |
+---------------+------------+------+-----+---------+----------------+

table two is

+---------------+---------+------+-----+---------+----------------+
| Field         | Type    | Null | Key | Default | Extra          |
+---------------+---------+------+-----+---------+----------------+
| attendance_id | int(11) | NO   | PRI | NULL    | auto_increment |
| user_id       | int(11) | YES  |     | NULL    |                |
| present       | int(11) | YES  |     | NULL    |                |
| absents       | int(11) | YES  |     | NULL    |                |
+---------------+---------+------+-----+---------+----------------+

table two is connected with table one on user_id as foreign key

So from what I understand, you want to link a table to another table.

Use a foreign key. A Foreign Key is used when linking one table to another by containing the primary key of the table you're pointing to.

CREATE TABLE someTable(
    ID INT PRIMARY KEY
);

CREATE TABLE linkingTable(
    ID INT PRIMARY KEY,
    linked_ID INT,
    FOREIGN KEY (linked_ID) REFERENCES someTable(ID) ON DELETE CASCADE
);

Good luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!