---
title: "oracle中删除属于主键的字段"
author: "wentry"
date: "2011-10-12T10:14:36Z"
tags: []
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2011-10-12T10:15:24Z"
id: 1190365
url: "https://wentry.iteye.com/blog/1190365"
---

在表中要删除一个数据字段时，可以先把该字段设置为无效，然后再删除表中无效的字段。
但是如果该字段属于主键时，首先需要删除该表中的主键，然再删除字段，最后再给表重新定义主键。

删除表的主键值：
-- ( KEY_NAME也就是定义表时，给表创建的主键名);
alter table 表名 drop constraint  KEY_NAME ; 

-- 重新创建主键名
alter table 表名
  add constraint KEY_NAME primary key (作为主键的字段名，多个时用逗号隔开)
  using index 
  tablespace tablespace_NAME（表空间名称）
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 192K
    minextents 1
    maxextents unlimited
  );

// 将要删除的字段设置为无效
alter table 表名 set unused column 字段名;
// 删除表中无效的字段
alter table 表名 drop unused columns;