Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet308/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   304   305   306   307   308   309   310   311   ...   443
Bog'liq
BookSQL

 

CHAPTER 8

 

Data Modification



 

 263

The 

TRUNCATE

 Statement

The TRUNCATE statement deletes all rows from a table. Unlike the DELETE statement, TRUNCATE has 

no filter. For example, to delete all rows from a table called dbo.T1, you run the following code.

TRUNCATE TABLE dbo.T1;

The advantage that TRUNCATE has over DELETE is that the former is minimally logged, whereas 

the latter is fully logged, resulting in significant performance differences. For example, if you use the 



TRUNCATE statement to delete all rows from a table with millions of rows, the operation will finish in 

a matter of seconds. If you use the DELETE statement, the operation can take minutes or even hours. 

Note that I said that TRUNCATE is minimally logged, as opposed to not being logged at all. This 

means that it’s fully transactional (despite the common misconception), and in case of a ROLLBACK

SQL Server can undo the truncation.

TRUNCATE and DELETE also have a functional difference when the table has an identity column. 

TRUNCATE resets the identity value back to the original seed, but DELETE doesn’t.

The TRUNCATE statement is not allowed when the target table is referenced by a foreign key con-

straint, even if the referencing table is empty and even if the foreign key is disabled. The only way to 

allow a TRUNCATE statement is to drop all foreign keys referencing the table. 

Accidents such as truncating or dropping the incorrect table can happen. For example, let’s say 

you have connections open against both the production and the development environments, and 

you submit your code in the wrong connection. Both the TRUNCATE and DROP statements are so 

fast that before you realize your mistake, the transaction is committed. To prevent such accidents, 

you can protect a production table by simply creating a dummy table with a foreign key pointing to 

the production table. You can even disable the foreign key so that it won’t have any impact on per-

formance. As I mentioned earlier, even when disabled, this foreign key prevents you from truncating 

or dropping the referenced table.




Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   304   305   306   307   308   309   310   311   ...   443




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish