SQL Server 中是否每条记录都有唯一的字段?

Does every record has an unique field in SQL Server?(SQL Server 中是否每条记录都有唯一的字段?)
本文介绍了SQL Server 中是否每条记录都有唯一的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio - VB.NET 中工作.

I'm working in Visual Studio - VB.NET.

我的问题是我想删除 SQL Server 中的特定行,但我唯一的唯一列是一个自动递增的标识.

My problem is that I want to delete a specific row in SQL Server but the only unique column I have is an Identity that increments automatically.

我的工作流程:
1.我在列中添加了一行(身份正在递增,但我不知道数字)
2.我想删除上一行

My process of work:
1. I add a row in the column (the identity is being incremented, but I don't know the number)
2. I want to delete the previous row

是否存在每条新记录都有的唯一 ID?可能我的表有 2 条完全相同的记录,只是序列(身份)不同.

Is there a sort of unique ID that every new record has? It's possible that my table has 2 exactly the same records, just the sequence (identity) is different.

任何想法如何处理这个问题?

Any ideas how to handle this problem?

推荐答案

SQL Server 有几个函数可以返回最后一行的生成 ID,每个函数都有自己特定的优点和缺点.

SQL Server has a few functions that return the generated ID for the last rows, each with it's own specific strengths and weaknesses.

基本上:

  • @@IDENTITY 如果您不使用触发器
  • SCOPE_IDENTITY() 适用于您明确调用的代码.
  • IDENT_CURRENT(‘tablename’) 适用于所有范围的特定表.
  • @@IDENTITY works if you do not use triggers
  • SCOPE_IDENTITY() works for the code you explicitly called.
  • IDENT_CURRENT(‘tablename’) works for a specific table, across all scopes.

在几乎所有情况下,SCOPE_IDENTITY() 都是您所需要的,并且使用它是一个好习惯,而不是其他选项.
此处.

In almost all scenarios SCOPE_IDENTITY() is what you need, and it's a good habit to use it, opposed to the other options.
A good discussion on the pros and cons of the approaches is also available here.

这篇关于SQL Server 中是否每条记录都有唯一的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)