Advertisement

02.15.2008 at 03:24AM PST, ID: 23165684
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

ensure certain combination of table data

Tags: Oracle, 10 11
I have a table with a simple structure of:
CREATE TABLE test (PK int primary key, LK int not null, histtop char(1) not null)
What I want to ensure is the following:
There should only be one entry for the combination of LK and histtop='Y' (like a unique index), but there might be a lot of LK and histtop<>'Y'
So a unique index of LK and histtop does not work.
How can I ensure that constraint via database.
Valid example:
PK,LK,histtop
1,1,N
2,1,N
3,1,Y
4,2,Y

Invalid database state
1,1,N
2,1,Y
3,1,Y
4,2,Y

Is this possible and which oracle code would be needed for that?
Start your free trial to view this solution
Question Stats
Zone: Database
Question Asked By: hernst42
Solution Provided By: sujith80
Participating Experts: 3
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
02.15.2008 at 03:35AM PST, ID: 20900848

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 03:36AM PST, ID: 20900850

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 03:44AM PST, ID: 20900883

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 03:57AM PST, ID: 20900936

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 05:13AM PST, ID: 20901303

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 05:20AM PST, ID: 20901358

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 06:43AM PST, ID: 20902084

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 08:40AM PST, ID: 20903379

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 08:50AM PST, ID: 20903484

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 09:27AM PST, ID: 20903878

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 09:39AM PST, ID: 20903996

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.16.2008 at 09:53PM PST, ID: 20913131

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.17.2008 at 03:06AM PST, ID: 20913661

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.17.2008 at 12:06PM PST, ID: 20915276

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.17.2008 at 12:07PM PST, ID: 20915284

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.18.2008 at 01:34AM PST, ID: 20918658

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.18.2008 at 05:30AM PST, ID: 20919662

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.15.2008 at 03:35AM PST, ID: 20900848
You can create a trigger everytime a record is inserted to check whether or not there is a record that contains LK and histtop equal to Y.
 
02.15.2008 at 03:36AM PST, ID: 20900850

Rank: Guru

I can't think of a way to achieve this using a CHECK constraint since GROUP functions like COUNT are not allowed while defining constraints. A trigger may work though. Let me know if a trigger is fine with you and I can help you with how to do it.
 
02.15.2008 at 03:44AM PST, ID: 20900883
I currently use triggers, but they are not 100% valid because a select is run on the same table as the action happens.

INSERT seem to work ok, but update does not work and give an ORA-error (no Application error) as the same table is used.

