Check Constraint For A Defaulting Value - oracle

I'm stuck and can't figure out how to add the condition to the CHECK constraint in oracle...
Basically I have a table with the following structure:
CREATE TABLE TEST_TBL
(
COL_1 VARCHAR2(100) NOT NULL,
COL_2 VARCHAR2(100) NOT NULL,
COL_3 VARCHAR2(100) NOT NULL,
COL_4 VARCHAR2(100) NOT NULL,
DEF_COL CHAR(1) DEFAULT 'Y',
CONSTRAINT def_check_const
CHECK (???????)
);
There may be multiple values in this table, however I require that only 'Y' or 'N' can be entered in the DEF_COL column. Also i want to add a constraint to restrict the number of 'Y' in the DEF_COL column to only one. There may be multiple 'N' entries allowed for DEF_COL column. Please could someone guide me as to what must be done to add the constraint in the check section of the create table query?
Thanks in advance....

Oracle Setup:
CREATE TABLE TEST_TBL(
COL_1 VARCHAR2(100) NOT NULL,
COL_2 VARCHAR2(100) NOT NULL,
COL_3 VARCHAR2(100) NOT NULL,
COL_4 VARCHAR2(100) NOT NULL,
DEF_COL CHAR(1) DEFAULT 'Y'
CONSTRAINT def_check_const
CHECK ( DEF_COL IN ('Y', 'N' ) )
);
CREATE UNIQUE INDEX def_col_only_one_y__U
ON TEST_TBL( CASE DEF_COL WHEN 'Y' THEN 'Y' END );
Insert a row:
INSERT INTO TEST_TBL VALUES ( 'A1', 'A2', 'A3', 'A4', 'Y' );
1 rows inserted.
Insert a second column with DEF_COL = 'Y':
INSERT INTO TEST_TBL VALUES ( 'B1', 'B2', 'B3', 'B4', 'Y' )
Error report -
SQL Error: ORA-00001: unique constraint (TEST.DEF_COL_ONLY_ONE_Y__U) violated
00001. 00000 - "unique constraint (%s.%s) violated"
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.
Update:
To have only one Y for each unique combination of 3 columns then try:
CREATE UNIQUE INDEX C1_2_3__def_col_only_one_y__U
ON TEST_TBL( COL1, COL2, COL3, CASE DEF_COL WHEN 'Y' THEN 'Y' END );

Related

Add a primary key column to an old table

So I have a table with some 50+ rows. And currently this tables doesnot have any primary key/ID column in it. Now if I have to add a primary key column, its not allowing me to because already data are present in the table and there is as such no unique column or combination of columns. Can anyone suggest me how to add a primary column to an existing table with data in it.
(From 12.1) You can add a new auto-incremented surrogate key to a table with either:
alter table t
add ( t_id integer generated by default as identity );
Or
create sequence s;
alter table t
add ( t_id integer default s.nextval );
These set the value for all the existing rows. So may take a while on large tables!
You should also look to add a unique constraint on the business keys too though. To do that, take the steps Marmite Bomber suggests.
In your case when the table due to missing PK definition suffers some duplicated records, you may do a stepwise recovery.
In the first step you disables the creation of the new duplicated rows.
Let's assume your PK candidate columns are col1, col2 such as in the example below:
CREATE TABLE test_pk as
SELECT 'A' col1, 1 col2 FROM dual UNION ALL
SELECT 'A' col1, 2 col2 FROM dual UNION ALL
SELECT 'B' col1, 1 col2 FROM dual UNION ALL
SELECT 'B' col1, 1 col2 FROM dual;
You can not define the PK because of the existing duplications
ALTER table test_pk ADD CONSTRAINT my_pk UNIQUE (col1, col2);
-- ORA-02299: cannot validate (xxx.MY_PK) - duplicate keys found
But you can crete an index on the PK columns and set up a constraint in the state ENABLE NOVALIDATE.
This will tolerate existing duplicates, but reject the new once.
CREATE INDEX my_pk_idx ON test_pk(col1, col2);
ALTER TABLE test_pk
ADD CONSTRAINT my_pk UNIQUE (col1,col2) USING INDEX my_pk_idx
ENABLE NOVALIDATE;
Now you may insert new unique rows ...
INSERT INTO test_pk (col1, col2) VALUES ('A', 3);
-- OK
... but you can't create new duplications:
INSERT INTO test_pk (col1, col2) VALUES ('A', 1);
-- ORA-00001: unique constraint (xxx.MY_PK) violated
Later in the second step you may decide to clenup the table and VALIDATE the constraint, which will make a perfect primary key as expected:
-- cleanup
DELETE FROM TEST_PK
WHERE col1 = 'B' AND col2 = 1 AND rownum = 1;
ALTER TABLE test_pk MODIFY CONSTRAINT my_pk ENABLE VALIDATE;

