PLSQL Trigger on a table with collection of columns - oracle

I have a table with a collections:
CS_PARAM_COLLECTION,
create table CS_TEST_WORK_DATA
(
TRANS_ID VARCHAR2(12) not null,
TEST_ID VARCHAR2(12),
ITEMS CS_PARAM_COLLECTION,
ACTIVE NUMBER default 1,
UPDATE_USER VARCHAR2(8),
UPDATE_DATE DATE
)
nested table ITEMS store as TEST_DATA
How can I create a trigger for these table collection columns.

Related

How to CREATE TABLE AS adding an identity?

In such way I can create table from a table adding a column number:
drop table A_TEST
/
CREATE TABLE A_TEST AS
SELECT CAST( null as NUMBER ) as ROW_ID,
C_CODE,B_CODE
FROM A
However I want to add the column as an identity how to do that ? I tried the below, but throwing an error:
CREATE TABLE A_TEST AS
SELECT CAST( null as NUMBER GENERATED BY DEFAULT AS IDENTITY ) as ROW_ID,
C_CODE,B_CODE
FROM A
You can not create the table using CTAS including the IDENTITY column.
But, You can simply create a table without an identity column using CTAS and then ALTER the table to include IDENTITY columns as following:
CREATE TABLE A_TEST
AS
SELECT
C_CODE,
B_CODE
FROM
A;
ALTER TABLE A_TEST ADD ROW_ID NUMBER
GENERATED BY DEFAULT AS IDENTITY;
Cheers!!
Do it in multiple steps: create the table from the other table without data; then alter the table to add the identity column; and finally insert the data.
Oracle Setup:
CREATE TABLE A ( A_CODE, B_CODE, C_CODE ) AS
SELECT 999, 'BBB', SYSDATE FROM DUAL UNION ALL
SELECT 0, NULL, DATE '1970-01-01' FROM DUAL;
Create Table:
Create the table without the IDENTITY column and with no rows:
CREATE TABLE A_TEST AS
SELECT C_CODE, B_CODE
FROM A
WHERE 1 = 0;
Then alter the table to add the IDENTITY column:
ALTER TABLE A_TEST ADD (
ROW_ID NUMBER
GENERATED ALWAYS AS IDENTITY
CONSTRAINT A_TEST__ROW_ID__PK PRIMARY KEY
);
Then insert the rows:
INSERT INTO A_TEST ( C_CODE, B_CODE )
SELECT C_CODE, B_CODE FROM A;
(Or you can create the table and insert the rows in the first step; and alter the table to add the identity column without a NOT NULL/PRIMARY KEY constraint in the second step; and, if you want to add a NOT NULL/PRIMARY KEY constraint afterwards then it must be done in a separate subsequent ALTER TABLE statement. db<>fiddle)
Output:
SELECT * FROM A_TEST;
C_CODE | B_CODE | ROW_ID
:------------------ | :----- | -----:
2019-12-19 09:06:27 | BBB | 1
1970-01-01 00:00:00 | null | 2
db<>fiddle here

Oracle rename table to temp table, create new, then move data from temp to new table

I have a table named MESSAGE_ID:
MESSAGE_ID VARCHAR2(36 BYTE) PRIMARY KEY
DATE TIMESTAMP(6)
STATUS VARCHAR2(200 BYTE)
I want to create a new table to add a new field which should be the primary key.
alter table MESSAGE_ID rename to MESSAGE_ID_OLD;
CREATE TABLE MESSAGE_ID
(
MY_ID NUMBER NOT NULL,
MESSAGE_ID VARCHAR2(36) NOT NULL,
DATE TIMESTAMP,
STATUS VARCHAR2(200),
PRIMARY KEY(MY_ID)
);
Now I want to take everything from the MESSAGE_ID_OLD and insert it into the new table, however I need to put some thing into MY_ID, some random number or something.
This is what I have, but it gives me error:
INSERT INTO MESSAGE_ID (MY_ID, MESSAGE_ID, DATE, STATUS)
(SELECT MY_SEQUENCE.nextval from dual), (SELECT MESSAGE_ID, DATE, STATUS FROM MESSAGE_ID_OLD));
Then I want to delete the old table:
DROP TABLE MESSAGE_ID_OLD;
The Insert statement you are using is not correct.
It should be
INSERT INTO MESSAGE_ID (
MY_ID
,MESSAGE_ID
,DATE_t
,STATUS
)
SELECT MY_SEQUENCE.nextval AS my_id
,MESSAGE_ID
,DATE_t
,STATUS
FROM MESSAGE_ID_OLD;
Also, change the column "DATE" to something else meaningful, as it's a reserved keyword.

