SunQuest
 
           MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old January 16th, 2003, 12:19 AM
JoePopovich JoePopovich is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 11 JoePopovich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
two-field keys in MySQL?

Quick question. Is it possible in MySQL to specify a key for a table that depends on two fields?
For example, I have the following fields:

Name, Address, Phone

Can I make the db not allow new rows ONLY if name AND address are the same as those in another previous row?
If only Name OR Address (but not both) are the same as another row, then there is no key violation and it gets added.


Thank you very much in advance.

Reply With Quote
  #2  
Old January 18th, 2003, 02:07 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Joe,

You can make those 2 fields UNIQUE... This will ensure that no duplicates are added.
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #3  
Old January 18th, 2003, 07:41 PM
JoePopovich JoePopovich is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 11 JoePopovich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the reply.
But I think that if I made both unique I would still have the problem...
Basically, i want to allow two names to be the same in different rows, or two addresses to be the same in different rows. BUT I dont want two rows with the same name AND address.

Do you know any way of getting this?

Thanks

Joe

Reply With Quote
  #4  
Old January 19th, 2003, 01:48 PM
hadley hadley is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 63 hadley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
The best way to do this would be to check before adding the record - I don't think mySQL has this capability built in.

Hadley

Reply With Quote
  #5  
Old January 19th, 2003, 07:47 PM
JoePopovich JoePopovich is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 11 JoePopovich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the help. Thats what I thought ...
The reason I wanted to do it this way is because this is a db with about 30k records. And it seems like it would be very inefficient to query ALL the records to see if this is happening every time a new record is added. Any comments on this?

Reply With Quote
  #6  
Old January 19th, 2003, 07:52 PM
hadley hadley is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 63 hadley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
What do you think mySQL would do, if it was built in?

The only way would be search through all the records looking for a match. However, this shouldn't be very time consuming provided that you have indexed both fields.

Hadley

Reply With Quote
  #7  
Old January 22nd, 2003, 07:05 PM
avit avit is offline
Not Yet Perfect
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Squamish, BC
Posts: 111 avit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to avit
If I understand you correctly, it is possible:

Code:
create table phonenumbers (
name varchar(64) not null, 
address varchar(128) not null, 
phone char(10), 
primary key (name, address)
);


Is that what you're looking for?

Cheers,
Andrew

Last edited by avit : January 23rd, 2003 at 08:38 PM.

Reply With Quote
  #8  
Old September 8th, 2004, 03:47 AM
Swafnil Swafnil is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Wegberg, Germany
Posts: 1 Swafnil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb thank you for your reply!

Hi Andrew,

although I didn't start the thread, I really appreciate your example mysql statement - I was happy to find an answer to my question that quickly!
Too bad that some posters always forget to write a simple "Thank you" after others spent their time to find answers to the posted problem(s) ...

Have a great day!

Sascha

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > two-field keys in MySQL?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway