Oracle常用命令

Oracle數據類型:

Create table test1(name char(10),sex char(1));
Insert into test1 values(‘tomcatt北京’,’f’);

Create table test2(name nchar(10),sex nchar(1));
Insert into test2 values(‘tomcatt北京’,’男’);

刪除表 drop table 表名;

Create table test3(name varchar2(10),sex varchar2(2));
Insert into test3 values(‘tomcatt北京’,’f’);//插入值過大
Insert into test3 values(‘tomcat北京’,’f’);

Create table test4(name varchar2(10),age number(3),salary number(8,2));

Create table test5(name varchar2(10),birth date);
Insert into test5 values(‘Tom’,’28-2月-08’);
Insert into test5 values(‘Allen’,sysdate);


DDL:
創建表
create table scott.test6(
 eid number(10),
 name varchar2(20),
 hiredate date default sysdate,
 salary number(8,2) default 0
)
插入數據時若沒有指定hiredate,salary的話則會取默認值


數據字典:

Dba-所有方案包含的對象信息
All-用戶可以訪問的對象信息
User-用戶方案的對象信息

Select * from user_tables;
Select * from all_tables;


約束:
域完整性約束:not null check
實體完整性約束:unique primary key
參照完整性約束:foreign key


視圖:
Create or replace view v1(eid,name,salary) as select empno,ename,sal from emp where deptno = 30;


序列:sequence
Create sequence mysequence1 increment by 1 start with 1 nomaxvalue nocycle;

Insert into test values(mysequence1.nextval,’tom’);


Create sequence student_sequence start with 1 increment by 1;
Insert into student values(student_sequence.nextval,’john’);


表間數據拷貝:
Insert into dept1(id,name) select deptno,dname from dept;


實例(創建表 ID字段自增):
--create table test2(id char(10) primary key not null, name char(10));
--create sequence test2_sequence increment by 1 start with 1 nomaxvalue nocycle;
--insert into test2 values(test2_sequence.nextval,'john');
--insert into test2 values(test2_sequence.nextval,'allen');
--insert into test2 values(test2_sequence.nextval,'candy');
--insert into test2 values(test2_sequence.nextval,'aaaa');
--insert into test2 values(test2_sequence.nextval,'bbbbb');
--insert into test2 values(test2_sequence.nextval,'cccccc');
--insert into test2 values(test2_sequence.nextval,'ddddd');
--insert into test2 values(test2_sequence.nextval,'eeeee');
--insert into test2 values(test2_sequence.nextval,'ggggg');
--insert into test2 values(test2_sequence.nextval,'jowwwwhn');
--insert into test2 values(test2_sequence.nextval,'aaaadd');
--insert into test2 values(test2_sequence.nextval,'ggghhh');
--insert into test2 values(test2_sequence.nextval,'eeettt');
--insert into test2 values(test2_sequence.nextval,'wwwttt');
select * from test2;

查看錶結構
EDITDATA 表名;

修改表字段:
Alter table 表名 modify(字段名 類型 約束);
alter table test modify (addd varchar2(10) null);

alter table 表名 add(字段名 類型 約束);
alter table test add(age varchar2(5));


1.登陸系統用戶
sqlplus 然後輸入系統用戶名和密碼
登陸別的用戶
conn 用戶名/密碼;
2.創建表空間
create tablespace 空間名
datafile 'c:\空間名' size 15M --表空間的存放路徑,初始值爲15M
autoExtend on next 10M --空間的自動增長的值是10M
permanent online; --永久使用
3.創建用戶
create user shi --創建用戶名爲shi
identified by scj --創建密碼爲scj
default tablespace 表空間名 --默認表空間名
temporary tablespace temp --臨時表空間爲temp
profile default --受profile文件的限制
quota unlimited on 表空間名; --在表空間下面建表不受限制

