Avoiding ORA-00955: name is already used by an existing object - sql

I am creating the table as below in the sql file, which will be called everytime there is a new file to process. So everytime a new file comes, I will drop this table and create again and load. When I am calling this from shell script, this table gets dropped and created successfully, but along with this i get an error as below. Any idea how to avoid this.
ERROR at line 1:
ORA-00955: name is already used by an existing object
Table Drop and create sql file :
DROP TABLE SCHEMA.TEMP_SOURCE;
CREATE TABLE SCHEMA.TEMP_SOURCE(
COL 1 VARCHAR2(30 CHAR),
COL 2 VARCHAR2(30 CHAR),
COL 3 VARCHAR2(30 CHAR),
);
/
EXIT;

ORA-00955: name is already used by an existing object
That's because, you have put a slash / in the end of the script.
);
/
Due to which, the previous statement in the buffer is executed again. Which means, the CREATE TABLE statement is executed twice.
Remove the slash from the end. The semi-colon is enough as statement terminator for individual queries.
This is how I would do:
SQL> BEGIN
2 EXECUTE IMMEDIATE 'DROP TABLE TEMP_SOURCE';
3 EXCEPTION
4 WHEN OTHERS THEN
5 IF SQLCODE != -942 THEN
6 RAISE;
7 END IF;
8 END;
9 /
PL/SQL procedure successfully completed.
SQL>
SQL> CREATE TABLE TEMP_SOURCE
2 (
3 COL_1 VARCHAR2(30 CHAR),
4 COL_2 VARCHAR2(30 CHAR),
5 COL_3 VARCHAR2(30 CHAR)
6 );
Table created.
SQL>
Having said that, you have multiple issues in the script.
COL 1 VARCHAR2(30 CHAR),
You cannot have a space in the column name. COL 1 is an invalid column name. You will get Invalid identifier error.
Another issue:
COL 3 VARCHAR2(30 CHAR),
);
There is an extra comma in the end of the column list.

Simply modify your script as below:
DROP TABLE SCHEMA.TEMP_SOURCE
/
CREATE TABLE SCHEMA.TEMP_SOURCE(
COL 1 VARCHAR2(30 CHAR),
COL 2 VARCHAR2(30 CHAR),
COL 3 VARCHAR2(30 CHAR),
)
/
EXIT;

Related

SQL: prevent update if field value below certain value

I would like to add a constraint to my table:
create table asset (
id number(19,0) not null,
name varchar2(255 char),
description varchar2(255 char),
type varchar2(255 char),
height number(10,0),
width number(10,0),
bytes blob,
primary key (id)
);
after have stored on it Assets with ID from 0 to 8:
ALTER TABLE ASSET ADD CONSTRAINT check_if_id_bigger_than_eight CHECK (ID > 8)
to avoid further modifications of rows with IDs from 0 to 8, unfortunately the above is not working because its not allowed to have data which violate the constraints, is there an easy way to do it?
This trigger may work (sql server syntax)
create trigger TR_8
on asset
after insert, update
as
if exists(
select * -- checks if new or modified datum has id between 0 and 8
from inserted
where inserted.id between 0 and 8
)
begin
RAISERROR ('Ids between 0 and 8 must stay untouched', 16, 10); --if so, rollback the transaction giving an error
rollback transaction
end

i want to update same table's value through trigger