Error: ora-00917: missing comma

Create table A_15006977.vehicle. (
Vin varchar(20) primary key,
Vehicle_type char(20) not null,
Mileage number(20) not null,
Manufacturer char(20) not null
);
Insert all
Into A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
values ('tf1bb2ve533093891','panel van',18 325,'man')
A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
values
('tf1bb2ve533093822','standard van',79 885,'ford')
Select * from dual;
Create table A_15006977.vehicle (
Vin varchar(20) CONSTRAINT vehicle__vin__pk PRIMARY KEY,
Vehicle_type char(20) CONSTRAINT vehicle__vehicle_type__nn not null,
Mileage number(20) CONSTRAINT vehicle__mileage__nn not null,
Manufacturer char(20) CONSTRAINT vehicle__manufacturer__nn not null
);
Insert all
Into A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
VALUES ( 'tf1bb2ve533093891', 'panel van', 18325, 'man' )
INTO A_15006977.vehicle (vin,vehicle_type,mileage,manufacturer)
values ( 'tf1bb2ve533093822', 'standard van', 79885, 'ford' )
SELECT 1 FROM DUAL;
Or:
Insert Into A_15006977.vehicle( vin,vehicle_type,mileage,manufacturer )
SELECT 'tf1bb2ve533093891','panel van', 18325, 'man' FROM DUAL UNION ALL
SELECT 'tf1bb2ve533093822','standard van', 79885, 'ford' FROM DUAL;
Note:
You had an extra . after the table name in the DDL statement and spaces in the mileage (18 325 and 79 885) which need removing and you needed an INTO keyword before the second insert.
It is also useful to name your constraints (then you can easily determine which constraint has been violated in later statements).

Oracle: MERGE INTO with invisible columns

I have got an problem with ORA-00904: invalid identifier.
As example:
I have a table created like this:
CREATE TABLE TEST_TABLE
(
COL_1 VARCHAR2(5 CHAR) NOT NULL,
COL_2 VARCHAR2(30 CHAR),
COL_3 RAW(16) INVISIBLE DEFAULT SYS_GUID ()
)
CREATE UNIQUE INDEX TEST_TABLE_PK ON TEST_TABLE
(COL_1);
A second table on a remote db (DBLINK: testdb) looks like this:
CREATE TABLE TEST_TABLE
(
COL_1 VARCHAR2(5 CHAR) NOT NULL,
COL_2 VARCHAR2(30 CHAR)
)
CREATE UNIQUE INDEX TEST_TABLE_PK ON TEST_TABLE
(COL_1);
In the next step I want to merge the data between the local and remote db with an merge into statement like this:
MERGE INTO TEST_TABLE#testdb target
USING (SELECT * FROM TEST_TABLE
WHERE COL_3 = '3F47613050860B4EE0539D0A10AC10B7') source
ON (target.COL_1 = source.COL_1)
WHEN MATCHED
THEN
UPDATE SET target.COL_2 = source.COL_2
WHEN NOT MATCHED
THEN
INSERT (COL_1, COL_2)
VALUES (source.COL_1, source.COL_2);
The merge into statement does not work, because of an ORA-00904: "A5".COL_3 invalid identifier. But the same merge into statement works fine if the COL_3 column is visible.
Where does the "A5" come from?
Whats the problem here? Does anyone have the same issues?
Oracle versions: The local db is 12cSE and the remote db is 11g.
Specify your source table select, by name COL_1 and COL_2.
The key is to get rid of SELECT * from
MERGE INTO TEST_TABLE#testdb target
USING (SELECT COL_1,COL_2 FROM TEST_TABLE
WHERE COL_3 = '3F47613050860B4EE0539D0A10AC10B7') source
ON (target.COL_1 = source.COL_1)
WHEN MATCHED
THEN
UPDATE SET target.COL_2 = source.COL_2
WHEN NOT MATCHED
THEN
INSERT (COL_1, COL_2)
VALUES (source.COL_1, source.COL_2);

ORA-00942 on INSERT to a newly created table

