1、連接
SQL*Plus system/manager
2、顯示當前連接用戶
SQL> show user
3、查看系統擁有哪些用戶
SQL> select * from all_users;
4、新建用戶並授權
SQL> create user a identified by a;(默認建在SYSTEM表空間下)
SQL> grant connect,resource to a;
5、連接到新用戶
SQL> conn a/a
6、查詢當前用戶下所有對象
SQL> select * from tab;
7、建立第一個表
SQL> create table a(a number);
8、查詢表結構
SQL> desc a
9、插入新記錄
SQL> insert into a values(1);
10、查詢記錄
SQL> select * from a;
11、更改記錄
SQL> update a set a=2;
12、刪除記錄
SQL> delete from a;
13、回滾
SQL> roll;
SQL> rollback;
14、提交
SQL> commit;
用戶授權:
GRANT ALTER ANY INDEX TO "user_id "
GRANT "dba " TO "user_id ";
ALTER USER "user_id " DEFAULT ROLE ALL
創建用戶:
CREATE USER "user_id " PROFILE "DEFAULT " IDENTIFIED BY " DEFAULT TABLESPACE "USERS " TEMPORARY TABLESPACE "TEMP " ACCOUNT UNLOCK;
GRANT "CONNECT " TO "user_id ";
用戶密碼設定:
ALTER USER "CMSDB " IDENTIFIED BY "pass_word "
表空間創建:
CREATE TABLESPACE "table_space " LOGGING DATAFILE 'C:\ORACLE\ORADATA\dbs\table_space.ora' SIZE 5M
------------------------------------------------------------------------
1、查看當前所有對象
SQL > select * from tab;
2、建一個和a表結構一樣的空表
SQL > create table b as select * from a where 1=2;
SQL > create table b(b1,b2,b3) as select a1,a2,a3 from a where 1=2;
3、察看數據庫的大小,和空間使用情況
SQL > col tablespace format a20
SQL > select b.file_id 文件ID,
b.tablespace_name 表空間,
b.file_name 物理文件名,
b.bytes 總字節數,
(b.bytes-sum(nvl(a.bytes,0))) 已使用,
sum(nvl(a.bytes,0)) 剩余,
sum(nvl(a.bytes,0))/(b.bytes)*100 剩余百分比
from dba_free_space a,dba_data_files b
where a.file_id=b.file_id
group by b.tablespace_name,b.file_name,b.file_id,b.bytes
order by b.tablespace_name
/
dba_free_space --表空間剩余空間狀況
dba_data_files --數據文件空間占用情況
4、查看現有回滾段及其狀態
SQL > col segment format a30
SQL > SELECT SEGMENT_NAME,OWNER,TABLESPACE_NAME,SEGMENT_ID,FILE_ID,STATUS FROM DBA_ROLLBACK_SEGS;
5、查看數據文件放置的路徑
SQL > col file_name format a50
SQL > select tablespace_name,file_id,bytes/1024/1024,file_name from dba_data_files order by file_id;
6、顯示當前連接用戶
SQL > show user
7、把SQL*Plus當計算器
SQL > select 100*20 from dual;
8、連接字符串
SQL > select 列1 | |列2 from 表1;
SQL > select concat(列1,列2) from 表1;
9、查詢當前日期
SQL > select to_char(sysdate,'yyyy-mm-dd,hh24:mi:ss') from dual;
10、用戶間復制數據
SQL > copy from user1 to user2 create table2 using select * from table1;
11、視圖中不能使用order by,但可用group by代替來達到排序目的
SQL > create view a as select b1,b2 from b group by b1,b2;
12、通過授權的方式來創建用戶
SQL > grant connect,resource to test identified by test;
SQL > conn test/test
13、查出當前用戶所有表名。
select unique tname from col;
-----------------------------------------------------------------------
alter table alist_table add address varchar2(100);
alter table alist_table modify address varchar2(80);
create table alist_table_copy as select ID,NAME,PHONE,EMAIL,
QQ as QQ2,
ADDRESS from alist_table;
drop table alist_table;
rename alist_table_copy to alist_table
空值處理
有時要求列值不能為空
create table dept (deptno number(2) not null, dname char(14), loc char(13));
在基表中增加一列
alter table dept
add (headcnt number(3));
修改已有列屬性
alter table dept
modify dname char(20);
注:只有當某列所有值都為空時,才能減小其列值寬度。
只有當某列所有值都為空時,才能改變其列值類型。
只有當某列所有值都為不空時,才能定義該列為not null。
例:
alter table dept modify (loc char(12));
alter table dept modify loc char(12);
alter table dept modify (dname char(13),loc char(12));
查找未斷連接
select process,osuser,username,machine,logon_time ,sql_text
from v$session a,v$sqltext b where a.sql_address=b.address;
-----------------------------------------------------------------
1.以USER_開始的數據字典視圖包含當前用戶所擁有的信息, 查詢當前用戶所擁有的表信息:
select * from user_tables;
2.以ALL_開始的數據字典視圖包含ORACLE用戶所擁有的信息,
查詢用戶擁有或有權訪問的所有表信息:
select * from all_tables;
3.以DBA_開始的視圖一般只有ORACLE數據庫管理員可以訪問:
select * from dba_tables;
4.查詢ORACLE用戶:
conn sys/change_on_install
select * from dba_users;
conn system/manager;
select * from all_users;
5.創建數據庫用戶:
CREATE USER user_name IDENTIFIED BY password;
GRANT CONNECT TO user_name;
GRANT RESOURCE TO user_name;
授權的格式: grant (權限) on tablename to username;
刪除用戶(或表):
drop user(table) username(tablename) (cascade);
6.向建好的用戶導入數據表
IMP SYSTEM/MANAGER FROMUSER = FUSER_NAME TOUSER = USER_NAME FILE = C:\EXPDAT.DMP COMMIT = Y
7.索引
create index [index_name] on [table_name]( "column_name ")