473,382 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to do this update?

al
Greetings,

In the customers table in Northwind db, one can update PK
(customerid) and all other fields in the same table. My question is
how can you do this in the udpate stat. That is, if one wants to
write update query to update all fields including PK, how it can be
set? Using PK in the SET statement, gives an error, because this field
might have changed during the update?

MTIA,
Grawsha
Jul 20 '05 #1
6 7628
al (gr*********@yahoo.com) writes:
In the customers table in Northwind db, one can update PK
(customerid) and all other fields in the same table. My question is
how can you do this in the udpate stat. That is, if one wants to
write update query to update all fields including PK, how it can be
set? Using PK in the SET statement, gives an error, because this field
might have changed during the update?


Updating the PK does not have to be a problem:

CREATE TABLE x (a int NOT NULL PRIMARY KEY,
b varchar(23) NOT NULL)
go
INSERT x VALUES( 1, 'KJK')
INSERT x VALUES( 2, 'NJJDGF')
go
UPDATE x
SET a = 10,
b = 'Hallå!'
WHERE a = 1
go
SELECT * FROM x

However, this fails:

UPDATE Northwind..Customers
SET CustomerID = 'KKKKK'
WHERE CustomerID = 'ALFKI'

And the error message tells us why:

Server: Msg 547, Level 16, State 1, Line 1
UPDATE statement conflicted with COLUMN REFERENCE constraint
'FK_Orders_Customers'. The conflict occurred in database 'Northwind',
table 'Orders', column 'CustomerID'.
The statement has been terminated.

Since there is a reference to the table, you cannot change the id
of a customer that has orders. If you added a new customer to the table,
you could easily change its ID, as you long as you don't add orders for
it.

One way to handle this, is to change the foreign-key defintion to say
ON UPDATE CASCADE, in which case the change would be propagated to
Orders.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
al
Erland Sommarskog <so****@algonet.se> wrote in message news:<Xn*********************@127.0.0.1>...
al (gr*********@yahoo.com) writes:
In the customers table in Northwind db, one can update PK
(customerid) and all other fields in the same table. My question is
how can you do this in the udpate stat. That is, if one wants to
write update query to update all fields including PK, how it can be
set? Using PK in the SET statement, gives an error, because this field
might have changed during the update?
Updating the PK does not have to be a problem:

CREATE TABLE x (a int NOT NULL PRIMARY KEY,
b varchar(23) NOT NULL)
go
INSERT x VALUES( 1, 'KJK')
INSERT x VALUES( 2, 'NJJDGF')
go
UPDATE x
SET a = 10,
b = 'Hallå!'
WHERE a = 1
go
SELECT * FROM x

However, this fails:

UPDATE Northwind..Customers
SET CustomerID = 'KKKKK'
WHERE CustomerID = 'ALFKI'

And the error message tells us why:

Server: Msg 547, Level 16, State 1, Line 1
UPDATE statement conflicted with COLUMN REFERENCE constraint
'FK_Orders_Customers'. The conflict occurred in database 'Northwind',
table 'Orders', column 'CustomerID'.
The statement has been terminated.

Since there is a reference to the table, you cannot change the id
of a customer that has orders. If you added a new customer to the table,
you could easily change its ID, as you long as you don't add orders for
it.

One way to handle this, is to change the foreign-key defintion to say
ON UPDATE CASCADE, in which case the change would be propagated to
Orders.


I don't have a problem with this. I did ticked the CascadeOnUpdate.
The problem is, the update woun't happen becuase there will be a
Concurency Violation. Try to do this(with all cascades, and still you
will recieve an err)
UPDATE Northwind..Customers SET CustomerID = 'KKKKK'
WHERE CustomerID = 'ALFKI'

Jul 20 '05 #3
> UPDATE Northwind..Customers
SET CustomerID = 'KKKKK'
WHERE CustomerID = 'ALFKI'


