Mysql - 使用主键作为外键

时间:2013-08-11 14:53:35

标签: mysql sql foreign-keys many-to-many primary-key

我有两个共享多对多关系的表,即重叠群路径

mysql> describe contig ;
+-------------------+----------------------+------+-----+---------+-------+
| Field             | Type                 | Null | Key | Default | Extra |
+-------------------+----------------------+------+-----+---------+-------+
| id_contig         | smallint(5) unsigned | NO   | PRI | NULL    |       |
| length            | smallint(5) unsigned | NO   |     | NULL    |       |
| total_rc          | int(11)              | NO   |     | NULL    |       |
| rc_es1            | smallint(6)          | NO   |     | NULL    |       |
| rc_es2            | smallint(6)          | NO   |     | NULL    |       |
| rc_ls1            | smallint(6)          | NO   |     | NULL    |       |
| rc_ls2            | smallint(6)          | NO   |     | NULL    |       |
| rpkm_es1          | float(10,2)          | NO   |     | NULL    |       |
| rpkm_es2          | float(10,2)          | NO   |     | NULL    |       |
| rpkm_ls1          | float(10,2)          | NO   |     | NULL    |       |
| rpkm_ls2          | float(10,2)          | NO   |     | NULL    |       |
| hit_id_nr         | varchar(50)          | YES  |     | NULL    |       |
| hit_id_tair       | varchar(50)          | YES  |     | NULL    |       |
| hit_id_uniprot    | varchar(50)          | YES  |     | NULL    |       |
| hit_desc_nr       | text                 | YES  |     | NULL    |       |
| hit_desc_tair     | text                 | YES  |     | NULL    |       |
| hit_desc_uniprot  | text                 | YES  |     | NULL    |       |
| bayseq_likelihood | float(11,9)          | YES  |     | NULL    |       |
| bayseq_fdr        | float(11,9)          | YES  |     | NULL    |       |
| deseq_fc          | float(10,2)          | YES  |     | NULL    |       |
| deseq_log2fc      | float(10,2)          | YES  |     | NULL    |       |
| deseq_pval        | double               | YES  |     | NULL    |       |
| deseq_padj        | double               | YES  |     | NULL    |       |
| edger_logconc     | double               | YES  |     | NULL    |       |
| edger_log2fc      | double               | YES  |     | NULL    |       |
| edger_pval        | double               | YES  |     | NULL    |       |
| edger_fdr         | double               | YES  |     | NULL    |       |
+-------------------+----------------------+------+-----+---------+-------+

mysql> describe path ;
+-----------+----------------------+------+-----+---------+-------+
| Field     | Type                 | Null | Key | Default | Extra |
+-----------+----------------------+------+-----+---------+-------+
| id_path   | tinyint(3) unsigned  | NO   | PRI | NULL    |       |
| path_name | text                 | NO   |     | NULL    |       |
| path_nseq | smallint(5) unsigned | NO   |     | NULL    |       |
| path_map  | varchar(8)           | NO   |     | NULL    |       |
| path_pix  | varchar(28)          | NO   |     | NULL    |       |
+-----------+----------------------+------+-----+---------+-------+

为了链接它们,我创建了一个“交叉表”,即 contig2path

mysql> describe contig2path ;
+-----------+----------------------+------+-----+---------+-------+
| Field     | Type                 | Null | Key | Default | Extra |
+-----------+----------------------+------+-----+---------+-------+
| id_contig | smallint(5) unsigned | NO   | PRI | NULL    |       |
| id_path   | tinyint(3) unsigned  | NO   | PRI | NULL    |       |
+-----------+----------------------+------+-----+---------+-------+

正如您可能想象的那样,我想使用 contig2path 链接表格重叠群路径中的数据。但在使用任何JOIN之前,我必须使用外键链接表。

-> contig.id_contig would be linked to conti2path.id_contig
-> path.id_path would be linked to contig2path.id_path

我的问题是:如何让主键成为外键?

编辑:您可以使用表格的创建方式:

contig:

CREATE DATABASE IF NOT EXISTS `nutsedge` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `nutsedge` ;

-- -----------------------------------------------------
-- Table `nutsedge`.`contig`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `nutsedge`.`contig` ;
CREATE  TABLE IF NOT EXISTS `nutsedge`.`contig` (
  `id_contig` SMALLINT UNSIGNED NOT NULL ,
  `length` SMALLINT UNSIGNED NOT NULL ,
  `total_rc` INT NOT NULL ,
  `rc_es1` SMALLINT NOT NULL ,
  `rc_es2` SMALLINT NOT NULL ,
  `rc_ls1` SMALLINT NOT NULL ,
  `rc_ls2` SMALLINT NOT NULL ,
  `rpkm_es1` FLOAT (10,2) NOT NULL ,
  `rpkm_es2` FLOAT (10,2) NOT NULL ,
  `rpkm_ls1` FLOAT (10,2) NOT NULL ,
  `rpkm_ls2` FLOAT (10,2) NOT NULL ,
  `hit_id_nr` VARCHAR(50) NULL ,
  `hit_id_tair` VARCHAR(50) NULL ,
  `hit_id_uniprot` VARCHAR(50) NULL ,
  `hit_desc_nr` TEXT NULL ,
  `hit_desc_tair` TEXT NULL ,
  `hit_desc_uniprot` TEXT NULL ,
  `bayseq_likelihood` FLOAT (11,9) NULL ,
  `bayseq_fdr` FLOAT (11,9) NULL ,
  `deseq_fc` FLOAT (10,2) NULL ,
  `deseq_log2fc` FLOAT (10,2) NULL,
  `deseq_pval` DOUBLE NULL,
  `deseq_padj` DOUBLE NULL,
  `edger_logconc` DOUBLE NULL,
  `edger_log2fc` DOUBLE NULL,
  `edger_pval` DOUBLE NULL,
  `edger_fdr` DOUBLE NULL,
  PRIMARY KEY (`id_contig`) )
ENGINE = InnoDB;
SELECT 'NUTSEDGE DATABASE CREATED';
SHOW warnings;

路径:

-- -----------------------------------------------------
-- Table `nutsedge`.`path`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `nutsedge`.`path` ;
CREATE  TABLE IF NOT EXISTS `nutsedge`.`path` (
  `id_path` TINYINT UNSIGNED NOT NULL ,
  `path_name` TEXT NOT NULL ,
  `path_nseq` SMALLINT UNSIGNED NOT NULL ,
  `path_map` VARCHAR(8) NOT NULL,
  `path_pix` VARCHAR(28) NOT NULL,
  PRIMARY KEY (`id_path`) )
ENGINE = InnoDB;
SELECT 'PATHWAY TABLE CREATED';
SHOW warnings;

contig2path:

-- -----------------------------------------------------
-- Table `nutsedge`.`contig2path`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `nutsedge`.`contig2path` ;
CREATE  TABLE IF NOT EXISTS `nutsedge`.`contig2path` (
  `id_contig` SMALLINT UNSIGNED NOT NULL ,
  `id_path` TINYINT UNSIGNED NOT NULL ,
  PRIMARY KEY (`id_contig`,`id_path`) )
ENGINE = InnoDB;
SELECT 'CONTIG2PATHWAY TABLE CREATED';
SHOW warnings;

2 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情: -

Add FOREIGN KEY (id_contig ) REFERENCES conti2path(id_contig)

答案 1 :(得分:0)

创建外键: 您需要分别从contig2path到contig和path的两个引用。 您可以这样创建这些键:

ALTER TABLE contig2path ADD CONSTRAINT fkey1 FOREIGN KEY id_contig REFERENCES contig (id_contig);
ALTER TABLE contig2path ADD CONSTRAINT fkey2 FOREIGN KEY id_path REFERENCES path (id_path);

要在contig2path中创建主键,您可以在创建表时执行以下操作:

create table contig2path (
...,
PRIMARY KEY (id_contig, id_path)
)