Technology of SQL Server database (46)
Tuesday, March 03, 2009 by rain
After the watch has been founded,The attribute such as the kind that wants pair of lists possibly according to need, tie undertakes additive, delete or revise,This modifies watch structure with respect to need.
7.4.1 is revised with Enterprise Manager
In the list that the choice in Enterprise Manager should have altering,Click right key,Choose from inside quick menu " DesignTable " option,The dialog box of modification watch structure that if place of graph 7-4 is shown,can appear.The attribute such as the data type that the line revises in the dialog box that can show in place of graph 7-4, name or add, delete a file,Also can appoint a watch advocate key word is restrained.The icon in clicking tool column,The dialog box of the attribute that the editor that if place of graph 7-5 is shown,appears is expressed and restrains.Can amid edits all sorts of ties and the attribute of a few watches.
7.4.2 is revised with command of ALTER TABLE
The file that command of ALTER TABLE can be added or deletes a watch, tie,Also can ban use or enable the tie that already existed
Or trigger.Its grammar is as follows:
ALTER TABLE Table
{[ALTER COLUMN Column_name
{New_data_type [(precision[, scale] ) ]
[COLLATE%26lt;Collation_name%26gt;Collation_name
[NULL | NOT NULL]
| {ADD | DROP} ROWGUIDCOL} ]
| ADD
{[
| Column_name AS Computed_column_expression
}[, . . .n]
| [WITH CHECK | WITH NOCHECK] ADD
{}[, . . .n]
| DROP
{[CONSTRAINT] Constraint_name
| COLUMN Column
}[, . . .n]
| {CHECK | NOCHECK} CONSTRAINT
{ALL | Constraint_name[, . . .n]}
| {ENABLE | DISABLE} TRIGGER
{ALL | Trigger_name[, . . .n]}
}
[[DEFAULT Constant_expression]
| [IDENTITY [(seed, increment) [NOT FOR REPLICATION] ] ]
Increment) [NOT FOR REPLICATION] ] ]
[ROWGUIDCOL]
[COLLATE%26lt;Collation_name%26gt;Collation_name
Collation_name
{[NULL | NOT NULL]
| [{PRIMARY KEY | UNIQUE}
[CLUSTERED | NONCLUSTERED]
[WITH FILLFACTOR = Fillfactor]
[ON {filegroup | DEFAULT} ] ] ]
| [[FOREIGN KEY]
REFERENCES Ref_table [(ref_column) ]
[ON DELETE {CASCADE | NO ACTION} ]
[ON UPDATE {CASCADE | NO ACTION} ]
[NOT FOR REPLICATION] ]
| CHECK [NOT FOR REPLICATION]
(Logical_expression)}
: : = [CONSTRAINT Constraint_name]
{[{PRIMARY KEY | UNIQUE}
[CLUSTERED | NONCLUSTERED]
{(Column [ASC | DESC] [, . . .n] ) }
[WITH FILLFACTOR = Fillfactor]
[ON {filegroup | DEFAULT} ] ]
| FOREIGN KEY
[(Column[, . . .n]) ]
REFERENCES Ref_table [(ref_column[, . . .n]) ]
[NOT FOR REPLICATION]
[ON DELETE {CASCADE | NO ACTION} ]
[ON UPDATE {CASCADE | NO ACTION} ]
| CHECK [NOT FOR REPLICATION]
(Search_conditions)}
Each parameter explains as follows:
Table
The name that designates the watch that should revise.If express,not be in current database or the watch does not belong to current user,must the database name that its belong to demonstrate and possessory name.ALTER COLUMNNew_data_type
Designate new data type name,Its use a standard to be as follows:
The raw data type of the row it is OK to answer changeover is new data type;
New data type cannot be TIMESTAMP;
New data type allows to label NULL to be worth;
If original list is IDENTITY,list,New data type should support IDENTITY character;
Setting of current SET ARITHABORT will be regarded as to be in ON condition.Precision
The digit that specifies new data type.Scale
The decimal digit that specifies new data type.NULL | NOT NULL
Whether does demonstrate row allow NULL to be worth.If add a row to arrive when the watch is medium,Appoint it for NOT NULL,The default that must appoint this to list is worth.After choosing this,New_data_type [(precision [, scale])] option must be appointed,Although Precision is mixed Scale option all changeless,Current data type also needs to point out come.WITH CHECK | WITH NOCHECK
Appoint already whether does the data in consist in watch need to use new those who add or the FOREIGN KEY that just enabled is restrained or CHECK tie comes test and verify.If be not appointed,WITH CHECK serves as new the default option that adds a tie,WITH NOCHECK regards the default that enables old tie as option.{ADD | DROP} ROWGUIDCOL
The ROWGUIDCOL attribute that add or deletes a file.ROWGUIDCOL attribute can appoint an UNIQUEIDENTIFIER to list only.ADD
The definition that adds row of one or more row, computation or watch tie.Computed_column_expression
The computational expression that computation lists.DROP {[CONSTRAINT] Constraint_name | COLUMN Column_name}
The name that designates the tie that should delete or row.The row that is in following case cannot be deleted;
Use at duplicate row;
The line that is used at index;
The line that is used at CHECK FOREIGN KEY UNIQUE or tie of PRIMARY KEY;
Defined default to restrain or tie the kind that decided a default to be worth an object;
Bind decided regulation (Rule) row.{CHECK | NOCHECK} CONSTRAINT
Enable or ban restrain with FOREIGN KEY or CHECK.ALL
Option of use NOCHECK is banned with all ties,Or option of use CHECK enables all ties.{ENABLE | DISABLE} TRIGGER
Enable or ban with trigger.ALL
Enable or ban be aimed at all trigger with option.Trigger_name
Designate trigger name.What other parameter tells with the place in establishing watch and tie is identical.
Exemple 7-13:Found news of an order trade to express,The data type that revises brief introduction line next.Create Table Order_firm (Order_firm_id Char (8) Primary Key, Firm_name Varchar (50) Not NullFirm_introduce Char(50) Null
) On [primary]Alter Table Order_firmAlter Column Firm_introduce Varchar(250) Null
Exemple 7-14:Found an indent watch to insert number of an order trade to list again.Create Table Orders(Order_id Char(8) , P_id Char(8) Foreign Key References Products(p_id) , Order_quantity Smallint Check (order_quantity%26gt;=10) , Constraint Pk_order_id Primary Key (order_id) ,
) On [primary]Alter Table OrdersAdd Order_firm_id Char(8) NullConstraint Fk_order_firm_id Foreign Key References Order_firm(order_firm_id)
Exemple 7-15:Change on the examination in the exemple is restrained,The key word outside deleting is restrained.Alter Table OrdersAdd Constraint Chk_order_quantity Check (order_quantity%26gt;=100)Drop Constraint Chk_order_quantity
7.4.3 revises watch name and row name with memory process Sp_rename
The name that Sp_rename stores the process can revise target of the user in current database,Wait like process of watch, row, index, memory.Its grammar is as follows:Sp_rename [@objname =] 'object_name' ,
[@newname =] 'new_name'
[, [@objtype =] 'object_type' ]
Among them [@objtype =] 'object_type' is the kind that wants incognito boy or girl friend,Its value can think ' COLUMN ' , ' DATABASE ' , ' INDEX ' , ' USERDATATYPE ' , ' OBJECT ' .Value ' OBJECT ' point to era all objects in systematic watch Sysobjects,Wait like process of watch, view, memory, trigger, regular, tie.' OBJECT ' the value is worth for acquiesce.
Exemple 7-16:The name of row P_id that changes Orders table is Products_idExec Sp_rename 'orders. [P_id]' , 'product_id' , 'column'
Moving result is as follows:
Caution: Changing Any Part Of An Object Name Could Break Scripts And Stored Procedures.
The Column Was Renamed To 'product_id' .
Exemple 7-17:The name that changes Orders table is P_orders.Exec Sp_rename 'orders' , 'p_orders''
Moving result is as follows:
Caution: Changing Any Part Of An Object Name Could Break Scripts And Stored Procedures.
The Object Was Renamed To 'p_orders' .