SQL Server TRUNCATE - sql-server

I have a question to the TRUNCATE command.
How does TRUNCATE work in background ?
I read a text that says TRUNCATE creates a copy from the table and then start to delete all rows of the table. Is it correct?

with the information from here
TRUNCATE logs the deallocation of the data pages in which the data
exists which means that your data rows still actually exist in the data pages, but the extents have been marked as empty for reuse. This is what
makes TRUNCATE a faster operation to perform over DELETE.
You cannot TRUNCATE a table that has any foreign key
constraints. You will have to remove the contraints, TRUNCATE the
table, and reapply the contraints.
TRUNCATE will reset any identity columns to the default seed
value. This means if you have a table with an identity column and
you have 264 rows with a seed value of 1, your last record will have
the value 264 (assuming you started with value 1) in its identity
columns. After TRUNCATEing your table, when you insert a new
record into the empty table, the identity column will have a value of
1

Related

will ms sql server reuse deleted primary keys in auto increment mode?

Suppose i have 100 rows in my table, my primary key is integer which is auto incrementing by 1 from 1. I consolidate my data and clear the table once row id reaches 100, will SQL server reuse the deleted primary keys ?
since primary key is incrementing by 1, if the answer of above question is no, what will happen to the next insert, once the primary key reached the biggest possible number an 'Int' can hold ?
MSSQL will not re-use the primary keys that have been deleted using DELETE (I'm assuming you are talking about the identity incrementation.) If you TRUNCATE the table it will reset the seed and reuse them.
If you go over the max for INT it will indeed just fail to make the next row. You can convert your INT column to BIGINT to avoid that.
BIGINT has a max of: 9,223,372,036,854,775,807 and INT has a cap of 2,147,483,647 but note.. you can also use those negative values too!
You can read about those caps here: https://learn.microsoft.com/fr-fr/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql?view=sql-server-2017
No, SQL Server won't reuse IDs. If you have 100 records with IDs 1 throught 100 and delete them all, next inserted row will have ID = 101.
But if you want to start from 1 after deleting, you can use this command:
DBCC CHECKIDENT('TableName', RESEED, 0)
It will reseed your identity and make it start from 1 all over again.
It depends on what you mean by
I...clear the table once row id reaches 100
If you're issuing a DELETE statement, then no, the identity column will issue 101 on the next insert.
If you're issuing a TRUNCATE TABLE statement, then yes, the table will reseed to 1 (or whatever your seed value is) on the next insert.

Identity column not incremented by 1?

I've created 4 tables:
`Patient` (Id, Name, ..)
`Donor` (Id, Name, ..)
`BloodBank` (Id, Name, ..)
BloodBankDonors(DonorId, BloodBankId, ..)
And set the Id columns to Identity incremented by 1, seed 1. and made a relationship between (Donor, BloodBank) and (BloodBankDonors).
The problem is when I entered some data in the tables BloodBank and the patient, the auto generated Id column was: 1,3,4 and 1,4,5,8 respectively?!
So many things can cause gaps in an IDENTITY column. For example rollbacks not resetting IDENTITY, deletes, etc.
So, why do you care about gaps? You shouldn't. If you need a contiguous sequence of numbers, stop using IDENTITY.
You might deleting (DELETE command) some records from tables "BloodBank" and the "patient".Deleting record from table holds the log info of column ID(auto generated column) for recovery Purpose. Instead use below mentioned code snippet after "DELETE" command:
DBCC CHECKIDENT('databasename.dbo.tablename', RESEED, number)
if number=0 then in the next insert the auto increment field will contain value 1
if number=101 then in the next insert the auto increment field will contain value 102.
for more clear answer, please share sql script which you are using to create tables and insert records.
Deleting data from table holds the log info holding ID (auto generated columns) for recovery purpose.
Try to truncate table and re-enter the data
truncate table Patient
May this help

Converting int primary key to bigint in Sql Server

We have a production table with 770 million rows and change. We want(/need?) to change the Primary ID column from int to bigint to allow for future growth (and to avoid the sudden stop when the 32bit integer space is exhausted)
Experiments in DEV have shown that this is not as simple as altering the column as we would need to drop the index and then re-create it. So far in DEV (which is a bit humbler than PROD) the dropping of the index has not finished after 1 and a half hours. This table is hit 24/7 and having it offline for such a long time is not an option.
Has anyone else had to deal with a similar situation? How did you get it done?
Are there alternatives?
Edit: Additional Info:
The Primary key is clustered.
You could attempt a staged approach.
Create a new bigint column
Create an insert trigger to keep new entries in sync with the 2 columns
Execute an update to populate all the empty values in the bigint column with the converted value
Change the primary index on the table from your old id column to the new one
Point any FK's and queries to use the new column
Change the new column to become your identity column and remove the insert trigger from #2
Delete the old ID column
You should end up spreading the pain out over these 7 steps instead of hitting it all at once.
Create a parallel table with the longer data type for new rows and UNION the results?
What I had to do was copy the data into a new table with the desired structure (primary/clustered key only, non-clustered/FK once complete). If you don't have the room, you could bcp out the data and back in. You may need an application outage to make this happen.
What doesn't work: alter table Orderhistory alter column ID bigint because of the primary key. Don't drop the key and alter column as you will just fill your log file and take much longer than copy/bcp.
Never use the SSMS tools designer to change a column property, it copies table into temp table then does a rename once done. Lookup the alter table alter column syntax and use it and possibly defrag once complete if you modified a column wider that sits in middle of table.

Removing allocated space for Table - SQL Server

I got a table where in got allocated space of 3gig but have no rows.
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.
You cannot use TRUNCATE TABLE on tables that:
Are referenced by a FOREIGN KEY
constraint. (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. To retain the identity counter, use DELETE instead. 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 FROM table WITH (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). 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')

Changing Identity Seed in SQL Server (Permanently!)

Is there any way of changing the identity seed for an identity column permanently? Using DBCC CHECKIDENT just seems to set the last_value. If the table is truncated all values are reset.
dbcc checkident ('__Test_SeedIdent', reseed, 1000)
select name, seed_value, increment_value, last_value
from sys.identity_columns
where [object_id] = OBJECT_ID('__Test_SeedIdent');
returns
name seed_value increment_value last_value
-------------------------------------------------
idIdent 1 1 1000
I was hoping that some syntax like
alter table dbo.__Test_SeedIdent alter column idIdent [int] identity(1000,1) NOT NULL
would exist.
Is it necessary to create a new column, move the values across, drop the original column and rename the new?
From Books Online:
"To change the original seed value and reseed any existing rows, you must drop the identity column and recreate it specifying the new seed value. When the table contains data, the identity numbers are added to the existing rows with the specified seed and increment values. The order in which the rows are updated is not guaranteed."
MSSQL does not allow you to add or alter an Identity on an existing column via TSQL very easily. You would have to drop the column and re-add it. Needless to say this can play hell with FK relations. You can do it directly in the enterprise manager. However that won't be fun if you have to do this to a LOT of columns.
Is it necessary to create a new
column, move the values across, drop
the original column and rename the
new?
Yup, and don't forget to fix/update all indexes, foreign key relationships, etc. that are tied to that column
You can use DBCC CHECKIDENT('tablename', RESEED, seedvalue)
example: DBCC CHECKIDENT('Customers',RESEED, 1350)
run DBCC CHECKIDENT('Customers') again to check if current seed value was set.
However as mentioned in previous answers this will not change existing values stored in the identity column. It will only change seed value so the next row that is inserted will start with that value. Identity increment remains same (not changed) and can not be changed with DBCC.
"Is it necessary to create a new column, move the values across, drop the original column and rename the new?"
Actually in Enterprise Manager, when you add an ID column to an existing table (or change an INT PK field to an INT PK ID), it does this behind the scene.

Resources