阿宽

Nothing is more powerful than habit!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

orcle常用語句

Posted on 2008-01-02 16:22  宽田  阅读(1033)  评论(0编辑  收藏  举报

//創建表格
create table UseInfo
(
userGUID varchar(
32),
useID varchar2(
10),
useName Nvarchar2(
5),
usePWD varchar2(
16),
useEmail varchar2(
50),
usePower integer,
useWriterDate varchar2(
8),
useExportExcel real
);


//修改欄位大小
alter table userinfo
modify ( useName varchar2(
10));


//增加欄位
alter table useinfo
add (useNote varchar2(
50));

//刪除欄位
 alter table useinfo
 drop column userguid;

//增加主鍵
 alter table useinfo
 add constraint useinfo_userGUID_pk
 primary key (useGUID);

//刪除主鍵
alter table  useinfo
drop constraint useinfo_userGUID_pk;
//增加外鍵
 alter table ConcernDetail
 add constraint ConcernDetail_ccdccbGUID_FK
 foreign key(ccdccbGUID) references ConcernBrand(ccbGUID);

//得到指定行數的資料
select * from erpuser.customer where rownum<4;

//查看所有表
 select * from tab;

//查看約束
select owner,constraint_name,constraint_type,table_name from user_constraints where owner='WEBUSER';

//冊除主鍵
alter table brand_customer_lot_detaildrop constraint brand_customer_lot_detail_pk;

//清除sql plus屏幕
clear screen;

  //更新值
1) 最简单的形式
   
--经确认customers表中所有customer_id小于1000均为'北京'
   
--1000以内的均是公司走向全国之前的本城市的老客户:)
   update customers
   
set    city_name='北京'
   
where  customer_id<1000

2) 两表(多表)关联update -- 仅在where字句中的连接
   
--这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别
   update customers  a       
-- 使用别名
   
set    customer_type='01' --01 为vip,00为普通
   
where  exists (select 1
                  from   tmp_cust_city b
                  
where  b.customer_id=a.customer_id
                 )

3) 两表(多表)关联update -- 被修改值由另一个表运算而来
   update customers a   
-- 使用别名
   
set    city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
   
where  exists (select 1
                  from   tmp_cust_city b
                  
where  b.customer_id=a.customer_id
                 )
   
-- update 超过2个值
   update customers a   
-- 使用别名
   
set    (city_name,customer_type)=(select b.city_name,b.customer_type
                                     from   tmp_cust_city b
                                     
where  b.customer_id=a.customer_id)
   
where  exists (select 1
                  from   tmp_cust_city b
                  
where  b.customer_id=a.customer_id
                 )

   //刪除表
drop table CONCERNDETAIL;

   //插入資料
   insert into brand_customer_lot
 values(
'2008010220','A00001','lot_200801020','A1');

  insert into brand_customer_lot_detail
 values('2008010220','CF','Acer',to_date('20080102','yyyy-mm-dd'),'Q',200,200000,'R');

方法:
round()是四舍五入,不是取整。 
ceil()是进一,floor()是去尾,都是返回整数。 
trunc()也是去尾,但是可以指定小数位数,不指定即为取整,所以也是对的。