columns data_type nullable default
ID VARCHAR2(30 BYTE) No
PASSWORD VARCHAR2(20 BYTE) No
NAME VARCHAR2(20 BYTE) No
BIRTH CHAR(11 BYTE) No
PHONE VARCHAR2(11 BYTE) No
CBD_1 VARCHAR2(20 BYTE) Yes
CBD_2 VARCHAR2(20 BYTE) Yes
CBD_3 VARCHAR2(20 BYTE) Yes
CBD_4 VARCHAR2(20 BYTE) Yes
CBD_5 VARCHAR2(20 BYTE) Yes
CDATE VARCHAR2(20 BYTE) Yes to_char(sysdate, 'dd-Mon-YYYY')
UDATE VARCHAR2(20 BYTE) Yes to_char(sysdate, 'dd-Mon-YYYY')
My question is: I want to insert update time when I update existed record in this table through a trigger
and here is my code
create or replace trigger udate
before update or delete on member for each row
declare
u_id varchar2(30);
begin
u_id := :old.id;
update member set udate = to_char(sysdate, 'dd-Mon-YYYY') where id = u_id;
end;
error message
One error saving changes to table "JSP"."MEMBER":
Row 6: ORA-04091: table JSP.MEMBER is mutating, trigger/function may not see
it
ORA-06512: at "JSP.UDATE", line 5
ORA-04088: error during execution of trigger 'JSP.UDATE'
I already tried :new.id but it didn't work
Don't use update, simply assign the value:
create or replace trigger udate
before update on member for each row
begin
:new.udate := to_char(sysdate, 'dd-Mon-YYYY');
end;
Doing that "before delete" doesn't make sense, as the row will be deleted anyway.
You should never store DATE values in a varchar column. That is a horrible idea. Don't do that.

Insert random values in Oracle for primary key

I have a small table which I use to insert configuration data.
CREATE SEQUENCE AGENT_GROUP_SEQ
INCREMENT BY 1
MAXVALUE 9999999999999999999999999999
NOMINVALUE
CACHE 20
/
CREATE TABLE AGENT_GROUP(
ID NUMBER(38,0) NOT NULL,
NAME VARCHAR2(40 ) NOT NULL,
STATUS VARCHAR2(30 ),
TYPE VARCHAR2(30 ),
DATE_ADDED DATE,
LAST_MODIFIED DATE,
DESCRIPTION CLOB
)
/
CREATE TRIGGER AGENT_GROUP_TRG
BEFORE INSERT
ON AGENT_GROUP
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
BEGIN
<<COLUMN_SEQUENCES>>
BEGIN
IF INSERTING AND :NEW.ID IS NULL THEN
SELECT AGENT_GROUP_SEQ.NEXTVAL INTO :NEW.ID FROM SYS.DUAL;
END IF;
END COLUMN_SEQUENCES;
END;
/
I have a ID column which will be used for unique number. I managed to create sequence which will auto-generate ID value on every insert. But the problem is that I have too many insert and delete operations After some time I will reach sequence maximum value.
Is there any other way to generate random ID on every insert?
Use some kind of UUID what u generate server side or with a db fuction.
http://lakenine.com/guid-values-as-primary-keys/
Your trigger as well as your question is quite "verbose".
Not a real answer to your question but this code does exactly the same as your code.
CREATE TRIGGER AGENT_GROUP_TRG
BEFORE INSERT ON AGENT_GROUP
FOR EACH ROW
BEGIN
:NEW.ID := NVL(:NEW.ID, AGENT_GROUP_SEQ.NEXTVAL);
END;
/
Simplify your life!
Regarding your sequence. You can make it CYCLE if you are concerned.
Don't worry, even if your table contains only one column with this primary key the amount of data would be more than all available storage capacity over the world.
CREATE SEQUENCE AGENT_GROUP_SEQ START WITH 1 NOMAXVALUE MINVALUE 1 CYCLE CACHE 20;

How do I fix SQL Error: ORA-00001: unique constraint (ALERTS2.PK_UP_MODULE_MASTER) violated

