Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

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

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]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!