DDL Triggers invoked on DROP COLUMN in Oracle 11.2.0.4.0 - oracle

If you run the following code snippet below the trigger on the table is invoked when a clumn is dropped which had been added after the table creation and has a default value and not null defined. This is behaviour I could only reproduce in Oracle 11.2.0.4.0 but not in 11.2.0.3.0. COuld somebody please help me with this? Is this expected behaviour? How can I drop the column without running the trigger?
CREATE TABLE T (
DESCRIPTION VARCHAR2(50)
);
INSERT INTO T (DESCRIPTION) VALUES ('asd');
COMMIT;
CREATE OR REPLACE TRIGGER BEFORE_T_U
BEFORE UPDATE ON T
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
BEGIN
RAISE_APPLICATION_ERROR(-20003, 'This before update trigger should not be invoked!');
END BEFORE_T_U;
/
ALTER TABLE T ADD AMOUNT NUMBER DEFAULT 0 NOT NULL;
ALTER TABLE T DROP COLUMN AMOUNT;

Related

Apex Oracle How To Create Conditional Column

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.

Sequence is not hitting in Oracle Application Apex After creating app

I am trying to create a app in oracle apex. I have installed it in my pc(Ubuntu).
I have created a table and a sequence through sql command in apex.
Like
create table test (
test_id number primary key,
test_name varchar2(20) not null
);
and sequence like-
create sequence test_seq start with 1 increment by 1;
Now adding a single value in my table through sql-comand
insert into test values(test_seq.nextval, 'Test');
Okey runnig successfully.[1 row added]
But when i create a app using this table -
Please check it for details
Okey - Page creation successful but when i try to add data in it then it says -
ORA-01400: cannot insert NULL into ("WORKSPACE"."TEST"."TEST_ID")
Click here to See the attachment
So why this problem occurs?
I think you have not associated sequence to primary column of your table. Alter your table and then try to add data using app.
ALTER TABLE TEST
MODIFY (TEST_ID DEFAULT TEST_SEQ.NEXTVAL );
Ideally, you would create sequence first and then while creating table, you can define column default as follows,
CREATE TABLE TEST (
TEST_ID NUMBER DEFAULT TEST_SEQ.NEXTVAL NOT NULL PRIMARY KEY,
TEST_NAME VARCHAR2(20) NOT NULL
);
Try:
Insert into test
(Test_id,test_name)
Select test_seq.nextval, 'something'
From dual;
Commit;

Using commit in Trigger in Oracle 11g

I have created the following trigger in oracle-
create or replace TRIGGER TODAY_TD_INSERT AFTER INSERT ON table1
FOR EACH ROW
DECLARE
BEGIN
INSERT INTO table2 (col1
,col2
,col3
)
VALUES (:NEW.,col1
,:NEW.,col2
,:NEW.,col3
);
END;
So if any data insert in table1 the same data is inserting in table 2 also. So my question is do i need commit the data in new table2 ? So far i know we cannot use commit from trigger.
No, you do not. The session that made the change to the table on which the trigger is placed issues the commit (or rollback), and that applies to all changes made by that session.

How to create a last_modified field in oracle 9i?

I'm using Oracle 10g Express or Oracle XE. In the EMPLOYEES table I want to add another column call last_modified that automatically generate current date or date/time if it has been modified. I know there is this rowscn(timestamp) thing that's only available from oracle 10g onwards but I want to manually create one because my client is using oracle 9i but I only have 10g to do the testing. Also I've never use oracle before. This is for some integration project with Lotus Notes. Therefore if possible I want the date or date/time to recognizable by LotusScript.
Add a column of type TIMESTAMP to the table (might not be available in the de-supported 9i version, but then just use DATE instead).
alter table employees add (modified_at timestamp);
Then create a trigger that is fired on update which simply sets that column to sysdate:
create or replace trigger update_modified
before update on employees
for each row
begin
:new.modified_at := sysdate;
end;
/
The usual way is to
add a modify_date / modified_by column to your table
create a trigger that updates this column whenever the row is modified
Example:
CREATE OR REPLACE TRIGGER TR_AUD_EMPLOYEE
BEFORE INSERT OR UPDATE OR DELETE ON EMPLOYEE
FOR EACH ROW
begin
if UPDATING then
:new.MODIFIED_BY := user;
:new.MODIFY_DATE := sysdate;
end if;
end;

Automatically populate date in oracle table

I have created a table in oracle XE, and I have a field with type date. I would like if possible when I insert a row, that it automatically fills that field with the current date from the system.
I am inserting the rows from the SQL prompt.
Thanks
Here is how, you need to format your table properly:
create table test (first number
, second timestamp default systimestamp
, third varchar2(12));
And your default value is always current system time formatted as timestamp.
change the field after creating the table
ALTER TABLE table MODIFY time_collumn TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
Or you could also use a trigger:
CREATE OR REPLACE TRIGGER date_trigger
BEFORE INSERT
ON table_name
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT sysdate INTO :NEW.column_name FROM dual;
END;
The below snippet might be helpful if we forget to add the constraint while creating the table:
ALTER TABLE TABLE_NAME
ADD CONSTRAINT CONSTRAINT_NAME
COLUMN_NAME DATA_TYPE DEFAULT CURRENT_DATE;

Resources