SQLite Java 多列主键不工作
SQLite Java Multiple columns primary key not working
我知道有很多关于这个主题的问题,但不幸的是到目前为止没有任何帮助。我想要我的数据库中有一个多列主键。数据库位于服务器上,应该为许多用户持有证书。证书的主键是序列号 + 颁发者(这使证书唯一),除此之外 - 因为多个用户可以拥有此证书 - 我想添加(唯一)用户名。
因此,我的主键应该是(序列号、发行人、用户名)。但它不起作用。一旦我尝试再次添加证书但对于不同的用户,他就会覆盖旧条目。
我的数据库设置如下
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(序列号、发行人、用户名)和 CONSTRAINTS uniqueEntry PRIMARY KEY(序列号、发行人、用户名),而不是 PRIMARY KEY(序列号、发行人、用户名)。两者给出相同的结果。
我正在使用 IntelliJ 15.0.3,方言是 SQLite,驱动程序是 sqlite-jdbc-2.8.11.2.jar .
是的,还有一些 table,但它们的结构相同。如果我找到了这个 table 的解决方案,所有其他人都应该相应地工作。
作为用户 CL。指出,我们还必须注意插入是如何完成的。问题在于我得到的代码的编写方式,其设计方式是必须将主键单独提供给所有其他 table-entries
我知道有很多关于这个主题的问题,但不幸的是到目前为止没有任何帮助。我想要我的数据库中有一个多列主键。数据库位于服务器上,应该为许多用户持有证书。证书的主键是序列号 + 颁发者(这使证书唯一),除此之外 - 因为多个用户可以拥有此证书 - 我想添加(唯一)用户名。 因此,我的主键应该是(序列号、发行人、用户名)。但它不起作用。一旦我尝试再次添加证书但对于不同的用户,他就会覆盖旧条目。
我的数据库设置如下
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(序列号、发行人、用户名)和 CONSTRAINTS uniqueEntry PRIMARY KEY(序列号、发行人、用户名),而不是 PRIMARY KEY(序列号、发行人、用户名)。两者给出相同的结果。
我正在使用 IntelliJ 15.0.3,方言是 SQLite,驱动程序是 sqlite-jdbc-2.8.11.2.jar .
是的,还有一些 table,但它们的结构相同。如果我找到了这个 table 的解决方案,所有其他人都应该相应地工作。
作为用户 CL。指出,我们还必须注意插入是如何完成的。问题在于我得到的代码的编写方式,其设计方式是必须将主键单独提供给所有其他 table-entries