SQLite Java多列主键无法正常工作

时间:2016-03-02 09:56:20

标签: java sqlite intellij-idea composite-primary-key

我知道关于这个话题有很多问题,但不幸的是到目前为止没有任何帮助。我想在我的数据库中使用多列主键。数据库位于服务器上,应该为许多用户保留证书。证书的主键是serialnumber + issuer(它使证书唯一),除此之外 - 因为多个用户可以拥有此证书 - 我想添加(唯一的)用户名。 因此,我的主键应该是(serial,issuer,username)。但它不起作用。一旦我尝试再次添加证书但是对于不同的用户,他只会覆盖旧条目。

我的数据库设置如下

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))");

声明stmt来自

poolManager = new MiniConnectionPoolManager(dataSource, MAX_CONNECTIONS);

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

我已经尝试过UNIQUE(serial,issuer,username)和CONSTRAINTS uniqueEntry PRIMARY KEY(序列号,发行者,用户名),而不是PRIMARY KEY(序列号,发行者号,用户名)。两者都给出了相同的结果。

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

是的,还有更多的表格,但它们具有相同的结构。如果我找到这个表的解决方案,所有其他人应该相应地工作。

1 个答案:

答案 0 :(得分:0)

作为用户CL。指出,我们必须注意如何完成插入。问题在于我编写的代码的编写方式,其设计方式是主键必须与所有其他表条目分开给出