Search code examples
sql-servert-sqlstored-proceduressql-server-2017sql-function

which is best using delete or truncate when i need to delete all data from table?


I work on sql server 2017

I need to delete all rows from table student_course

but I don't know which is best for use

delete from student_course

OR

truncate table student_course

Tables Structure

table student_course Structure

studentid pk
courseid  pk

table student table

studentid pk

table courses

courseid pk

student_course table have relation with courses table and student table

so which command will be used to delete all rows from table student_course

truncate or delete ?


Solution

  • I have an foreign key to the table so I will use delete, instead of truncate.

    but if i not have forign key then i will use truncate because it faster than delete