Database Fundamentals #4: Create a Database
These are the fundamentals of creating a database. Long-term, it's best to always focus on how to automate your processes because you will regularly repeat them.
Join the DZone community and get the full member experience.Join For Free
sql server provides multiple ways to perform most functions. in order to maximize your understanding of how sql server works and have as many different mechanisms as possible for getting work done, you’ll use the gui and tsql to learn how to create and drop databases. you can then use whichever mechanism works best for you at the moment.
using each method, we’ll first create a database using the least amount of work possible, just so you can see how easy it is to create a database. we’ll go over how to remove databases from the system, getting rid of the database you just created. from there we’ll drill down to create another database, exploring some of the different mechanisms you can use to change how databases get created. then, we’ll clean up behind ourselves and remove all those databases, too. performing the actions repeatedly will help you to understand what you’re doing better and increase your retention of the information.
creating a database with the gui
connect to your server using ssms . in the object explorer window, right-click on the databases folder (if it’s not visible, you may have to click on the plus sign next to the server to expand the list for that server). this will open up a context menu. right at the top will be a menu choice for new database… . clicking that will open the new database window as shown below:
creating your first database is very simple. type a value into the
text box. try “myfirstdb.” as you type, you’ll see that the
down in the
section will be filled in as you type. the data file will have a logical name that is the same as the database name. the log file will have a logical name that has
appended to it. you can leave these as they are for the moment. to create a database, nothing else needs to be done. your screen should look similar to this:
click the ok button and a database will be created using the name you gave it and the automatic file location defined by default. the new database window will close and the database you just created will be shown at the bottom of the list in the object explorer window. normally, that window is sorted alphabetically. if you click on the refresh button at the top of the object explorer , the list of databases will resort and your database will appear in its appropriate location.
you can click on the plus signs in the object explorer window now and see that you have a database, but one without any objects under than system objects. databases are created by copying a system database named “model.” if you want an object, such as a table or a stored procedure, to always appear in any database you create, you can add that object to the model database.
to remove the database, right-click the database name in the object explorer window. this will bring up a context menu. near the bottom of the menu will be a menu selection labeled delete . clicking that will open the delete object window just like in figure 2.4:
there are options that you can manipulate with this window, but it’s unlikely you’ll need to unless other people are connected to the database currently. clicking the ok button will remove the database from your system.
let’s create another database, but this time, manipulate the file settings to see how that works.
manipulating files with the gui
creating a database using just the defaults is fine in many situations, but what if you need to deviate from the defaults for some reason? for example, you might have one drive on the system where you’re putting most of your databases, but you have a special, very large drive, that you need to map a new database to because you expect it to grow in size. to make this type of manipulation with the gui, you would start the process exactly the same way. connect to the server, if you’re not already, then navigate to the databases folder in the object explorer window. right-click on that folder and select new database… from the menu choice. this will open the new database window that you’ve already seen.
type in a database name, “myseconddb.” now move the cursor down to the grid under database files . click inside the text box under the logical name column. you can modify this. you would do that if you had a particular standard for naming files you wanted to maintain. click the initial size column. here, you can adjust the size of the database up or down. since i don’t know your system at all, let’s move the initial size up a very small amount and create a 4mb data file. you can either highlight the value and type a replacement or use the scroll buttons to change this value. you would make changes to this, usually to larger numbers, not smaller, when you had a database that you knew needed more space when you created it. this way, you don’t have to rely on the autogrow settings. speaking of autogrow settings, let’s modify those.
click on the box under the autogrowth/max size column. you’ll see an ellipsis on the right side of the box. if you click on that ellipsis, you’ll open a new window that controls autogrowth. it will look like this:
most of this window should be self-explanatory. the checkbox at the top, enable autogrowth , is for turning autogrowth on or off for this file. that’s important to remember; autogrowth is set on each file. this is so you can pick and choose which files get autogrowth and which don’t. you can change the autogrowth to use a percentage or grow by a fixed amount. unless your databases are extremely small, i would not recommend letting it grow by 1mb at a time. it depends on the size of your databases, but generally, this number should be a lot higher. finally, the most important setting is maximum file size . as you can see above, the default value is “unlimited.” this means that with this setting, sql server will grow to until it fills the drive, just on this file alone. i strongly recommend that if you do enable autogrowth, you put limits on the files. this won’t prevent a drive from being filled, but it does make it less likely. once you’re done making adjustments to the autogrowth settings, click ok to close this window and save the changes.
if you scroll all the way to the right in the database files window, the last column is the physical file name of the file that will be stored on your windows file system. this is usually named by default from the logical name , but you can control it directly by typing into this box. i’ve shown an example here:
finally, you have the capability to add new files to the database. this is necessary so that you can create multiple storage locations for your data. sql server will automatically distribute the data across these locations, or you can directly manipulate what gets stored on a given file when you create the database objects. all of this is additional management capabilities that enable your flexibility in making sure that you can store information in the most optimal fashion for your database. most databases you create are just going to have the two files though.
before proceeding, go ahead and delete this database the same way you did before. now let’s try creating a database using tsql.
creating a database with tsql
tsql is the scripting language for sql server. as such, it’s your primary means of automating sql server. if you want to create a database and you need to create that database on more than one server or you want to recreate it on one server, you’re not going to want to use the gui to click through all sorts of different settings. it will be much easier to type the appropriate information and then save the file. you can re-run it anytime you need to.
the basic approach for creating a database using tsql is actually very simple. the language is largely self-explanatory for simple operations like this:
create database myfirsttdb;
if you click on the button that is labeled new query… , which you can see in the upper left of the image below, a query window will open inside ssms. you can type this code or copy and paste it into the window. when you’re ready to execute the code, click on the button that says execute and has a red exclamation point next to it, again, visible below. this will execute the query and you’ll have a new database.
you’ll also see the screen change. a new section of a window will open up below your tsql code. this is your introduction to the results and messages pane. when you run a query in ssms, by default, the results and any messages from that query appear in a separate pane in the query window along with tabs to separate the two. assuming you typed everything correctly, you should see something like this:
it’s a good thing that you see this message because if you look over at the object explorer , you won’t see your new database. you need to click on the databases folder in the object explorer and then click on the refresh button at the top of that window. you’ll then see your database listed.
since you provided no other information, sql server used the defaults, including the default file size and location for building your database. it’s that easy. removing the database from the system is just as easy. type this command:
drop database myfirsttdb;
before you click the
button, you need to know something about how the query window works. depending on how you followed the directions you’ve been given, you may have two different statements: one for
and one for
. if you click on the
button now, sql server will attempt to execute both statements. since there is already a database there called “myfirsttdb”, you’ll get an error. you need to be careful about how you execute your code from within the tsql window. if you have two separate commands, the best thing to do is use the mouse to select the statement you wish to execute. your tsql window would then look like this:
statement selected, you can now click on the
button, or, if your fingers are already on the keyboard, just type
, which will also cause the query to execute. you’ll once again see that the statement has completed successfully. the
window will not reflect the change, so you’ll want to refresh that at some point.
you’ve now used tsql to create and drop a database. as you can see, it can be very simple. now we’re going to use tsql to manipulate the files in the same way we did using the gui. this will not be quite as simple, but we’ll walk through the process so that it’s completely pain-free.
manipulating files with tsql
using tsql to manipulate the files is not difficult, but it does require quite a lot more syntax than if you simply rely on the defaults to do everything for you. there are actually a very large number of parameters and settings that you can use when running the
database script. they give you all sorts of control that you may need for creating particular databases, but most of the time, they’re not needed. going through every possible permutation of the command is way beyond our scope. for more information always remember that you can refer to the
to manipulate the file sizes, autogrowth settings, logical names, and file names as you did previously through the gui, this script should suffice:
create database mysecondtdb on primary (name = n'mysecondtdb', filename = n'c:\data\mysecondtdbdata.mdf', size = 4mb , filegrowth = 1mb, maxsize = 10mb) log on (name = n'mysecondtdb_log', filename = n'c:\data\mysecondtdblog.ldf', size = 1mb , filegrowth = 10%);
let me point out several things in this script. first, and most importantly, the script could be all on one line. but that makes it very difficult to read. instead, it’s broken down into individual sections that logically group better together. even those sections are broke down with line breaks. for most commands in tsql, this type of rearranging for aesthetic purposes is fine, and actively encouraged by anyone who has to look at scripts after you’re gone. the second thing is that i’ve changed the default location of my databases, not to illustrate a point, but just so that there is less useless information in the script. you will either need to create a directory on your system on the c:\ drive as i have done, or you’ll need to modify the path for these scripts to fit your system.
the basic command is exactly as you would expect from the previous script. the very next line begins to deviate. by default, you get a filegroup. a filegroup is a further mechanism for managing your files and placement of objects on those files. it’s not that important at the moment to understand these and they will be covered in more detail in a later blog post. suffice it to say that i had to define one and tell sql server which files were going into it as part of creating the database when i’m not using the defaults. the first of these is the
filegroup. you can see the same thing back in the images earlier of the gui.
defining the file is logically straightforward. i’m giving a
, which is the logical name. note the letter a
directly in front of the value. this is because the text for the definition of objects within sql server is unicode. unicode is used so that you can create objects using different character sets for different languages. the text values are differentiated from objects within the code by placing them inside of single quote marks. you can see that in the
and also in the
values. placing the a
directly in front makes that value into a unicode value.
setting the database size is very straightforward. in fact, it’s actually quite a bit easier than using the gui since you can define the values so that it’s clear you want an mb or a gb instead of having to rely on just creating numbers in the thousands to achieve larger databases. setting autogrowth is also very straightforward, but uses the property
instead of autogrow. you’ll note that i’ve left the defaults in place in this case so that you can see how to set to a value or a percentage. i also added the
to limit the amount of growth allowed for this database to 10mb.
that’s all that’s needed to directly manipulate the database files. if you ran that script on your system, please delete that database by either running a
command or deleting it from the
these are the fundamentals of creating a database. the better method to learn and understand is the method using tsql, however, the gui is there and you can use it. you’ll be better-served long-term to focus always on how to automate your processes because you will regularly repeat them.
Published at DZone with permission of Grant Fritchey, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.