Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
|
MySQL General discussion about the MySQL database.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the MySQL section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 23rd, 2004, 03:12 AM
Authorized User
 
Join Date: Apr 2004
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to junemo
Default create unique index

Hi, can someone help me how to create a UNIQUE constraint on the table to allow MySQL to reject the duplicate records?

Can MySQL database to create unique index?

I want it can ensure that the combination of product ID and serial number is unique. I can have multiple rows with the same product ID, and multiple rows with the same serial number, but not multiple rows with the same product ID and serial number.

I using MySQL Control Center to create a table. So, have anyone can help me how to do?

Please...

Thanks in advance...
 
Old July 24th, 2004, 04:24 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi there,
You can use this syntax for doing so.

Code:
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [index_type]
    ON tbl_name (index_col_name,...)
    Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old July 25th, 2004, 07:23 PM
Authorized User
 
Join Date: Apr 2004
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to junemo
Default

Hi,
if using MySQL Control Center then how to create it?
if not, where should I run this script?

I have a table:
Table name: prod_batch
I want this 2 field to create as unique index.
field: prod_id , s1.

Then, what should I fill in with this script?

Quote:
quote:
Code:
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [index_type]
    ON tbl_name (index_col_name,...)
Thanks in advance.
 
Old July 26th, 2004, 01:28 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

This should be enough.
Code:
CREATE UNIQUE INDEX MyUniqueIndex
    ON prod_batch (prod_id , s1)
     You can run this from your mysql prompt.

Or from Mysql Control center (I forgot the exact location where the options are found.but still you can...) there is an option to run sql statements, you can copy/paste it there and execute it to create one.

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old July 26th, 2004, 07:08 PM
Authorized User
 
Join Date: Apr 2004
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to junemo
Default

HI,

unique index created.

Thanks in advance..........

 
Old September 20th, 2004, 11:34 PM
Authorized User
 
Join Date: Sep 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You know what I'm wondering about:
If you have a unique index on both fields, and you say that
one field can have a value and other left blank NULL, and
vice-versa, wouldn't you violate the index if you have multiple rows with the same product ID, because the serial number will be NULL.?

:D
 
Old September 22nd, 2004, 05:11 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:I can have multiple rows with the same product ID, and multiple rows with the same serial number, but not multiple rows with the same product ID and serial number.
Did you read that from original post?

Unique index means it lets the user have every row as unique to the other. So it lets you have
1) Same productId but different serial number
2) Different ProductId with same serial number.
3) one NULL value for productID/Serial number. Stil this can be restricted to NOT NULL at the column level.

I don't see this setup violates the index or the original post saying serial numbers will be NULL...

Hope that explains.
Cheers!

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL script to create a unique ID for records chubnut SQL Server 2000 1 October 16th, 2006 07:25 AM
create an index bonekrusher XSLT 5 July 31st, 2006 08:14 AM
Create Unique Id langer123 Classic ASP Basics 0 April 6th, 2005 01:27 PM
create a unique id in asp code langer123 Classic ASP Basics 1 March 15th, 2005 03:55 AM
Unique Index there but not found mmcdonal Access 5 January 4th, 2005 10:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.