Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Chapter 5: Creating and Altering Tables The ALTER Statement OK, so now we have a database and a couple of nice tables — isn’t life grand? If only things always stayed the same, but they don’t. Sometimes (actually, far more often than we would like), we get requests to change a table rather than re-create it. Likewise, we may need to change the size, file locations, or some other feature of our database. That’s where our ALTER statement comes in. Much like the CREATE statement, our ALTER statement pretty much always starts out the same: ALTER This is totally boring so far, but it won’t stay that way. We’ll see the beginnings of issues with this statement right away, and things will get really interesting (read: convoluted and confusing!) when we deal with this even further in our next chapter (when we deal with constraints). ALTER DATABASE 136 Let’s get right into it by taking a look at changing our database. We’ll actually make a couple of changes just so we can see the effects of different things and how their syntax can vary. Perhaps the biggest trick with the ALTER statement is to remember what you already have. With that in mind, let’s take a look again at what we already have: EXEC sp_helpdb Accounting Notice that I didn’t put the quotation marks in this time as I did when we used this stored proc earlier. That’s because this system procedure, like many of them, accepts a special data type called sysname. As long as what you pass in is a name of a valid object in the system, the quotes are optional for this data type. So, the results should be just like they were when we created the database: Name db_ size Owner dbid Created Status Compatibility_ level Accounting 15.00 MB sa 9 May 28 2000 Status=ONLINE, Updateability=READ_WRITE, UserAccess=MULTI_USER, Recovery=FULL, Version=598, Collation=SQL_Latin1_ General_CP1_CI_AS, SQLSort-Order=52, IsAutoCreateStatistics, IsAutoUpdate- Statistics, IsFullTextEnabled 90
And . . . Name Fileid Filename Filegroup Accounting 1 c:\ Program Files\ Microsoft SQL Server\ MSSQL10 .MSSQLSERVER \MSSQL\DATA \Accounting- Data.mdf AccountingLog 2 c:\ Program Files\ Microsoft SQL Server\ MSSQL10 .MSSQLSERVER \MSSQL\DATA \Accounting- Log.ldf Let’s say you want to change things a bit. For example, let’s say that we know that we are going to be doing a large import into our database. Currently, our database is only 15MB in size — that doesn’t hold much these days. Since we have Autogrow turned on, we could just start our import, and SQL Server would automatically enlarge the database 5MB at a time. Keep in mind, however, that it’s actually a fair amount of work to reallocate the size of the database. If we were inserting 100MB worth of data, then the server would have to deal with that reallocation at least 16 times (at 20MB, 25MB, 30MB, and so on). Since we know that we’re going to be getting up to 100MB of data, why not just do it in one shot? To do this, we would use the ALTER DATABASE command. The general syntax looks like this: Chapter 5: Creating and Altering Tables PRI- MARY Size Maxsize Growth Usage 10240 BK NULL 5120K B 51200KB 5120KB data only 25600KB 5120KB log only ALTER DATABASE ADD FILE ([NAME = ,] FILENAME = [, SIZE = ] [, MAXSIZE = < size in KB, MB, GB or TB >] [, FILEGROWTH = ]) [,...n] [ TO FILEGROUP filegroup_name] [, OFFLINE ] 137
- Page 124 and 125: Chapter 4: JOINs 86 Uh, oh — this
- Page 126 and 127: Chapter 4: JOINs How It Works Unlik
- Page 128 and 129: Chapter 4: JOINs If we were to look
- Page 130 and 131: Chapter 4: JOINs 92 What I’m tryi
- Page 132 and 133: Chapter 4: JOINs 16 Mountain-500 Si
- Page 134 and 135: Chapter 4: JOINs 96 Let’s use thi
- Page 136 and 137: Chapter 4: JOINs 98 vendor. We’re
- Page 138 and 139: Chapter 4: JOINs 100 Somehow, we lo
- Page 140 and 141: Chapter 4: JOINs Try It Out FULL JO
- Page 142 and 143: Chapter 4: JOINs Every time I teach
- Page 144 and 145: Chapter 4: JOINs JOIN Sales.Special
- Page 146 and 147: Chapter 4: JOINs Figure 4-1 As alwa
- Page 148 and 149: Chapter 4: JOINs 110 CREATE TABLE U
- Page 150 and 151: Chapter 4: JOINs There are two diff
- Page 152 and 153: Chapter 5: Creating and Altering Ta
- Page 154 and 155: Chapter 5: Creating and Altering Ta
- Page 156 and 157: Chapter 5: Creating and Altering Ta
- Page 158 and 159: Chapter 5: Creating and Altering Ta
- Page 160 and 161: Chapter 5: Creating and Altering Ta
- Page 162 and 163: Chapter 5: Creating and Altering Ta
- Page 164 and 165: Chapter 5: Creating and Altering Ta
- Page 166 and 167: Chapter 5: Creating and Altering Ta
- Page 168 and 169: Chapter 5: Creating and Altering Ta
- Page 170 and 171: Chapter 5: Creating and Altering Ta
- Page 172 and 173: Chapter 5: Creating and Altering Ta
- Page 176 and 177: Chapter 5: Creating and Altering Ta
- Page 178 and 179: Chapter 5: Creating and Altering Ta
- Page 180 and 181: Chapter 5: Creating and Altering Ta
- Page 182 and 183: Chapter 5: Creating and Altering Ta
- Page 184 and 185: Chapter 5: Creating and Altering Ta
- Page 186 and 187: Chapter 5: Creating and Altering Ta
- Page 188 and 189: Chapter 5: Creating and Altering Ta
- Page 190 and 191: Chapter 5: Creating and Altering Ta
- Page 192 and 193: Chapter 6: Constraints At a more sp
- Page 194 and 195: Chapter 6: Constraints We can then
- Page 196 and 197: Chapter 6: Constraints A table can
- Page 198 and 199: Chapter 6: Constraints FOREIGN KEY
- Page 200 and 201: Chapter 6: Constraints How It Works
- Page 202 and 203: Chapter 6: Constraints Cascading Ac
- Page 204 and 205: Chapter 6: Constraints 166 This is
- Page 206 and 207: Chapter 6: Constraints 168 FROM Ord
- Page 208 and 209: Chapter 6: Constraints 170 Let’s
- Page 210 and 211: Chapter 6: Constraints CHECK Constr
- Page 212 and 213: Chapter 6: Constraints Defining a D
- Page 214 and 215: Chapter 6: Constraints Ignoring Bad
- Page 216 and 217: Chapter 6: Constraints Try running
- Page 218 and 219: Chapter 6: Constraints Rules and De
- Page 220 and 221: Chapter 6: Constraints Dropping Rul
- Page 222 and 223: Chapter 6: Constraints 184 Restrict
And . . .<br />
Name Fileid Filename Filegroup<br />
Accounting 1 c:\<br />
Program<br />
Files\<br />
<strong>Microsoft</strong><br />
<strong>SQL</strong> <strong>Server</strong>\<br />
MS<strong>SQL</strong>10<br />
.MS<strong>SQL</strong>SERVER<br />
\MS<strong>SQL</strong>\DATA<br />
\Accounting-<br />
Data.mdf<br />
AccountingLog 2 c:\<br />
Program<br />
Files\<br />
<strong>Microsoft</strong><br />
<strong>SQL</strong> <strong>Server</strong>\<br />
MS<strong>SQL</strong>10<br />
.MS<strong>SQL</strong>SERVER<br />
\MS<strong>SQL</strong>\DATA<br />
\Accounting-<br />
Log.ldf<br />
Let’s say you want to change things a bit. For example, let’s say that we know that we are going to be<br />
doing a large import into our database. Currently, our database is only 15MB in size — that doesn’t hold<br />
much these days. Since we have Autogrow turned on, we could just start our import, and <strong>SQL</strong> <strong>Server</strong><br />
would automatically enlarge the database 5MB at a time. Keep in mind, however, that it’s actually a fair<br />
amount of work to reallocate the size of the database. If we were inserting 100MB worth of data, then the<br />
server would have to deal with that reallocation at least 16 times (at 20MB, 25MB, 30MB, and so on).<br />
Since we know that we’re going to be getting up to 100MB of data, why not just do it in one shot? To do<br />
this, we would use the ALTER DATABASE command.<br />
The general syntax looks like this:<br />
Chapter 5: Creating and Altering Tables<br />
PRI-<br />
MARY<br />
Size Maxsize Growth Usage<br />
10240<br />
BK<br />
NULL 5120K<br />
B<br />
51200KB 5120KB data<br />
only<br />
25600KB 5120KB log<br />
only<br />
ALTER DATABASE <br />
ADD FILE<br />
([NAME = ,]<br />
FILENAME = <br />
[, SIZE = ]<br />
[, MAXSIZE = < size in KB, MB, GB or TB >]<br />
[, FILEGROWTH = ]) [,...n]<br />
[ TO FILEGROUP filegroup_name]<br />
[, OFFLINE ]<br />
137