In Table A I have three fields. I want ID to be auto incremented without setting it as the primary key, But I want to make Color SN and Color PN the primary Key.
But the database will not allow me. The error says that AutoIncrement has to be an primary key. can someone assist me. My goal is to not have a duplicate Color SN or Color PN
Table A
ID
Color SN
Color PN
Loading

Javeed M ShaikhRank #839 · 2.7K reputationPosted Nov 8, 2011, 5:23 AM EST
Dipal ChoksiRank #199 · 12K reputationPosted Nov 8, 2011, 5:40 AM EST
http://www.mysqlfaqs.net/mysql-faqs/Indexes/Unique-Key-or-Index/How-to-create-multi-column-unique-key-or-index-in-MySQL
David SmithRank #999 · 2K reputationPosted Nov 8, 2011, 6:10 AM EST
David SmithRank #999 · 2K reputationPosted Nov 8, 2011, 6:14 AM EST
VulpesRank #55 · 29.2K reputationPosted Nov 8, 2011, 6:28 AM EST
David SmithRank #999 · 2K reputationPosted Nov 8, 2011, 6:35 AM EST
So are you saying there is no way to an auto incremented id thats noot a primary key.
Is i were to make ID, Color Sn, ans Color Pn a primary I can have duplicate data.
Javeed M ShaikhRank #839 · 2.7K reputationPosted Nov 8, 2011, 6:39 AM EST
David SmithRank #999 · 2K reputationPosted Nov 8, 2011, 6:47 AM EST
Are you saying with the composite key. Can you give me an example of how to make a primary key.
This is the situation I do not want.
id color sn color pn
1 sn A pn A
2 sn A pn A
VulpesRank #55 · 29.2K reputationPosted Nov 8, 2011, 6:51 AM EST
There's another example here:
http://www.java2s.com/Code/SQL/Key/Usingthreecolumnastheprimarykey.htm
David SmithRank #999 · 2K reputationPosted Nov 8, 2011, 7:28 AM EST
VulpesRank #55 · 29.2K reputationPosted Nov 8, 2011, 7:38 AM EST
The designer should then realize that a composite primary key is required.
David SmithRank #999 · 2K reputationPosted Nov 8, 2011, 8:26 AM EST
id color sn color pn
1 sn A pn A
2 sn A pn A
David SmithRank #999 · 2K reputationPosted Nov 8, 2011, 8:49 AM EST
id color sn color pn
1 sn A pn A
2 sn A pn A
Javeed M ShaikhRank #839 · 2.7K reputationPosted Nov 8, 2011, 8:49 AM EST
you are right, this is a composite key so combination of all three is unique in this case. I tried to do the same thing in SQL Server and it works fine, following is the DDL:
CREATE TABLE [dbo].[mytable](
[id] [int] IDENTITY(1,1) NOT NULL,
[colorsn] [varchar](50) NOT NULL,
[colorpn] [varchar](50) NOT NULL,
CONSTRAINT [PK_mytable] PRIMARY KEY CLUSTERED
(
[colorsn] ASC,
[colorpn] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
as you can see here keys are colorsn and colorpn and id is just an identity column and not primary key.
VulpesRank #55 · 29.2K reputationPosted Nov 8, 2011, 8:54 AM EST
If the primary key had consisted of just Color SN and Color PN, then this wouldn't have been possible.
David SmithRank #999 · 2K reputationPosted Nov 8, 2011, 8:54 AM EST
David SmithRank #999 · 2K reputationPosted Nov 8, 2011, 9:00 AM EST
David SmithRank #999 · 2K reputationPosted Nov 8, 2011, 9:11 AM EST
VulpesRank #55 · 29.2K reputationPosted Nov 8, 2011, 9:15 AM EST
However, may be you could figure out a way to increment the last value 'manually' when you create a new record.