I am running Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production, I am trying to find an equivalent to 12c's "DEFAULT ON NULL" for a table. Basically I have to create a table where the requirement is than whenever someone intentionally or inadvertently passes a NULL value, it is replaced with a DEFAULT value (in this case a NUMBER type equal to 1). Is there any easy way to do this in 11g? I know I could do a trigger on the table, but I would have to put in logic for every single column, and that seems ridiculous.
My table definition currently looks like this:
CREATE TABLE MYTABLE
(
FLAG NUMBER(1) DEFAULT 0
)
If I explicitly pass in null it WILL get stored. In that situation I was expecting the default value to be placed instead.
You can use simple 'DEFAULT' behavior for columns and don't provide anything when inserting.
CREATE TABLE table1 (
id NUMBER(8.0) NOT NULL,
col1 NUMBER(8.0) NOT NULL DEFAULT 10,
col2 NUMBER(8.0) NOT NULL DEFAULT 20,
PRIMARY KEY (id);
);
INSERT INTO table1 (id) values (123); //will result in creating a row with default values.
Also if you're using some kind of ORM you can use dynamic insert and dynamic update options. This way if you don't provide values on insert they will be set to defaults;
and on update the unchanged values will not be included into query.
This is how you can bypass explicit null values in query and defaults will apply.
Related
I am new to using Apex Oracle to create a table and insert values in it. I need to create a column that is only mandatory if the value for another column is "Y" (if it is "N", then it is not mandatory). The type of the other column is a CHAR with length 1. How could I do this? Would this be done in SQL Scripts or SQL Commands? Similarly, is there a way to delete old SQL commands that were used (that now I realize are incorrect)?
Thank you!
Welcome to Oracle APEX and Stack Overflow. You can create objects (tables/views) in both SQL commands and SQL Scripts. For ad hoc creating, SQL Commands is probably easier. To remove (called "drop" in oracle) objects that you create, that can be done in SQL Commands, or even easier in the "Object Browser" - locate the object and select "drop". Note that this cannot be undone.
About the requirement for a column to be conditionally mandatory:
This can be enforced in the database using a check constraint.
CREATE TABLE test_table (
id NUMBER GENERATED AS IDENTITY,
col1 VARCHAR2(1),
col2 VARCHAR2(10));
Table TEST_TABLE created.
ALTER TABLE test_table ADD CONSTRAINT test_table_c1
CHECK ((col1 = 'Y' AND col2 IS NOT NULL) or (col1 != 'Y'));
Table TEST_TABLE altered.
INSERT INTO test_table(col1,col2) VALUES ('N',NULL);
1 row inserted.
INSERT INTO test_table(col1,col2) VALUES ('Y',NULL);
INSERT INTO test_table(col1,col2) VALUES ('Y',NULL)
Error report -
ORA-02290: check constraint (SAMPLEAPPS.TEST_TABLE_C1) violated
INSERT INTO test_table(col1,col2) VALUES ('Y','Some Value');
1 row inserted.
I am trying to insert rows in a ErrorTable which has a few fields plus an idError which is supossed to be the primary key. I need idError to be autoincremented. However one requirement is we cannot use a trigger, so using O.AutoInc would not work for us.
We also tried to use plain sql using a sequence. However we have two blob fields which makes the query too long ( getting the string literal too long error).
Any idea about how to attack this problem? I am also considering to use UUID.
Note: we are using oracle-xe-11g
In 11g you can have only implement an auto-incrementing identifier with a trigger. So it seems your requirements rule out anything except SYS_GUID. Find out more.
" it also represents another query to get that value "
Not necessarily. If you have the option to define the target table you can set a default values for the UUID column like this:
create table t23 (
id raw(16) default sys_guid() not null primary key
, col1 varchar2(10)
);
Then
SQL> insert into t23 (col1) values ('ABC');
1 row created.
SQL> select * from t23;
ID COL1
-------------------------------- -----------
7DD7216E731C126537615FE1244B4B50 ABC
SQL>
Note: tested on 12C but should work in earlier versions.
Something strange happened to me today ,my friends(developers) said its probably a bug ... Usually when I want to see the DDL of a table in PL/SQL , I right-click on the table and then I click on view and I get the DDL .However, there is another way I can get the DDL of a table and by right clicking on the table and there something called DBMS_METADATA then I put my cursor on it and it will show me DDL. In the image that I upload there a difference between the DBMS_METADATA and the 'view' .Number 1 represents the 'view' and the 2 represents the DBMS_METADATA, if you notice there is a huge difference between the two .The first one shows column organization_code its not null (because its not checked) but the 2nd one shows organization_code is null. this made the developers confused, which one they should count on ? .But after testing the column its not null. I should mention that is column is a primary key so it should be NOT null why in medata showed a wrongs information ? does that happened to anyone before ?(by the way i am using 11g)
A column used for a primary key cannot be null1. However, that restriction can be enforced solely through a primary key constraint, and does not require a separate not null constraint. The IDE, PL/SQL Developer, is showing you a generally more useful combination of primary key constraints and not null constraints. DBMS_METADATA is showing you exactly how the tables were specified, which is irrelevant unless you plan on dropping the primary keys.
create table table1(a number not null primary key);
create table table2(a number primary key);
begin
dbms_metadata.set_transform_param(dbms_metadata.session_transform,
'SEGMENT_ATTRIBUTES',false);
end;
/
select dbms_metadata.get_ddl('TABLE', 'TABLE1') from dual;
CREATE TABLE "JHELLER"."TABLE1"
( "A" NUMBER NOT NULL ENABLE,
PRIMARY KEY ("A") ENABLE
)
select dbms_metadata.get_ddl('TABLE', 'TABLE2') from dual;
CREATE TABLE "JHELLER"."TABLE2"
( "A" NUMBER,
PRIMARY KEY ("A") ENABLE
)
In PL/SQL Developer, neither column has Nullable checked.
1 Unless you use a novalidated non-unique index, which is extremely rare.
I'd like to add a NOT NULL column to a table called server. Problem is, the table already contains records. When I invoke ALTER TABLE server ADD COLUMN full_discovery_duration BIGINT NOT NULL H2 complains that full_discovery_duration may not be null. I can work around the problem by specifying DEFAULT 0 but I don't want a default valuefor future inserts. What am I supposed to do?
Should I add the column with a default and then remove DEFAULT 0 from the column definition in a subsequent statement? Is there a better way?
You can first add the column with a default value, and then set the default to null. Getting rid of the default definition is not possible however as far as I know.
An alternative is to first allow null, then set the values, and later not allow nulls.
drop table server;
create table server(id int);
insert into server values(1);
alter table server
add column
full_discovery_duration bigint;
update server set full_discovery_duration = 0;
alter table server
alter column
full_discovery_duration set not null;
While adding columns to existing tables, it should either be a nullable column or a default value must be specified. And what do you mean by removing the default? how can you remove value from a not null column?
I'm trying to update some existing code that is supposed to write data to a variety of Databases (SQL, Access, Oracle) via ODBC, but I'm having a few problems with Oracle and am looking for any suggestions.
I've set my Oracle database up using a Trigger (basic tutorial online, which I'd like to support).
CREATE TABLE TABLE1 (
RECORDID NUMBER NOT NULL PRIMARY KEY,
ID VARCHAR(40) NULL,
COUNT NUMBER NULL
);
GO
CREATE SEQUENCE TABLE1_SEQ
GO
CREATE or REPLACE TRIGGER TABLE1_TRG
BEFORE INSERT ON TABLE1
FOR EACH ROW
WHEN (new.RECORDID IS NULL)
BEGIN
SELECT TABLE1_SEQ.nextval
INTO :new.RECORDID
FROM dual;
end;
GO
I then populate a DataTable using a SELECT * FROM TABLE1. The first problem is that this DataTable doesn't know that the RecordId column is auto-generated. If I have data in my table then I can't alter it because I get a error
Cannot change AutoIncrement of a DataColumn with type 'Double' once it
has data.
If I continue, ignoring this, then I quickly get stuck. If I create a new DataRow and try to insert it, I can't set RecordID to DBNull.Value because it complains that the column has to be non-null (NoNullAllowedException). I can't however generate a value myself, because I don't know what value I should be using really, and don't want to screw up the trigger by using the next available value.
Any suggestions on how I should insert data without ODBC complaining?
It does not appear that your first problem is with an Oracle database. There is no such thing as an "Autoincrement" column in Oracle. Are you sure that message is coming from an Oracle database?
With Oracle, you should be able to provide any dummy value on insert for the primary key, and the trigger will overwrite it.
There is also nothing in your provided description that would prevent you from updating this value in Oracle (since your trigger is on insert only) unless you have foreign key references to the key.