赞
踩
微软的SQL Server Migration Assistant for MySQL(此后简称SSMA)用于MySQL迁移非常好用,如何使用,网上大把,本篇原创博客旨在善后工作。
迁移之后,需要把ssma$rowid这字段删除,该字段的作用就是给每个行做一个GUID的唯一标识,用以迁移数据使用。
注意:以下脚本均以既定的格式为例,自定义的约束名、索引名等无法识别。
step 1: 由于ssma$rowid拥有唯一约束,在删除这一列前,必须将约束删除
- --删除rowid的唯一约束
- select 'ALTER TABLE ['+t.name+'] DROP CONSTRAINT '+i.name from sys.indexes i
- inner join sys.tables t on i.object_id=t.object_id
- where i.type_desc='NONCLUSTERED'
- and i.name like '%ssma$rowid'
- --删除rowid这一列
- select distinct 'ALTER TABLE ['+t.name+'] DROP column ssma$rowid ' from sys.tables t
- left join sys.columns c on t.object_id=t.object_id
- where t.name <> '__MigrationHistory' and c.name = 'ssma$rowid'
在更改前,需要将主键约束去除。
而主键约束会被其他表的外键引用,得先把外键约束去除。
step 1:
- --删除每个表的外键约束
- select 'ALTER TABLE ['+t.name+'] DROP CONSTRAINT '+o.name from sys.objects o
- inner join sys.tables t on o.parent_object_id=t.object_id
- where o.type_desc='FOREIGN_KEY_CONSTRAINT'
- and o.name like '%$FK_%'
step 2:
- --删除每个表的主键约束
- select 'ALTER TABLE ['+t.name+'] DROP CONSTRAINT '+i.name from sys.indexes i
- inner join sys.tables t on i.object_id=t.object_id
- where i.type_desc='CLUSTERED'
- and i.name like '%_Id'
step 3:
- --删除每个表的默认值约束
- select 'ALTER TABLE ['+t.name+'] DROP CONSTRAINT '+o.name from sys.objects o
- inner join sys.tables t on o.parent_object_id=t.object_id
- where o.type_desc='DEFAULT_CONSTRAINT'
- and o.name like 'DF_%'
- --删除每个表的索引
- select 'DROP INDEX ['+i.name+'] ON ['+t.name+'] WITH ( ONLINE = OFF )' from sys.indexes i
- inner join sys.tables t on i.object_id=t.object_id
- where i.type_desc='NONCLUSTERED'
- and i.name like 'IX_%'
- /*GUID & pk*/
- select
- (case Is_Nullable when 'NO'
- then concat('alter table [',table_name,'] alter column ',column_name,' uniqueidentifier not null')
- else concat('alter table [',table_name,'] alter column ',column_name,' uniqueidentifier') end)
- from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME like '%Id' and DATA_TYPE='nchar' and CHARACTER_MAXIMUM_LENGTH=36;
迁移后的数据类型为datetime,需要更改为timestamp
- /*RowVersion*/
- -- 添加新列 RowVersion1
- select concat('alter table [',table_name,'] add ',column_name,'1 timestamp not null') from INFORMATION_SCHEMA.COLUMNS where DATA_TYPE='datetime' and column_name='rowVersion'
-
- -- 删除旧的RowVersion列
- select concat('alter table [',table_name,'] drop column [',column_name,']') from INFORMATION_SCHEMA.COLUMNS where DATA_TYPE='datetime' and column_name='rowVersion'
-
- --重命名RowVersion1=>RowVersion
- select 'exec sp_rename '''+TABLE_NAME+'.'+COLUMN_NAME+''','''+'RowVersion'+''',''column''' from INFORMATION_SCHEMA.COLUMNS where DATA_TYPE='timestamp' and column_name='rowVersion1'
后记:这些约束在更改数据类型的时候,会碍事,在更改完成后,自然需重新加上。
谢谢观看
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。