Search in sources :

Example 31 with Constraint

use of org.h2.constraint.Constraint in project h2database by h2database.

the class TestMVTableEngine method testReferentialIntegrity.

private void testReferentialIntegrity() throws Exception {
    Connection conn;
    Statement stat;
    deleteDb(getTestName());
    conn = getConnection(getTestName() + ";MV_STORE=TRUE");
    stat = conn.createStatement();
    stat.execute("create table test(id int, parent int " + "references test(id) on delete cascade)");
    stat.execute("insert into test values(0, 0)");
    stat.execute("delete from test");
    stat.execute("drop table test");
    stat.execute("create table parent(id int, name varchar)");
    stat.execute("create table child(id int, parentid int, " + "foreign key(parentid) references parent(id))");
    stat.execute("insert into parent values(1, 'mary'), (2, 'john')");
    stat.execute("insert into child values(10, 1), (11, 1), (20, 2), (21, 2)");
    stat.execute("update parent set name = 'marc' where id = 1");
    stat.execute("merge into parent key(id) values(1, 'marcy')");
    stat.execute("drop table parent, child");
    stat.execute("create table test(id identity, parent bigint, " + "foreign key(parent) references(id))");
    stat.execute("insert into test values(0, 0), (1, NULL), " + "(2, 1), (3, 3), (4, 3)");
    stat.execute("drop table test");
    stat.execute("create table parent(id int)");
    stat.execute("create table child(pid int)");
    stat.execute("insert into parent values(1)");
    stat.execute("insert into child values(2)");
    try {
        stat.execute("alter table child add constraint cp " + "foreign key(pid) references parent(id)");
        fail();
    } catch (SQLException e) {
        assertEquals(ErrorCode.REFERENTIAL_INTEGRITY_VIOLATED_PARENT_MISSING_1, e.getErrorCode());
    }
    stat.execute("update child set pid=1");
    stat.execute("drop table child, parent");
    stat.execute("create table parent(id int)");
    stat.execute("create table child(pid int)");
    stat.execute("insert into parent values(1)");
    stat.execute("insert into child values(2)");
    try {
        stat.execute("alter table child add constraint cp " + "foreign key(pid) references parent(id)");
        fail();
    } catch (SQLException e) {
        assertEquals(ErrorCode.REFERENTIAL_INTEGRITY_VIOLATED_PARENT_MISSING_1, e.getErrorCode());
    }
    stat.execute("drop table child, parent");
    stat.execute("create table test(id identity, parent bigint, " + "foreign key(parent) references(id))");
    stat.execute("insert into test values(0, 0), (1, NULL), " + "(2, 1), (3, 3), (4, 3)");
    stat.execute("drop table test");
    stat.execute("create table parent(id int, x int)");
    stat.execute("insert into parent values(1, 2)");
    stat.execute("create table child(id int references parent(id)) as select 1");
    conn.close();
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 32 with Constraint

use of org.h2.constraint.Constraint in project h2database by h2database.

the class TestIndex method testRenamePrimaryKey.

private void testRenamePrimaryKey() throws SQLException {
    if (config.memory) {
        return;
    }
    reconnect();
    stat.execute("create table test(id int not null)");
    stat.execute("alter table test add constraint x primary key(id)");
    ResultSet rs;
    rs = conn.getMetaData().getIndexInfo(null, null, "TEST", true, false);
    rs.next();
    String old = rs.getString("INDEX_NAME");
    stat.execute("alter index " + old + " rename to y");
    rs = conn.getMetaData().getIndexInfo(null, null, "TEST", true, false);
    rs.next();
    assertEquals("Y", rs.getString("INDEX_NAME"));
    reconnect();
    rs = conn.getMetaData().getIndexInfo(null, null, "TEST", true, false);
    rs.next();
    assertEquals("Y", rs.getString("INDEX_NAME"));
    stat.execute("drop table test");
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 33 with Constraint

use of org.h2.constraint.Constraint in project h2database by h2database.

the class TestTriggersConstraints method testConstraints.

private void testConstraints() throws SQLException {
    Connection conn = getConnection("trigger");
    Statement stat = conn.createStatement();
    stat.execute("DROP TABLE IF EXISTS TEST");
    stat.execute("create table test(id int primary key, parent int)");
    stat.execute("alter table test add constraint test_parent_id " + "foreign key(parent) references test (id) on delete cascade");
    stat.execute("insert into test select x, x/2 from system_range(0, 100)");
    stat.execute("delete from test");
    assertSingleValue(stat, "select count(*) from test", 0);
    stat.execute("drop table test");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 34 with Constraint

use of org.h2.constraint.Constraint in project h2database by h2database.

the class TestTriggersConstraints method testMultiPartForeignKeys.

/**
 * Regression test: we had a bug where the AlterTableAddConstraint class
 * used to sometimes pick the wrong unique index for a foreign key.
 */
private void testMultiPartForeignKeys() throws SQLException {
    Connection conn = getConnection("trigger");
    Statement stat = conn.createStatement();
    stat.execute("DROP TABLE IF EXISTS TEST1");
    stat.execute("DROP TABLE IF EXISTS TEST2");
    stat.execute("create table test1(id int primary key, col1 int)");
    stat.execute("alter table test1 add constraint unique_test1 " + "unique (id,col1)");
    stat.execute("create table test2(id int primary key, col1 int)");
    stat.execute("alter table test2 add constraint fk_test2 " + "foreign key(id,col1) references test1 (id,col1)");
    stat.execute("insert into test1 values (1,1)");
    stat.execute("insert into test1 values (2,2)");
    stat.execute("insert into test1 values (3,3)");
    stat.execute("insert into test2 values (1,1)");
    assertThrows(23506, stat).execute("insert into test2 values (2,1)");
    assertSingleValue(stat, "select count(*) from test1", 3);
    assertSingleValue(stat, "select count(*) from test2", 1);
    stat.execute("drop table test1");
    stat.execute("drop table test2");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Aggregations

Constraint (org.h2.constraint.Constraint)15 Index (org.h2.index.Index)10 ValueString (org.h2.value.ValueString)9 Table (org.h2.table.Table)8 Column (org.h2.table.Column)6 IndexColumn (org.h2.table.IndexColumn)6 Connection (java.sql.Connection)5 Statement (java.sql.Statement)5 Database (org.h2.engine.Database)5 StatementBuilder (org.h2.util.StatementBuilder)5 ResultSet (java.sql.ResultSet)4 ArrayList (java.util.ArrayList)4 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)4 DbObject (org.h2.engine.DbObject)4 Right (org.h2.engine.Right)4 Expression (org.h2.expression.Expression)4 ExpressionColumn (org.h2.expression.ExpressionColumn)4 Schema (org.h2.schema.Schema)4 Sequence (org.h2.schema.Sequence)4 PreparedStatement (java.sql.PreparedStatement)3