I want to add an auto_increment column in an oracle database using liquibase script.
I have tried using the auto_increment attribute in liquibase :
<column name="SUPPLEMENT_ID" type="INTEGER" autoIncrement="true" >
<constraints primaryKey="true"></constraints>
</column>
If oracle supports auto_increment, how can I achieve it using liquibase scripts?
IDENTITY columns are introduced in oracle 12c which allows users to create auto increment columns
But if you are using older versions you have to model the column with sequence and triggers as there is no such option as auto_increment or identity.
Please see this answer on how to do it in both cases
https://stackoverflow.com/a/11296469/8330426
According to #APC's comment under the question, Liquibase supports Oracle auto increment since version 3.4.0. See https://liquibase.jira.com/browse/CORE-1731.
Related
I am new to SAP PowerDesigner I am trying to created tables and link them together to get the DB model and I am having difficulties on enabling Auto increment for the primary Key column of tables. Can someone please guide me
I have looked online and there was mentioning of check marking something called as Identity. But I do not see that option on Column properties.Image2
Image1
Which version of Oracle are you using?
Oracle 12+ supports identity columns. In PowerDesigner, the Identityoption is available in the Oracle tab on the Column, when the DBMS for the Physical Data Model is ORACLE Version 12c.
create table CONTACTS (
ID int
generated always as identity ( start with 1 nocycle noorder) not null,
NAME varchar(100) not null,
constraint PK_CONTACTS primary key (ID)
);
For previous versions of Oracle, the autoincrement was implemented with sequence, and triggers. See this page of PowerDesigner online documentation for example.
I'm working on migrating all the tables in my schema to be partitioned tables. Since I'm on 11g instead of 12.2 I have to use DBMS_REDEFINITION. I've automated this with some Ruby code that executes the redefinition procedures for each table in my schema.
My issue is that after migrating, all of my non-nullable columns show NULLABLE - YES. This isn't actually true however, as I can check the constraints and there are check constraints defined for each of the columns that used to be NOT NULL, still enforcing the NOT NULL status. It seems like DBMS_REDEFINITION successfully copied the constraints, but it didn't reflect the NULLABLE status.
Is there a way to tell Oracle to get this back in sync?
I found the answer elsewhere:
https://dba.stackexchange.com/questions/55081/why-is-sqlplus-desc-table-not-showing-not-null-constraints-after-using-dbms-re
Oracle copies the constraints but never turns validate back on. You have to manually do that with:
ALTER TABLE <table_name> ENABLE VALIDATE CONSTRAINT <constraint_name>;
I am running the following changeset on an Oracle 12 database (the latest official docker image). When the changeset is executed, I get a SQL error:
Error: ORA-01735: invalid ALTER TABLE option
[Failed SQL: ALTER TABLE TEST.LANGUAGE ADD CONSTRAINT SYS_C0010528 PRIMARY KEY (ID) USING INDEX TEST.SYS_C0010528]
I generated the changeset from an existing Oracle database schema using Liquibase tools, with minor alterations.
Does liquibase not support Oracle 12?
How do I configure liquibase to generate Oracle 12-compatible SQL?
Is this a settings issue, or a bug in Liquibase?
Details:
It looks like Oracle doesn't accept the qualifier for the index name in the USING INDEX statement. The following revised SQL seems to work as intended:
ALTER TABLE TEST.LANGUAGE ADD CONSTRAINT SYS_C0010528 PRIMARY KEY (ID) USING INDEX SYS_C0010528
Here is the changeset definition:
<changeSet author="rmorrise (generated)" id="1507039841670-7">
<preConditions onFail="MARK_RAN">
<not>
<primaryKeyExists tableName="LANGUAGE"/>
</not>
</preConditions>
<createIndex indexName="SYS_C0010528" tableName="LANGUAGE" unique="true">
<column name="ID"/>
</createIndex>
<addPrimaryKey columnNames="ID" constraintName="SYS_C0010528" forIndexName="SYS_C0010528" tableName="LANGUAGE"/>
</changeSet>
Update:
I fixed the <addPrimaryKey> entries by replacing them with <sql> tags, but I got the same error on <addUniqueConstraint> with forIndexName.
Here is the generated SQL to create the index. It appears to be created in the same schema.
CREATE UNIQUE INDEX TEST.UK46FCA532E38E4F92860631AA28CB ON TEST.MESSAGE_ROLE(TEMPLATE_ID, LABEL);
ALTER TABLE TEST.MESSAGE_ROLE ADD CONSTRAINT UK46FCA532E38E4F92860631AA28CB UNIQUE (TEMPLATE_ID, LABEL) USING INDEX TEST.UK46FCA532E38E4F92860631AA28CB;
To answer Alex's question about the use of system object names: The schema was originally generated by hbm2ddl and migrated to liquibase using generateChangelog.
A query to dba_objects confirms that the OWNER is TEST.
I had a similar problem and as I was reading this topic I see that we both named our schema "TEST".
I changed the schema's name and it worked immediatly.
does DB2 support enums? I didn't really find anything online.
The Query doesn't work:
create table prototype.test(id int not null primary key, level ENUM('upper', 'lower') not null);
Thanks in advance!
You can create a check constraint for that.
alter table prototype.test add constraint checklevel check (level in ('upper', 'lower'));
Or you can include this in the create table:
create table prototype.test(
id int not null primary key,
level varchar(5) check (level in ('upper', 'lower')
);
No DB2 does not support ENUMS. The are some database which I am aware of which suports Enums is MySql and Postgresql but DB2 for sure does not support it.
So I have an Oracle database which I am updating in liquibase... in the process of testing my DB build system, I created a new change which is identical in content to an older change with a different ID. Both are adding a column with the same name, using SQL. I would expect the 2nd to fail, since it would attempt to create a column which already exists, but the liquibase command updateTestingRollback reports:
Liquibase 'updateTestingRollback' Successful
Any ideas what the potential cause may be?
<changeSet id="2" author="anonymous" failOnError="true">
<sql>ALTER TABLE person ADD (col_a VARCHAR(1));</sql>
<rollback>ALTER TABLE person DROP COLUMN col_a;</rollback>
</changeSet>
<changeSet id="3" author="anonymous" failOnError="true">
<sql>ALTER TABLE person ADD (col_a VARCHAR(1));</sql>
<rollback>ALTER TABLE person DROP COLUMN col_a;</rollback>
</changeSet>
You might try running with the --logLevel=debug flag added to make sure, but my guess is that oracle is not returning an error code when the sql for the second change is applied. if you use the <sql> tag rather than more specific change type tags (i.e. the <addColumn> tag), liquibase has a more difficult time detecting problems.