Why do we need the LDF files along with MDF files?

前端 未结 4 1423
难免孤独
难免孤独 2021-01-05 05:43

What would happen if I delete the .ldf file that is in the same folder that my .mdf file?

4条回答
  •  难免孤独
    2021-01-05 06:01

    As already pointed out, the .LDF file is crucial for the DB and the DB will not be available without one. The .LDF can be deleted only if the DB is offline, or detached or SQL Service is stopped.

    Assuming that one of the above 3 scenario was true and you did delete the .LDF file, then the DB would be suspect when SQL server is restarted. If the DB was offline and you try to bring it back online, it will give you an error:

    File activation failure. The physical file name "<>" may be incorrect.
    Msg 945, Level 14, State 2, Line 1
    Database '<>' cannot be opened due to inaccessible files or insufficient
    memory or disk space.  See the SQL Server errorlog for details.
    Msg 5069, Level 16, State 1, Line 1
    ALTER DATABASE statement failed.
    

    It is quite simple to fix this error, but you will need to delete all the Metadata pertaining to the DB from system files. The easiest way to do that would be to drop the DB. Here is what you will need to do.

    • Take the DB offline or detach the DB
    • Make a copy of the .MDF file to your backup directory
    • Now Drop the DB (You will have to reattach the DB, if you detached, of course)
    • Copy the .MDF back to the original location

    After these steps, run the following:

    SP_ATTACH_SINGLE_FILE_DB @dbname='<>' ,@physname=N'<>'

    This should return you the following:

    File activation failure. The physical file name "<>" 
    may be incorrect.
    New log file '<>' was created.
    

    That would bring your DB back to an usable state.

    The million $$$$ question still remains - why would anyone want to delete the .LDF file???

    Raj

提交回复
热议问题