How to restore SQL Server database - sql

I am playing with MySQL but reading this post before
https://blog.stackoverflow.com/2011/04/creative-commons-data-dump-apr-11/.
I want to play with this data in SQL Server.
When I download them I found many rar files there. When I extract one of them, I found the xml file but I really do not know how I can restore them.
Can anyone can explain what I need to do to restore them.

You can do this from shell command/command line
$ mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]
http://www.webcheatsheet.com/SQL/mysql_backup_restore.php

Related

Is there an alternative way to import data into Postgres than using psql?

I am under strict corporate environment and don't have access to Postgres' psql. Therefore I can't do what's shown e.g. in the SO Convert SQLITE SQL dump file to POSTGRESQL. However, I can generate the sqlite dump file .sql. The resulting dump.sql file is 1.3gb big.
What would be the best way to import this data into Postgres? I also have DBeaver and can connect to both databases simultaneously but unfortunately can't do INSERT from SELECT.
I think the term for that is 'absurd', not 'strict'.
DBeaver has an 'execute script' feature. But who knows, maybe it will be blocked.
EnterpriseDB offers binary downloads. If you unzip those to a local drive you might be able to execute psql from the bin subdirectory.
If you can install psycopg2 or pg8000 for python, you should be able to connect to the database and then loop over the dump file sending each line to the database with cur.execute(line) . It might take some fiddling if the dump file has any multi-line commands, but the example you linked to doesn't show any of those.

How to load chinook database in SQLite

I want to load chinook database in SQLite but I have no idea how can I do that.
First, I used this command but it just made a file without anything in it:
sqlite3 chinook.db
I also downloaded this archive that contains chinook database but I couldn't find chinook.db file to open it with this command:
.open chinook.db
If you have a copy of the chinook database then just use the restore database feature.
cd /home/sqlite
mv sample.db sample.db.old
sqlite3 sample.db < sample.bak
For more information go to http://www.ibiblio.org/elemental/howto/sqlite-backup.html
We should script the database before using it. Here is the link that discussed the whole procedure.

Uploading dump file to PostgreSQL

i am totally new to postgresql and i have database as dump file on postgresql which i need to restore/upload it back.. i didnt dumped the file its already available online. I read the documentation of restore command on postgresql but it didnt work w me and i am not sure from the steps that I should follow to successfully upload it!! Please if anyone has an idea or could at least refer me to a really good resources for how to do it I will really appreciate it
Usually its as simple as:
psql dbname < infile
or if it's a custom-format dump:
pg_restore --dbname mydb infile

Restore SQL Server database from .sql files

So we had an encrypted database that puked and killed our whole SQL Server setup. Sucks about our data, but we were smart enough to have our Data structure/Stored Procs/Functions in Git
The problem is they're saved as .sql files.
Is there any way we can batch restore our schema from directories full of these files?
I've looked around and I can only find tutorials for restoring from .bak files or .mdf's. This isn't the lazy man's way out- I just need to find a solution ASAP. Any advice or resources/ anything at all would be greatly appreciated.
Thanks Interwebs,
Dylan
Considering the large size of the data structure I was trying restore, running each files individually was not a practical solution. I'm sure I could have written a bat fie, but I got it done pretty quickly in python:
import os, subprocess
processDir = 'C:\\Database-master\\'
files = os.listdir(processDir)
for f in files:
db = processDir + f
#potentially drop corrupt db and create new ones with f
scripts = os.listdir(db)
for script in scripts:
path = db + '\\' + script
proc = subprocess.Popen('sqlcmd -S 127.0.0.1 -i "' + path +'"', shell=True)
proc.wait()
If your database was large/complex the real problem you are going to encounter is not batch execution but the order in which scripts should be executed.
Unless you have some backup file this is going to be the real problem here.
If you only have your scripts then I'd suggest something like this.
tables
views
everything else...
Just execute one query after another until you get an error. When you do encounter an error this is most probably because you are trying to reference object that doesn't already exist. Just save that script for later and continue with executing scripts. Then start from the beginning again and go through scripts that caused an error. Now objects are probably there. Repeat this as many times as needed until you create all your objects.

How do I import a sql data file into SQL Server?

I have a .sql file and I am trying to import it into SQL Server 2008. What is the proper way to do this?
If your file is a large file, 50MB+, then I recommend you use sqlcmd, the command line utility that comes bundled with SQL Server. It is easy to use and it handles large files well. I tried it yesterday with a 22GB file using the following command:
sqlcmd -S SERVERNAME\INSTANCE_NAME -i C:\path\mysqlfile.sql -o C:\path\output_file.txt
The command above assumes that your server name is SERVERNAME, that you SQL Server installation uses the instance name INSTANCE_NAME, and that windows auth is the default auth method. After execution output.txt will contain something like the following:
...
(1 rows affected)
Processed 100 total records
(1 rows affected)
Processed 200 total records
(1 rows affected)
Processed 300 total records
...
use readfileonline.com if you need to see the contents of huge files.
UPDATE
This link provides more command line options and details such as username and password:
https://dba.stackexchange.com/questions/44101/importing-sql-server-database-from-a-sql-file
If you are talking about an actual database (an mdf file) you would Attach it
.sql files are typically run using SQL Server Management Studio. They are basically saved SQL statements, so could be anything. You don't "import" them. More precisely, you "execute" them. Even though the script may indeed insert data.
Also, to expand on Jamie F's answer, don't run a SQL file against your database unless you know what it is doing. SQL scripts can be as dangerous as unchecked exe's
Start SQL Server Management Studio
Connect to your database
File > Open > File and pick your file
Execute it
Try this process -
Open the Query Analyzer
Start --> Programs --> MS SQL Server --> Query Analyzer
Once opened, connect to the database that you are wish running the script on.
Next, open the SQL file using File --> Open option. Select .sql file.
Once it is open, you can execute the file by pressing F5.
In order to import your .sql try the following steps
Start SQL Server Management Studio
Connect to your Database
Open the Query Editor
Drag and Drop your .sql File into the editor
Execute the import
A .sql file is a set of commands that can be executed against the SQL server.
Sometimes the .sql file will specify the database, other times you may need to specify this.
You should talk to your DBA or whoever is responsible for maintaining your databases. They will probably want to give the file a quick look. .sql files can do a lot of harm, even inadvertantly.
See the other answers if you want to plunge ahead.
Get the names of the server and database in SSMS:
Run the following command in PowerShell or CMD:
sqlcmd -S "[SERVER NAME]" -d [DATABASE NAME] -i .\[SCRIPT].sql
Here is a screenshot of what it might look like:
There is no such thing as importing in MS SQL. I understand what you mean. It is so simple. Whenever you get/have a something.SQL file, you should just double click and it will directly open in your MS SQL Studio.