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
10
down vote

favorite
1












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.

























    up vote
    10
    down vote

    favorite
    1












    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.





















      up vote
      10
      down vote

      favorite
      1









      up vote
      10
      down vote

      favorite
      1






      1





      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 ddl master-system-database






      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 30 mins ago









      Paul White♦

      47.7k14257407




      47.7k14257407






      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 yesterday









      S Nell

      534




      534




      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.




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          12
          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
            16
            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
















            • 1




              @aron Bertrand +1 for the specific cases. I have never gone this deep when to use master , the only instance is setting the database to single user mode :)
              – Biju jose
              10 hours ago

















            up vote
            2
            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
              1
              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|improve this answer




















                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%2fwhy-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
                12
                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
                  12
                  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
                    12
                    down vote



                    accepted







                    up vote
                    12
                    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 yesterday









                    Biju jose

                    866821




                    866821






















                        up vote
                        16
                        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
















                        • 1




                          @aron Bertrand +1 for the specific cases. I have never gone this deep when to use master , the only instance is setting the database to single user mode :)
                          – Biju jose
                          10 hours ago














                        up vote
                        16
                        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
















                        • 1




                          @aron Bertrand +1 for the specific cases. I have never gone this deep when to use master , the only instance is setting the database to single user mode :)
                          – Biju jose
                          10 hours ago












                        up vote
                        16
                        down vote










                        up vote
                        16
                        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 23 hours ago









                        Aaron Bertrand♦

                        147k19280475




                        147k19280475







                        • 1




                          @aron Bertrand +1 for the specific cases. I have never gone this deep when to use master , the only instance is setting the database to single user mode :)
                          – Biju jose
                          10 hours ago












                        • 1




                          @aron Bertrand +1 for the specific cases. I have never gone this deep when to use master , the only instance is setting the database to single user mode :)
                          – Biju jose
                          10 hours ago







                        1




                        1




                        @aron Bertrand +1 for the specific cases. I have never gone this deep when to use master , the only instance is setting the database to single user mode :)
                        – Biju jose
                        10 hours ago




                        @aron Bertrand +1 for the specific cases. I have never gone this deep when to use master , the only instance is setting the database to single user mode :)
                        – Biju jose
                        10 hours ago










                        up vote
                        2
                        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
                          2
                          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
                            2
                            down vote










                            up vote
                            2
                            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 23 hours ago









                            Tibor Karaszi

                            1,1415




                            1,1415




















                                up vote
                                1
                                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|improve this answer
























                                  up vote
                                  1
                                  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|improve this answer






















                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    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|improve this answer












                                    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|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered 21 hours ago









                                    Md Haidar Ali Khan

                                    3,33052240




                                    3,33052240




















                                        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%2fwhy-use-master-to-create-a-database%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Comments

                                        Popular posts from this blog

                                        What does second last employer means? [closed]

                                        List of Gilmore Girls characters

                                        Confectionery