Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


working with primary key column


working with primary key column

Author
Message
harish_ravi
harish_ravi
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 76
if a table is having 2 columns col1 and col2. if col1 is primary key.
col1 col2
1   "aa"
2   "bb"
3   "cc"
4   "dd"

if i want to run some update query on this table as
update table tab-name
set col1 = col1 + 1

it will cause primary key constraint violation error.
So is it ok, we can do the same work by disabling the primary key constraint ??
GURSETHI
GURSETHI
SSC Journeyman
SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)

Group: General Forum Members
Points: 96 Visits: 469
Hi,

Please run below steps on your test database.

create table test (col1 int primary key, col2 varchar(10) )

insert into test values(1,'col1')
insert into test values(2,'col2')
insert into test values(3,'col3')
insert into test values(4,'col4')

select * from test

update test set col1=col1+1

select * from test

You will see it will update the column without any problem...Smile

Now the reason:

We know SQL server works in Auto Commit mode means what ever query we write on Query Window automatically got comitted. Now this will only happen by the time it will complete its execution. So Once execution will be done all rows will be succesfully updated by new values.

Please let me know if you are facing any problem. Please share your script as well if its there.

Regards

Gurpreet Sethi
dhulme
dhulme
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 14
Updating the Primary Key (1) to (1+1=2) would create two records with the same Primary Key (2). This will not work, and should not be attempted.

A Primary Key constraint requires the column to be unique; and constrains the user's actions to keep this rule.

Perhaps you are trying to update the Foreign Keys (the related records)?

Table 1
======
ColourID | Colour
1 | red
2 | blue
3 | green
4 | yellow

Table 2
=====
ItemID | Item | ColourID
1 |Red Car |1
2 |Red Brick |1
3 |Red Pencil|1
4 |Blue things|2

If ColourID in table 1 was updated from 1 to 2, the PKEY 2 would mean both red and blue, and the related items in table 2 would appear as both red and blue (1,red car,code for blue & code for red)
etc.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search