I'm confused by all the different names the same database is given. Take this code for example:
CREATE DATABASE DB_nameA
ON
( NAME = DB_nameB, FILENAME = 'C:\myDatabases\DB_nameC.mdf')
LOG ON
( NAME = DB_log, FILENAME = 'C:\myDatabases\DB_log.ldf' ) ;
GO
What's the difference between DB_nameA, DB_nameB, and DB_nameC? How does SQL Server keep track of which names are associated with each other? E.g. Somehow when I write "DROP DATABASE DB_nameA", SQL knows to delete the "DB_nameC.mdf" file.
DB_nameA is how I'd refer to a database in SQL (e.g. DROP DATABASE DB_nameA). And DB_nameC is the file name I'd see in Windows Explorer. But when is DB_nameB ever used or seen again?
CREATE DATABASE <logical db name>
ON
( NAME = <logical data filename>, FILENAME = <physical data file path>)
LOG ON
( NAME = <logical log filename>, FILENAME = <physical log file path>);
GO
DB_nameA is your Database Name
For each database, MDF and LDF files are required.
MDF - Master Database File(Primary data file)
LDF - Log Database File(Transaction File)
So whenever creating a database,we have to specify "mdf" and "ldf" names along with the respective file path. All the data and transaction log files will be stored in respective files.
ex :
CREATE DATABASE DB_nameA
ON
( NAME = DB_nameA_Data, FILENAME = 'C:\myDatabases\DB_nameA_Data.mdf')
LOG ON
( NAME = DB_nameA_log, FILENAME = 'C:\myDatabases\DB_nameA_log.ldf' ) ;
GO
Related
I just started learning SQL. I downloaded the mdf file and moved it to the SQL folder. The course instructor told us to enter these commands to display the finished database. It throws me the error that my file doesn't exist even though I entered the correct path name. What is the matter? Sorry if the question is too stupid I just started learning.
Here is some pictures of the problem.
Your .mdf file should be in this path:
FILENAME = N'C:\MSSQL\SQLData\<database name>.mdf'
To attach a database there are some ways to do it like :
The one i've showed you:
USE [master]
GO
create database <DatabaseName> ON
(name='LogicalName of the Data file', FileName='Data File Name'),
(name='LogicalName of the Log file', FileName='Log File Name')
FOR ATTACH;
Or this way:
USE [master]
GO
EXEC sp_attach_db #dbname = N'DatabaseName',
#filename1 = '<Location of the database file>',
#filename2 = '<Location of the Log file>';
I started to go through the first tutorial for how to load data into Snowflake from a local file.
This is what I have set up so far:
CREATE WAREHOUSE mywh;
CREATE DATABASE Mydb;
Use Database mydb;
CREATE ROLE ANALYST;
grant usage on database mydb to role sysadmin;
grant usage on database mydb to role analyst;
grant usage, create file format, create stage, create table on schema mydb.public to role analyst;
grant operate, usage on warehouse mywh to role analyst;
//tutorial 1 loading data
CREATE FILE FORMAT mycsvformat
TYPE = "CSV"
FIELD_DELIMITER= ','
SKIP_HEADER = 1;
CREATE FILE FORMAT myjsonformat
TYPE="JSON"
STRIP_OUTER_ARRAY = true;
//create stage
CREATE OR REPLACE STAGE my_stage
FILE_FORMAT = mycsvformat;
//Use snowsql for this and make sure that the role, db, and warehouse are seelcted: put file:///data/data.csv #my_stage;
// put file on stage
PUT file://contacts.csv #my
List #~;
list #%mytable;
Then in my active Snowsql when I run:
Put file:///Users/<user>/Documents/data/data.csv #my_table;
I have confirmed I am in the correct role Accountadmin:
002003 (02000): SQL compilation error:
Stage 'MYDB.PUBLIC.MY_TABLE' does not exist or not authorized.
So then I try to create the table in Snowsql and am successful:
create or replace table my_table(id varchar, link varchar, stuff string);
I still run into this error after I run:
Put file:///Users/<>/Documents/data/data.csv #my_table;
002003 (02000): SQL compilation error:
Stage 'MYDB.PUBLIC.MY_TABLE' does not exist or not authorized.
What is the difference between putting a file to a my_table and a my_stage in this scenario? Thanks for your help!
EDIT:
CREATE OR REPLACE TABLE myjsontable(json variant);
COPY INTO myjsontable
FROM #my_stage/random.json.gz
FILE_FORMAT = (TYPE= 'JSON')
ON_ERROR = 'skip_file';
CREATE OR REPLACE TABLE save_copy_errors AS SELECT * FROM TABLE(VALIDATE(myjsontable, JOB_ID=>'enterid'));
SELECT * FROM SAVE_COPY_ERRORS;
//error for random: Error parsing JSON: invalid character outside of a string: '\\'
//no error for generated
SELECT * FROM Myjsontable;
REMOVE #My_stage pattern = '.*.csv.gz';
REMOVE #My_stage pattern = '.*.json.gz';
//yay your are done!
The put command copies the file from your local drive to the stage. You should do the put to the stage, not that table.
put file:///Users/<>/Documents/data/data.csv #my_stage;
The copy command loads it from the stage.
But in document its mention like it gets created by default for every stage
Each table has a Snowflake stage allocated to it by default for storing files. This stage is a convenient option if your files need to be accessible to multiple users and only need to be copied into a single table.
Table stages have the following characteristics and limitations:
Table stages have the same name as the table; e.g. a table named mytable has a stage referenced as #%mytable
in this case without creating stage its should load into default Snowflake stage allocated
I have a database called IND_Master which I have backed up in a file named "IND_Master.bak".
I would like to restore this into another database called 'IND_test" so that they are identical. both the data and structure needs to be identical.
Can someone either give me the script or tell me how to do this from sql server..even if it means creating another IND_master and changing the name to IND_test.
The following script will restore your backup file to a new database called IND_test and rename the logical file names accordingly. Obviously you'll need to alter the paths though.
USE [master]
RESTORE DATABASE [IND_test]
FROM DISK = N'C:\SQL\Backups\IND_Master.bak' WITH FILE = 1,
MOVE N'IND_master' TO N'C:\SQL\Data\IND_test.mdf',
MOVE N'IND_master_log' TO N'C:\SQL\Logs\IND_test_log.ldf'
GO
ALTER DATABASE [IND_test]
MODIFY FILE (NAME = 'IND_master', NEWNAME = 'IND_test')
GO
ALTER DATABASE [IND_test]
MODIFY FILE (NAME = 'IND_master_log', NEWNAME = 'IND_test_log')
GO
Im making a program with visual basic 2010 and using sqlserver compact as database. I have two folders named "Year2015" and "Year2016". The folders are in the same location where the program is. Both folders has a database named "MyData.sdf" in themselves. Both of the "MyData.sdf" has the same tables etc. Im trying to do something like that:
When user select "Year2015", program starts to run with the data of "MyData.sdf" that is in the folder "Year2015" and when user select "Year2016", program starts to run with the data of "MyData.sdf" that is in the folder "Year2016". I mean that i want to change the datasource address programmaticly. Searched net for that. There are some explanations but no codes i could find. If this is a bad question sorry for that.
Dave Pinal is a genius at this stuff, and I happened to read his blog on this very topic:
ALTER DATABASE TestDB
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
-- Detach DB
EXEC MASTER.dbo.sp_detach_db #dbname = N'TestDB'
GO
-- Move MDF File from Loc1 to Loc 2
-- Re-Attached DB
CREATE DATABASE [TestDB] ON
( FILENAME = N'F:\loc2\TestDB.mdf' ),
( FILENAME = N'F:\loc2\TestDB_log.ldf' )
FOR ATTACH
GO
Note: even his comment sections are great, too!
*SOURCE SCRIPT CAME FROM PINAL.
http://blog.sqlauthority.com/2012/10/28/sql-server-move-database-files-mdf-and-ldf-to-another-location/
I finally create my own code for this issue. I want to share it for the people who uses VB2010 and SQL Server Compact and want to change the data source for the active form. The code is:
Dim sConnectionString As String
sConnectionString = "Data Source=" & My.Computer.FileSystem.CurrentDirectory & "\Year2015\MyData.sdf"
TableAdapterManager.Connection.ConnectionString = sConnectionString
This will change your data source of active form. The other forms continues to use the default source
I want to create a database from my local computer through a stored procedure it is not allowing to create the database .
I'm getting this error:
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "D:\SHA_dat\SHA.dat" failed with the operating system error 2(The system cannot find the file specified.).
Msg 1802, Level 16, State 1, Line 1
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
And my code is here:
SELECT #cSQL = 'CREATE DATABASE '+#cDBName+' ON ( NAME = '''+#cDBName+'_data'''+',
FILENAME = ' + quotename(#cDbPath)+ ') LOG ON ( NAME = '''+#cDBName+'_Log'',
FILENAME = ' + quotename(#cLogPath)+ ')'
select #cSQL
where #cDBName refers for the Datbase name to create and #cDBPath and #cLogPath refers to netork paths to create the .dat and .log files
Can anyone help me?
You write that #cDBPath and #cLogPath are network paths. This won't work. While the network path is available for you and you are submitting the CREATE DATABASE, the SQL Server service is processing the CREATE DATABASE. The service is running in it's own logon session with it's own user account and it does not have the same drives mapped as you have. So you cannot use a network drive as you do it.
BTW, the same applies when you want to recover from a backup. The backup needs to be available on a local drive so that the SQL Server service can access it.