簡體   English   中英

在sqlite數據庫android中對表的兩個引用

[英]Two references to table in sqlite database android

我正在嘗試將匹配結果存儲在數據庫中。 我有一個播放器表,其中包含一個ID,名稱密碼。 另一個存儲匹配項的表。 然后,最后是一個比賽參與者表,該表將具有一個idplayer_id (外鍵), match_id (外鍵)和一個整數score 我正在嘗試創建Match表,以便它可以引用兩個Match參與者,但是我不確定如何執行此操作。

我創建表的代碼

 myDB.execSQL("CREATE TABLE IF NOT EXISTS "
                    + playerTableName
                    + " (Player_id integer primary key autoincrement, " +
            "UserName VARCHAR, Password VARCHAR, Experience INTEGER, Rating INTEGER);");

            myDB.execSQL("CREATE TABLE IF NOT EXISTS "
                    + matchTableName
                    + "(Match_id integer primary key autoincrement, " +
            "? not sure here");");

            myDB.execSQL("CREATE TABLE IF NOT EXISTS "
                    + participantTableName
                    + "(Participant_id integer primary key autoincrement, " +
                    "player_id INTEGER, match_id Integer," +
                    "FOREIGN KEY(player_id) REFERENCES "+ playerTableName+"(Player_id)," +
                    " FOREIGN KEY(match_id) REFERENCES "+ matchTableName+"(Match_id)," +
            " Score INTEGER);");
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
                + matchTableName
                + "(Match_id integer primary key autoincrement, "
                + "Participant1_id integer, ");
                + "Participant2_id integer, ");
                + "FOREIGN KEY(Participant1_id) REFERENCES "+ participantTableName+"(Participant_id),"
                + "FOREIGN KEY(Participant2_id) REFERENCES "+ participantTableName+"(Participant_id)"
                + ");";

有兩個鏈接到同一個表根本沒有問題。 在查詢中進行選擇或插入時,請確保在表中保存正確的ID,這將起作用。

一些注意事項:

  • 參與者表中不需要match_id。 這只會復制您的對照表中的參與者ID。 在SQL表中有重復的信息永遠都不是一件好事,它們可能不同步,並且約束可能會變成地獄(刪除時)。 或者,如果您想保留該match_id,則不需要匹配表中的兩個參與者ID。

  • 您應該將所有主ID命名為_id ,這是SQLite通常的良好做法(而且我認為在某些情況下,Android / SQLite在找不到它們時會崩潰)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM