将增量值插入到SQL列中 [英] Insert increment values into column SQL

查看:100
本文介绍了将增量值插入到SQL列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我是SQL的新手。我有2张桌子车和卡。

<前lang =文字>表:车辆
ID_Vehicles(Pk)
137
138
139
140

表:卡
ID_Cards(Pk)VehiclesID(Fk)
338 137
339 138
340 NULL
341 NULL

我想将车辆表中的ID_vehicles(主键)更新为Cards表中的VehiclesID(外键)。



这样的



在牌桌上。

ID_cards 340将链接至VehiclesID 139



341将链接至VehiclesID 140



342将链接到VehiclesID 141 ...

并且链接一直持续到所有ID_cards链接到vehiclesID。



应该我使用插入功能将增量值插入卡片表中的vehiclesID列?



任何一个愿意帮助新手使用SQL的灵魂请提供建议。

如果这是一个错误的论坛要求。请告知我可以发布到哪个网站?[在此处输入图片说明] [1]



我尝试了什么:



我的代码如下:

  DECLARE  < span class =code-sdkkeyword> @ IncrementValue   int   SET   @ IncrementValue  =  139   UPDATE  CARDS  SET  VehiclesID =  @ IncrementValue ,@ IncrementValue = @ IncrementValue + 1 其中​​ ID_CARDS 之间  340    537  

但是下面提示错误消息: UPDATE 语句冲突 t他 FOREIGN KEY 约束 FK_CARDS_VEHICLESID发​​生冲突 数据库 HECPOLL table dbo.VEHICLES ' ID_VEHICLES'

解决方案

在SQL中,外键只允许引用表的主键列中的值。在这种情况下,首先需要在主表中查找值,然后将其更新到辅助表中。为此,您可以将output inserted.column值用于临时表,然后将其作为外键值用于辅助表。有关更多信息,请查看以下链接,您可以在其中获取插入主键的值并将其用于辅助表格。

INSERT和DELETE语句的OUTPUT子句 - SQLServerCentral [ ^ ]


你好



来自新手的解决方案:



  ALTER   TABLE 
ADD FOREIGN KEY (ID_Vehicles) REFERENCES 车辆(ID_Vehicles)





1.更改表。

2.添加一个可以创建链接的新列(外键)。

3.将外键引用到Vehicle表中的Primary Key列。 / blockquot e>

   -   如果要更新父表行在孩子身上反映相同而不是更好地使用ON UPDATE CASCADE和外键参考水平..  

创建 TABLE 工具(ID_Vehicles int primary key
insert into Vehicle 137 ),( 138 ),(< span class =code-digit> 139 ),( 140 );

CREATE TABLE 卡片(ID_Cards int primary key ,VehiclesID int 外来 密钥(VehiclesID)参考车辆(ID_Vehicles) ON 更新 CASCADE

INSERT INTO
338 137
,( 339 138
,( 340 NULL
,( 341 NULL

选择 VehiclesID 来自卡< span class =code-keyword>其中 ID_Cards = 339;更新父表之前 -

更新车辆设置 ID_Vehicles = 136 其中​​ ID_Vehicles = 338;

选择 VehiclesID 来自其中 ID_Cards = 339;更新父表后 -


Hi Guys. I'm new to SQL. I have 2 table Vehicle and Cards.

Table: Vehicle
ID_Vehicles(Pk)
137
138
139
140

Table: Cards	 
ID_Cards(Pk)	VehiclesID(Fk)
338	          137
339	          138
340	          NULL
341	          NULL

I wanted to update the ID_vehicles(primary key) in vehicles table, to VehiclesID(Foreign key) in Cards table.

Such that

In cards table.
ID_cards 340 will link to VehiclesID 139

341 will link to VehiclesID 140

342 will link to VehiclesID 141 ...
and the link goes on until the all ID_cards linked to vehiclesID.

Should i use insert function to insert increment values into vehiclesID column in cards table?

Any kind soul willing to help a newbie to SQL here Pls advise.
If this is a wrong forum to ask. Please advise which website i can post to?[enter image description here][1]

What I have tried:

My Codes as as below:

DECLARE @IncrementValue int SET @IncrementValue = 139 UPDATE CARDS SET VehiclesID = @IncrementValue,@IncrementValue=@IncrementValue+1 where ID_CARDS between 340 and 537

However error message prompted below: The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_CARDS_VEHICLESID" The conflict occurred in database "HECPOLL", table "dbo.VEHICLES", column 'ID_VEHICLES'

解决方案

Hi, In SQL foreign key allows only values in the primary key column of the reference table. In this case first you need to find the value in the primary table and then update it into the secondary table. For this you can use output inserted.column value into a temp table and then use it into secondary table as foreign key value. For more please check below link in which you can get value of inserted primary key and use it into secondary table.
The OUTPUT Clause for INSERT and DELETE Statements - SQLServerCentral[^]


Hello

A solution from a newbie:

ALTER TABLE Cards
ADD FOREIGN KEY (ID_Vehicles) REFERENCES Vehicle (ID_Vehicles)



1. Alter table.
2. Add a new column (Foreign Key) that can create a link.
3. Reference the Foreign Key to the Primary Key column in the Vehicle table.


--if want to update parent table row will reflect same in child than better to use "ON UPDATE CASCADE " with foreign key reference level..

CREATE TABLE Vehicle(ID_Vehicles int primary key)
insert into Vehicle values(137),(138),(139),(140);

CREATE TABLE Cards(ID_Cards  int primary key,VehiclesID  int foreign key(VehiclesID) references Vehicle(ID_Vehicles) ON UPDATE CASCADE)

INSERT INTO Cards values
(338,137 )
,(339,138 )
,(340,NULL)
,(341,NULL)

select VehiclesID from Cards where ID_Cards=339; --before updating parent table

update Vehicle set ID_Vehicles=136 where ID_Vehicles=338;

select VehiclesID from Cards where ID_Cards=339; --after updating parent table


这篇关于将增量值插入到SQL列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