【问题标题】:SQLite Java Multiple columns primary key not workingSQLite Java多列主键不起作用
【发布时间】:2016-03-02 20:27:46
【问题描述】:

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

我的数据库设置如下

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 (serial, issuer, username) 而不是 PRIMARY KEY (serial, issuer, username)。两者都给出相同的结果。

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

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

【问题讨论】:

  • 如果用户名是唯一的,为什么不将其用作主键??
  • 你到底想插入什么?
  • @Zion:因为一个用户可能拥有多个证书。
  • @CL。非常感谢您。这就是问题所在。在我的前辈编写插入语句的方式中,主键必须单独给出。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-07-22
    • 2010-10-18
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多