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
VulpesPosted Nov 8, 2011, 2:15 PM
However, may be you could figure out a way to increment the last value 'manually' when you create a new record.
David SmithPosted Nov 8, 2011, 2:11 PM
David SmithPosted Nov 8, 2011, 2:00 PM
David SmithPosted Nov 8, 2011, 1:54 PM
VulpesPosted Nov 8, 2011, 1:54 PM
If the primary key had consisted of just Color SN and Color PN, then this wouldn't have been possible.
Javeed M ShaikhPosted Nov 8, 2011, 1:49 PM
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.
David SmithPosted Nov 8, 2011, 1:49 PM
id color sn color pn
1 sn A pn A
2 sn A pn A
David SmithPosted Nov 8, 2011, 1:26 PM
id color sn color pn
1 sn A pn A
2 sn A pn A
VulpesPosted Nov 8, 2011, 12:38 PM
The designer should then realize that a composite primary key is required.
David SmithPosted Nov 8, 2011, 12:28 PM
VulpesPosted Nov 8, 2011, 11:51 AM
There's another example here:
http://www.java2s.com/Code/SQL/Key/Usingthreecolumnastheprimarykey.htm
David SmithPosted Nov 8, 2011, 11:47 AM
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
Javeed M ShaikhPosted Nov 8, 2011, 11:39 AM
David SmithPosted Nov 8, 2011, 11:35 AM
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.
VulpesPosted Nov 8, 2011, 11:28 AM
David SmithPosted Nov 8, 2011, 11:14 AM
David SmithPosted Nov 8, 2011, 11:10 AM
Dipal ChoksiPosted Nov 8, 2011, 10:40 AM
http://www.mysqlfaqs.net/mysql-faqs/Indexes/Unique-Key-or-Index/How-to-create-multi-column-unique-key-or-index-in-MySQL
Javeed M ShaikhPosted Nov 8, 2011, 10:23 AM