1 --============================== Data Sheet Management ===============================2 --input of special characters3 --Tab:char (9)4 --newline: char (Ten)5 --Enter: char (+)6 7 --varchar (n): 1 Characters of 1 bytes; nvarchar (n): 1 Characters of 2 bytes8 --' I am sky ' exists in the varchar field, accounting for 7 bytes; in nvarchar, 10 bytes9 Ten --Create a new table and specify primary key, self-growth, non-null, UNIQUE constraint, sort type, default value, check field constraint, check data table constraint One --constraint format: CONSTRAINT custom constraint name constraint type other parameters A CREATE TABLETEST1 - ( -Numberint IDENTITY(1,1)PRIMARY KEY,--self-growth, PRIMARY KEY constraint theNamenvarchar( -) COLLATE Chinese_prc_ci_as not NULL,--sort, non-empty -Aliasnvarchar( -)UNIQUE,--Unique key constraint -Genderbit DEFAULT 1,--Default Value -Agetinyint CONSTRAINTCk_test1_ AgeCHECK(age> 0 andAge< 101),--Check field Constraints +Department numberint CONSTRAINTfk_test1_ Department Number - FOREIGN KEY + REFERENCESDepartment Table (Department number),--FOREIGN KEY Constraints AAddressnvarchar( $), atPhonevarchar( -), - CHECK(Telephone is not NULL orAddress is not NULL)--Check data table Constraints - ) - - - --Create a computed column in CREATE TABLETEST2 - ( toNumberint, +Ref. 2int, -Price Money not NULL, theNumberint not NULL * CONSTRAINTNumber of ck_test2_CHECK(Quantity> 0), $Total asPrice*Number--computed columns are read-onlyPanax Notoginseng ) - the --Add Field + ALTER TABLETEST1 A ADDNotenvarchar( -) the + --Delete a field - ALTER TABLETEST1 $ DROP COLUMNNotes $ - --Modifying Fields - ALTER TABLETEST2 the ALTER COLUMNNumberint not NULL - Wuyi --Renaming table names and field names the EXECsp_rename'TEST1','Test 1' --Modify Table name - EXECsp_rename'TEST1. Aliases','Alias','COLUMN' --Modify column names Wu - --Add primary Key About ALTER TABLETEST2 $ ADD CONSTRAINTPk_test2PRIMARY KEY(number) - - --Add foreign Key - ALTER TABLETEST2 A ADD CONSTRAINTFk_test2_test1FOREIGN KEY(No. 2)REFERENCESTest 1 (number) + the --Add a Check constraint - ALTER TABLETEST2 $ with NOCHECK --Optional: Do not check existing data when adding constraints the ADD CONSTRAINTCk_test2_ PriceCHECK(Unit Price> 0) the the --Add a default value constraint the ALTER TABLETEST2 - ADD DEFAULT(1) forQuantity in the --Delete Constraint the ALTER TABLETEST2 About DROP CONSTRAINTck_test2_ Price the the --Disabling Constraints the ALTER TABLETEST2 + NOCHECK CONSTRAINTck_test2_ Price - --Disable all Constraints the ALTER TABLETEST2Bayi NOCHECK CONSTRAINT All the the --Enable Constraints - ALTER TABLETEST2 - CHECK CONSTRAINTck_test2_ Price the --Enable all Constraints the ALTER TABLETEST2 the CHECK CONSTRAINT All the - --Delete a table the --You must remove the association between the table and the table before you can delete the table the DROP TABLETEST2 the 94 --determine if a table exists the IF not EXISTS(SELECT * fromSys.objectsWHERE object_id = object_id(N'[dbo]. [TEST2]') andTypeinch(N'U')) the BEGIN the PRINT 'You can create the table'98 END
Sqlcollections-Data Sheet management