1 UseQzone2 3 --select * from sysobjects where name= ' Q_user '4 if object_id('Q_user','u') is not NULL --obejct_id a method based on table sysobjects (object name, object type)5 Drop TableQ_user--Delete a table6 Create TableQ_user7 (8Uidint,--identity (10001,1),--PRIMARY KEY constraint9Ulognamenvarchar( -)Unique,--Unique constraint ck__q_user__upwd__1cf15040 (automatically generated constraint name is inconvenient for subsequent operations)TenUNamenvarchar( -), OneUpwdnvarchar( -)Check(Len(UPWD)<= -),--CHECK Constraints AUisonlinebit default(0),--Default Constraints -Uisfrezeebit, -Ufrezeetimenvarchar( -), theUismulloginbit - --PRIMARY Key - constraintPk_uidPrimary Key(uId)--You can specify a primary key name - --primary KEY (UId) + --primary KEY (ULOGNAME,UPWD) two together to form a primary key - --FOREIGN Key + --Foreign Key (Sno) references student (Sno) A --all constraints are similar to primary keys at ) - execsp_rename'Q_user','q_users' --(Modify table name) - execsp_rename'q_users','Q_user' - --can also be used to modify the specified - execsp_rename'Q_user.uismullogin','Upwds' - execsp_rename'Q_user.upwds','Uismullogin'--There's a constraint that doesn't seem to be renamed in - to Alter Table Book + AddBook_copy_novarchar(Ten), Publish_datesmalldatetime - the Alter TableQ_user * --add test nvarchar check (len (test) >6), SS Int--Add a column $ --Drop Column Test-when you delete a constrained column, you delete its subordinate columns (if this is done directly with visual action)Panax Notoginseng --ALTER column Uisonline INT--Modify the type to remove its subordinate columns first (if this is done directly with visual action) - --add constraint Df_uisonline default (0) for Uisonline the --add constraint ck_uisonline check (uisonline in (0,1)) + --add constraint Pk_stuno primary key (Stuno) A --add constraint uq_stuid unique (stuid) the --ALTER column uisonline int NOT null + DropCk_uisonline--Delete Constraint
SQL Server Getting Started with mastering----table operations