×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Two fields be unique, like a compund primary key
2

Two fields be unique, like a compund primary key

Two fields be unique, like a compund primary key

(OP)
Here is the table:

CREATE TABLE `fs_gallery` (
  `gallery_id` int(10) unsigned NOT NULL auto_increment,
  `visible_gallery_id` varchar(20) NOT NULL default '' UNIQUE,
  `freesite_id` int(10) unsigned NOT NULL default '0',
  `name` varchar(50) NOT NULL default '',
  PRIMARY KEY  (`gallery_id`),
  KEY `freesite_id` (`freesite_id`),
  CONSTRAINT `fs_gallery_ibfk_1` FOREIGN KEY (`freesite_id`) REFERENCES `fs_freesite` (`freesite_id`) ON DELETE CASCADE ON UPDATE CASCADE
) TYPE=InnoDB;

As it is now my visible_gallery_id's must be unique or the database won't insert the record. I understand this is how UNIQUE works. UNIQUE will probably have to be removed...

I didn't realize until now what I really wanted is to have visible_gallery_id and freesite_id work like a primary key so i can have
duplicate visible_gallery_ids as long as in the same record has a different freesite_id.

I do not want to nix gallery_id and make them primary keys.

How can I make it so I can have records with duplicate visible_gallery_ids AS LONG as the records don't share the same freesite_id? freesite_ids need not be unique in this table. i just want to prevent two or more records from having the same visible_gallery_id and freesite_id, but you should be able to have many records having the same freesite_id but NOT the same visible_gallery_id.

I'm sure this is simple for those who are more experienced in DB design than me.
Thanks

RE: Two fields be unique, like a compund primary key

2
PRIMARY KEY  (gallery_id, freesite_id) should work.

Depends on the dialect of SQL you're using.

Geoff Franklin
www.alvechurchdata.co.uk

RE: Two fields be unique, like a compund primary key

(OP)
OK, my table definition now looks like this:

CREATE TABLE `fs_gallery` (
  `gallery_id` int(10) unsigned NOT NULL auto_increment,
  `visible_gallery_id` varchar(20) NOT NULL default '',
  `freesite_id` int(10) unsigned NOT NULL default '0',
  `name` varchar(50) NOT NULL default '',
  PRIMARY KEY  (`visible_gallery_id`, `freesite_id`),
  KEY `gallery_id` (`gallery_id`),
  KEY `freesite_id` (`freesite_id`),
  CONSTRAINT `fs_gallery_ibfk_1` FOREIGN KEY (`freesite_id`) REFERENCES `fs_freesite` (`freesite_id`) ON DELETE CASCADE ON UPDATE CASCADE
) TYPE=InnoDB;

...and it seems to work well. Is this the best way to do it?
Thanks

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close