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 not something to be taken lightly. Such an action is usually in the realm of the database administrator. As such, we will consider further discussion out of the scope of this book. ALTER TABLE 140 A far, far more common need is the situation where we need to change the makeup of our table. This can range from simple things like adding a new column to more complex issues such as changing a data type. Let’s start by taking a look at the basic syntax for changing a table: ALTER TABLE table_name {[ALTER COLUMN { []. [(precision [, scale])] max | [COLLATE ] [NULL|NOT NULL] |[{ADD|DROP} ROWGUIDCOL] | PERSISTED}] |ADD [[DEFAULT ] |[IDENTITY [(, ) [NOT FOR REPLICATION]]]] [ROWGUIDCOL] [COLLATE ] [NULL|NOT NULL] [] |[ AS ] |ADD [CONSTRAINT ] {[{PRIMARY KEY|UNIQUE} [CLUSTERED|NONCLUSTERED] {([ ,...n ])} [WITH FILLFACTOR = ] [ON { | DEFAULT}] ] |FOREIGN KEY [([ ,...n])] REFERENCES [([ ,...n])] [ON DELETE {CASCADE|NO ACTION}] [ON UPDATE {CASCADE|NO ACTION}] [NOT FOR REPLICATION] |DEFAULT [FOR ] |CHECK [NOT FOR REPLICATION] () [,...n][ ,...n] |[WITH CHECK|WITH NOCHECK] | { ENABLE | DISABLE } TRIGGER { ALL | [ ,...n ] } |DROP {[CONSTRAINT] |COLUMN }[ ,...n]
} |{CHECK|NOCHECK} CONSTRAINT {ALL|[ ,...n]} |{ENABLE|DISABLE} TRIGGER {ALL|[ ,...n]} | SWITCH [ PARTITION ] TO [ schema_name. ] target_table [ PARTITION ] As with the CREATE TABLE command, there’s quite a handful there to deal with. So let’s start an example of using this by looking back at the Employees table in the Accounting database: EXEC sp_help Employees Chapter 5: Creating and Altering Tables For the sake of saving a few trees, I’m going to edit the results that I show here to just the part we care about — you’ll actually see much more than this: Column_name Type Computed Length Prec Scale Nullable EmployeeID int no 4 10 0 no FirstName varchar no 25 no MiddleInitial char no 1 yes LastName varchar no 25 no Title varchar no 25 no SSN varchar no 11 no Salary money no 8 19 4 no PriorSalary money no 8 19 4 no LastRaise money yes 8 19 4 yes HireDate smalldatetime no 4 no TerminationDate smalldatetime no 4 yes ManagerEmpID int no 4 10 0 no Department varchar no 25 no 141
- 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 174 and 175: Chapter 5: Creating and Altering Ta
- Page 176 and 177: 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
- Page 225 and 226: 7 Adding More to Our Queries When I
- Page 227 and 228: Building a Nested Subquery A nested
Chapter 5: Creating and Altering Tables<br />
not something to be taken lightly. Such an action is usually in the realm of the database administrator. As<br />
such, we will consider further discussion out of the scope of this book.<br />
ALTER TABLE<br />
140<br />
A far, far more common need is the situation where we need to change the makeup of our table. This can<br />
range from simple things like adding a new column to more complex issues such as changing a data type.<br />
Let’s start by taking a look at the basic syntax for changing a table:<br />
ALTER TABLE table_name<br />
{[ALTER COLUMN <br />
{ []. [(precision [, scale])] max |<br />
<br />
[COLLATE ]<br />
[NULL|NOT NULL]<br />
|[{ADD|DROP} ROWGUIDCOL] | PERSISTED}]<br />
|ADD<br />
<br />
[[DEFAULT ]<br />
|[IDENTITY [(, ) [NOT FOR REPLICATION]]]]<br />
[ROWGUIDCOL]<br />
[COLLATE ]<br />
[NULL|NOT NULL]<br />
[]<br />
|[ AS ]<br />
|ADD<br />
[CONSTRAINT ]<br />
{[{PRIMARY KEY|UNIQUE}<br />
[CLUSTERED|NONCLUSTERED]<br />
{([ ,...n ])}<br />
[WITH FILLFACTOR = ]<br />
[ON { | DEFAULT}]<br />
]<br />
|FOREIGN KEY<br />
[([ ,...n])]<br />
REFERENCES [([ ,...n])]<br />
[ON DELETE {CASCADE|NO ACTION}]<br />
[ON UPDATE {CASCADE|NO ACTION}]<br />
[NOT FOR REPLICATION]<br />
|DEFAULT <br />
[FOR ]<br />
|CHECK [NOT FOR REPLICATION]<br />
()<br />
[,...n][ ,...n]<br />
|[WITH CHECK|WITH NOCHECK]<br />
| { ENABLE | DISABLE } TRIGGER<br />
{ ALL | [ ,...n ] }<br />
|DROP<br />
{[CONSTRAINT] <br />
|COLUMN }[ ,...n]