在oracle sql中更改主键的数据类型时应遵循的步骤

问题描述 投票:0回答:1

我有一张与此类似的表。

CREATE TABLE customers (
    customer_id NUMBER(7,0) NOT NULL,
    customer_name VARCHAR2(50) NOT NULL,
    CONSTRAINT customers_pk PRIMARY_KEY (customer_id)
);

表中有一些值。

我想将主键customer_id的数据类型更改为NUMBER(10, 0)。那么在执行ALTER命令之前我们应该遵循哪些步骤? (此列在任何表中均未作为外键引用)

特别是

默认情况下,在oracle SQL中,我们在主列上有一个索引。那么,我们应该删除主键约束,然后执行ALTER命令吗?还有我们需要考虑的其他事项吗?

sql database oracle primary-key alter
1个回答
0
投票

对于像您这样的情况,您无需执行任何操作-只需执行

SQL> create table customers (
  2  customer_id number(7, 0),
  3  customer_name varchar2(50),
  4  constraint customer_pk primary key (customer_id));

Table created.

SQL> insert into customers
  2  select 1234566, 'Little' from dual union all
  3  select 98876  , 'Foot'   from dual;

2 rows created.

SQL> alter table customers modify customer_id number(8, 0);

Table altered.

SQL> select constraint_name from user_constraints where table_name = 'CUSTOMERS';

CONSTRAINT_NAME
------------------------------
CUSTOMER_PK

SQL>

但是,如果您必须将列设为[[smaller或修改其数据类型-那是另一回事了。幸运的你,不是你的。

© www.soinside.com 2019 - 2024. All rights reserved.