So if you update a row which has hittop=Y (e.g. update another column) you get that ora-error (can check for that ora-code if needed, just haven't listed that one here.

Should have posted the triggers directly, sorry. Maybe there is another way for using the triggers.
1:
2:
3:
4:
CREATE OR REPLACE TRIGGER htcu_Testtable BEFORE UPDATE ON Testtable FOR EACH ROW when (new."histtop" = 'Y') DECLARE num int; BEGIN SELECT count(*) INTO num FROM Testtable WHERE Testtable."histtop"='Y' AND Testtable."LK"=:new."LK" AND Testtable."PK"<>:new."PK"; if (num > 0) THEN RAISE_APPLICATION_ERROR (num => -20000, msg => 'duplicate histtop'); END IF; END;
/
CREATE OR REPLACE TRIGGER htci_Testtable BEFORE INSERT ON Testtable FOR EACH ROW when (new."histtop" = 'Y') DECLARE num int; BEGIN SELECT count(*) INTO num FROM Testtable WHERE Testtable."histtop"='Y' AND Testtable."LK"=:new."LK"; if (num > 0) THEN RAISE_APPLICATION_ERROR (num => -20000, msg => 'duplicate histtop'); END IF; END;
/
Open in New Window
 
02.15.2008 at 03:57AM PST, ID: 20900936
Ok here is the Ora-code: ORA-04091
ORA-04091: Table Testtable mutating, trigger/function may not see it
 
02.15.2008 at 05:13AM PST, ID: 20901303
The main reason behind the ORA-04091 is that your trying to use a select statement on a table that is being modified which is not allowed and the only way to resolve this error is through removing the select statement that is being done on the table. Therefore i would i advise that you change the trigger to after update instead of before.

 
02.15.2008 at 05:20AM PST, ID: 20901358
In addition to what i said earlier you will have to use the new and old values to be able to undo whatever has been updated in case it was not acceptable
 
02.15.2008 at 06:43AM PST, ID: 20902084
So using the trigger as:

CREATE OR REPLACE TRIGGER htcu_Testtable AFTER UPDATE ON Testtable FOR EACH ROW when (new."histtop" = 'Y') DECLARE num int; BEGIN SELECT count(*) INTO num FROM Testtable WHERE Testtable."histtop"='Y' AND Testtable."LK"=:new."LK" AND Testtable."PK"<>:new."PK"; if (num > 0) THEN RAISE_APPLICATION_ERROR (num => -20000, msg => 'duplicate histtop'); END IF; END;

will do the trick? I don't really care about restoring the correct value. In this case the UPDATE will fail with an application error and the complete transaction is rolled back. I will try that on Monday and report back.
 
02.15.2008 at 08:40AM PST, ID: 20903379

Rank: Guru

No, that won't work either. You will have to use a statement-level trigger, something like below.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
CREATE OR REPLACE TRIGGER htciu_TestTable
   AFTER INSERT OR UPDATE
   ON TestTable
DECLARE
   cnt   INT;
   dcnt  INT;
BEGIN
   SELECT COUNT(lk), COUNT(DISTINCT lk)
     INTO cnt, dcnt
     FROM TestTable
    WHERE histtop = 'Y';
 
   IF cnt <> dcnt THEN
      RAISE_APPLICATION_ERROR(num => -20000, msg => 'duplicate histtop');
   END IF;
END htciu_TestTable;
/
Open in New Window
 
02.15.2008 at 08:50AM PST, ID: 20903484

Rank: Sage

I guess the best is to handle this case programmatically,
See a dirty workaround below.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
drop table test
/
drop sequence test_seq
/
CREATE TABLE test (PK int primary key, LK int not null, histtop char(1) not null,dummy_col number)
/
create sequence test_seq start with 1 increment by 1
/
create unique index test_idx on test(LK, dummy_col)
/
create trigger test_trg
before insert on test
for each row
declare
 l_n number;
begin
 if :new.histtop = 'Y' then
  :new.dummy_col := null;
 else
  select test_seq.nextval into :new.dummy_col from dual;
 end if;
end;
/
insert into test(PK, LK, histtop) values( 1,1,'N');
insert into test(PK, LK, histtop) values( 2,1,'Y');
insert into test(PK, LK, histtop) values( 3,1,'Y');
insert into test(PK, LK, histtop) values( 4,2,'Y');
Open in New Window
 
02.15.2008 at 09:27AM PST, ID: 20903878

Rank: Sage

Pls ignore the above comment. This is a better workaround to do it.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
drop table test
/
CREATE TABLE test (PK int primary key, LK int not null, histtop char(1) not null )
/
create or replace function test_func1(par char)
return char
deterministic
as
begin
 if par = 'Y' then
  return par;
 else 
  return null;
 end if;
end;
/
create or replace function test_func2(par1 int, par2 char)
return int
deterministic
as
begin
 if par2 = 'Y' then 
  return par1;
 else 
  return null;
 end if;
end;
/
create unique index test_idx on test(test_func2(LK, histtop), test_func1(histtop))
/
 
insert into test values( 1,1,'N');
insert into test values( 2,1,'Y');
insert into test values( 3,1,'Y');
insert into test values( 4,2,'Y');
Open in New Window
 
02.15.2008 at 09:39AM PST, ID: 20903996

Rank: Sage

Well that further simplifies into this.
A single function based index will do it.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
CREATE TABLE test (PK int primary key, LK int not null, histtop char(1) not null )
/
create or replace function test_func2(par1 int, par2 char)
return int
deterministic
as
begin
 if par2 = 'Y' then 
  return par1;
 else 
  return null;
 end if;
end;
/
create unique index test_idx on test(test_func2(LK, histtop))
/
 
insert into test values( 1,1,'N');
insert into test values( 2,1,'Y');
insert into test values( 3,1,'Y');
insert into test values( 4,2,'Y');
Open in New Window
Accepted Solution
 
02.16.2008 at 09:53PM PST, ID: 20913131

Rank: Guru

@sujith80: When I tried that in my DB, I'm getting insufficient privileges error i.e. whenever I try to create any function-based UNIQUE index. Do i need to get the DBA to provide me any specific grant to achieve this?
 
02.17.2008 at 03:06AM PST, ID: 20913661

Rank: Sage

Hi jinesh_kamdar: You should have the QUERY REWRITE prvilege to create function based indexes. Hope the function is owned by the same user. Otherwise the user must be granted execute privilege on the function as well.
 
02.17.2008 at 12:06PM PST, ID: 20915276

Rank: Guru

Hi Sujith, I created the table and function under SCOTT, so it would obviously have the EXECUTE privilege on test_func2. However, when I tried to create the function-based index on the table, it didn't work, even without the UNIQUE specifier citing - "insufficient privileges"! Do u think I still need the QUERY REWRITE privilege to be granted to SCOTT? Thx for the response.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
Enter user-name: SCOTT
Enter password: *****
 
Connected to:
Oracle9i Enterprise Edition Release 9.0.1.1.1 - Production
With the Partitioning option
JServer Release 9.0.1.1.1 - Production
 
SQL> CREATE TABLE test (PK int primary key, LK int not null, histtop char(1) not null )
  2  /
 
Table created.
 
SQL> create or replace function test_func2(par1 int, par2 char)
  2  return int
  3  deterministic
  4  as
  5  begin
  6   if par2 = 'Y' then 
  7    return par1;
  8   else 
  9    return null;
 10   end if;
 11  end;
 12  /
 
Function created.
 
SQL> create unique index test_idx on test(test_func2(LK, histtop))
  2  /
create unique index test_idx on test(test_func2(LK, histtop))
                                                    *
ERROR at line 1:
ORA-01031: insufficient privileges
 
 
SQL> 
Open in New Window
 
02.17.2008 at 12:07PM PST, ID: 20915284

Rank: Guru

Posting the end part of the output ...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
...
Function created.
 
SQL> create unique index test_idx on test(test_func2(LK, histtop))
  2  /
create unique index test_idx on test(test_func2(LK, histtop))
                                                    *
ERROR at line 1:
ORA-01031: insufficient privileges
 
 
SQL> create index test_idx on test(test_func2(LK, histtop))
  2  /
create index test_idx on test(test_func2(LK, histtop))
                                             *
ERROR at line 1:
ORA-01031: insufficient privileges
 
 
SQL> 
Open in New Window
 
02.18.2008 at 01:34AM PST, ID: 20918658

Rank: Sage

Are you able to create a function based index on a standard oracle function, like trim, trunc etc?
What is the value of COMPATIBLE in your database? What is the version of database?
 
02.18.2008 at 05:30AM PST, ID: 20919662

Rank: Guru

Nope, i could not even create an index for the emp table on UPPER(ename). Its a 9i DB on Win XP. How do i check the COMPATIBLE value?
 
 
02.18.2008 at 07:06AM PST, ID: 20920452
I accepted that solution, as it additional ships me around another problem and I don't need to make any modification to my current Database structure and also don't need to create temp tables.
 
 
02.18.2008 at 07:08AM PST, ID: 20920457
Additional Info. The user where I tested it was created with the following SQL:
grant create session, create procedure, create sequence, create table, create trigger, alter session to $sqluser
 
 
02.18.2008 at 06:53PM PST, ID: 20925338
@jinesh_kamdar:
pls grant QUERY REWRITE to the user and try.
you can see the value of COMPATIBLE from v$Parameter.
 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628