现在的位置: 首页 > 综合 > 正文

SQL alter

2013年09月07日 ⁄ 综合 ⁄ 共 1248字 ⁄ 字号 评论关闭

1. 查表约束

  select * from user_constraints

 

2. 删除约束

  alter table TABLE_NAME drop constraint CONSTRAINT_NAME;

 

3. 添加约束

  alter table TABLE_NAME add constraint CONSTR_NAME primary key(column1,...);

 

4. 建表时指定主键名称

  create table employee

  (

    emp_no varchar2(5),

    emp_name varchar2(10),

    birthday timestamp(6),

    salary number(9,2),

    constraint employee_pk primary key(emp_no));

 

5. 修改字段类型

  (1) 有精度改变或者改变数据类型,所改字段的列必须为空。但char与varchar2可以直接修改。

    alter table employee modify emp_name varchar2(30); ---

    alter table employee modify salary number(10,2); ---salary列必须为空才能执行

  (2) 有数据时可以用以下两种方法:方法一

    alter table employee add salary_temp number(10,2);

    update employee set salary_temp = salary;

    commit;

    alter table employee drop column salary;

    alter table employee rename column salary_temp to salary;

   这种方法会使列名发生变化,而且字段顺序增加 有可能发生行迁移,对应用程序会产生影响

  (3) 方法二:

    alter table employee add salary_temp number(9); ---首先新增一列,与salary一致

    alter table employee modify salary null;

    update employee set salary_temp=salary,salary=null;

    commit;

    alter table employee modify salary number(10,2);

    update employee set salary=salary_temp,salary_temp=null;

    commit;

    alter table employee drop column salary_temp;

    alter table employee modify salary is not null;

    select * form employee;

这种方法不用使列名发生变化 也不会发生表迁移,但这个有个缺点是表要更新两次,

如果数据量较大的话 产生的undo和redo更多 ,前提也是要停机做;要是不停机的话 ,也可以采用在线重定义方式来做。

 

 

抱歉!评论已关闭.