1. 程式人生 > >oracle初理解

oracle初理解

回收站:
show recyclebin (命令列語句)
清空回收站:
purge recyclebin
回收站還原已刪除表:
flashback table table_name to before dorp
獲取系統時間:
sysdate
平均值:
avg
條件為真:where 1=1 條件為假:where 1=2
分頁:rownum 索引:rowid
左右模糊查詢(INSTR相比LIKE效率高):
INSTR(column_name,value) > 0 或 LIKE ‘%value%’

INSTR(column_name,value) = 0 或 NO LIKE ‘%value%’

日期精確到日:
TRUNC(date)
數字精度擷取(擷取時不進行四捨五入。number擷取的數字,num_digits擷取精度,預設為0):
TRUNC(number,num_digits)
NVL函式(expr1為空,返回expr2,否則返回expr1):
NVL(expr1,expr2)
求總和:sum(column_name)求個數:count(column_name)
where和having的區別:
1、where不能放在group by後面
2、having通常跟group by一起使用,放在group by後面,此時作用相當於where
3、where後面的條件中不能有聚集函式,比如SUM(),AVG()等,而having可以

登入 oracle資料庫

1、執行SQLPLUS工具

  C:\Users\wd-pc>sqlplus

2、直接進入SQLPLUS命令提示符

  C:\Users\wd-pc>sqlplus /nolog

3、以OS身份連線

  C:\Users\wd-pc>sqlplus / as sysdba 或

  SQL>connect / as sysdba

4、普通使用者登入

  C:\Users\wd-pc>sqlplus scott/123456  或

  SQL>connect scott/123456 或

  SQL>connect scott/

[email protected]

5、以管理員登入

  C:\Users\wd-pc>sqlplus sys/123456 as sysdba 或

  SQL>connect sys/123456 as sysdba

6、切換使用者

  SQL>conn hr/123456

  注:conn同connect

7、退出

  exit


欄位預設系統時間
create table users(id number(5) primary key,birthday date default sysdate)
建立檢視(只讀):
create view view_name(view_column1,view_column2) as select column1,column2 from table_name with read only;
修改檢視:
create or replace view view_name…
建立索引:
create index 索引名 on 表名(欄位,…)


刪除表內容(相比delete速度快,不可帶where):
truncate table
刪除欄位:
alter table table_name drop column_name datatype;
刪除表:
drop table table_name;
徹底刪除表:
drop table table_name purge;
刪除表中多餘的重複記錄,重複記錄是根據單個欄位來判斷,只留有rowid最小的記錄
delete from table_name where column_name in (select column_name from table_name group by column_name having count(column_name) > 1) and rowid not in (select min(rowid) from table_name group
by column_name having count(column_name ) > 1);
刪除索引:
drop index 索引名


更改欄位資料型別:
alter table table_name modify column_name datatype;
修改欄位名:
alter table table_name rename column column_name to new_column_name;
修改表名:
rename table_name to new_table_name;
多表關聯更新:
update unicom_card_detail u set u.zone = (select d.zone from unicom_device_info d where d.deviceip = u.ip) where EXISTS (select 1 from unicom_device_info d where d.deviceip = u.ip);
已有表新增欄位:
alter table table_name add column_name datatype;


查詢表中多餘的重複資料(多個欄位)
select column_name1,column_name2 from table_name group by column_name1,column_name2 having count(*) > 1;
查詢表有多少條資料:
select count(primary_key) from table_name;
查詢表中某欄位有多少個不重複的資料:
select count(distinct column_name) from table_name;
查看錶結構(命令列語句):
desc table_name;
檢視序列當前值和下一個值:
select seq_name.currval from dual;
select seq_name.nextval from dual;
左連線查詢(以左表為基礎,加上左表和右表匹配後的資料):
select t.column_name1,t.column_name2,u.column_name1 from table_name1 t left join table_name2 u on t.column_name1 = u.column_name1 where …
右連線查詢(以右表為基礎,加上左表和右表匹配後的資料):

select t.column_name1,t.column_name2,u.column_name1 from table_name1 t right join table_name2 u on t.column_name1 = u.column_name1 where …

Oracle增加一列、修改一列資料型別:

新增一列:
alter table A add( CFYJSNR varchar2(20));
修改列:
alter table A rename column CFYJSNR to CFJYSNR;
修改列的型別:
alter table A modify cfyj varchar(200);
刪除資料庫一列
alter table A drop column CFYJSNR

插入資料 insert into test values (6,’楊七’);
commit;

查詢表結構 desc tableName;