473,698 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interesting Unique Index Needed - Functional?

Given:

CREATE TABLE Usrs(
usr_id SERIAL NOT NULL PRIMARY KEY,
usr VARCHAR(64) NOT NULL UNIQUE
);
CREATE TABLE Emails(
email_id SERIAL NOT NULL PRIMARY KEY,
email VARCHAR(128) NOT NULL UNIQUE
);
CREATE TABLE EmailTypes(
email_type_id SERIAL NOT NULL PRIMARY KEY,
email_type VARCHAR(64) NOT NULL UNIQUE,
pri BOOL NOT NULL,
multiples_allow ed BOOL NOT NULL
);
CREATE TABLE UsrEmails(
usr_id INT4 NOT NULL,
email_id INT4 NOT NULL,
email_type_id INT4 NOT NULL,
validated BOOL NOT NULL DEFAULT 'T'::BOOL,
validation _hash VARCHAR(64) NOT NULL
);
ALTER TABLE UserEmails
ADD CONSTRAINT Usrs_11_1M_UsrE mails FOREIGN KEY (usr_id) REFERENCES
Usrs(usr_id);
ALTER TABLE UserEmails
ADD CONSTRAINT Emails_11_0M_Us rEmails FOREIGN KEY (email_id)
REFERENCES Emails(email_id );
ALTER TABLE UserEmails
ADD CONSTRAINT EmailTypes_11_0 M_UsrEmails FOREIGN KEY
(email_type_id) REFERENCES EmaiTypesl(emai l_type_id);

INSERT INTO Usrs( usr ) VALUES( 'John' );

INSERT INTO Emails( email ) VALUES( 'some email one' );
INSERT INTO Emails( email ) VALUES( 'some email two' );
INSERT INTO Emails( email ) VALUES( 'some email three' );
INSERT INTO Emails( email ) VALUES( 'some email four' );
INSERT INTO Emails( email ) VALUES( 'some email five' );

INSERT INTO EmailTypes( email_type, pri, multiples_allow ed ) VALUES(
'home', 'T'::BOOL, 'N'::BOOL );
INSERT INTO EmailTypes( email_type, pri, multiples_allow ed) VALUES(
'work', 'F'::BOOL, 'Y'::BOOL );
INSERT INTO EmailTypes( email_type, pri, multiples_allow ed ) VALUES(
'extra_contact' , 'T'::BOOL, 'Y'::BOOL );

Now, for a little business logic:
1/ emails are entered by client, choosing which type, and having to
supply at least the primary type.
2/ At first UsrEmails( validated ) = 'FALSE" and the validation_hash
is some 160 bit number using the newer hash type.
3/ The usual, 'send a reply to this email or if you are computer
illiterate, click on this link' validation message gets sent out for
each email entered. The hash is embedded in the subject or GET
parameters as usual.
4/ User can have MULTIPLE emails PENDING (validated = 'FALSE') for
any of the EmailTypes, but only ONE email of any type which has
EmailTypes( multiples_allow d ) = 'TRUE' AND UserEmails( validated ) = 'TRUE'

How can I enforce number two, i.e.
How can I have a Unique index on UserEmails( usr_id, email_type_id )
where EmailTypes( multiples_allow d ) = 'TRUE' for that type AND
UserEmails( validated ) = 'TRUE'

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #1
0 1191

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3894
by: Mansoor Azam | last post by:
When I add a unique key constraint to column in SQL 6.5 why does it also create an index. e.g. In the table subaccounts I added a unique key constraint for the column login and SQL creates an index with the name UQ_SubAccounts_2__19 (UKC). Does this also mean that there is no need to create an index for this column? thx
0
1785
by: Reece Hart | last post by:
Here's the basic issue: PostgreSQL doesn't use indexes unless a query criterion is of exactly the same type as the index type. This occurs even when a cast would enable the use of an index and greatly improve performance. I understand that casting is needed to use an index and will therefore affect performance -- the part I don't understand is why postgresql doesn't automatically cast query arguments to the column type, thereby enabling...
1
9315
by: Rajesh Kumar Mallah | last post by:
Hi , Looks like ADD UNIQUE( some_fuc( some_feild) ) is not supported with add constraint. the only way is to add the constriant is using UNIQUE INDEX . Is it a bug or intended behaviour? Regds Mallah.
5
16724
by: aj | last post by:
DB2 WSE 8.1 FP5 Red Hat AS 2.1 What is the difference between adding a unique constraint like: ALTER TABLE <SCHEMA>.<TABLE> ADD CONSTRAINT CC1131378283225 UNIQUE ( <COL1>) ; and adding a unique index like:
4
2840
by: Dennis Gearon | last post by:
Is the following example a good, and correct one, for using a functional index? (the 'flip_bits' function would have to be written, and for the correct size of bit(N) ): I think I came up with a better way to search through some dates. It would use a functional index. I am trying to see if any of some appointments fall between two dates. To explain this, think of a 16 day long year, represented by the binary bits in two bytes of a bit...
10
14689
by: Laurence | last post by:
Hi there, How to differentiate between unique constraint and unique index? These are very similar but I cannot differentiate them? Could someone give me a hand? Thanks in advance
8
2885
by: paii, Ron | last post by:
I have a table listing drawing numbers for jobs. It's primary key combines Job and numeric part of the drawing number. The structure allows the number part to repeat for each job. Job Dwg 6692 001 6692 002 6692 003 6721 001 6721 002
22
1905
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
0
1379
by: Gabriel Genellina | last post by:
En Fri, 18 Apr 2008 12:23:08 -0300, Shawn Milochik <Shawn@Milochik.comescribió: A dictionary with keys is perfectly reasonable. But a *list* of values has to be searched linearly for every value: a O(n) process. As your friend suggested, searching a dictionary requires O(1) time. A set is even better in this case, because you don't have any use for the values in the inner dictionary (sets and dictionaries are very similar in the...
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.