简体   繁体   English

删除表的已分配空间-SQL Server

[英]Removing allocated space for Table - SQL Server

I got a table where in got allocated space of 3gig but have no rows. 我有一张表,其中有3gig分配的空间,但没有行。
How to remove this allocated space ? 如何删除此分配的空间?

There are some restriction with TRUNCATE TABLE, and if can't do a truncate then you can rebuild your clustered index to free up the allocated space. TRUNCATE TABLE有一些限制,如果不能进行截断,则可以重建聚簇索引以释放分配的空间。

You cannot use TRUNCATE TABLE on tables that: 您不能在以下表上使用TRUNCATE TABLE:

  • Are referenced by a FOREIGN KEY constraint. 由FOREIGN KEY约束引用。 (You can truncate a table that has a foreign key that references itself.) (您可以截断具有引用自身的外键的表。)
  • Participate in an indexed view. 参与索引视图。
  • Are published by using transactional replication or merge replication. 通过使用事务复制或合并复制来发布。

If the table contains an identity column, the counter for that column is reset to the seed value defined for the column. 如果表包含一个标识列,则该列的计数器将重置为该列定义的种子值。 If no seed was defined, the default value 1 is used. 如果未定义种子,则使用默认值1。 To retain the identity counter, use DELETE instead. 要保留身份计数器,请改用DELETE。 If this is the case then you need to reseed the table to continue the identity increment from where it was. 如果是这种情况,那么您需要重新设置表的种子,以从原位置继续进行身份增量。

If the table does not have a clustered index you can use TABLOCK hint in your DELETE statement to free up the allocated space. 如果表没有聚集索引,则可以在DELETE语句中使用TABLOCK提示释放已分配的空间。

DELETE FROM table WITH (TABLOCK) 使用(TABLOCK)从表中删除

If table is using a clustered index you can rebuild the index to free the space. 如果表使用聚集索引,则可以重建索引以释放空间。

You can do: 你可以做:

truncate table [table_name]

Then in Sql Server Express Manager, right click on the database and go to tasks->shrink->database/files (do both). 然后在Sql Server Express管理器中,右键单击数据库,然后转到任务->收缩->数据库/文件(同时执行两个操作)。 Should clear you up. 应该让你清理干净。

These should do it, they will shrink your database files. 这些应该做到,它们将收缩您的数据库文件。 If the table is empty the reserved space for the table will be released with the following commands 如果表为空,将使用以下命令释放表的保留空间

DBCC SHRINKDATABASE ('DBName')
DBCC SHRINKFILE ('LogicalFileName')

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

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