【问题标题】:How to store data which is essentially a list tied to a unique value in SQL如何存储本质上是与 SQL 中的唯一值相关联的列表的数据
【发布时间】:2016-01-21 02:17:07
【问题描述】:

这个问题可能已经被问了一千次了,但我不知道如何在谷歌中表述它,所以我没有得到任何好的答案。

假设我在 SQL 中有一个“单词”表,它在每行的第一列中存储了一些单词。我想在这里做的是存储该词的所有同义词,并将它们链接到第一列中的那个词。例如,如果我有“愤怒”这个词,我想将“疯狂、苦涩、愤怒、愤怒、恼怒”等词与那个词联系起来,所以如果我想通过 SQL 查询获得所有同义词,我可以轻松地这样做。

据我了解,尝试将列表存储在单元格中是行不通的。我唯一能想到的就是为每个同义词的“愤怒”设置一个行条目。但是如果一个词有很多同义词,或者我有很多词,感觉就会有很多条目(每个词都有很多不必要的重复)。还有比这更好的做法来实现我的目标吗?

【问题讨论】:

    标签: mysql sql list store synonym


    【解决方案1】:

    我也会使用两张桌子,但我的第二张与 Gordon 的不同。

     table word
     wordID  int pk
     word varchar
     other fields
    
    table synonym
    wordID int FK to word
    synonymID int FK to word
    pk is both fields
    

    查询愤怒的同义词

    select s.word
    from word w join synonym sy on w.wordID = sy.wordID
    join word s on sy.synonymID = s.wordID
    where w.word = 'angry'
    

    【讨论】:

    • 对于合成主键和复合主键的使用,我们可能总是不同意。两种方法都有其优点。干杯。
    • 当两个人意见不合时,你唯一可以确定的是,每个人都能形成一个。
    【解决方案2】:

    您使用两个表,一个用于定义外键关系的每个实体。表格如下所示:

    create table words (
        WordId int not null primary key auto_increment,
        Word varchar(255)
        . . .
    );
    
    create table synonyms (
        SynonymId int not null primary key auto_increment,
        WordId int not null,
        Synonym varchar(255),
        . . .
        constraint fk_wordid foreign key (WordId) references Words(WordId)
    );
    

    如果同义词必须是单词,那么您将使用第二个表中的 id:

    create table synonyms (
        SynonymId int not null primary key auto_increment,
        WordId int not null,
        SynonymWordId int not null,
        . . .
        constraint fk_wordid foreign key (WordId) references Words(WordId),
        constraint fk_synonymwordid foreign key (SynonymWordId) references Words(WordId)
    );
    

    【讨论】:

      【解决方案3】:

      有文字的表格:

      CREATE TABLE words (
        id integer PRIMARY KEY,
        word varchar(100) NOT NULL
      );
      INSERT INTO words VALUES (1,'angry'), (2,'mad'), (3,'bitter'),
      (4,'enraged'), (5,'furious'), (6,'irritated'),
      (7,'nice'), (8,'pleasant'), (9,'cute');
      

      具有同义词组的表

      CREATE TABLE synonymgroups (
        id integer PRIMARY KEY,
        description varchar(100)
      ); 
      INSERT INTO synonymgroups VALUES (1,'synonyms for "angry"'),
      (2,'synonyms for "nice"');
      

      还有一个带有组词映射的表格

      CREATE TABLE synonyms (
        synonymgroupid integer NOT NULL REFERENCES synonymgroups,
        wordid integer NOT NULL REFERENCES words,
        PRIMARY KEY (synonymgroupid,wordid)
      ); 
      INSERT INTO synonyms(synonymgroupid,wordid) VALUES (1,1), (1,2), (1,3),
      (1,4), (1,5), (1,6), (2,7), (2,8), (2,9);
      

      查找单词“mad”的所有同义词:

      SELECT s.wordid, w.word, s.synonymgroupid, sg.description
      FROM synonyms s
        JOIN words w ON w.id=s.wordid
        JOIN synonymgroups sg ON sg.id=s.synonymgroupid
      WHERE s.synonymgroupid IN (
        SELECT synonymgroupid
        FROM synonyms
        JOIN words ON words.id=synonyms.wordid
        WHERE words.word='mad'
      );
      

      PS。 SQL 数据库可能不是完成此类任务的最佳工具。这只是一个例子。

      【讨论】:

        猜你喜欢
        • 2011-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-09
        • 1970-01-01
        • 2014-06-30
        • 2013-04-10
        • 2017-05-15
        相关资源
        最近更新 更多