Inserting Into A Table With 2 Pks (1 Viewer)

MsLady

Traumatized by Access
Local time
Yesterday, 22:53
Joined
Jun 14, 2004
Messages
438
The security table is made up of two primary keys: thing, personorgroup
When i run this statement to insert into the security table

Code:
INSERT INTO security (thing, personorgroup, accessrights)
SELECT '252600649', '4020', '255'
FROM PROFILE
WHERE not exists(select * from security
where security.thing = '252600649' and security.personorgroup = '4020');

I get this error:
Server: Msg 2627, Level 14, State 1, Line 25
Violation of PRIMARY KEY constraint 'PK_SECURITY'. Cannot insert duplicate key in object 'SECURITY'.
The statement has been terminated.


Anybody know how i can perform my insert successfully? :D
 

KeithG

AWF VIP
Local time
Yesterday, 22:53
Joined
Mar 23, 2006
Messages
2,592
Sound like you already have a record with the specified primary key. Is this true?
 

boblarson

Smeghead
Local time
Yesterday, 22:53
Joined
Jan 12, 2001
Messages
32,059
It sure sounds like you already have a combination of

security.thing = '252600649' and security.personorgroup = '4020'

which would cause the violation. Why do you have a compound key?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 00:53
Joined
Feb 28, 2001
Messages
27,321
The wording of your question makes me pause. I request clarification.

When you say "two primary keys" do you mean...

1. A single primary key with two fields.

2. Two keys with unique indexes as independent fields.

English, being multi-valued, allows me to see either possibility. (If it is #2, you are hosed.)
 

MsLady

Traumatized by Access
Local time
Yesterday, 22:53
Joined
Jun 14, 2004
Messages
438
It sure sounds like you already have a combination of

security.thing = '252600649' and security.personorgroup = '4020'

which would cause the violation. Why do you have a compound key?

tha's what it sounds like but when i run this, it works perfectly and does the insert!
Code:
INSERT INTO docsadm.security (thing, personorgroup, accessrights)
values ('252600649', '4020', '255')
the above code works. Meaning i don't have the combination of those values. But what puzzles me is, why do i get that error message when i use the first code i posted.

@Docman: What i mean is "1. A single primary key with two fields." sorry i wasn't clear
 

MsLady

Traumatized by Access
Local time
Yesterday, 22:53
Joined
Jun 14, 2004
Messages
438
why doesn't this work:
PHP:
INSERT INTO security (thing, personorgroup, accessrights)
SELECT '252600649', '4020', '255'
FROM PROFILE
WHERE not exists(select * from security
where security.thing = '252600649' and security.personorgroup = '4020');

But this works:

PHP:
INSERT INTO docsadm.security (thing, personorgroup, accessrights)
values ('252600649', '4020', '255')
 

MsLady

Traumatized by Access
Local time
Yesterday, 22:53
Joined
Jun 14, 2004
Messages
438
This doesn't work either :eek: :mad:
PHP:
IF NOT EXISTS(select thing, personorgroup, accessrights from docsadm.security
where docsadm.security.thing = '252600649' and docsadm.security.personorgroup = '4038')
INSERT INTO docsadm.security (thing, personorgroup, accessrights)
SELECT '252600649', '4038', '255'
FROM docsadm.PROFILE

This is the PK_security contraint code:
ALTER TABLE [DOCSADM].[SECURITY] ADD CONSTRAINT [PK_SECURITY] PRIMARY KEY CLUSTERED
(
[THING],
[PERSONORGROUP]
) ON [PRIMARY]
GO



retarded error message :rolleyes:
Server: Msg 2627, Level 14, State 1, Line 48
Violation of PRIMARY KEY constraint 'PK_SECURITY'. Cannot insert duplicate key in object 'SECURITY'.
The statement has been terminated.

 

KeithG

AWF VIP
Local time
Yesterday, 22:53
Joined
Mar 23, 2006
Messages
2,592
Your problem is because when you add table PROFILE to your from clause you are trying to add a record of '252600649', '4038', '255' for each row returned.

So for example it seems that there is more than one record that exists in Profile that dosen't exist in Security so you are trying to create a record with the same values for each record from Profile that dosen't exist in Security.
 
Last edited:

MsLady

Traumatized by Access
Local time
Yesterday, 22:53
Joined
Jun 14, 2004
Messages
438
Your problem is because when you add table PROFILE to your from clause you are trying to add a record of '252600649', '4038', '255' for each row returned.

So for example it seems that there is more than one record that exists in Profile that dosen't exist in Security so you are trying to create a record with the same values for each record from Profile that dosen't exist in Security.
that's right. what can i do to make this happen? :)
 

KeithG

AWF VIP
Local time
Yesterday, 22:53
Joined
Mar 23, 2006
Messages
2,592
So you want to create duplicate records in the Security table? If so you need to change your PK. A primary key is used to uniquely identify a record that is why you are getting the error.
 

Users who are viewing this thread

Top Bottom