cannot insert data into the 4th table

I used the following script to create 4 test tables: dept1, dept2, dept3, and dept4. The code is exactly the same for each table. All tables are created successfully but the insert into statements only worked for the first three tables. After dropping all tables and purge the recyclebin, I moved Create Table dept4 and the following insert into statements to the top of the code. Then the insert into statement does not work for Dept3. Basically, I can only insert data into the first three tables in the script and not able to insert data into the 4th table on the script.
The error message is the following:
SQL Error: ORA-00604: error occurred at recursive SQL level 1
ORA-30667: cannot drop NOT NULL constraint on a DEFAULT ON NULL column
00604. 00000 - "error occurred at recursive SQL level %s"
This happened after some users created identity columns in Oracle 12.1 database. Some users used:
GENERATED ALWAYS AS IDENTITY
Other users used:
GENERATED BY DEFAULT ON NULL AS IDENTITY
Code used:
CREATE TABLE DEPT1 (
DEPT1NO NUMBER(2) NOT NULL,
DNAME VARCHAR2(14),
LOC VARCHAR2(13),
CONSTRAINT DEPT1_PRIMARY_KEY PRIMARY KEY (DEPT1NO));
INSERT INTO DEPT1 VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT1 VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT1 VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT1 VALUES (40,'OPERATIONS','BOSTON');
CREATE TABLE DEPT2 (
DEPT2NO NUMBER(2) NOT NULL,
DNAME VARCHAR2(14),
LOC VARCHAR2(13),
CONSTRAINT DEPT2_PRIMARY_KEY PRIMARY KEY (DEPT2NO));
INSERT INTO DEPT2 VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT2 VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT2 VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT2 VALUES (40,'OPERATIONS','BOSTON');
CREATE TABLE DEPT3 (
DEPT3NO NUMBER(2) NOT NULL,
DNAME VARCHAR2(14),
LOC VARCHAR2(13),
CONSTRAINT DEPT3_PRIMARY_KEY PRIMARY KEY (DEPT3NO));
INSERT INTO DEPT3 VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT3 VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT3 VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT3 VALUES (40,'OPERATIONS','BOSTON');
CREATE TABLE DEPT4 (
DEPT4NO NUMBER(2) NOT NULL,
DNAME VARCHAR2(14),
LOC VARCHAR2(13),
CONSTRAINT DEPT4_PRIMARY_KEY PRIMARY KEY (DEPT4NO));
INSERT INTO DEPT4 VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT4 VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT4 VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT4 VALUES (40,'OPERATIONS','BOSTON');

Copy or show the primary key value into another table as foreign key APEX

I have two tables A and B .i want to copy or show the primary key column value from table A in the foreign key column in table B respectively .is there any method kindly help me.
Regards,
You can populate your primary key value while populating table B or by using a trigger when you are populating table A.
CREATE TABLE t1 (id1 NUMBER, dt DATE);
ALTER TABLE t1 ADD (
CONSTRAINT t1_pk
PRIMARY KEY
(id1));
CREATE TABLE t2 (id2 NUMBER, id1 NUMBER, dt2 DATE);
ALTER TABLE t2 ADD (
CONSTRAINT t2_pk
PRIMARY KEY
(id2));
ALTER TABLE t2
ADD CONSTRAINT t2_r01
FOREIGN KEY (id2)
REFERENCES t1 (id1);
First Approach, by this way you could populate second table when you are inserting values.
INSERT INTO t1
VALUES (1, SYSDATE
);
INSERT INTO t2
VALUES (1, 1, SYSDATE
);
With trigger, so when values are inserted into first table second tables values are populated using a trigger. So primary key value of first table is being inserted into foreign key of table 2.
CREATE OR REPLACE TRIGGER my_trigger
AFTER INSERT
ON t1
FOR EACH ROW
BEGIN
INSERT INTO t2
VALUES (1, :new.id1, SYSDATE
);
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.put_line (TO_CHAR (SQLERRM (-20299)));
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (TO_CHAR (SQLERRM (-20298)));
END;

