oracle基本操作

今天來複習一下oracle的基本操作,後來在項目中也經常使用oracle,但是隻是部分,今天覆習一下學了的oracle常用的基礎操作。

  • 登錄oracle
sqlplus/as sysdba;
  • 啓動oracle
startup;
  • 停止oracle
shutdown;
  • 創建新用戶
create user username identified by password;
  • 授予用戶權限
grant connect,resource to username;
  • 授予用戶具體的權限
grant create session,create table;unlimited tablespace to username;
  • 連接用戶
conn username/password;
  • 創建表空間
create tablespace tablespacename datafile '路徑/test.dbf' size 50m; 
  • 給用戶指定默認表空間
alter user username default tablespace tablespacename;
  • 查詢表空間
select *from user_tablespaces;
  • 刪除表數據
drop table tablename;
  • 複製空表結構
create table newtable as select *from oldtable where 1=2;
  • 複製表(含記錄)
create table newtable as select *from oldtable;
  • 修改約束名稱
alter table tablename rename constraint oldname to newname;
  • 刪除約束
alter table tablename drop constraint cname;
  • 停止約束
alter table tablename modify constraint cname disable;
  • 啓用約束
alter table tablename modify constraint cname enable validate;
  • 新增約束
alter table tablename add constraint constraintname foreigh(org_id) key (外鍵)references ref_table(ord_id);
  • 更改字段數據類型
alter table tablename modify(org_id varchar2(50 byte));
  • 更改字段數據長度
alter table tablename modify(org_id varchar2(80 byte));
  • 修改表的列名
alter table tablename rename column xx to yy;
  • 創建表(舉例)
create table tablename(
sid number(10) primary key,
sname varchar2(100) not null, pwd varchar2(100) not null);
--屬性之間逗號隔開,最後一個不加逗號
  • 插入一條數據
insert into tablename values(sequencename.nextval,?,?);
  • 創建一條序列
create sequence seq_tablename_tid start with 1001;
  • 修改表中一條數據
update tablename set tid=?,sname=?,pwd=? where sid=?
  • 修改數據時要拼接數據
update tablename set sid=? where tid in("+tid+");

對應的java中Dao層語句:

String sql=null;
if(tid.constains(",") && !tid.constains(" or ")){
sql="update tablename set sid=? where tid in("+tid+")"; return  .......}else{
sql="update tablename set sid=? where tid=?"; return ........}
  • 級聯查詢表數據(在做分頁時用的sql語句)
select * from (select a.*,rownum rn from(select sid from tablename where tid=? order by sid)a where rownum<=?)where rn>?
  • 查詢總數
select count(sid) as tatal from tablename where條件
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章