我正在尝试插入一项涉及许多朋友的费用。但是使用外键,它只能引用一个friendId,但我想在费用表中存储2个friendId而不是一个为expenseFriend。因为每个朋友可以有很多费用,而一笔费用可以被很多朋友分担。我该如何实现这种关系?也许不使用外键?谁能给我这样做的想法?非常感谢。
这是我的创建数据库代码:
public void onCreate(SQLiteDatabase database) {
String query, query1, query2, query3;
query = "CREATE TABLE events ( eventId INTEGER PRIMARY KEY, eventName TEXT, eventDate TEXT, eventLocation TEXT, eventDescription TEXT);";
query1 = "CREATE TABLE friends ( friendId INTEGER PRIMARY KEY, friendName TEXT, friendNumber TEXT, friendEmail TEXT, friendSpending DOUBLE, friendEvent INTEGER, FOREIGN KEY(friendEvent) REFERENCES events(eventId) ON DELETE CASCADE);";
query2 = "CREATE TABLE expenses ( expenseId INTEGER PRIMARY KEY, expenseName TEXT, expenseType TEXT, expensePrice REAL, expenseQuantity INTEGER, expenseEvent INTEGER REFERENCES events(eventId) ON DELETE CASCADE, expenseFriend INTEGER REFERENCES friends(friendId) ON DELETE CASCADE);";
query3 = "CREATE TABLE temporary (_id INTEGER PRIMARY KEY, spendingId INTEGER)";
database.execSQL(query);
database.execSQL(query1);
database.execSQL(query2);
database.execSQL(query3);
Log.d(LOGCAT, "events Created");
Log.d(LOGCAT, "friends Created");
Log.d(LOGCAT, "expenses Created");