This UPDATE works for me once I've enabled Cascading updates on the child
tables (CustomerCustomerDemo and Orders). Exactly what error message are you
getting? Maybe you already have a row where CustomerID = 'KKKKK' so this
violates the primary key?

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #4
al (gr*********@yahoo.com) writes:
I don't have a problem with this. I did ticked the CascadeOnUpdate.
The problem is, the update woun't happen becuase there will be a
Concurency Violation. Try to do this(with all cascades, and still you
will recieve an err)
UPDATE Northwind..Customers
SET CustomerID = 'KKKKK'
WHERE CustomerID = 'ALFKI'


Since I am lazy I did not even try this. I know that it does not produce
any error with with the appropriate cascade. Least of all concurrency
violation, because SQL Server does not produce any such errors.

However, some client tools and libraries are doing smart things behind
your back, and may be outsmarted by your manoevre.

So you need to tell us in which context you get the error message (as
well as of course the exact text of the error message). I have a strong
feeling that you are not submitting the above from Query Analyzer.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5
al
Erland Sommarskog <so****@algonet.se> wrote in message news:<Xn**********************@127.0.0.1>...
al (gr*********@yahoo.com) writes:
I don't have a problem with this. I did ticked the CascadeOnUpdate.
The problem is, the update woun't happen becuase there will be a
Concurency Violation. Try to do this(with all cascades, and still you
will recieve an err)
UPDATE Northwind..Customers
SET CustomerID = 'KKKKK'
WHERE CustomerID = 'ALFKI'


Since I am lazy I did not even try this. I know that it does not produce
any error with with the appropriate cascade. Least of all concurrency
violation, because SQL Server does not produce any such errors.

However, some client tools and libraries are doing smart things behind
your back, and may be outsmarted by your manoevre.

So you need to tell us in which context you get the error message (as
well as of course the exact text of the error message). I have a strong
feeling that you are not submitting the above from Query Analyzer.


You are right! I'm doing this from VB.NET. But since this is not the
group for such post and since I have found out about this late, how
can I fix this? I gusse I need to submit the original value+the
changed value..may be??
Jul 20 '05 #6
al (gr*********@yahoo.com) writes:
You are right! I'm doing this from VB.NET. But since this is not the
group for such post and since I have found out about this late, how
can I fix this? I gusse I need to submit the original value+the
changed value..may be??


I'm still a learner of ADO .Net, so maybe I am not the one to give
expert advice. But even as an expert, I would have problems without
your code at hand.

The answer to your question may be in David Sceppa's book on ADO .Net
which lies next to me on the table. I don't find anything on a quick
look, though. But it's a good book.

Being an SQL person, I would probably define my own UpdateCommand
for the DataAdapter, but there may be better support build into
ADO .Net. If you find some ADO .Net group, you might get better
answers there.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
8
by: Lauren Quantrell | last post by:
In VBA, I constructed the following to update all records in tblmyTable with each records in tblmyTableTEMP having the same UniqueID: UPDATE tblMyTable RIGHT JOIN tblMyTableTEMP ON...
27
by: VK | last post by:
<http://www.jibbering.com/faq/#FAQ3_2> The parts where update, replacement or add-on is needed are in <update> tag. 3.2 What online resources are available? Javascript FAQ sites, please...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
3
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
9
by: jaYPee | last post by:
I have search a lot of thread in google newsgroup and read a lot of articles but still i don't know how to update the dataset that has 3 tables. my 3 tables looks like the 3 tables from...
5
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I...
8
by: Zorpiedoman | last post by:
I keep getting a concurrency exception the second time I make a change and attempt to update a dataadapter. It appears this is by design, so there must be something I can do to avoid it. ...
5
by: Stephen Plotnick | last post by:
I'm very new to VB.NET 2003 Here is what I have accomplished: MainSelectForm - Selects an item In a public class I pass a DataViewRow to ItemInformation1 Form ItemInformation2 Form
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.