简体   繁体   English

将INSERT与INSERT UPDATE插入具有许多NOT NULL约束列的表中

[英]INSERT vs INSERT UPDATE into table with many NOT NULL constraint columns

It's recommended do define column names in INSERT statement, eg: 建议在INSERT语句中定义列名,例如:

INSERT INTO TableName(FirstColumn, SecondColumn, ThirdColumn,...) VALUES ('A','B','C',...);

But if there are 10+ columns in table this could not help that much to find relation between column name and value. 但是,如果表中有10列以上的列,那么在查找列名和值之间的关系时就无济于事。 In this case it's better to break insertion in two parts (insertion of primary key and update of other fields): 在这种情况下,最好将插入分为两部分(插入主键和更新其他字段):

INSERT INTO TableName(PKColumn) VALUES (12345);
UPDATE TableName SET FirstColumn = 'A', SecondColumn = 'B', ThirdColumn = 'C',... 
WHERE PKColumn = 12345;

The problem is for tables with lots of NOT NULL constraint columns so you have to define them in INSERT statement and it becomes unreadable. 问题是对于具有很多NOT NULL约束列的表,因此您必须在INSERT语句中对其进行定义,并且该表变得不可读。 Is there any good solution? 有什么好的解决办法吗?

You do not have to define all the column in an INSERT. 您不必在INSERT中定义所有列。

It's poor practice to put tight restraints on columns where it is not required. 在不需要的地方施加严格的约束是不好的做法。

It's much better to define the default values for columns that are not required or are typically the same value. 最好为不需要的列或通常是相同值的列定义默认值。

`QTY` int(11) DEFAULT '1',
`NUMBER` int(11) DEFAULT '0',
`PRICE` decimal(7,2) DEFAULT '0.00',

When I am updating a table with INSERT and UPDATE I usually just do both, rather than check to see if the record exists before doing an insert. 当我使用INSERT和UPDATE更新表时,通常只执行这两种操作,而不是在执行插入操作之前先检查记录是否存在。 I do she INSERT then follow the INSERT with and UPDATE. 我对她进行INSERT,然后按照INSERT进行更新。

I can check the INSERT for a duplicate: 我可以检查INSERT是否重复:

if (mysql_errno() == 1062){$dups++;}

And I check to see if the UPDATE updated anything. 然后检查UPDATE是否更新了任何内容。

if (mysql_affected_rows() > 0){$update++;}

What I do very often is to align column names and values: 我经常要做的是对齐列名和值:

INSERT INTO TableName 
  (FirstColumn, SecondColumn, ThirdColumn, FourthColumn...) 
VALUES 
  ('A',         'B',          'C',         'D', ...);

This makes quite long lines, but with the current trend for wide-screen monitors this isn't such a big problem nowadays (I think). 这需要很长的时间,但是随着当前宽屏显示器的发展趋势,这已经不是什么大问题了(我认为)。

If you need to do this very often, you might want to consider writing a stored procedure where you can pass named parameters: 如果您需要经常执行此操作,则可能需要考虑编写一个存储过程,可以在其中传递命名参数:

execute do_insert_tableX @FirstColumn = 'A', @SecondColumn = 'B', ...

Here is a suggestion if you are using SMSS: 如果您正在使用SMSS,这是一个建议:

  1. Go to object explorer. 转到对象资源管理器。
  2. Expand your Database then expand its tables. 展开数据库,然后展开其表。
  3. Right Click on the table for which you want to generate a dummy insert statement. 右键单击要为其生成虚拟插入语句的表。
  4. select this option : Script Table as > INSERT To > New Query Editor Window. 选择此选项:脚本表为>插入到>新建查询编辑器窗口。
  5. It will create a dummy insert statement you just need to remove the unwanted columns from the list and fill the values. 它将创建一个虚拟插入语句,您只需要从列表中删除不需要的列并填充值即可。

I do the things like that if I have so many columns in my table. 如果我的表中有很多列,我会做类似的事情。

Example : 范例: 例

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

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