Changing the data type of a column in Oracle

I created the following table
CREATE TABLE PLACE(
POSTCODE VARCHAR(10) PRIMARY KEY,
STREET_NAME VARCHAR(10),
COUNTY VARCHAR(10),
CITY VARCHAR(10));
I want to change the name, county and city from varchar(10) to varchar(20). How do I do that?
ALTER TABLE place
MODIFY( street_name VARCHAR2(20),
county VARCHAR2(20),
city VARCHAR2(20) )
Note that I am also changing the data type from VARCHAR to VARCHAR2 to be more conventional. There is no functional difference at present between the two though the behavior of VARCHAR may change in the future to match the SQL standard.
if you want to change only type of column use below:
ALTER TABLE <table_name> MODIFY (<column_name> <new_Type>)
in your case:
ALTER TABLE place MODIFY (street_name VARCHAR2(20),
county VARCHAR2(20),
city VARCHAR2(20))
If your table has data you could act below:
add a column with new type to table.
copy data from old column to new column.
drop old column.
rename new column to old.
For rename a column use below:
ALTER TABLE <table_name> rename column <column_name> to <new_column_name>
Oracle 10G and later
ALTER TABLE table_name
MODIFY column_name datatype;
A very general example is here to do the same -
Table:
CREATE TABLE TABLE_NAME(
ID NUMBER PRIMARY KEY,
COLUMN_NAME NUMBER NOT NULL, -- Modify with varchar2(20) NOT NULL
.
.
.
);
Step to modify the datatype of COLUMN_NAME from NUMBER to VARCHAR2
STEPS:
--Step 1: Add a temp column COLUMN_NAME_TEMP in table TABLE_NAME to hold data temporary
ALTER TABLE TABLE_NAME
ADD( COLUMN_NAME_TEMP varchar2(20) );
--Step 2: Update temp column COLUMN_NAME_TEMP with Old columns COLUMN_NAME data
UPDATE TABLE_NAME
SET COLUMN_NAME_TEMP = COLUMN_NAME;
--Step 3: Remove NOT NULL constrain from old columns COLUMN_NAME
ALTER TABLE TABLE_NAME MODIFY (COLUMN_NAME NULL);
--Step 4: Update old columns COLUMN_NAME data with NULL
UPDATE TABLE_NAME SET COLUMN_NAME = NULL;
--Step 5: Alter table old columns COLUMN_NAME to new data type varchar2(20)
ALTER TABLE TABLE_NAME MODIFY COLUMN_NAME varchar2(20);
--Step 6: Update old columns COLUMN_NAME with data from temp columns COLUMN_NAME_TEMP
UPDATE TABLE_NAME
SET COLUMN_NAME = COLUMN_NAME_TEMP;
--Step 7: Add NOT NULL constrain from old columns [COLUMN_NAME]
ALTER TABLE TABLE_NAME MODIFY (COLUMN_NAME NOT NULL);
--Step 8: Drop the temp column [COLUMN_NAME_TEMP]
alter table TABLE_NAME drop column COLUMN_NAME_TEMP;
If NOT NULL constrain is not exist the omitte step-3 and step-7
Alter table placemodify(street name varchar2(20),city varchar2(20)
You can't modify the data type of a table if you have some amount of records already present in the table.
You have to empty the table records of the column (you want to modify the data type) first and then use the below command :
alter table place
modify ( street_name varchar2(20), country varchar2(20), city varchar2(20) );
Definitely it will work!

Resources