SQL Server CREATE TABLE add primary key Add column common SQL statement

Source: Internet
Author: User

--Delete primary Key Alter TableTable nameDrop constraintPRIMARY Key Name--Add primary Key Alter TableTable nameAdd constraintPrimary Key NamePrimary Key(field name 1, field Name 2 ...)--to add a primary key for a nonclustered index Alter TableTable nameAdd constraintPrimary Key NamePrimary Key nonclustered(field name 1, field Name 2 ...) New table:Create Table [Table name]  (  [AutoNumber field] int IDENTITY(1,1)PRIMARY KEY ,  [Field 1] NVarChar( -)default\'default value \' NULL ,  [Field 2] ntext NULL ,  [Field 3] datetime,  [Field 4]  Money NULL ,  [Field 5] int default 0,  [Field 6] Decimal( A,4)default 0,  [Field 7] Image NULL,) to delete the table:Drop Table [Table name]Delete all tables:DECLARECuritemsCURSOR  for Select [name]  fromsysobjectswhereXtype='U'  for READ  only OPENCuritemsDECLARE @n  NVARCHAR( -),@m NVARCHAR( -) FETCH  fromCuritems into @n  while @ @FETCH_STATUS=0 BEGIN  Set @m=@n     exec('Drop Table' + @m) FETCH NEXT  fromCuritems into  @n END CLOSECuritemsdeallocatecuritems inserting data:INSERT  into [Table name](Field 1, Field 2)VALUES( -,\'51windows.net\') Delete data:DELETE  from [Table name] WHERE [Field name]> -Update Data:UPDATE [Table name] SET [Field 1] =  $,[Field 2] =\'51windows.net\' WHERE [Field Three] =\'haiwa\'New fields:ALTER TABLE [Table name] ADD [Field name] NVARCHAR( -)NULLTo delete a field:ALTER TABLE [Table name] DROP COLUMN [Field name]To Modify a field:ALTER TABLE [Table name] ALTER COLUMN [Field name] NVARCHAR( -)NULLRename table: (Access renames the table, refer to the article: Renaming a table in an Access database) sp_rename \'table name \', \'new table name \', \'object\'new constraint:ALTER TABLE [Table name] ADD CONSTRAINTConstraint nameCHECK([constraint fields] <=\'2000-1-1\') to delete the constraint:ALTER TABLE [Table name] DROP CONSTRAINTconstraint name new default valueALTER TABLE [Table name] ADD CONSTRAINTDefault Value NameDEFAULT\'51windows.net\'  for [Field name]Delete default valuesALTER TABLE [Table name] DROP CONSTRAINTDefault Value name delete logs from SQL Server, reducing database file sizeDump TransactionDatabase name withno_logBackup LogDatabase name withno_logDBCCshrinkdatabase (database name)execsp_dboption \'database name \', \'autoshrink\', \'true\'\\\'Add field General function Sub AddColumn (tablename,columnname,columntype) Conn.execute (\ "Alter Table \" &tablename&\ "add \" &columnname&\ "\" &columntype&\ "\") End sub\\\'Change the field general function Sub Modcolumn (tablename,columnname,columntype) Conn.Execute(\"Alter Table\"&TableName&\"Alter Column\"&ColumnName&\" \"&ColumnType&\"\")  Endsub\\\'Check if the table exists sql=\ "SELECT COUNT (*) as Dida from sysobjects where id = object_id (n\' [owner].[Table name]\') and OBJECTPROPERTY (ID, n\'Isusertable\') = 1\ "set Rs=conn.execute (SQL) Response.Write rs (\" Dida\ ") \'returns a numeric value, 0 for no, 1 for existence of a judgment table:Select *  fromsysobjectswhereId= object_id(n\'[dbo]. [Tablename]\') and ObjectProperty(ID, n\'isusertable\')= 1structure of a tableSelect *  fromsyscolumnswhereId= object_id(n\'[dbo]. [Your table name is]\') and ObjectProperty(ID, n\'isusertable\')= 1to modify the table prefix:ALTER SCHEMAdbo TRANSFER prename.tablename; If table 2 already exists, add the records in table 1 to the statement in Table 2:Insert    intoTable 2 (Field 1, Field 2,...)SelectField 1, Field 2,.. fromTable 2where   ... If Table 2 does not exist, table 2 is automatically generated with the following statement, with the same type of field as Table 1:SelectField 1, Field 2,.. intoTable 2 fromTable 1where...

SQL Server CREATE TABLE add primary key Add column common SQL statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.