Gives me error when I try to insert and it finally inserts after couple of retries with out any error.
SQL Error: ORA-00001: unique constraint (ALERTS2.PK_UP_MODULE_MASTER) violated.
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
INSERT statement:
INSERT INTO up_module_master
(
mr_id,
mr_name,
mr_desc,
mr_parent,
created_by,
created_date,
modified_date,
module_type,
module_link,
link_text,
help_text shortcut_link,
shortcut_name,
shortcut_flag,
module_display_name
)
SELECT max(u1.mr_id)+1,
'Notification Engine',
'Notification Engine Module',
0,1,
SYSDATE,
'',
'',
'../../jalsweb/Logout.jsp',
'HTTP',
'',
'',
'',
0,
'Notification Engine'
FROM up_module_master u1;
Below is the table definition:
CREATE TABLE "up_module_master"
(
"mr_id" NUMBER (10, 0) NOT NULL ENABLE,
"mr_name" VARCHAR2 (200 byte) NOT NULL ENABLE,
"mr_desc" VARCHAR2 (250 byte),
"mr_parent" NUMBER,
"created_by" NUMBER,
"created_date" TIMESTAMP (6),
"modified_date" TIMESTAMP (6),
"module_type" VARCHAR2 (100 byte),
"module_link" VARCHAR2 (200 byte),
"link_text" VARCHAR2 (250 byte),
"help_text" VARCHAR2 (250 byte),
"shortcut_link" VARCHAR2 (400 byte),
"shortcut_name" VARCHAR2 (100 byte),
"shortcut_flag" NUMBER,
"module_display_name" VARCHAR2 (100 byte),
"audit_type" VARCHAR2 (100 byte),
"service_id" NUMBER,
"module_regis_type" NUMBER DEFAULT 1,
CONSTRAINT "PK_UP_MODULE_MASTER" PRIMARY KEY ("mr_id"),
CONSTRAINT "UP_MODULE_MASTER_UP_MODUL_FK1" FOREIGN KEY ("mr_parent")
REFERENCES "up_module_master" ("mr_id") ENABLE
)
Looks like MR_ID is not an identity seed. If another process is attempting to insert a record at the same time as you, both will try to insert the same ID and you will get this error. I suggest that you change the column to be auto incrementing. You will need to create a sequence and then a trigger if using pre version 12 or the identity column if using later versions:
Oracle: how to create an identity column?
The link above has all the details for doing this.
the problem is, that someone other inserted and commited a row with the same key meanwhile
consider using a sequence (it looks like the sequence is already defined):
SELECT seq_name.nextval, ... FROM dual
The reason why it's failing is because "select max(mr_id) + 1 ..." is going to be a fixed number for all rows that you're selecting - it's not going to increment for each row in your select statement. (Duh! See comments below!)
As others have said, use a sequence! (Yes, there are other ways you could use to get unique values as part of your select statement, but they'd be the wrong thing to use. Sequences FTW!)

Oracle auto increment - newbie so go easy please [duplicate]

This question already has answers here:
Auto-increment in Oracle without using a trigger
(9 answers)
Auto Increment for Oracle
(7 answers)
Closed 9 years ago.
Hi all I am looking to create an auto increment in the following table using GUEST_ID as the column that would increment.
CREATE TABLE "HOTEL_BOOKINGS"."GUEST" (
"GUEST_ID" NUMBER,
"LASTNAME" VARCHAR2(100 BYTE),
"FIRSTNAME" VARCHAR2(100 BYTE),
"ADDRESS" VARCHAR2(255 BYTE),
"TOWN" VARCHAR2(100 BYTE),
"PHONE" NUMBER,
"POSTCODE" VARCHAR2(10 BYTE),
"EMAIL" VARCHAR2(255 BYTE)
);
I have tried nearly everything to get this working and am at my wits end
once again sorry if this is a newbie question but I need coursework handed in tomorrow
Oracle doesn't have an autoincrement property, but you could use a sequence and an ON INSERT trigger to utilize its value:
CREATE SEQUENCE hotel_bookings_seq
START WITH 1
INCREMENT BY 1
NOMAXVALUE;
CREATE TRIGGER hotel_bookings_tr
BEFORE INSERT ON hotel_bookings
FOR EACH ROW
BEGIN
SELECT hotel_bookings_seq.nextval INTO :new.guest_id FROM dual;
END;
/