I'm doing an evaluation of FLYWAY and running a very simple script that creates a new table and insert a row to that table. The table creates successfully but I get an ORA-942 error on the INSERT. I have tried every permutation I can think of using upper/lower case and quoted and unquoted schema and table names. I have also tried separating out the INSERT statement into different script from the CREATE statement - all to no avail. Can anyone help explain the format flyway needs for the INSERT statements?
Here are the statements from my most recent attempt. I have tried the same statements with quotes around the table_name, with all lower case, with no quotes around the schema - nothing works. Logged directly into the database as SYS, I can describe the table with this command:
desc "FLYWAY_USER".department
CREATE TABLE "FLYWAY_USER".DEPARTMENT
( DEPARTMENT_id NUMBER(3) NOT NULL,
DEPARTMENT VARCHAR2(64) NOT NULL,
display_name VARCHAR2(64) NOT NULL,
description VARCHAR2(400),
create_date DATE NOT NULL,
update_date DATE NOT NULL,
created_by VARCHAR2(80) NOT NULL,
updated_by VARCHAR2(80) NOT NULL )
TABLESPACE users;
INSERT INTO "FLYWAY_USER".DEPARTMENT
( DEPARTMENT_id,
DEPARTMENT,
display_name,
description,
create_date,
update_date,
created_by,
updated_by )
VALUES
( ( SELECT Nvl( Max(DEPARTMENT_id), 0) + 1 FROM DEPARTMENT ),
'HUMAN_RESOURCES',
'Human Resources',
'The best place to eat smores or get a raise.',
Sysdate,
Sysdate,
'AT09001',
'AT09001' );
Thanks
Logged directly into the database as SYS
Use better a dedicated application user.
Anyway if you use a different user that the schema owner of your objects (FLYWAY_USER) you must qualify ALL your references.
especially
VALUES
( ( SELECT Nvl( Max(DEPARTMENT_id), 0) + 1 FROM DEPARTMENT ),
should be
VALUES
( ( SELECT Nvl( Max(DEPARTMENT_id), 0) + 1 FROM FLYWAY_USER.DEPARTMENT ),

handling exception for insert into select from

SCHEMA 1
I have table transaction table
create table TXN_HEADER
(
txn_id NUMBER(10) not null,
txn_Date date
product_id NUMBER(10),
company_id NUMBER(10),
dealer_id NUMBER(10),
tran_amt number(10,2)
)
The above table having foreign key references to product.product_id and company.company_id.
This table having 5m rows
SCHEMA 2
create table TXN_HEADER_REPORTS
(
txn_id NUMBER(10) not null,
txn_Date date
product_id NUMBER(10),
company_id NUMBER(10),
dealer_id NUMBER(10),
tran_amt number(10,2)
)
here also we have the same constraints , having foreign key references to product.product_id and company.company_id.
in schema 2 we are trying to insert all the rows from schemea 1 to schema 2 in one shot, like this
begin
insert into TXN_HEADER_REPORTS (
txn_id, txn_Date ,product_id,company_id , dealer_id , tran_amt)
select
txn_id, txn_Date ,product_id,company_id , dealer_id , tran_amt
from schema1.TXN_HEADER;
commit;
exception
when others then
< ... procedure to log the errors >
end;
now we are trying to execute the above procedure , and it failed due to foreign key constraint of one rows. But entire my transaction rollback. Actually i dont want to use cursor to process the rows one by one , at it takes long time. So i used to "insert into .. SElect from " but due to constraints of 1 row all my transaction not moved to schema2.txn_Extract_hdr.
Is there any way to trap only that failed and to process the other rows without terminating
Please advice ..
You can create an error log table, and then use a single insert:
exec dbms_errlog.create_error_log(dml_table_name => 'TXN_HEADER_REPORTS');
insert into TXN_HEADER_REPORTS ( txn_id, txn_Date ,product_id,company_id ,
dealer_id , tran_amt)
select txn_id, txn_Date ,product_id,company_id , dealer_id , tran_amt
from schema1.TXN_HEADER
log errors into ERR$_TXN_HEADER_REPORTS reject limit unlimited;
Any rows that can't inserted will be recorded in the ERR table. Note that this is plain SQL, it doesn't need to be in a PL/SQL block.
Read more in the documentation.
I don't understand your constraint.
Does your insert fail because the product_id and company_id don't exists in schema2?
In that case, it may be better to insert the missing company and product records before you insert records into TXN_HEADER_REPORTS of schema2.
insert into company com_sch2
(col1, col2, col2,...)
select col1, col2, col3, ...
from schema1.company com_sch1
where not exists (select 'x'
from company com2
where com2.company_id = com_sch1.company_id);
And the seem for the product table.

Resources