4.創建角色
create role 角色名 identified by 密碼;
5.給角色授權
grant create session to 角色名;--給角色授予創建會話的權限
grant 角色名 to 用戶名; --把角色授予用戶
6.給用戶授予權限
grant connect,resource to shi;--給shi用戶授予所有權限
Grant dba to shi;-給shi 用戶授予DBA權限
grant create table to shi; --給shi用戶授予創建表的權限
7.select table_name from user_tables;   察看當前用戶下的所有表
8.select tablespace_name from user_tablespaces; 察看當前用戶下的 表空間
9.select username from dba_users;察看所有用戶名稱命令 必須用sys as sysdba登陸
10.創建表
create table 表名
(
id int not null,
name varchar2(20) not null
)tablespace 表空間名 --所屬的表空間
storage
(
   initial 64K --表的初始值
   minextents 1 --最小擴展值
   maxextents unlimited --最大擴展值
);
11.--爲usrs表添加主鍵和索引
alter table users
add constraint pk primary key (ID);
12.爲已經創建users表添加外鍵
alter table users
  add constraint fk_roleid foreign key (roleid)
  references role(role_id) on delete cascad; --下邊寫主表的列
on delete cascad是創建級聯
13.把兩個列連接起來
select concat(name,id) from 表名; --把name和id連接起來
14.截取字符串
select column(name,'李') from 表名; --把name中的‘李’去掉
15.運行事務之前必須寫
set serveroutput on; --打開輸入輸出(不寫的話,打印不出信息)
16.while的應用
declare --聲明部分
ccc number:=1; --復職
a number:=0;
begin --事務的開始
while ccc<=100 loop --循環
if((ccc mod 3)=0) then --條件
dbms_output.put_line(ccc||',');    --打印顯示
a:=a+ccc;
end if; --結束if
ccc:=ccc+1;
end loop; --結束循環
dbms_output.put_line(a);
end; --結束事務
/
17.select into 的用法 --只能處理一行結果集
declare
  name varchar(30);
begin
select username into name
from users
where id=2;
dbms_output.put_line('姓名爲:'||name);
end;
/
18.利用%rowtype屬性可以在運行時方便的聲明記錄變量和其他結構
Set serveroutput on;
Declare
   utype users%rowtype;
Begin
Select * into utype from users where id=20;
Dbms_output.put_line('姓名'|| utype.username);
Dbms_output.put_line('生日'|| utype.brithday);
end;
/ --%rowtype想當於複製一個表
19.遊標的定義和使用
Declare
Cursor ucur is select * from users; --聲明遊標
Us users%rowtype;--定義與遊標想匹配的變量
Begin
Open ucur;--打開遊標
Fetch ucur into us;
While ucur %found loop --使用循環遍歷遊標的查詢結果
Dbms_output.put_line('姓名:'||us.username||'生日'||us.brithday);
Fetch ucur into us;
End loop;
Close ucur; --關閉遊標
End;
=======================================
%found在前一條的fetch語句至少對應數據庫的一行時,%found屬性值爲true,否則爲false;
% notfound 在前一條fetch語句沒有對應的數據庫行時,%notfound屬性值爲true,否則爲false;
%isopen 在遊標打開時%isopen屬性值爲true;否則爲false;
%rowcount顯示迄今爲止從顯示遊標中取出的行數

20.
刪除
drop tablespace 空間名 including contents; --刪除表空間和裏面的內容
drop table 表名 --刪除表
drop user 用戶名 --刪除用戶


查看有哪些表空間:
Select tablespace_name from dba_tablespaces;

查看某個表屬於哪個表空間:
Select tablespace_name from tabs where table_name=表名;

在指定的表空間創建表:
create   table(...)   tablespace   users;
查詢前N條數據:
select * from 表名 where rownum<=N;

Oracle中創建一個數據庫:
打開Database Configuration Assistant,可以通過視圖創建數據庫。創建成功後,系統服務裏面會多一個OracleServer 數據庫名 的服務項。

查看所有表空間大小:
select * from dba_free_space;

問題:user lacks create session privilege logon denied
grant create session to the_user;

刪除用戶:
Drop user 用戶名 cascade;

導入數據庫對象:
IMP <用戶名>/<口令>@<數據庫別名> file=<導出數據文件名> fromuser=<導出用戶> tousr=<導出用戶>
如:IMP jcyy/jcyy@jcyy file=d:\jcyy.dmp fromuser=jcyy touser=jcyy

導出數據庫對象:
EXP <用戶名>/<口令>@<數據庫別名> file=<導出文件名> owner=<導出用戶>
如:EXP jcyy/jcyy@jcyy file=d:\lo.dmp owner=jcyy

 

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