Home » SQL & PL/SQL » SQL & PL/SQL » Restricting blank entries
Restricting blank entries [message #8533] Thu, 28 August 2003 09:48 Go to next message
Kuldeep
Messages: 21
Registered: June 2002
Junior Member
I have a table that has one field, "ID" as the primary key. ID is CHAR and of lenght 8. The table has 15 other fields. ID has been designated as NOT NULL and PRIMARY KEY. However, I can still insert blanks into this field. So when i write my insert statement [[insert into sale_client (ID) Values (' ');]] 1 row is inserted. The problem is that though oracle allows this to happen, Delphi does not like this blank field. I want to be able to prevent the user from entering a blank in this field, either from the database level [[by modifying the table]] or from the program. Could you help?
Thank you,
Kuldeep
Re: Restricting blank entries [message #8535 is a reply to message #8533] Thu, 28 August 2003 10:00 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
please check the ddl of the table.
if column ID is defined as primary key, there is no way that oracle can accept a null value.

Re: Restricting blank entries [message #8536 is a reply to message #8535] Thu, 28 August 2003 10:17 Go to previous messageGo to next message
Kuldeep
Messages: 21
Registered: June 2002
Junior Member
Mahesh,
Thank you for your reply. Well, the entry is not a null. It is a blank. like when you insert [[insert into table (ID) values (' '); you hit enter 4 or five times and a row is created. this is different from NULL.

Try it out:
Create table XXX
(ID CHAR(8) NOT NULL,
NAME VARCHAR2(12),
PRIMARY KEY (ID));

Then do:

insert into XXX (ID,NAME) VALUES (' ', 'MAHESH');

You will be able to insert. I want to prevent this.

Thanks,
Kuldeep
It's a primary key also ? [message #8537 is a reply to message #8536] Thu, 28 August 2003 10:26 Go to previous messageGo to next message
aix_tom
Messages: 4
Registered: August 2003
Junior Member
You could simly leave ONE entry with a blank in there and nobody will be able to insert a second one. ;-)

Id is an Char field, a blank is an char, so oracle will insert it. You could only catch in on the application level, or maybe with an "before insert" trigger that would set the value to NULL if it is an blank, so Oracle would block it.
Re: It's a primary key also ? [message #8538 is a reply to message #8537] Thu, 28 August 2003 10:35 Go to previous messageGo to next message
Kuldeep
Messages: 21
Registered: June 2002
Junior Member
You are right it is a primary key too. THe problem with leaving a blank key in there is that this table is loaded into a Borland Delphi database every week through a batch job and also into a sql server database on a nightly basis. The other two databases do not like the blank to be there.

Well, I would have hoped that since this is a critical table to our application, the business rules should have been strictly enforced. But the before insert trigger should take care of this. You are right there. I wish there was some kind of simple contraint that would restrict this.

Thank you for your reply.

-Kuldeep
Re: It's a primary key also ? [message #8539 is a reply to message #8538] Thu, 28 August 2003 10:53 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
-- try something like this
-- this will restrict the first character from being ' '
--
"afiedt.buf" 6 lines, 123 characters

  1  create table test
  2     (id char(8) primary key constraint check_blank check (substr(id,1,1)!=' '),
  3      name varchar(10)
  4*  )
  5  /

Table created.

dbadmin@mutation_mutation > insert into test values (' ','ma');
insert into test values (' ','ma')
*
ERROR at line 1:
ORA-02290: check constraint (DBADMIN.CHECK_BLANK) violated

Re: It's a primary key also ? [message #8547 is a reply to message #8539] Thu, 28 August 2003 15:44 Go to previous message
Kuldeep Singh
Messages: 4
Registered: February 2002
Junior Member
Mahesh,
Thank you for answering my question. I think we will use the option that you suggested. I will also write BI triggers for other similar issues that we have. Kuldeep
Previous Topic: Trigger for Insert and Update
Next Topic: exception in trigger
Goto Forum:
  


Current Time: Fri Apr 26 11:34:21 CDT 2024