MS SQL, 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
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 ) ;
sql-server t-sql
New contributor
add a comment |Â
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 ) ;
sql-server t-sql
New contributor
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
add a comment |Â
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 ) ;
sql-server t-sql
New contributor
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
sql-server t-sql
New contributor
New contributor
edited 1 hour ago
Aaron Bertrandâ¦
147k19280473
147k19280473
New contributor
asked 7 hours ago
S Nell
82
82
New contributor
New contributor
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
add a comment |Â
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
add a comment |Â
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!
add a comment |Â
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.
Nice explaination with example.
â Md Haidar Ali Khan
5 mins ago
add a comment |Â
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).
add a comment |Â
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.
add a comment |Â
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!
add a comment |Â
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!
add a comment |Â
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!
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 6 hours ago
Biju jose
776620
776620
add a comment |Â
add a comment |Â
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.
Nice explaination with example.
â Md Haidar Ali Khan
5 mins ago
add a comment |Â
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.
Nice explaination with example.
â Md Haidar Ali Khan
5 mins ago
add a comment |Â
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.
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 1 hour ago
Aaron Bertrandâ¦
147k19280473
147k19280473
Nice explaination with example.
â Md Haidar Ali Khan
5 mins ago
add a comment |Â
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
add a comment |Â
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).
add a comment |Â
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).
add a comment |Â
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).
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 1 hour ago
Tibor Karaszi
1,1115
1,1115
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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 7 mins ago
Md Haidar Ali Khan
3,31252240
3,31252240
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%2fms-sql-why-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
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