0

I'm studying a course about database management based on the Transact-SQL language. The first problem I founded was in my OS because I'm using a MAC. I had to create a SQL container in Docker and create a connection using Azure Data Studio, where I have my server, but my problem (where I'm blocked) is this:

when I have to create the FILENAME for this database I don't know where is it the directory which I have to save the .mdf and .ldf files because I'm running my server from a 'virtual' location which is not in my computer system.

How can I solve this?

Because I know nothing about containers, is the first time using Docker or similar...

I already tried to do it in a virtual machine running windows but is too much slow so I have discarded that option at the moment and I have to use SQL Server because is the language used in the course, I don't have any other option.

9
  • 3
    "When I have to create the FILENAME for this database I don't know where is it the directory which I have to save the .mdf and .ldf files " then why do you need to specify the filename/path if you don't know where you want them? Why not just omit it and the instance will use the default path? Commented Feb 12, 2023 at 11:44
  • 3
    You can run SELECT * FROM sys.database_files when connected to the system databases - such as master - to see where it stores the files for those databases but the minimal SQL needed to create a database is CREATE DATABASE Foo so you don't need to specify this Commented Feb 12, 2023 at 11:50
  • Who is "they"..? Commented Feb 12, 2023 at 20:06
  • Hi @Larnu I know it but the people who teach me demands me to specify name, filename, size, max_size and filegrowth. The only way that I was doing this kind of exercises was in an old windows laptop that I had. In that computer I can see the files ubication through the interface but is not the case in MAC where I run SQL in Azure Data Studio, not in SQL Server and files aren't inside the PC's memory but in a virtual containt and I don't know how can I have access to this datafiles. Commented Feb 12, 2023 at 20:07
  • Again, who is "they"? If "they" are telling you to specify the filepath, then presumably they are telling you what that path is, so what path did they give you and why doesn't that path work? Commented Feb 12, 2023 at 20:08

1 Answer 1

3

On a default Docker container (which you can check with EXEC master..sp_helpfile;), database files get created in:

/var/opt/mssql/data/

So you can say:

CREATE DATABASE floob 
ON 
(
  NAME     = N'floob_data', 
  FILENAME = N'/var/opt/mssql/data/floob.mdf'
)
LOG ON
(
  NAME     = N'floob_log',
  FILENAME = N'/var/opt/mssql/data/floob.ldf'
);

This is the bare minimum set of properties you need in order to dictate the location of the files; it will take other properties (like size/growth) from model.

But unless the course materials dictate that your CREATE DATABASE command absolutely must include FILENAME parameters, you can generally leave them out when building some skunkworks stuff on your own machine, as they will be placed in the default location that you don't really need to know (until that drive fills up).

In production that's generally not the best idea, though, because we often want to customize size, growth, separate data from log, etc.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much Aaron for your response. there is no way for opening this files on docker (I mean as whether it had an interface like windows directories), isn't you?
@molik why would you ever need to "open" an mdf or ldf file? SQL Server can see them, that's what's important.
@molik Though I do mention here how you can access the folders inside a Docker container from the command line: Attaching / restoring databases inside a container

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.