Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 基于另一列的外键_Mysql_Sql_Database_Foreign Keys_One To Many - Fatal编程技术网

Mysql 基于另一列的外键

Mysql 基于另一列的外键,mysql,sql,database,foreign-keys,one-to-many,Mysql,Sql,Database,Foreign Keys,One To Many,我有一个数据库,它有以下表格:评论,文章,视频,歌曲,游戏,书籍,在评论和其他之间有一对多的关系(一篇文章、视频、歌曲……可能有许多评论)。我有一个关联表CommentBelongsTo有三列comment\u id,page\u type,page\u id),例如,如果page\u type='article'和page\u id=1,则表示注释属于id=1的文章。问题是如何根据页面类型将页面id设置为外键?或者有更好的表设计吗?下面的表结构可以解决这个问题 DROP TABLE IF EX

我有一个数据库,它有以下表格:
评论
文章
视频
歌曲
游戏
书籍
,在
评论
和其他之间有一对多的关系(一篇文章、视频、歌曲……可能有许多评论)。我有一个关联表
CommentBelongsTo
有三列
comment\u id
page\u type
page\u id
),例如,如果
page\u type='article'
page\u id=1
,则表示注释属于id=1的文章。问题是如何根据
页面类型将
页面id
设置为外键?或者有更好的表设计吗?

下面的表结构可以解决这个问题

DROP TABLE IF EXISTS `test`.`article`;
CREATE TABLE  `test`.`article` (
  `idArticle` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Name` varchar(45) NOT NULL DEFAULT '',
  PRIMARY KEY (`idArticle`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `test`.`games`;
CREATE TABLE  `test`.`games` (
  `idGames` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL DEFAULT '',
  PRIMARY KEY (`idGames`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `test`.`comments`;
CREATE TABLE  `test`.`comments` (
  `idComments` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `a_id` int(10) unsigned DEFAULT NULL,
  `g_id` int(10) unsigned DEFAULT NULL,
  `Comment` varchar(45) NOT NULL DEFAULT '',
  PRIMARY KEY (`idComments`),
  KEY `FK_Comments_1` (`a_id`),
  KEY `FK_Comments_2` (`g_id`),
  CONSTRAINT `FK_Comments_1` FOREIGN KEY (`a_id`) REFERENCES `article` (`idArticle`),
  CONSTRAINT `FK_Comments_2` FOREIGN KEY (`g_id`) REFERENCES `games` (`idGames`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

insert into article values (1,'A1');
insert into comments values (1,1,null,'A1 comment')
insert into games values (1,'G1');
insert into comments values (1,null,1,'G1 comment')

select * from comments where a_id<>'NULL';
删除表(如果存在)`test`.`article`);
创建表“test”。“article”(
`idArticle`int(10)无符号非空自动增量,
`名称“varchar(45)非空默认值”,
主键(`idArticle`)
)ENGINE=InnoDB AUTO_INCREMENT=4默认字符集=1;
删除表(如果存在)`test`.`games`);
创建表“测试”。“游戏”(
`idGames`int(10)无符号非空自动增量,
`名称“varchar(45)非空默认值”,
主键(`idGames`)
)ENGINE=InnoDB AUTO_INCREMENT=4默认字符集=1;
删除表(如果存在)`test`.`comments`);
创建表“test”。“comments”(
`idComments`int(10)无符号非空自动增量,
`a_id`int(10)无符号默认为空,
`g_id`int(10)无符号默认为空,
`注释'varchar(45)非空默认值',
主键(`idComments`),
键'FK_Comments_1'('a_id'),
键'FK_Comments_2'('g_id'),
约束'FK_Comments_1'外键('a_id')引用'article'('idArticle'),
约束'FK_Comments_2'外键('g_id')引用'games'('idGames`)
)ENGINE=InnoDB AUTO_INCREMENT=3默认字符集=1;
插入物品价值(1,'A1');
插入注释值(1,1,null,'A1注释')
在游戏中插入值(1,'G1');
插入注释值(1,null,1,'G1注释')
从注释中选择*,其中有一个_id'NULL';

要获取特定类型的注释

请使用正在使用的数据库标记您的问题。