我有测试数据库,我试图用索引调整列的某些列。 表有几行。 我们不说话,让我们展示代码
Create Table Country (ID INT PRIMARY KEY IDENTITY, NAME VARCHAR(48),ENEMY VARCHAR(32),AMOUNT INT);
CREATE NONCLUSTERED INDEX IDX_COUNTRY_NAME ON Country(NAME);
我添加了一些行。稍后使用sql语句调整列的大小如下:
ALTER INDEX IDX_COUNTRY_NAME ON Country disable;
alter table COUNTRY alter column NAME VARCHAR(40);
ALTER INDEX IDX_COUNTRY_NAME ON Country rebuild;`
这是我在尝试更新列
后得到的java.sql.BatchUpdateException: The index 'IDX_COUNTRY_NAME' is dependent on column 'NAME'.
at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:947)
at com.veraxsystems.commons.updater.tasks.ResizeColumnTask.execute(ResizeColumnTask.java:52)
at com.veraxsystems.commons.updater.tasks.AbstractTask.process(AbstractTask.java:79)
at com.veraxsystems.commons.Updater.executeUpdate(Updater.java:169)
at com.veraxsystems.commons.Updater.afterPropertiesSet(Updater.java:201)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
我想避免丢弃索引,因为我不知道将来是否存在随机索引的脚本,我将有问题重新加载它们。 我正在使用sql server 2008 我尝试了很多方法来完成这个。只有希望是使用drop,但正如我之前所说,我宁愿不使用它。
修改
这些只是示例sql,我知道我更新了哪个表和列。为了找到它,我使用sys.indexes,sys.index_columns并通过object_id连接它们。这很好,因为我打印了所有结果,就像预期的那样。
答案 0 :(得分:1)
从ALTER TABLE上的manual,ALTER COLUMN部分:
修改后的列不能是以下任何一种:
[...]
- 在索引中使用,除非该列是varchar,nvarchar或varbinary数据类型,否则数据类型不会更改,新大小等于或大于比旧size ,索引不是PRIMARY KEY约束的结果。
[...]
(斜体和重点是我的。)
您正在尝试减小索引列的大小(从(varchar(48)
到varchar(40)
),这是“违反规则”。似乎除了删除/重新创建之外没有其他选择索引。