MS SQL, Why use master to create a database?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I have a short question, why do I use use master; to create a database?
Here is the example from the Microsoft documentation



USE master ;
GO
CREATE DATABASE Sales
ON
( NAME = Sales_dat,
FILENAME = 'C:Program Files...saledat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = Sales_log,
FILENAME = 'C:Program Files...salelog.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB ) ;









share|improve this question









New contributor




S Nell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    Welcome to the StackExchange site. The master database contains all of the system level information for SQL Server – all of the logins, linked servers, endpoints, and other system-wide configuration settings. The master database is also where SQL Server stores information about the other databases on this instance and the location of their files. If the master database is not present, SQL Server cannot start.
    – Md Haidar Ali Khan
    6 hours ago










  • Thanks both of you!
    – S Nell
    6 hours ago










  • @MdHaidarAliKhan Your Comment should be an Answer.
    – Basil Bourque
    14 mins ago










  • @Basilbourque, As per your recommendation I have replied my comments as an answer. For future reference to other professional.
    – Md Haidar Ali Khan
    3 mins ago
















up vote
1
down vote

favorite












I have a short question, why do I use use master; to create a database?
Here is the example from the Microsoft documentation



USE master ;
GO
CREATE DATABASE Sales
ON
( NAME = Sales_dat,
FILENAME = 'C:Program Files...saledat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = Sales_log,
FILENAME = 'C:Program Files...salelog.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB ) ;









share|improve this question









New contributor




S Nell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    Welcome to the StackExchange site. The master database contains all of the system level information for SQL Server – all of the logins, linked servers, endpoints, and other system-wide configuration settings. The master database is also where SQL Server stores information about the other databases on this instance and the location of their files. If the master database is not present, SQL Server cannot start.
    – Md Haidar Ali Khan
    6 hours ago










  • Thanks both of you!
    – S Nell
    6 hours ago










  • @MdHaidarAliKhan Your Comment should be an Answer.
    – Basil Bourque
    14 mins ago










  • @Basilbourque, As per your recommendation I have replied my comments as an answer. For future reference to other professional.
    – Md Haidar Ali Khan
    3 mins ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a short question, why do I use use master; to create a database?
Here is the example from the Microsoft documentation



USE master ;
GO
CREATE DATABASE Sales
ON
( NAME = Sales_dat,
FILENAME = 'C:Program Files...saledat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = Sales_log,
FILENAME = 'C:Program Files...salelog.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB ) ;









share|improve this question









New contributor




S Nell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have a short question, why do I use use master; to create a database?
Here is the example from the Microsoft documentation



USE master ;
GO
CREATE DATABASE Sales
ON
( NAME = Sales_dat,
FILENAME = 'C:Program Files...saledat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = Sales_log,
FILENAME = 'C:Program Files...salelog.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB ) ;






sql-server t-sql






share|improve this question









New contributor




S Nell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




S Nell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









Aaron Bertrand♦

147k19280473




147k19280473






New contributor




S Nell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 7 hours ago









S Nell

82




82




New contributor




S Nell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





S Nell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






S Nell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    Welcome to the StackExchange site. The master database contains all of the system level information for SQL Server – all of the logins, linked servers, endpoints, and other system-wide configuration settings. The master database is also where SQL Server stores information about the other databases on this instance and the location of their files. If the master database is not present, SQL Server cannot start.
    – Md Haidar Ali Khan
    6 hours ago










  • Thanks both of you!
    – S Nell
    6 hours ago










  • @MdHaidarAliKhan Your Comment should be an Answer.
    – Basil Bourque
    14 mins ago










  • @Basilbourque, As per your recommendation I have replied my comments as an answer. For future reference to other professional.
    – Md Haidar Ali Khan
    3 mins ago












  • 1




    Welcome to the StackExchange site. The master database contains all of the system level information for SQL Server – all of the logins, linked servers, endpoints, and other system-wide configuration settings. The master database is also where SQL Server stores information about the other databases on this instance and the location of their files. If the master database is not present, SQL Server cannot start.
    – Md Haidar Ali Khan
    6 hours ago










  • Thanks both of you!
    – S Nell
    6 hours ago










  • @MdHaidarAliKhan Your Comment should be an Answer.
    – Basil Bourque
    14 mins ago










  • @Basilbourque, As per your recommendation I have replied my comments as an answer. For future reference to other professional.
    – Md Haidar Ali Khan
    3 mins ago







1




1




Welcome to the StackExchange site. The master database contains all of the system level information for SQL Server – all of the logins, linked servers, endpoints, and other system-wide configuration settings. The master database is also where SQL Server stores information about the other databases on this instance and the location of their files. If the master database is not present, SQL Server cannot start.
– Md Haidar Ali Khan
6 hours ago




Welcome to the StackExchange site. The master database contains all of the system level information for SQL Server – all of the logins, linked servers, endpoints, and other system-wide configuration settings. The master database is also where SQL Server stores information about the other databases on this instance and the location of their files. If the master database is not present, SQL Server cannot start.
– Md Haidar Ali Khan
6 hours ago












Thanks both of you!
– S Nell
6 hours ago




Thanks both of you!
– S Nell
6 hours ago












@MdHaidarAliKhan Your Comment should be an Answer.
– Basil Bourque
14 mins ago




@MdHaidarAliKhan Your Comment should be an Answer.
– Basil Bourque
14 mins ago












@Basilbourque, As per your recommendation I have replied my comments as an answer. For future reference to other professional.
– Md Haidar Ali Khan
3 mins ago




@Basilbourque, As per your recommendation I have replied my comments as an answer. For future reference to other professional.
– Md Haidar Ali Khan
3 mins ago










4 Answers
4






active

oldest

votes

















up vote
3
down vote



accepted










I believe it's not a requirement that you should use the master database to create a database. Since the create database command should be run in a database context the documentation always uses a default database which is master and it's a system database which will be always there no matter what so the script doesn't fail!






share|improve this answer



























    up vote
    1
    down vote













    It is absolutely not a requirement in this very specific case, but it is a requirement in many other scenarios. If you're creating a database called Sales, and you arelady have a database called Sales, you'll need to change your database context before you:



    • Restore with replace; or,

    • Drop the current database and then:

      • Create from scratch; or,

      • Create for attach.


    There are plenty of other scenarios outside of database creation that also require either (a) not being in the context of the current database, or (b) being in the context of master specifically (or at least not a specific database), and many of these things you may be doing during or around creating databases:



    • Setting a database to a different state, like single_user

    • Preventing errors when a script has a USE command but that user database may be offline or otherwise inaccessible

    • Granting server-level permissions like CREATE DATABASE

    • Granting server-level role membership

    • Marking a module as a system object (sp_MS_marksystemobject) or as a startup procedure

    • Certain types of certificate, server audit, and Availability Group operations

    Probably a slew of other things. USE master; isn't always necessary, but sometimes it is, and it doesn't hurt to always execute server-level commands from that database.






    share|improve this answer




















    • Nice explaination with example.
      – Md Haidar Ali Khan
      5 mins ago

















    up vote
    0
    down vote













    I believe that we, a very very long time ago, had to be in master when executing the CREATE DATABASE command. I'm too lazy to fire up some 20 years old version to verify. So probably a combination of that and that it "feels natural" to be in master. Like if you are to create a folder (imagine there are just one level of folders), you probably feel "better" to say CD first (comparing the root here with master database).






    share|improve this answer



























      up vote
      0
      down vote













      The master database contains all of the system level information for SQL Server – all of the logins, linked servers, endpoints, and other system-wide configuration settings. The master database is also where SQL Server stores information about the other databases on this instance and the location of their files. If the master database is not present, SQL Server cannot start.





      share




















        Your Answer







        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "182"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );






        S Nell is a new contributor. Be nice, and check out our Code of Conduct.









         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f221191%2fms-sql-why-use-master-to-create-a-database%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        3
        down vote



        accepted










        I believe it's not a requirement that you should use the master database to create a database. Since the create database command should be run in a database context the documentation always uses a default database which is master and it's a system database which will be always there no matter what so the script doesn't fail!






        share|improve this answer
























          up vote
          3
          down vote



          accepted










          I believe it's not a requirement that you should use the master database to create a database. Since the create database command should be run in a database context the documentation always uses a default database which is master and it's a system database which will be always there no matter what so the script doesn't fail!






          share|improve this answer






















            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            I believe it's not a requirement that you should use the master database to create a database. Since the create database command should be run in a database context the documentation always uses a default database which is master and it's a system database which will be always there no matter what so the script doesn't fail!






            share|improve this answer












            I believe it's not a requirement that you should use the master database to create a database. Since the create database command should be run in a database context the documentation always uses a default database which is master and it's a system database which will be always there no matter what so the script doesn't fail!







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 6 hours ago









            Biju jose

            776620




            776620






















                up vote
                1
                down vote













                It is absolutely not a requirement in this very specific case, but it is a requirement in many other scenarios. If you're creating a database called Sales, and you arelady have a database called Sales, you'll need to change your database context before you:



                • Restore with replace; or,

                • Drop the current database and then:

                  • Create from scratch; or,

                  • Create for attach.


                There are plenty of other scenarios outside of database creation that also require either (a) not being in the context of the current database, or (b) being in the context of master specifically (or at least not a specific database), and many of these things you may be doing during or around creating databases:



                • Setting a database to a different state, like single_user

                • Preventing errors when a script has a USE command but that user database may be offline or otherwise inaccessible

                • Granting server-level permissions like CREATE DATABASE

                • Granting server-level role membership

                • Marking a module as a system object (sp_MS_marksystemobject) or as a startup procedure

                • Certain types of certificate, server audit, and Availability Group operations

                Probably a slew of other things. USE master; isn't always necessary, but sometimes it is, and it doesn't hurt to always execute server-level commands from that database.






                share|improve this answer




















                • Nice explaination with example.
                  – Md Haidar Ali Khan
                  5 mins ago














                up vote
                1
                down vote













                It is absolutely not a requirement in this very specific case, but it is a requirement in many other scenarios. If you're creating a database called Sales, and you arelady have a database called Sales, you'll need to change your database context before you:



                • Restore with replace; or,

                • Drop the current database and then:

                  • Create from scratch; or,

                  • Create for attach.


                There are plenty of other scenarios outside of database creation that also require either (a) not being in the context of the current database, or (b) being in the context of master specifically (or at least not a specific database), and many of these things you may be doing during or around creating databases:



                • Setting a database to a different state, like single_user

                • Preventing errors when a script has a USE command but that user database may be offline or otherwise inaccessible

                • Granting server-level permissions like CREATE DATABASE

                • Granting server-level role membership

                • Marking a module as a system object (sp_MS_marksystemobject) or as a startup procedure

                • Certain types of certificate, server audit, and Availability Group operations

                Probably a slew of other things. USE master; isn't always necessary, but sometimes it is, and it doesn't hurt to always execute server-level commands from that database.






                share|improve this answer




















                • Nice explaination with example.
                  – Md Haidar Ali Khan
                  5 mins ago












                up vote
                1
                down vote










                up vote
                1
                down vote









                It is absolutely not a requirement in this very specific case, but it is a requirement in many other scenarios. If you're creating a database called Sales, and you arelady have a database called Sales, you'll need to change your database context before you:



                • Restore with replace; or,

                • Drop the current database and then:

                  • Create from scratch; or,

                  • Create for attach.


                There are plenty of other scenarios outside of database creation that also require either (a) not being in the context of the current database, or (b) being in the context of master specifically (or at least not a specific database), and many of these things you may be doing during or around creating databases:



                • Setting a database to a different state, like single_user

                • Preventing errors when a script has a USE command but that user database may be offline or otherwise inaccessible

                • Granting server-level permissions like CREATE DATABASE

                • Granting server-level role membership

                • Marking a module as a system object (sp_MS_marksystemobject) or as a startup procedure

                • Certain types of certificate, server audit, and Availability Group operations

                Probably a slew of other things. USE master; isn't always necessary, but sometimes it is, and it doesn't hurt to always execute server-level commands from that database.






                share|improve this answer












                It is absolutely not a requirement in this very specific case, but it is a requirement in many other scenarios. If you're creating a database called Sales, and you arelady have a database called Sales, you'll need to change your database context before you:



                • Restore with replace; or,

                • Drop the current database and then:

                  • Create from scratch; or,

                  • Create for attach.


                There are plenty of other scenarios outside of database creation that also require either (a) not being in the context of the current database, or (b) being in the context of master specifically (or at least not a specific database), and many of these things you may be doing during or around creating databases:



                • Setting a database to a different state, like single_user

                • Preventing errors when a script has a USE command but that user database may be offline or otherwise inaccessible

                • Granting server-level permissions like CREATE DATABASE

                • Granting server-level role membership

                • Marking a module as a system object (sp_MS_marksystemobject) or as a startup procedure

                • Certain types of certificate, server audit, and Availability Group operations

                Probably a slew of other things. USE master; isn't always necessary, but sometimes it is, and it doesn't hurt to always execute server-level commands from that database.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                Aaron Bertrand♦

                147k19280473




                147k19280473











                • Nice explaination with example.
                  – Md Haidar Ali Khan
                  5 mins ago
















                • Nice explaination with example.
                  – Md Haidar Ali Khan
                  5 mins ago















                Nice explaination with example.
                – Md Haidar Ali Khan
                5 mins ago




                Nice explaination with example.
                – Md Haidar Ali Khan
                5 mins ago










                up vote
                0
                down vote













                I believe that we, a very very long time ago, had to be in master when executing the CREATE DATABASE command. I'm too lazy to fire up some 20 years old version to verify. So probably a combination of that and that it "feels natural" to be in master. Like if you are to create a folder (imagine there are just one level of folders), you probably feel "better" to say CD first (comparing the root here with master database).






                share|improve this answer
























                  up vote
                  0
                  down vote













                  I believe that we, a very very long time ago, had to be in master when executing the CREATE DATABASE command. I'm too lazy to fire up some 20 years old version to verify. So probably a combination of that and that it "feels natural" to be in master. Like if you are to create a folder (imagine there are just one level of folders), you probably feel "better" to say CD first (comparing the root here with master database).






                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    I believe that we, a very very long time ago, had to be in master when executing the CREATE DATABASE command. I'm too lazy to fire up some 20 years old version to verify. So probably a combination of that and that it "feels natural" to be in master. Like if you are to create a folder (imagine there are just one level of folders), you probably feel "better" to say CD first (comparing the root here with master database).






                    share|improve this answer












                    I believe that we, a very very long time ago, had to be in master when executing the CREATE DATABASE command. I'm too lazy to fire up some 20 years old version to verify. So probably a combination of that and that it "feels natural" to be in master. Like if you are to create a folder (imagine there are just one level of folders), you probably feel "better" to say CD first (comparing the root here with master database).







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 1 hour ago









                    Tibor Karaszi

                    1,1115




                    1,1115




















                        up vote
                        0
                        down vote













                        The master database contains all of the system level information for SQL Server – all of the logins, linked servers, endpoints, and other system-wide configuration settings. The master database is also where SQL Server stores information about the other databases on this instance and the location of their files. If the master database is not present, SQL Server cannot start.





                        share
























                          up vote
                          0
                          down vote













                          The master database contains all of the system level information for SQL Server – all of the logins, linked servers, endpoints, and other system-wide configuration settings. The master database is also where SQL Server stores information about the other databases on this instance and the location of their files. If the master database is not present, SQL Server cannot start.





                          share






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            The master database contains all of the system level information for SQL Server – all of the logins, linked servers, endpoints, and other system-wide configuration settings. The master database is also where SQL Server stores information about the other databases on this instance and the location of their files. If the master database is not present, SQL Server cannot start.





                            share












                            The master database contains all of the system level information for SQL Server – all of the logins, linked servers, endpoints, and other system-wide configuration settings. The master database is also where SQL Server stores information about the other databases on this instance and the location of their files. If the master database is not present, SQL Server cannot start.






                            share











                            share


                            share










                            answered 7 mins ago









                            Md Haidar Ali Khan

                            3,31252240




                            3,31252240




















                                S Nell is a new contributor. Be nice, and check out our Code of Conduct.









                                 

                                draft saved


                                draft discarded


















                                S Nell is a new contributor. Be nice, and check out our Code of Conduct.












                                S Nell is a new contributor. Be nice, and check out our Code of Conduct.











                                S Nell is a new contributor. Be nice, and check out our Code of Conduct.













                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f221191%2fms-sql-why-use-master-to-create-a-database%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                Confectionery