Oracle Constraint Letters Only SQL REGEXP - sql

I already have my tables full of data, I am trying to add a constraint to only allow letters to be entered into the attribute I have named as studentfname. I have tried various different REGEXP but cannot seem to get any to work without getting an error when inserting them.
Im not sure if its a syntax error or what!
Edit: Here is what I have tried and the error I am getting:
ALTER TABLE STUDENT
ADD CONSTRAINT
check_name
CHECK(regexp_like(studentfname,'^[A-Za-z''-]+$'));
Error:
QL Error: ORA-02293: cannot validate (JEIGH7.CHECK_NAME) - check constraint violated
02293. 00000 - "cannot validate (%s.%s) - check constraint violated"
*Cause: an alter table operation tried to validate a check constraint to
populated table that had nocomplying values.
*Action: Obvious

Apparently you can try to run this below query to check if any non integral data resides in the above mentioned column which is preventing you from applying a check constraints. Once you get the output from this query try UPDATING/DELETING the row and then apply the constraint. Hope this helps.
SELECT studentfname nm FROM STUDENT a
WHERE NOT regexp_like(a.nm,'^[A-Za-z''-]+$');

Related

SQL (Oracle) : How to improve logic model and correct identifer errors?

This is the logic model of a video store. The client borrows the copy from the store.
logic model (please see the logic model)
After creating the logic model, the generate sequences had 5 errors.
Some of them are:
remove the primary key
--> I cannot understand why should remove primary key.When I remove the primary key "clientno" on the client table. The software "oracle developer" doesn't let me remove the primary key.
invalid identifer
--> Googled the info on the internet but could not understand.
The problem is on the logic model. The model want to check the average video that a client borrowed. What is the popular movie at the moment?
These are errors:
create table client
(
clientno number(8) not null,
clientname varchar2(30),
clientadress varchar2(50),
clientclassification varchar2(30)
)
Error:
SQL Error: ORA-00955: name is already used by an existing object
00955. 00000 - "name is already used by an existing object”
alter table client
add constraint client_pk primary key (clientno)
Error:
SQL Error: ORA-02260: table can have only one primary key
02260. 00000 - "table can have only one primary key"
*Cause: Self-evident.
*Action: Remove the extra primary key.
alter table copy add constraint copy_pk primary key (copycode, client_clientno, movie_movieno)
Error:
SQL Error: ORA-00904: "COPYCODE": invalid identifier
00904. 00000 - "%s: invalid identifier”
alter table store add constraint store_pk primary key (storecode, storecode1)
Error:
SQL Error: ORA-02260: table can have only one primary key
02260. 00000 - "table can have only one primary key"
*Cause: Self-evident.
*Action: Remove the extra primary key.
alter table copy
add constraint copy_client_fk
foreign key (client_clientno) references client (clientno)
Error:
SQL Error: ORA-00904: "CLIENT_CLIENTNO": invalid identifier
00904. 00000 - "%s: invalid identifier”
alter table copy
add constraint copy_movie_fk
foreign key (movie_movieno) references movie (movieno)
Error:
SQL Error: ORA-00904: "MOVIE_MOVIENO": invalid identifier
00904. 00000 - "%s: invalid identifier"
There are any number of reasons why you might get one of those errors. But altogether? There's really only one explanation: you are running this script in a schema where you (or somebody else) has previously run a version of this script and created tables with those names already (perhaps with different projections).
Assuming you want to replace that schema you need to write some drop table statements and run those before you run your script. Or perhaps you need a different account.
Every single error you posted is self-evident and can easily be fixed, only if you read & understand what Oracle is telling you.
One
create table client ends up with
ORA-00955: name is already used by an existing object
Obviously, you can't create two objects with the same name. There's something that is called CLIENT (most probably a table you forgot to drop prior to executing the command). If you're unsure of what that might be, run
select * from user_objects where object_name = 'CLIENT';
and you'll find it. Then: either drop that object, or rename the one you're currently trying to create.
Two
As of
ORA-02260: table can have only one primary key
*Cause: Self-evident.
*Action: Remove the extra primary key.
there's really nothing one should say ... self-evident, is it not?
Three
ORA-00904: "COPYCODE": invalid identifier
means that you used COPYCODE column in one of your commands, while that column doesn't exist in a table affected by that command. For example, you're trying to
alter table client modify copycode not null;
If there's no such a column in a table, you'd get that error.
I'd suggest you to go step by step: run one command, make sure it is OK. Then write another, execute it, make sure it is OK too. Go on to the next, and so forth. Baby steps.

