Showing posts with label website. Show all posts
Showing posts with label website. Show all posts

Saturday, February 25, 2012

OverWrite SqlExpress .LDF file ??

Hi :)

I have a website that uses SqlExpress ...on it i have a database that was working ok ...until i made a few modifications to the database (had a few rows).

I have upload the database (only the .mdf file) to the app_data folder ...but now i get this message :

One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup.
Cannot open database "ArtWork" requested by the login. The login failed.
Login failed for user 'NT AUTHORITY\NETWORK SERVICE'.
Log file 'c:\xxxx\xxx\xxxx\xxxx\xxxx\App_Data\ArtWork_log.LDF' does not match the primary file. It may be from a different database or the log may have been rebuilt previously.

I try to delete the .LDF file but it gives me access denied ...i cheched the permissions for the file and everything is ok

How can i solve this??

Thanks and Cheers

Have you moved both the .mdf file and .ldf file to new location (c:\xxxx\xxx\xxxx\xxxx\xxxx\App_Data\)?

Override a foreign key constraint

A website that I'm working on has users sign in and keeps a log of the pages they go to. The log table has a foreign key in it that links to the username is the users table. I need to update the username for one of the users but the foreign key is preventing me from doing so. What is the benefit of having a foreign key like this? Can I delete it to update the username or is there a better way?

Foreign Key constraints ensure the integrity of the data is maintained. If there was no FK in place. then it would be easy to get orphaned and inconsistent data. In your example, if you just update a username to be Bob from Terry then all the records associated with Terry in the log table will now have no link back to the users record ie the Terry records will be orphaned.

Soooooo, in answer to your question, you can drop it make your update but it will throw an error when you try and recreate the FK unless you update the log records too.

You can create these constraints with a cascading updates/deletes which will filter your changes down to the child tables but in this case, it looks like its turned off.

I'd suggest just doing the following

Psuedo code:

UPDATE Log set Username = Bob WHERE username = Terry

UPDATE user set username = Bob WHERE username = Terry.

HTH!