简体   繁体   English

SQL将新行插入到现有表中,更改除一列外的所有列

[英]SQL Insert a new row into an existing table changing all but one column

I have a table with 14 columns. 我有一张有14列的桌子。 The first column is Part #. 第一列是零件号。 I need to insert a new line and add different data to the other 13 columns if the data in column 10 is different. 如果第10列中的数据不同,我需要插入新行并将不同的数据添加到其他13列中。

For example: 例如:

Part #  Description Lead Time   Price 1 Price 2 Price 3 Min. Buy    Date    ATA
1693    SHORTY PLUG     045  $-      $-      $4.25  100 110612  STC
2617    DOME PLUG            045     $-      $-      $0.75   100    111912  CIS
2617    DOME PLUG            045     $-      $-      $0.50  100 91012   STC
2617    DOME PLUG            045     $-      $-      $0.50  100 91012   STD
2646    PLUG (BLACK 045  $-      $-      $6.75  100 91012   STC
2646    PLUG (BLACK)    045  $-      $-      $6.00  100 91012   STD

The key column is the "ATA" column. 关键列是“ ATA”列。 How would I add, say Part # 2646 which had an ATA Code of "CIS". 我要如何添加ATA代码为“ CIS”的零件号2646。 The Part # is the only Column which will stay the same. 部件号是唯一保持不变的列。 I want to leave the other Rows in the table, just add another row with a Part # of 2646 with the rest of the data different. 我要在表中保留其他行,只需添加另一行,其部件号为2646,其余数据不同。

I'm not very proficient in SQL, and have tried everything I can find on the Internet with no success. 我对SQL的了解不是很熟练,并且尝试了我在Internet上可以找到的所有内容,但都没有成功。 Thanks for your help. 谢谢你的帮助。

Sounds like you just need a simple insert statement. 听起来您只需要一个简单的insert语句即可。 Replace the values part with the actual values you want to add to your database table. 将值部分替换为要添加到数据库表中的实际值。

Insert into YOUR_TABLE ([Part #], [Description], [Lead Time], [Price 1], [Price 2], [Price 3], [Min. Buy], [Date], [ATA])
Values (OLD_PART_NO, NEW_DESCRIPTION, NEW_LEAD_TIME, NEW_PRICE_1, NEW_PRICE_2, NEW_PRICE_3, NEW_MIN_BUY, NEW_DATE, NEW_ATA)

If you need a table where you can have multiple items with the same part number but must vary on a second column and still want to prevent duplicates you need to edit your PRIMARY KEY to include both the Part # and the ATA columns like so: 如果您需要一个表,其中可以有多个具有相同零件号的项目,但必须在第二列上有所不同,并且仍然希望防止重复,则需要编辑PRIMARY KEY以同时包括Part #ATA列,如下所示:

ALTER TABLE [name]
DROP CONSTRAINT [pk_constraint_name] [or DROP PRIMARY KEY for MySQL]

ALTER TABLE [name]
ADD PRIMARY KEY([Part #],[ATA])

Or you could have another column be the primary key and then define a UNIQUE INDEX on those two columns, either would likely have the desired end result. 或者,您可以将另一列用作主键,然后在这两列上定义UNIQUE INDEX ,这两种方法都有可能获得所需的最终结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM