Why use master to create a database?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
10
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 ) ;
sql-server t-sql ddl master-system-database
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.
add a comment |Â
up vote
10
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 ) ;
sql-server t-sql ddl master-system-database
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.
add a comment |Â
up vote
10
down vote
favorite
up vote
10
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 ) ;
sql-server t-sql ddl master-system-database
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
sql-server t-sql ddl master-system-database
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.
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.
add a comment |Â
add a comment |Â
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!
add a comment |Â
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.
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
add a comment |Â
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).
add a comment |Â
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.
add a comment |Â
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!
add a comment |Â
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!
add a comment |Â
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!
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!
answered yesterday
Biju jose
866821
866821
add a comment |Â
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
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).
add a comment |Â
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).
add a comment |Â
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).
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).
answered 23 hours ago
Tibor Karaszi
1,1415
1,1415
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered 21 hours ago


Md Haidar Ali Khan
3,33052240
3,33052240
add a comment |Â
add a comment |Â
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.
S Nell is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password