TestReport数据创建索引删除索引和修改主键

本文介绍如何在SQLServer2000中调整表的主键和索引,包括删除旧的主键约束并创建新的主键,以及删除和重建索引的过程。这些操作在SQLServer2015上可以正常运行,但在SQLServer2000环境下遇到了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SQLServer2000版本和SQLServer2015操作还不一样
SQLServer2000数据库操作方法
公司的数据库创建有点问题,原数据创建和建立索引命令如下

--创建数据库带主键
create table TestReport(
TeleName char(4) not null,
AnalogType int not null,
AnalogNo int not null,
AnalogName char(50) not null,
StatDate int not null,
TestValue float not null,
TestTime int not null,
constraint PK_TESTREPORT Primary Key (TeleName,AnalogType,AnalogNo,StatDate));

--创建索引
/*==============================================================*/
/* Index: DayReport_Index                                       */
/*==============================================================*/
create unique index DayReport_Index on DayReport (
TeleName ,
AnalogType ,
AnalogNo ,
StatDate 
);

将数据有问题部分处理:

use csm2010;
--先删除主键,然后再创建主键
alter table TestReport drop constraint PK_TESTREPORT;
alter table TestReport add constraint PK_TestReport Primary Key(TeleName,AnalogType,AnalogNo,TestTime);

--先删除索引,再创建索引
drop index TestReport.TestReport_Index;
create unique index TestReport_Index on TestReport(TeleName ,AnalogType ,AnalogNo ,TestTime );

我在SQLServer2015上实验都成功,但是拷贝到SQLServer2000虚拟机上实验就不行了
准确的讲,这里是现场方法,以前的方法是古老方法

alter table TestReport drop constraint PK_TESTREPORT;
alter table TestReport add constraint PK_TestReport Primary Key(TeleName,AnalogType,AnalogNo,TestTime);

drop index TestReport_Index on TestReport;
create unique index TestReport_Index on TestReport (TeleName ,AnalogType ,AnalogNo ,TestTime );

我将以上命令写了个批处理脚本,方便客户操作
updateTestReport.cmd

osql /d CSM2010 -E -i updateTestReprot.sql
pause

updateTestReprot.sql

use csm2010;
--先删除主键,然后再创建主键
alter table TestReport drop constraint PK_TESTREPORT;
alter table TestReport add constraint PK_TestReport Primary Key(TeleName,AnalogType,AnalogNo,TestTime);

--先删除索引,再创建索引
drop index TestReport.TestReport_Index; --此处不同于SQLServer2015操作
create unique index TestReport_Index on TestReport(TeleName ,AnalogType ,AnalogNo ,TestTime );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值