How to create and Oracle trigger to return some DB Error code, not the Custom Exception but real DB ERROR?

I would like to know is it possible to return a DB ERROR code from trigger, rather than Custom exception created with RAISE_APPLICATION_ERROR. I have some requirement where i need to return an DB ERROR code like 1 for Unique Constraint. I have a purpose to ask this, because for some reason with some hash based partition we are not creating unique constraints on table and rather have one trigher with which i want to return Unique constraint as an error code after checking if the record exists in DB.
You can use:
RAISE DUP_VAL_ON_INDEX;
Only in this case you can't pass any arguments to this error and you will get:
ORA-00001: unique constraint (.) violated

SQL complains about a non-existing constraint

I'm really stumped on this one. We had a table with a two part primary key. The parts were a review_id (a foreign key from another table) and a time-stamp. Whoever designed this table didn't realize that some situations could result in two entries having identical time-stamps, and I was getting "ORA-00001: unique constraint" errors.
However, as this table was a log, it had no real need to have a primary key in the first place, so I removed the primary key constraint. Despite this constraint no longer existing, I'm still getting the same error.
I've tried adding elements to the PK to prevent the conflict as well as restoring the constraint but disabling it. Oracle SQL Developer insists that the database reflects the changes I've made, but the behavior suggests that it's still using the original PK. I thought it might be a caching problem, but even a complete reboot of my computer doesn't change it.
Any advice is appreciated.
An example of the commands I've run:
alter table "DATABASE"."DB_REVIEW_LOG" drop constraint "DB_REVIEW_LOG_PK";
update database.db_review_log set review_id=17494 where review_id = 17495;
and this is what I get back:
Error starting at line : 2 in command -
update database.db_review_log set review_id=17494 where review_id = 17495
Error report -
SQL Error: ORA-00001: unique constraint (DATABASE.DB_REVIEW_LOG_PK) 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.
Turns out that despite it complaining about a constraint, the problem was a unique index with the same name that was causing the problems.
Thank you, Justin Cave

Insert statement attempted to include a duplicate key

Trying to run
INSERT INTO BOOKING_EXTRAS (BOOKING_ID, EXTRAS_, EXTRAS_PRICE) VALUES ('1','Phone call: 1.80','1.8');
in Oracle SQL Developer. Ive had it running but when I close it, then reopen it I get this error:
Error starting at line 1 in command:
INSERT INTO BOOKING_EXTRAS (BOOKING_ID, EXTRAS_, EXTRAS_PRICE) VALUES ('1','Phone call: 1.80','1.8')
Error report:
SQL Error: ORA-00001: unique constraint (COURSEWORK_XE.BOOKING_EXTRAS_PK) 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.
how would I fix this? Its happening to every table I run!
You need to either clear the tables or insert new information, a database doesn't want duplicate rows because that makes it impossible to find the correct rows later.
In addition, if that BOOKING_ID (currently serves as primary key if I guess right) doesn't actually mean something for you, you can set it as AUTO INCREMENT in your schema, then afterwards you don't need to insert a value for BOOKING_ID, the system will automatically find a value which is not duplicate for you. This might save you a lot effort.
INSERT INTO BOOKING_EXTRAS (EXTRAS_, EXTRAS_PRICE) VALUES ('Phone call: 1.80','1.8');
If your unique column is being populated by a sequence, check to make sure that your sequence has the same 'last value' as your highest-value unique column.
I just encountered a problem where the sequence had a 'last value in' that was much lower than the highest value in my unique column. The DBA ran a script to update the sequence to what the number should have been, and my error went away.
Now to find out why the sequence was so wrong...

Modify unique constraint in Oracle

I need to update an existing constraint in Oracle database to add a new column there.
ALTER TABLE MY_PARTNER_DETAILS
MODIFY CONSTRAINT UQ_MY_PARTNER_DETAILS
UNIQUE(PARTNER_CODE,PGOOD_CODE,SITE_CODE,PARTNER_PLACEMENT,PARTNER_PARTICIPATION)
Gives the error:
Error at line 1
ORA-00933: SQL command not properly ended
What's the problem with that?
You should drop and recreate the constraint. modify constraint allows you to change constraint's state not definition.
See: Oracle Docs