为了账号安全,请及时绑定邮箱和手机立即绑定

Oracle数据库表修改操作

标签:
Oracle

一:Oracle修改字段名、字段数据类型


语句:
alter table tableName rename column oldCName to newCName; -- 修改字段名
alter table tableName modify (cloumnName 数据类型); -- 修改数据类型

例如:
1、创建表:
  CREATE TABLE Student(
    id varchar2(32) primary key,
    name varchar2(8) not null,
    age number
  );
2、修改字段名:
  alter table Student rename name to StuName;
3、修改数据类型:
  alter table Student modify (id varchar2(64));



问题

数据库中某表字段为number类型,需要修改成varchar类型。

修改步骤

--备份表--create table xxtable_copy20171215 as select * from xxtable;--复制表结构成新表--create table xxtable_new as select * from xxtable where 1=2;--改变新表的数据结构--alter table xxtable_new modify (CANCELRENO varchar(25));--导入原数据--insert into xxtable_new select * from xxtable_copy20171215;--插入新数据--insert into xxtable_new (...) values (...);--将原表更名--alter table xxtable rename to xxtable_bak20171215;--将新表更名成原表名--alter table xxtable_new rename to xxtable;--删除第一次备份的表--drop table xxtable_copy20171215;


二:Oracle中给表添加主键、外键



1、创建表的同时创建主键约束

 

(1)无命名 create table student ( studentid int primary key not null, studentname varchar(8), age int);

 

(2)有命名 create table students ( studentid int , studentname varchar(8), age int, constraint yy primary key(studentid));

 

2、删除表中已有的主键约束

 

(1)无命名可用 SELECT * from user_cons_columns; 查找表中主键名称得student表中的主键名为SYS_C002715 alter table student drop constraint SYS_C002715;

 

(2)有命名 alter table students drop constraint yy;

 

3、向表中添加主键约束 alter table student add constraint pk_student primary key(studentid);

 

4、向表中添加外键约束 ALTER TABLE table_A ADD CONSTRAINT FK_name FOREIGN KEY(id) REFERENCES table_B(id);


三:自增量 sequence

-- Create sequence 
create sequence S_SNOWBALLING
minvalue 1
maxvalue 9999999999999999999999999999
start with 3397
increment by 1
cache 20;


select S_snowballing.nextval from   dual

四:清空表

truncate table T_SBL_RollBack_PROJECTINFO


五:包创建和调用

  1. 创建包  

  2. create or replace package pck_snowballing is  

  3.   --获取数据  

  4.   type SnowballingList is record(  

  5.     guid         char(36),  

  6.     loanGuid     char(36),  

  7.     custName     nvarchar2(50),            --客户姓名  

  8.     Idcard       nvarchar2(50),            --客户身份证  

  9.     bankcardNo       nvarchar2(50),            --客户银行卡号  

  10.     BankMobile   nvarchar2(50),            --客户银行卡预留手机号  

  11.     IsConversbl  number                 --是否已经转换了数据,1:是,0:否  

  12.     );  

  13.   type sblTable is table of SnowballingList;  

  14.   function GetSnowballingList(i_custName nvarchar2) return sblTable  

  15.     PIPELINED;  

  16. end pck_snowballing;  

  17.   

  18.   

  19.   

  20. create or replace package body pck_snowballing is  

  21.   

  22. function GetSnowballingList(i_custName nvarchar2) return sblTable  

  23.   PIPELINED is  

  24.   Results SnowballingList;  

  25.   sys_cur_contract sys_refcursor;  

  26.   begin  

  27.     open sys_cur_contract for  

  28.            select (  

  29.            case when t3.guid is null then t2.guid else t3.guid end) guid  

  30.            ,t1.loanguid  

  31.            ,t2.custname  

  32.            ,t2.idcard  

  33.            ,t3.bankcardno  

  34.            ,t3.bankmobile  

  35.             ,(case when t3.guid is null then 0 else 1 end)  as IsConversbl from t_loanrecord t1  

  36.             join t_custinfo t2  

  37.             on t1.custguid=t2.guid  

  38.             left join t_sbl_Userinfo t3  

  39.             on t1.loanguid=t3.loanguid  

  40.             where t1.status!=9  

  41.             order by t1.createdate desc;  

  42.     begin  

  43.          loop  

  44.            fetch sys_cur_contract  

  45.            into  Results ;  

  46.            exit when sys_cur_contract%notfound;  

  47.            pipe row(Results);  

  48.          end loop;  

  49.     end;  

  50.   return;  

  51.   end GetSnowballingList;  

  52. end pck_snowballing;  

  53.   

  54. 调用包  

  55. select * from table(pck_snowballing.GetSnowballingList(''))   

原文出处


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消