简体   繁体   English

在数据库中将nvarchar(MAX)更改为nvarchar(n)

[英]Changing nvarchar(MAX) to nvarchar(n) in database

I just changed a nvarchar(MAX) field in a table to nvarchar(250). 我刚刚将表中的nvarchar(MAX)字段更改为nvarchar(250)。 Could someone please tell me what happens to the data if there was an entry larger than 250 characters? 如果有超过250个字符的条目,有人可以告诉我数据会发生什么变化吗?

My concern is not with the visible data, but what happens behind the scenes: 我关心的不是可见数据,而是幕后发生的事情:

  • What is done to the data which overshoots the limit of that container of data? 对超过该容器数据限制的数据做了什么?
  • I read in a few places that the table has to be deleted and re created again. 我在几个地方读到必须删除该表并重新创建。 Is this true and why? 这是真的吗?为什么? I didn't see any errors which the others received. 我没有看到其他人收到的任何错误。
  • Is there a way to recover the truncated data after making this change? 有没有办法在进行此更改后恢复截断的数据? (I dont want to do it, but I'm curious) (我不想这样做,但我很好奇)

If you have altered/changed column nvarchar(MAX) field into nvarchar(250) and you did not receive any error, it means that none on rows contains the data more than 250 characters that why SQL server successfully changed the column length and your data is accurate/complete. 如果您已将列nvarchar(MAX)字段更改/更改为nvarchar(250)并且您没有收到任何错误,则表示行上没有包含超过250个字符的数据,这就是SQL Server成功更改列长度和数据的原因准确/完整。

If any of row contains more than 250 characters then SQL server will give you an error and alter statement will be failed. 如果任何行包含超过250个字符,那么SQL服务器将给您一个错误,并且alter语句将失败。 It means that data type length will not be changed. 这意味着不会更改数据类型长度。

Msg 8152, Level 16, State 13, Line 12 String or binary data would be truncated. 消息8152,级别16,状态13,行12字符串或二进制数据将被截断。 The statement has been terminated. 该语句已终止。

While altering column length if SET ANSI_WARNINGS OFF then SQL server will change the column length without any warning and extra data will be truncated. 如果SET ANSI_WARNINGS OFF则更改列长度,然后SQL Server将更改列长度而不发出任何警告,并且将截断额外数据。

By Default, it is SET ANSI_WARNINGS ON to warn the user. 默认情况下,它是SET ANSI_WARNINGS ON以警告用户。

I think Once data is truncated it can't be recovered later. 我认为一旦数据被截断,以后就无法恢复。

The system should prevent you or at least warn you of possible data loss when changing column length if any row exceeds the new length. 如果任何行超过新长度,系统应该阻止您或至少警告您在更改列长度时可能丢失数据。

Depending on DBMS and version, you may even not be able to change column length. 根据DBMS和版本,您甚至可能无法更改列长度。

However, if you don't have any rows exceeding 250, as you said, then there should be no problem. 但是,如果你没有超过250的行,正如你所说,那么应该没有问题。

There is no way to recover truncated data unless you have access to a database backup that's just before the change 除非您可以访问更改之前的数据库备份,否则无法恢复截断的数据

On a side note, regardless of what you intend to do with that change, I should suggest to avoid columns of variable-length 在旁注中,无论您打算如何处理该更改,我都应该建议避免使用可变长度的列

MySQL automatically reserves maximum-possible length for a variable-length column, regardless of whether a row is 15 characters or 45 or 250. This, as you can imagine, eventually leads to bottlenecks in the system. MySQL会自动为可变长度列保留最大可能长度,无论行是15个字符还是45个或250个。这可以想象,最终会导致系统出现瓶颈。 (Maybe you don't have a database large enough for this to show effects, but my motto is "forewarned is forearmed" ) (也许你没有一个足够大的数据库来显示效果,但我的座右铭是“预先警告是预先设定的”)

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

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