马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
一个语句分成两个event(实际上不止,其他可以忽略),一个table_mapevent和一个Rows_log_event。Table_mapevent是一样的,主要看Rows_log_event。以下是使用sql语句对表,列,束缚,主键举行操纵的语句模板,来历于msdn。详细语法商定请参考上面链接:
Transact-SQL语法商定(Transact-SQL)
创立表sql语句模板:
IFOBJECT_ID(<schema_name,sysname,dbo>.<table_name,sysname,sample_table>,U)ISNOTNULL
DROPTABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
GO
CREATETABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
(
<columns_in_primary_key,,c1><column1_datatype,,int><column1_nullability,,NOTNULL>,
<column2_name,sysname,c2><column2_datatype,,char(10)><column2_nullability,,NULL>,
<column3_name,sysname,c3><column3_datatype,,datetime><column3_nullability,,NULL>,
CONSTRAINT<contraint_name,sysname,PK_sample_table>PRIMARYKEY(<columns_in_primary_key,,c1>)
)
GO
新增列的sql语句模板:
IFOBJECT_ID(<schema_name,sysname,dbo>.<table_name,sysname,sample_table>,U)ISNOTNULL
DROPTABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
GO
CREATETABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
(
column1intNOTNULL,
column2char(10)NULL
)
GO
--Addanewcolumntothetable
ALTERTABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
ADD<new_column_name,sysname,column3><new_column_datatype,,datetime><new_column_nullability,,NULL>
GO
新增束缚的sql语句模板:
IFOBJECT_ID(<schema_name,sysname,dbo>.<table_name,sysname,sample_table>,U)ISNOTNULL
DROPTABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
GO
CREATETABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
(
column1intNOTNULL,
<constraint_column_name,sysname,phone>char(8)NOTNULL
)
GO
--AddanewCHECKCONSTRAINTtothetable
ALTERTABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
ADDCONSTRAINT<contraint_name,sysname,phone_chk><constraint_type,,CHECK>(<constraint_column_name,sysname,phone><logical_expression,,LIKE[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9])>
GO
新增主键的sql语句模板:
IFOBJECT_ID(<schema_name,sysname,dbo>.<table_name,sysname,sample_table>,U)ISNOTNULL
DROPTABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
GO
CREATETABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
(
<columns_in_primary_key,,column1>intNOTNULL,
column2char(8)NOTNULL
)
GO
--AddanewPRIMARYKEYCONSTRAINTtothetable
ALTERTABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
ADDCONSTRAINT<constraint_name,sysname,PK_sample_table>PRIMARYKEY(<columns_in_primary_key,,column1>)
GO
删除列的sql语句模板:
IFOBJECT_ID(<schema_name,sysname,dbo>.<table_name,sysname,sample_table>,U)ISNOTNULL
DROPTABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
GO
CREATETABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
(
column1intNOTNULL,
column2char(10)NULL,
<new_column_name,sysname,column3>datetimeNULL
)
GO
--Dropacolumnfromthetable
ALTERTABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
DROPCOLUMN<new_column_name,sysname,column3>
GO
删除束缚的sql语句模板:
IFOBJECT_ID(<schema_name,sysname,dbo>.<table_name,sysname,sample_table>,U)ISNOTNULL
DROPTABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
GO
CREATETABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
(
column1intNOTNULL,
salarymoneyNOTNULLCONSTRAINT<constraint_name,sysname,salary_cap>CHECK(salary<500000)
)
GO
--DropCHECKCONSTRAINTfromthetable
ALTERTABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
DROPCONSTRAINT<constraint_name,sysname,salary_cap>
GO
删除主键的sql语句模板:
IFOBJECT_ID(N<schema_name,sysname,dbo>.<table_name,sysname,sample_table>,U)ISNOTNULL
DROPTABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
GO
CREATETABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
(
c1intNOTNULL,
c2char(10)NULL,
c3datetimeNULL,
CONSTRAINT<primary_key_name,sysname,primary_key_name>PRIMARYKEY(c1)
)
GO
ALTERTABLE<schema_name,sysname,dbo>.<table_name,sysname,sample_table>
DROPCONSTRAINT<primary_key_name,sysname,primary_key_name>
GO
删除表的的sql语句模板:
IFEXISTS(
SELECT*
FROMsys.tables
JOINsys.schemas
ONsys.tables.schema_id=sys.schemas.schema_id
WHEREsys.schemas.name=N<schema_name,sysname,your_schema_name>
ANDsys.tables.name=N<table_name,sysname,your_table_name>
)
DROPTABLE<schema_name,sysname,your_schema_name>.<table_name,sysname,your_table_name>
GO
支持多线程,充分利用CPU资源 |