| |

VerySource

 Forgot password?
 Register
Search
View: 749|Reply: 8

How do I modify the value of the SQL primary key column?

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-30 01:20:01
| Show all posts |Read mode
For example, there is a table
create table Province
(
   ProID int primary key,
   ProName varchar (20) not null
)
The inserted data is: (1, Guangzhou)
So how to modify the primary key value with SQL statement so that it becomes (2, Guangzhou)
Waiting online ...
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-3-5 13:15:01
| Show all posts
Is there a foreign key that references the primary key?
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-3-5 16:00:02
| Show all posts
update Province set ProID = 2
where ProID = 1
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-3-5 16:45:01
| Show all posts
create table Province
(
   ProID int primary key,
   ProName varchar (20) not null
)
insert Province select 1, 'Guangzhou'

update Province set ProID = 2
where ProID = 1

select * from Province

--result
ProID ProName
----------- --------------------
2 Guangzhou

(1 row (s) affected)
Reply

Use magic Report

0

Threads

21

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-3-7 22:45:01
| Show all posts
Make sure that the primary key does not conflict, and there is no foreign key that references the primary key.
update Province set ProID = 2
where ProID = 1 and ProName like 'Guangzhou'
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-9 23:00:02
| Show all posts
Thank you upstairs, suddenly I was wrong ...
What if there are foreign key references?
Reply

Use magic Report

0

Threads

14

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-3-10 09:45:02
| Show all posts
Set the foreign key relationship to on update cascade, directly update the primary key of the primary table, and the foreign key of the secondary table will be cascaded automatically.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-17 17:15:01
| Show all posts
Thank you, I've now understood..
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-4-2 16:15:01
| Show all posts
No foreign key, feel free
In the case of foreign keys, cascading is required
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list