oracle基本操作之(一)

oracle基本操作之()

 

1.創建表

create table 表名(

 列名類型,

 列名類型

);

 

2.修改類屬性

alter table 表名 modify(列名 類型);

 

3.添加列

alter table 表名 add(列名 類型);

 

4.添加主鍵約束和非空約束

alter table 表名 add constraint pk_表名 primary key(列名);

alter table 表名 modify(列名 not null);

 

5.刪除主鍵約束

alter table 表名 drop primary key;

alter table 表名 drop constraint pk_表名;

 

6.失效約束

alter table 表名 disable primary key;

alter table 表名 disable constraint pk_表名;

 

7.有效約束

alter table 表名 enable primary key;

alter table 表名 enable constraint pk_表名;

 

8.刪除列

alter table 表名 drop column 列名;

 

9.設置某列不可用,然後刪除

alter table 表名 set unused(列名);

alter table 表名 drop unused columns;

 

10.修改表名

rename 表名1 to 表名2

alter 表名1 rename to 表名2;

 

11.截斷表

truncate table 表名;

 

12.截斷表保留行空間

truncate table 表名 resue storage;

 

13.查看錶結構

desc table 表名;

 

14.刪除表

drop table 表名;

 

15.插入記錄

例:insert into 表名 values(內容1,內容2,內容3,內容4);

 

16.帶參數對話方式插入行

:insert into 表名 values(&列名1,&列名2);

  insert into 表名 values(內容1,內容2);

 

17.插入某幾列記錄

insert into 表名(列名1,列名2) values(內容1,內容2);

 

18.爲列插入空值(其列不能爲not null

insert into 表名 values(內容1,null,null);

 

19.創建表(包括主鍵及外鍵設置)方法一

create table 表名(

  列名類型

  constraint pk_表名 primary key,

  列名類型 not null,

  列名類型 

  constraint fk_表名 reference 表名(列名),

  列名類型

  constraint ck_表名 check(列名3 in(''內容1'',''內容2'',''內容3'')) 

);

 

20.查詢所有行

select * from 表名;

 

21.查詢某幾列

select 列名1,列名2 from 表名;

 

22.重複行消除

select distict 列名 from 表名;

 

23.where語句查詢

select * from 表名 where 條件 order by 列名;

(注:如number類型查出自動按升序排列,如要按降序排列,則select * from 表名 where 條件 order by 列名 desc;)

 

24.創建表,方法二

create table 表名(

 列名類型 primary key,

 列名類型 not null,

 列名類型 check(列名3 in('''','''','''')),

 列名類型 refernce 表名(列名)

);

 

25.修改 =‘的數據

update 表名 set (=) where =‘’;

 

26.刪除行

delete from 表名 where 條件;

 

27.事務處理

--事務處理

update 表名

set 列名(日期) = ''30-5-98''

where 條件;

savepoint mark1;

delete from 表名 where 條件;

savepoint mark2;

rollback to savepoint mark1;

rollback;

 

28.建立用戶user1,密碼爲password

授予用戶connect,resource的權限

connect角色用於登錄

resource角色用於建表等.

connect system/manager

create user user1 identified by password;

grant connect,resource to password;

 

29.數據控制語言

connect scott/tiger

 

30.把對錶1查詢和修改的權限授予user1

grant select,update on 1 to user1;

 

31.把對錶表1中列1和列2修改的權限授予user1

grant update(1,2) on 1 to user1;

 

32.把對錶表1查詢的權限授予用戶user1

並且user1用戶還可以把這個權限授予別的用戶(with grant option)

grant select on 1 to user1 with grant option;

 

33.從用戶user1撤銷對錶1查詢和修改的權限

revoke select,update on 1 from user1;

 

34.創建表空間語句

create tablespace tableSpaceName datafile filename size **M   online;

例如:

create tablespace photo datafile 'e:/photo.dbf' size 1000M online;

 

35. 修改表空間語句

alter database datafile filename resize   **M   online;

例如:

alter database datafile 'e:/photo.dbf' resize   2000M   online;

 

36. 增加額外的數據文件到表空間

alter tablespace tablespaceName add datafile filename size **M

例如:

alter tablespace photo add datafile 'e:/photo2.dbf' size 2000M

 

36. 修改表空間名稱

ALTER TABLESPACE oldTSName RENAME TO newTSName

 

37、查看錶空間的名稱及大小

select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size

from dba_tablespaces t, dba_data_files d

where t.tablespace_name = d.tablespace_name

group by t.tablespace_name;

 

38、查看錶空間物理文件的名稱及大小

select tablespace_name, file_id, file_name,

round(bytes/(1024*1024),0) total_space

from dba_data_files

order by tablespace_name;

 

39、查看回滾段名稱及大小

select segment_name, tablespace_name, r.status,

(initial_extent/1024) InitialExtent,(next_extent/1024) NextExtent,

max_extents, v.curext CurExtent

From dba_rollback_segs r, v$rollstat v

Where r.segment_id = v.usn(+)

order by segment_name ;

 

40、查看控制文件

select name from v$controlfile;

select member from v$logfile;

 

41、查看錶空間的使用情況

select sum(bytes)/(1024*1024) as free_space,tablespace_name

from dba_free_space

group by tablespace_name;

SELECT A.TABLESPACE_NAME,A.BYTES TOTAL,B.BYTES USED, C.BYTES FREE,

(B.BYTES*100)/A.BYTES "% USED",(C.BYTES*100)/A.BYTES "% FREE"

FROM SYS.SM$TS_AVAIL A,SYS.SM$TS_USED B,SYS.SM$TS_FREE C

WHERE A.TABLESPACE_NAME=B.TABLESPACE_NAME AND A.TABLESPACE_NAME=C.TABLESPACE_NAME;

 

42、查看數據庫庫對象

select owner, object_type, status, count(*) count# from all_objects group by owner, object_type, status;

 

43、查看數據庫的版本 

Select version FROM Product_component_version

Where SUBSTR(PRODUCT,1,6)='Oracle';

 

44、查看數據庫的創建日期和歸檔方式

Select Created, Log_Mode, Log_Mode From V$Database;

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章