2016-03-02 68 views
0

我知道這個主題有大量的問題,但不幸的是迄今爲止還沒有幫助。我想在我的數據庫中使用多列主鍵。數據庫位於服務器上,應該擁有許多用戶的證書。證書的主鍵是serialnumber + issuer(使證書唯一),除此之外 - 因爲多個用戶可以擁有此證書 - 我想添加(唯一)用戶名。 因此我的主鍵應該是(系列,發行者,用戶名)。但它不起作用。只要我嘗試再次添加證書,但對於不同的用戶,他只是覆蓋舊的條目。SQLite Java多列主鍵不工作

我的數據庫設置如下所示

stmt.execute(
       "CREATE TABLE IF NOT EXISTS certificates (" + 
         "serial VARCHAR NOT NULL," +  // serial 
         "issuer VARCHAR NOT NULL," +  // issuer 
         "subject VARCHAR NOT NULL," + // subject 
         "publickey VARCHAR NOT NULL," + // public key 
         "notbefore DATETIME NOT NULL," + // not before 
         "notafter DATETIME NOT NULL," + // not after 
         "certdata BLOB," +    // DER-encoded certificate 
         "revoked BOOLEAN NOT NULL," + // is certificate revoked 
         "trusted BOOLEAN NOT NULL," + // is certificate trusted 
         "untrusted BOOLEAN NOT NULL," + // is certificate untrusted 
         "S BOOLEAN NOT NULL," +   // is certificate in the S set 
         "" +        // of the related assessment 
         "username TEXT NOT NULL," + // the user to which this certificate belongs 
         "" + 
         "CHECK (S IN (0, 1))," + 
         "CHECK (trusted IN (0, 1))," + 
         "CHECK (untrusted IN (0, 1))," + 
         "CHECK (NOT (trusted = 1 AND untrusted = 1))," + 
         "" + 
         "FOREIGN KEY (username)" + 
         " REFERENCES users(username)" + 
         " ON DELETE CASCADE," + 
         "PRIMARY KEY (serial, issuer, username))"); 

聲明語句來自

poolManager = new MiniConnectionPoolManager(dataSource, MAX_CONNECTIONS); 

    try (Connection connection = poolManager.getConnection(); 
     Statement stmt = connection.createStatement()) { 

相反PRIMARY KEY(串行,發行人,用戶名),我已經嘗試UNIQUE(串行,發行人,用戶名)和CONSTRAINTS uniqueEntry PRIMARY KEY(串行,發行者,用戶名)。兩者都給出了相同的結果。

我正在使用IntelliJ 15.0.3,方言是SQLite,驅動程序是sqlite-jdbc-2.8.11.2.jar。

是的,還有更多的表格,但它們具有相同的結構。如果我找到該表的解決方案,所有其他人都應該相應地工作。

+0

如果用戶名是唯一的,爲什麼不把它用作主鍵?? – Zion

+1

你究竟如何插入? –

+0

@Zion:因爲一個用戶可能有多個證書。 – Isabella

回答

0

作爲用戶CL。指出,我們必須注意插入是如何完成的。問題在於我給出的代碼的寫法,其設計方式是主鍵必須分開給所有其他表條目