Creating
Database
Objects with
SQL Server
I
n this c hapter I’m go ing to sho w yo u ho w to use SQL Server 7’s Enterprise Manager utility to c reate vario us o bjec ts in yo ur database. These o bjec ts inc lude databases, tables, indexes, database diagrams, and sto red pro c edures. Yo u c an also use Enterprise Manager to manage sec urity by c reating lo gins, map them to users and assign sec urity to the vario us database o bjec ts.Introducing Enterprise M anager
Enterprise Manager is the primary utility that yo u use to man-age an SQL Server database system. It has the ability to c reate databases and database o bjec ts, c o ntro l the database’s sec u-rity, and perfo rm ro utine o peratio nal ac tivities like database bac kups and reo rganizatio ns. It c an c o ntro l the pro perties o f the database server itself, as well as start and sto p the server fro m a remo te lo c atio n. In sho rt, it is a very po werful utility that is used by several different types o f users to administer an SQL Server database.Enterprise M anager fundamentals
Enterprise Manager is a Mic ro so ft Management Co nso le (MMC) applic atio n, meaning that it shares its lo o k and feel with many o ther system management to o ls pro -vided by Mic ro so ft (see Figure 24-1). To start Enterprise Manager, c ho o se Start➪
Pro grams➪Mic ro so ft SQL Server 7.0➪Enterprise Manager. Like many to o ls avail-able fro m Mic ro so ft, mo st o f its o peratio ns c an be perfo rmed by using a wizard o r by manually c o nfiguring pro perty windo ws.
Figure 24-1: View ing the Enterprise Manager w indow Tree view Details w indow
Ac tion menu
View menu
Tools menu
✦Tre e vie wc o ntains a hierarc hic al list o f the database servers, databases, and c lasses o f database o bjec ts that c an be viewed o r c hanged.
✦De tails windowpro vides additio nal info rmatio n fo r the selec ted ic o n in the tree view.
✦Action me nuc o ntains a list o f menu items that c an be perfo rmed based o n the c o ntext.
✦Vie w me nuc o ntro ls ho w the info rmatio n will be displayed using MMC. Typic ally, all o f the items will be selec ted inc luding Co nso le Tree, Desc riptio n Bar, Status Bar, and the to o lbars. Yo u c an also c ho o se to display ho w the info rmatio n is displayed in the details windo w using the Large, Small, Details, and List fo rmats.
Enterprise Manager is c apable o f managing several SQL Server database servers ac ro ss multiple remo te c o mputers. These c o mputers are listed under the SQL Server Gro up ic o n in the tree view. Ho wever, befo re yo u c an ac c ess a remo te server, yo u must register the server with Enterprise Manager.
Duh:In order to run Enterprise Manager on your com puter, you first have to install it. If you are using the sam e com puter on w hich you are running the database server, Enterprise Manager is already installed. If you are using a different com -puter, you can either choose to install the desktop version of SQL Server or just the SQL Server utilities from the server version.
Registering a server is very easy. Just fo llo w these steps:
1.Right c lic k o n the SQL Server Gro up ic o n in the tree view and right c lic k to display the po p-up menu. Cho o se New SQL Server Registratio n. Yo u c an also c ho o se Ac tio n➪New SQL Server Registratio n. After c ho o sing either o ptio n, the Register SQL Server Wizard will be displayed.
2.After pressing Next, the wizard will display a list o f available database servers (see Figure 24-2). Selec t o ne and press the Add butto n. If the server yo u want to use isn’t listed, type the name in the text bo x at the to p o f the list and press Add. Then press Next to c o ntinue.
Figure 24-2: Choosing the database server to add
3.In the next step, yo u have a c ho ic e o f whic h authentic atio n system to use (see Figure 24-3). Sinc e yo u are already using a Windo ws c o mputer (it do esn’t mat-ter whether it’s Windo ws 98 o r Windo ws 2000/ NT), yo u sho uld pro bably use Windo ws NT ac c o unt info rmatio n. Ho wever, yo u must use a lo gin that is already defined o n the server. If yo u c ho o se SQL Server authentic atio n, the wizard will then pro mpt yo u fo r ho w yo u want to lo g in to the server. Yo u c an supply yo ur lo gin info rmatio n, whic h Enterprise Manager will use eac h time it starts, o r yo u c an c ho o se to be pro mpted fo r the info rmatio n.
4.After yo u finish spec ifying ho w Enterprise Manager sho uld lo g in to the database server, yo u c an c ho o se where the ic o n fo r the server will be sto red (see Figure 24-4). By default, it will be sto red under the SQL Server Gro up, but yo u c an c ho o se ano ther gro up o r c reate a new gro up.
Figure 24-4: Placing the icon for the database server in the tree view
5.When yo u reac h the end o f the wizard, yo u will see a list o f database servers yo u want to register. Pressing Finish will start a pro c ess to c o nnec t to eac h o f the database servers yo u spec ified (see Figure 24-5). This pro c ess will repo rt any erro rs that are enc o untered. When the pro c ess is finished, press Clo se to finish the wizard and return to Enterprise Manager.
Refer to Chapter 23 for a discussion on Window s NT Authentication vs. SQL Server Authentication.
Viewing database servers and their objects
Onc e yo u have registered yo ur database server, yo u c an expand the ic o n tree to sho w the database servers yo u c an ac c ess and the server-level o bjec ts available (see Figure 24-6).
✦Database sc o ntains the c o llec tio n o f databases o n the server yo u c an ac c ess. Yo u c an c reate, bro wse, edit, mo dify and delete tables, indexes, sto red pro c e-dures, users, ro les, and o ther database o bjec ts.
✦Data Transformationc o ntains info rmatio n abo ut data transfo rmatio n pac k-ages and the data repo sito ry. Yo u c an view and mo dify data transfo rmatio n jo bs, view and edit metadata, and searc h fo r info rmatio n in the repo sito ry.
✦Database sc o ntains the c o llec tio n o f databases o n the server yo u c an ac c ess. Yo u c an c reate, bro wse, edit, mo dify and delete tables, indexes, sto red pro c e-dures, users, ro les, and o ther database o bjec ts.
Figure 24-5: Registering the database servers
✦Data Transformationc o ntains info rmatio n abo ut data transfo rmatio n pac k-ages and the data repo sito ry. Yo u c an view and mo dify data transfo rmatio n jo bs, view and edit metadata, and searc h fo r info rmatio n in the repo sito ry.
✦Manage me ntc o ntains info rmatio n abo ut o peratio nal ac tivities perfo rmed by the database server. This inc ludes things suc h as database bac kups, lo g files, maintenanc e jo bs, and SQL Server Agent ac tivities.
✦Se curityc o ntains info rmatio n abo ut lo gin and server ro les, plus info rmatio n abo ut ho w to ac c ess remo te database servers and linked database servers.
✦Support Se rvice sc o ntains info rmatio n abo ut the Distributed Transac tio n Co o rdinato r, the Full Text Searc h feature, and SQL Mail.
Expanding the ic o n asso c iated with a database reveals the o bjec ts inside that c an be managed by Enterprise Manager (see Figure 24-7). These o bjec ts exist o nly in the spec ified database, and no t at the database server level. In o rder to ac c ess the database, yo ur lo gin must be mapped to a user in the database.
Figure 24-7: Brow sing the objects in a database
✦Diagrams c o ntains a set o f database diagrams sho wing ho w the tables are related to eac h o ther.
✦Vie wsc o ntains the c o llec tio n o f views in yo ur database.
✦Rule sis maintained fo r bac kwards c o mpatibility with o lder versio ns o f SQL Server. They have been superc eded by Che ckc o nstraints, whic h are main-tained in the Design Table windo w.
✦De faultsc o ntains the c o llec tio n o f default values that may be referenc ed by name o r asso c iated with a partic ular c o lumn in a table.
✦Use r De fine d Data Type sc o ntains the set o f user defined data types that may be used as the type fo r a partic ular c o lumn.
✦Full-Te xt Catalogsc o ntains the definitio ns asso c iated fo r the full-text c ata-lo gs, whic h ho ld the set o f full-text indexes in the database.
Browsing data
When yo u selec t a partic ular table o r view, yo u c an c ho o se to bro wse its c o ntents by right c lic king o n its ic o n and c ho o sing Open Table o r Open View fro m the po p-up menu. Yo u c an c ho o se to return all ro ws in the table o r spec ify the number o f ro ws yo u wish to see starting fro m the to p o f the table. In either c ase, yo u’ll see a display similar to that sho wn in Figure 24-8.
The info rmatio n in the table is displayed using a grid, where eac h ro w in the grid c o rrespo nds to a ro w in the table, and eac h c o lumn c o rrespo nds to a c o lumn in the table. Yo u c an sc ro ll thro ugh the values using the sc ro ll bars and adjust the widths o f eac h c o lumn to best display the info rmatio n. The c urrent ro w will be indic ated with an arro w in the ro w prefix area, while the name o f eac h c o lumn will be display in the c o lumn prefix area.
In additio n to viewing the data fro m the table, yo u c an also mo dify the data, assum-ing that yo u have the pro per sec urity permissio ns. To c hange a value in an existassum-ing ro w, simply edit the value in the c ell. When yo u mo ve to ano ther ro w, the data in the table will be updated.
Figure 24-8: Brow sing the contents of a table
Other o peratio ns, inc luding delete, may be perfo rmed o n a ro w by right c lic king o n the ro w and c ho o sing o ne o f the func tio ns listed in the po p-up menu. No te yo u c an use the po p-up menu to perfo rm the same func tio n o n multiple ro ws by c lic king o n the ro w prefix area, ho lding the left mo use butto n do wn, and dragging the c urso r to mark the ro ws as selec ted, then right c lic king o n the selec ted ro ws to display the po p-up menu.
A short term lock:When you brow se row s in your table or view, you m ay be hold-ing locks in the database. If there has been no activity for a w hile, the Enterprise Manager w ill ask you if you w ant to continue w orking w ith the results pane. If you respond No, or don’t respond w ithin a m inute, Enterprise Manager w ill close the pane, and any uncom m itted changes you have m ade w ill be discarded. You can refresh the data by right clicking anyw here on the pane and choosing Run from the pop-up m enu.
There’s more than meets the eye:Open Table and Open View functions exist on top of a table-oriented query facility. You can m odify the underlying query to change the row s returned either by changing the SQL Selectstatem ent directly, or by using the grid and diagram graphical tools. You can view the panes that contain this inform ation by right clicking on the results pane and selecting the panes you w ish to view under the Select Panes pop-up m enu item .
Databases and Tables
Databases exist in SQL Server to ho ld tables, and tables exist in the database to ho ld yo ur data. Creating them with Enterprise Manager’s wizards makes the jo b muc h easier than in earlier versio ns o f SQL Server.
Creating a database
To c reate a new database, yo u must use a lo gin that is assigned to either the sysad-min o r dbc reater ro les. Typic ally, yo u’ll c reate yo ur database using the sa, lo gin, but depending o n ho w yo ur database sec urity is o rganized, yo u may use ano ther lo gin.
Before you create a database
When yo u c reate a database, yo u sho uld have the fo llo wing info rmatio n ready:
✦The name o f the database server where yo u want to c reate the database.
✦The name o f the database itself. While yo u c an use nearly any c o mbinatio n o f c harac ters in the name, I suggest beginning the name with a letter, fo llo wed by any c o mbinatio n o f letters, numbers, and spec ial c harac ters suc h as @, $, #, and _ up to a maximum o f 123 c harac ters. If yo u do n’t fo llo w this suggestio n, yo u may have to surro und the name o f the database with quo tes o r square brac kets to ensure that the name is pro perly understo o d.
✦The lo c atio n o f the database files. This is a referenc e to a direc to ry so me-where o n the database server me-where the files c o ntaining the o bjec ts inside the database will be held.
✦The lo c atio n o f the lo g files. This is a referenc e to a direc to ry so mewhere o n the database server where the file c o ntaining the info rmatio n used to ho ld c hanges made to the database is kept.
✦The initial size o f the database files and ho w they sho uld gro w o ver time. Yo u sho uld c ho o se initial values that are suffic ient to c o ver the spac e yo u need in the beginning, and a gro wth metho d that allo ws yo ur database o ver time.
✦The initial size o f the lo g files and ho w they sho uld gro w o ver time. The initial size determines ho w muc h spac e is o riginally allo c ated fo r the lo g, and the gro wth metho d determines ho w the lo g sho uld be inc reased in c ase the server fills it up.
Creating your database
To c reate a database using Enterprise Manager, fo llo w these steps:
Figure 24-9: Choosing the Create Database w izard
2.When the wizard starts, press Next to go to the first step (see Figure 24-10). In this step, yo u’ll have to pro vide a name fo r the database, plus the lo c atio n o f the files that ho ld the database.
3.In the next step o f the wizard, yo u’ll be pro mpted to enter the name o f the file o r files yo u want to use, and their initial size (see Figure 24-11). By default, the wizard will c reate o ne file with the name o f the database, fo llo wed by _Data and an initial size o f o ne megabyte.
Figure 24-11: Specifying the files to hold your database
4.After spec ifying the file o r files that will ho ld the database, yo u’ll be pro mpted to c ho o se ho w the database server sho uld gro w yo ur files (see Figure 24-12). Yo u c an c ho o se to gro w yo ur database file, in fixed sized c hunks o r by adding a fixed perc entage o f the c urrently allo c ated spac e. Yo u c an also plac e a maxi-mum file size to prevent unlimited file gro wth.
Limits are good:When allocating space for a database file, you should place a lim it on the file size. Unless you carefully m onitor the am ount of space your database uses, and periodically reorganize and com press the files, a ten-m egabyte database m ay end up using ten gigabytes of disk space.
5.The wizard will repeat steps three and fo ur, but fo r the transac tio n lo g files. The default file name is the name o f the database fo llo wed by _Log.
6.In the last step o f the wizard, all o f the info rmatio n c o llec ted will be displayed. If yo u want to c hange any o f these parameters, press Bac k Otherwise, pressing Finish will begin the pro c ess to c reate the database. Bec ause SQL Server physi-c ally writes binary zero s o ver every byte o f spaphysi-c e allo physi-c ated fo r the database files and lo gs, the ac tual c reatio n pro c ess may take a while if yo u are building a big database. A message bo x will be displayed letting yo u kno w if the database was suc c essfully c reated. Press No to return to Enterprise Manager.
Figure 24-12: Choosing file lim its
Covering your butt: If you don’t have a m aintenance plan for your database server that covers your new database, I strongly recom m end that you create one w hen the Create Database Wizard finishes. See the SQL Server 7 for Dum m ies by Anthony T. Mann for details on how to create a m aintenance plan.
Property pages:While this m ay be obvious, it’s w orth m entioning anyw ay. All of the inform ation entered in the Create Database Wizard can also be entered using a database property page by right clicking on the Databases icon beneath the database server icon and choosing New Database from the pop-up m enu. Once the database has been created, you can review and change this inform ation by right clicking on the database and selecting Properties from the pop-up m enu.
Creating a table
A database is pretty useless witho ut any tables, so after c reating yo ur database, yo u’ll pro bably want to c reate so me tables. While the Enterprise Manager do esn’t inc lude a wizard to help yo u c reate a table, the Design Table windo w is very easy to use. Tip
Before you create your table
Befo re yo u c reate yo ur table, yo u sho uld have a list o f c o lumns that the table will c o ntain, alo ng with values fo r the fo llo wing attributes fo r eac h c o lumn:
✦The name o f eac h c o lumn. The name sho uld begin with a letter and c an be fo l-lo wed by any c o mbinatio n o f letters, numbers, and spec ial c harac ters suc h as @, $, #, and _ up to a to tal length o f 128 c harac ters. While spac es and o ther spec ial c harac ters may be used, yo u may have to surro und the c o lumn name with quo tes o r square brac kets when c o ding SQL statements.
✦The data type fo r eac h c o lumn. The data type sho uld be o ne o f the data types listed in Chapter 23. Depending o n the exac t data type, yo u may have to adjust values fo r Length, Prec isio n, and Sc ale.
✦Whether the c o lumn will ac c ept Nullvalues. No te that Nullvalues sho uld no t be used in any c o lumn that is used as part o f a primary o r fo reign key.
Database Performance
Believe it or not, having a faster CPU w ill not necessarily m ake your database server faster. A database server is very I/ O intensive. Anything that allow s your database to retrieve data faster from the disk w ill help the server’s perform ance.
Before looking at hardw are solutions to im prove your database server’s perform ance, you should ensure that the database server isn’t com peting w ith other processes on the server for m em ory and disk I/ O. Dedicating a com puter for the database server is highly recom -m ended. If you can’t dedicate a co-m puter, you should at least try to place your database files on dedicated disk drives.
Adding m em ory to your server allow s the database server to cache m ore data in m em ory; this is the biggest change you can m ake to im prove database perform ance. After all, retriev-ing data from m em ory is m uch faster than retrievretriev-ing it from disk. This is w hy Microsoft has gone to the trouble of m aking special versions of Window s NT to support m ore than the standard 2 gigabytes of m ain m em ory and to create the Data Center version of Window s 2000 w hich can support even m ore m ain m em ory.
After adding m em ory to your system , using SCSI disk drives is the next m ost im portant im provem ent. They not only allow you to m anage up to 15 disk drives on a single card, but they also support concurrent operations on each drive. Thus, you can have m ultiple disk drives perform ing seeks, w hile others are transferring data. SCSI-III can transfer data faster than SCSI-II or SCSI-I and should be used for best perform ance.
✦The default value fo r the c o lumn. This is an o ptio nal attribute that allo ws yo u to spec ify a default value rather than assigning a value o f Nullwhen adding a new ro w to the table.
✦Whether to make the c o lumn an identity c o lumn. An identity c o lumn c o ntains a value that c an uniquely identify a ro w in the table. Only o ne c o lumn per table may be an identity c o lumn. This value is an integer value, whic h inc reases with eac h new ro w. It c an be used with Int, De cimal(4,0)and any o ther data type that represents a who le number. Yo u c an spec ify the initial value in Identity Seed, and the inc rement added after eac h value is used under Identity Inc rement.
✦Whether to make the c o lumn a ro w GUID c o lumn. A ro w GUID c o lumn is similar to an identity c o lumn, but rather than sto ring an integer value, a GUID value is sto red instead. As with identity c o lumns, o nly o ne ro w GUID c o lumn is permit-ted per table. Typic ally, yo u wo uld use a ro w GUID c o lumn o nly if yo u were planning to implement database replic atio n.
Creating your table
Fo llo w these steps to c reate a table:
1.Expand the ic o n tree to reveal the Tables ic o n in the database where yo u want to build the table. Then right c lic k o n the Tables ic o n and selec t New Table fro m the po p-up menu. This will display the Design Table windo w, plus a message bo x that pro mpts yo u fo r the name o f the table (see Figure 24-13).
2.Enter the name o f yo ur table and press OK.
3.Fill o ut the grid by entering the info rmatio n fo r eac h c o lumn in yo ur table as a ro w in the fo rm. Yo u must enter values fo r Co lumn Name and Datatype. Selec ting a data type will auto matic ally fill in values fo r the Length, Prec isio n, and Sc ale c o lumns. If the data type is a variable length data type suc h as Charo r Decimal, then yo u sho uld adjust these values as appro priate.
4.Insert additio nal c o lumns if yo u need them.
Figure 24-13: Creating a new table
M odifying your table
Onc e yo u’ve finished c reating yo ur table, yo u c an always go bac k and mo dify it. Simply right c lic k o n the Tables ic o n under the appro priate database ic o n and c ho o se Design Table (see Figure 24-14). The same windo w yo u used to c reate the table will be displayed c o ntaining yo ur table’s c urrent definitio n. Other o ptio ns o n the same po p-up menu will allo w yo u to rename and delete the table. Use these o ptio ns with c are bec ause if yo ur table partic ipates in a fo reign key o r referenc ed by a sto red pro c edure, the name c hange may no t be pro pagated.
Don’t do it:Do not change or delete any of the system tables or you w ill corrupt your database. If you do, the only w ay to recover your database w ill be to com -pletely restore it from a database backup.
As lo ng as yo ur table is empty, yo u c an make any c hanges yo u want to the table’s definitio n. Ho wever, this isn’t true if the table c o ntains data. If yo u add a c o lumn to a table with data, the c o lumn’s value will be set to Nullunless yo u define a default value fo r the c o lumn, in whic h c ase the default value will be used. If yo u delete a c o lumn, all o f the data sto red in the c o lumn will be lo st. Ho wever, yo u will be pre-vented fro m deleting the c o lumn if it is part o f an index, used in a c o nstraint, part o f a De faultdefinitio n, bo und to a rule, o r used as part o f a full-text index.
Figure 24-14: Selecting a table
Yo u c an c hange c ertain attributes o f a c o lumn, suc h as making a c o lumn that previ-o usly rejec ted Nullsto ac c ept Nullvalues, witho ut impac ting the data already in the table. Ho wever, mo st c hanges will fo rc e Enterprise Manager to pro c ess eac h ro w physic ally and c ause the table to apply the c hange. If yo u switc h data types, Enterprise Manager will attempt to c o nvert the value fro m the o ld data type to the new data type. Likewise, if yo u c hange the prec isio n o r length, Enterprise Manager will also have to perfo rm the appro priate c o nversio n.
Safety first:Before you m ake a change to a table, m ake sure that you have a good backup copy of your database before you begin. No m atter how careful you are you can alw ays recover your database to the point before you m ade your first change.
A better way to reformat a table:If you m ust change the data type for a colum n, and you’re not com fortable w ith how Enterprise Manager w ill perform the func-tion, consider using the Data Transform ation Services. It allow s you to code a VBScript m acro to handle the actual conversion process.
See Chapter 23 for m ore inform ation about DTS.
Indexes and Diagrams
No t o nly do database diagrams pro vide a lo gic al view o f yo ur database design, they pro vide yo u with the c apability to design yo ur database interac tively. Mo st o f the func tio ns to c reate and mo dify o bjec ts that are available to yo u in Enterprise Manager are also available in the database diagram fac ility. This means that yo u do n’t have to learn yet ano ther set o f database design to o ls.
Creating an index
After c reating yo ur tables, the next step is to add indexes to yo ur tables. Witho ut indexes, SQL Server will have to sc an every rec o rd in the table to find the ro ws yo u spec ify in a query. With an index, SQL Server c an go direc tly to the ro ws yo u spec -ify, assuming o f c o urse that yo ur query to o k advantage o f the index.
There are two main ways to c reate an index. The first is to c reate the index manually by using the Create Index wizard, while the sec o nd is to run the Index Tuning wizard that examines a typic al set o f queries to determine the o ptimal set o f indexes.
Running the Create Index wizard
To run the Create Index wizard, fo llo w these steps:
1.Expand the ic o n tree to expo se the database where yo u want to add the index. Then selec t Wizards fro m the To o ls menu. Cho o se Database➪Create Index Wizard to start the Create Index wizard.
2.Press Next to mo ve fro m the intro duc tio n sc reen to the first step in the wiz-ard, whic h asks yo u to selec t the database and table to be indexed (see Figure 24-15). By default, the c urrently selec ted database and the first user table in the database will be displayed.
3.In the next step o f the wizard, all o f the indexes asso c iated with the table will be listed, alo ng with the c o lumns indexed (see Figure 24-16), No te that the pri-mary key sho uld always have an index and any fo reign key c o nstraints will also have an index. Yo u sho uld make sure that yo u do n’t duplic ate an index that already exists.
Figure 24-16: View ing the existing indexes
4.After viewing the existing indexes, yo u need to c ho o se the c o lumn o r c o lumns that will make up yo ur new index by plac ing a c hec k mark in the appro priate bo x (see Figure 24-17). No te that so me c o lumns may have data types that c an’t be indexed, in whic h c ase a big red X will be displayed in plac e o f the c hec k bo x.
5.In the next to last step o f the wizard, yo u c an spec ify so me o ptio ns fo r the index (see Figure 24-18). By selec ting Make this a c lustered index, yo u c an instruc t SQL Server to keep the ro ws in the table in the same physic al o rder as the index. This c an impro ve perfo rmanc e dramatic ally when yo u retrieve a set o f rec o rds that have a c o mmo n key value. Only o ne c lustered index is per-mitted per table ho wever. Yo u c an ensure that o nly o ne ro w in the table c an have a partic ular value in the key by selec ting Make this a unique index. Yo u c an also influenc e the perfo rmanc e o f the index by spec ifying ho w muc h spac e, o r fill, SQL Server sho uld leave in the individual index pages fo r adding new index values. Unless yo u really understand ho w this wo rks, yo u sho uld c ho o se Optimal Fill Fac to r.
Figure 24-18: Specifying index options
6.In the last step o f the wizard, yo u assign the index a name and adjust the o rder o f the c o lumns inc luded in the table. When yo u’re finished, press Finish to c reate the index. Sinc e the index is c reated in real-time, yo u may have to wait a few minutes fo r this pro c ess to finish, depending o n the size o f yo ur table and the speed o f yo ur database server. A message bo x will be displayed when the index has been c reated.
M anaging your indexes
Yo u c an get a list o f the indexes yo u c reated fo r a partic ular table by right c lic king o n a table in the Details windo w and c ho o sing All Tasks➪Manage Indexes fro m the po p-up menu. The Manage Indexes dialo g bo x will be displayed, as sho wn in Figure 24-19. This windo w will display the set o f the user c reated indexes. Using the but-to ns at the bo tbut-to m o f the dialo g bo x, yo u c an c reate a new index, edit, o r delete an to o few indexes, yo u will waste time trying to retrieve ro ws fro m yo ur table, while to o many indexes inc reases the wo rk needed to insert a new ro w.
The Index Tuning Wizard will analyze trac e data c o llec ted by the SQL Server Pro filer o r a list o f spec ific queries that yo u spec ify. It also takes into c o nsideratio n the c urrent set o f indexes. Then, based o n its analysis, the Index Tuning Wizard will make a list o f rec o mmendatio ns that yo u c an apply immediately o r save to a disk file to be applied later with Query Analyzer.
To c reate the wo rklo ad file, enter the SQL Queries yo u wish to o ptimize into a no r-mal text file using No tepad. Make sure yo u save the file with a file type o f .SQL.
To c apture trac e info rmatio n, run the SQL Server Pro filer utility and fo llo w these steps:
2.Enter a name fo r the trac e in the Trac e Name field and selec t the name o f the database server where the trac e info rmatio n will be generated o n the General tab. Then spec ify the name o f a file o r database table where the trac e info rma-tio n will be sto red.
3.On the Events tab, add the TSQL event to the list o f selec ted events and press OK to start the trac e.
Tracing can be hazardous to your database server’s performance: Running a trace increases the am ount of w ork for your database server to perform , and it m ay adversely affect your server’s perform ance.
To analyze trac e data o r analyze a few spec ific queries, fo llo w these steps:
1.In Enterprise Manager, selec t Wizards fro m the To o ls menu, then selec t the Management➪Index Tuning Wizard and press OK. After the initial sc reen o f the wizard is displayed, press Next to begin the tuning pro c ess.
2.On the Selec t Server and Database step o f the wizard (see Figure 24-20), selec t the name o f the database server and database yo u wish to analyze. Also , yo u need to c ho o se whether yo u want to keep yo ur existing indexes. If yo u’re o nly analyzing a few pro blem queries, yo u sho uld keep yo ur c urrent indexes, but if yo u are analyzing a large vo lume o f trac e data, then yo u may want to replac e yo ur c urrent indexes. Cho o se Perfo rm tho ro ugh analysis if yo u want to c ho o se the o ptimal set o f indexes. No te that this o ptio n will signific antly inc rease the amo unt o f time required to run the analysis.
Figure 24-20: Select the database server and database you w ant to analyze
Figure 24-21: Specify the location of your w orkload
4.Yo u c an c ho o se the tables yo u wish to tune in the next step o f the wizard ( see Figure 24-23) . Unless yo u are wo rried abo ut the amo unt o f time to run the analysis o r want to fo c us o n a subset o f the tables, yo u sho uld selec t all o f the tables in the database.
Figure 24-22: Selecting the tables to tune
Figure 24-23: Review ing the index recom m endations
6.In the next to last step o f the wizard, yo u c an c ho o se to apply the rec o mmen-datio ns immediately, sc hedule them to be applied as a batc h jo b, o r save the rec o mmendatio ns as a sc ript file that yo u c an apply with Query Analyzer (see Figure 24-24). After making yo ur c ho ic e, press Next to display the final step o f the wizard, and Finish to c o mplete the pro c ess.
Creating a database diagram
Ano ther useful database o bjec t yo u c an c reate with Enterprise Manager is a database diagram. A database diagram c o ntains a lo gic al view o f yo ur database similar to an Entity/Relatio nship diagram. Yo u c an add new tables direc tly in the database diagram, as well as mo dify existing tables. Yo u c an also c reate relatio nships between tables by dragging and dro pping c o lumn referenc es. It is a po werful to o l that makes it easy to design yo ur database by allo wing yo u to visualize yo ur database and the relatio nships between its tables.
Refer to Chapter 3 for a discussion of Entity/ Relationship diagram s.
Creating your database diagram
To c reate a database diagram, fo llo w these steps:
1.Expand the ic o n tree to selec t the database server and database where yo u want to c reate the database diagram. Right c lic k o n the Diagrams ic o n and c ho o se New Database Diagram fro m the po p-up menu. This will c reate a blank database diagram and start the Create Database Diagram Wizard. Press Next to begin c reating yo ur database diagram.
2.In the Selec t Tables to be Added step, yo u c an c ho o se whic h tables in the database will be inc luded in yo ur database diagram (see Figure 24-25). So metimes it’s useful to have a diagram that inc ludes o nly a small subset o f the tables in the database that fo c us o n a partic ular func tio n. Chec king the Add related tables butto n, and then adding the table yo u want to fo c us o n, will auto matic ally add all o f the tables that have a relatio nship to the o riginal table. As tables are added to the Tables to add to diagram list, they will be remo ved fro m the Available tables list.
3.In the last step o f the wizard, yo u see the list o f tables yo u selec ted. Press Finish to add the tables to the diagram and auto matic ally arrange them. Behind the wizard, yo u’ll see yo ur tables appear and then be arranged. When the diagram is c o mplete, a message bo x will appear and yo u’ll be left with a brand new database diagram to edit and save (see Figure 24-26).
Using the database diagram
Onc e yo ur database diagram is c o mplete, yo u sho uld no tic e that eac h table is simi-lar to the fo rm yo u used to design a table disc ussed in the Creating a Table sec tio n earlier in this c hapter. That’s bec ause it is the same, but o nly the first c o lumn fro m the fo rm is sho wn in the diagram. Yo u c hange the c o lumns displayed by right c lic k-ing o n the table and selec tk-ing o ne o f the fo llo wk-ing views: Co lumn Pro perties, Co lumn Names, Keys, Name Only, and Custo m View fro m the po p-up menu. To c ho o se the info rmatio n displayed in Custo m View, c ho o se Mo dify Custo m View.
Figure 24-25: Selecting the tables for the database diagram
The sec o nd thing yo u sho uld no tic e abo ut the diagram are the lines drawn between the tables. These lines represent fo reign key c o nstraints. To define a fo reign key c o n-straint, c lic k o n the prefix area in fro nt o f a c o lumn in o ne table, drag it to the o ther table, and dro p it. A dialo g bo x will be displayed that sho ws bo th the primary and fo reign key tables and the c o lumns that fo rm the fo reign key (see Figure 24-27). Yo u c an then c ho o se the c o lumns in eac h table that reflec t the c o lumns that c o mprise the fo reign key.
Figure 24-27: Creating a foreign key relationship
One nic e thing abo ut database diagrams is that yo u c an print them. When pro gram-ming, I usually have a printed c o py o f the database diagram handy to refer to table and c o lumn names and to identify relatio nships. Sinc e a database diagram c an get rather c o mplic ated, and may no t fit o n a single sheet o f paper, yo u c an zo o m the diagram to c reate the best fit.
Yo u c an also drag the tables aro und o n the diagram to arrange them the way yo u want. Yo u sho uld no tic e that the fo reign key c o nstraints will auto matic ally fo llo w the table. If yo u do n’t like where the c o nstraints are plac ed, yo u c an simply drag them aro und until yo u are happy with them.
Figure 24-28: Saving the changes to the database diagram
M anaging Security
Managing sec urity is bo th easy and hard at the same time. Understanding the c o m-mands yo u use to apply sec urity is easy. Making sure that yo u do n’t have any ho les in yo ur sec urity is diffic ult.
Creating a login
As yo u might expec t, Enterprise Manager inc ludes wizards fo r c reating lo gins. The Create Lo gin Wizard no t o nly c reates a lo gin id, but it also lets yo u pic k database server ro les and c reate a user with the same name in o ne o r mo re databases.
To c reate a lo gin, fo llo w these steps:
1.Cho o se To o ls➪Wizards and then selec t Database➪Create Lo gin Wizard. Press OK to start the wizard. After skipping o ver the intro duc tio n to the wiz-ard by pressing Next, yo u’ll be pro mpted to c ho o se the authentic atio n mo de fo r the user (see Figure 24-29). Selec t the desired authentic atio n metho d and press Next.
Figure 24-29: Choosing an authentication m ode for your login
Figure 24-30: Entering inform ation for a Window s NT Authentication login
Figure 24-31: Entering inform ation for an SQL Server Authentication login
4.No matter whic h authentic atio n metho d yo u c ho o se, the next step will ask yo u to c ho o se whic h server sec urity ro les sho uld be assigned to the lo gin (see Figure 24-32).
5.In the next to last step o f the wizard, yo u’ll be pro mpted to selec t the
databases in whic h the new lo gin sho uld be granted ac c ess (see Figure 24-33). Press Next to review the info rmatio n yo u entered and press Finish to c reate the lo gin. No te that a user with the same name as the lo gin will be c reated in eac h database yo u selec ted.
Figure 24-33: Grant your new login access to the databases on the server
What did I do?: You can see the list of logins on the database server by expanding the icon tree for the database server to show Logins, w hich is found under the Security icon. Selecting this icon w ill display all of the logins in the Details w indow. The list of users for a database can be displayed in the Details w indow by select-ing the Users icon under the appropriate database icon.
Granting permissions in a database
Just bec ause yo u have granted a user ac c ess to a database do esn’t mean that they have c o mplete ac c ess to yo ur database. By default, they will be assigned to the public ro le, whic h means that any reso urc es granted to the public will auto mati-c ally be granted to the new user. Yo u mati-c an review and mati-c hange their assigned ro les by expanding the ic o n tree to sho w the Users ic o n beneath the database yo u wish to manage. Then, by right c lic king and c ho o sing Pro perties fro m the po p-up menu, yo u c an spec ify whic h ro les they have been permitted (see Figure 24-34). On the General tab, yo u c an add new ro les by plac ing a c hec k mark next to the desired ro le o r revo ke a ro le by remo ving the c hec k mark. Yo u c an see the user’s spec ific sec u-rity permissio ns by pressing the Permissio ns butto n.
Figure 24-34: Review ing security roles in a database
By c lic king o n the List o nly o bjec ts with permissio ns fo r this user o n the Permissio ns tab, yo u c an quic kly determine whic h database o bjec ts, if any, this user has ac c ess to . The c hec k marks indic ate whic h c o mmands the users may issue against whic h database o bjec ts.
Yo u c an mo dify the permissio ns asso c iated with a ro le in the same way yo u mo dify them fo r a user. The o nly differenc e is that yo u have to selec t the ro le fro m the list o f ro les displayed in the Details windo w when yo u selec t the Ro les ic o n fo r the desired database in the tree view area o f Enterprise Manager.
A list o f users assigned to that partic ular ro le will be displayed in the Ro le Pro perties dialo g bo x (see Figure 24-36). Yo u c an then display the same detailed Sec urity Permissio ns dialo g bo x sho wn in Figure 24-35 by pressing the Permissio ns butto n. Yo u c an also get a list o f users assigned to the ro le by selec ting a ro le in the User Pro perties dialo g bo x and pressing the Pro perties butto n.
Too much security can cause problems: While you can m odify the perm issions for each individual user, you w ill be m uch better off assigning the user to a role and m odifying the perm issions for the role. When you m aintain security perm is-sions at the user level, it is very easy to m ake a m istake and grant the user per-m ission to soper-m ething they should be denied. While it is still possible to do this w ith a role, double checking the roles w ill be a m uch easier job than double checking each user.
Figure 24-35: Review ing the detailed security perm issions for a user
Summary
In this c hapter yo u learned:
✦abo ut the vario us features o f SQL Server Enterprise Manager.
✦ho w to bro wse data in yo ur database.
✦ho w to c reate a new table.
✦ho w to add an index to a table.
✦ho w to c reate a database diagram.
✦ho w to c reate a lo gin and assign sec urity permissio ns.
✦
✦
✦
Thoughts on Enterprise M anager
Enterprise Manager is a very pow erful tool that sim plifies the adm inistration of an SQL Server database. In this chapter, I’ve barely begun to com m unicate w hat you can do w ith Enterprise Manager. I strongly recom m end that you take the tim e to explore its capabilities, especially if you plan to do a lot of w ork w ith SQL Server.
If you look carefully, m any of the tools that are used in Enterprise Manager look like those available in the Enterprise Edition of Visual Basic. This is no coincidence. The sam e code base is used for both. I feel that Enterprise Manager does a better job of im plem enting the tools than Visual Basic, but it is nice to be able to use som e of the sam e tools w ith other databases also.