ORACLE學習筆記
數據庫全名是:ORACLEDB ; Net本地服務名:ORACLEDB01;用戶名:haha;密碼haha。
在數據庫里有一個測試用的表:student
一:查詢語句
select * from student;
select name,age from student;
select name as "姓名" from student;
select name as 姓名 from student;
select name 姓名 from student;
select * from student where age>23;
二:插入數據
insert into student(id,name,age) values ('7','張三','34');
insert into student(id,name,age,adddate) values ('7','zhangsan','55',TO_DATE('2002-2-2','yyyy-mm-dd'));
三:更新數據
update student set name = '王五' where id = '7';
四:刪除數據
delete student where id = '7';
五:看一個表的詳細結構
desc student;
六:連接到某個數據庫
Connect haha/haha@oracledb01
七:從一張表來創建另一張表并復制其全部數據
create table student01
as
select * from student;
八:從一張表來創建另一張表并指明用那些字段
create table student02
as
select id,name from student;
九:清空表中所有的數據
truncate table student02;
十:返回數據庫中前五條記錄
select * from student where rownum<6;
select * from student where rownum!=7;
十一:取2-5之間的記錄
select * from (
select rownum r,id,name,age,adddate from student
where rownum <=5 order by name
)
where r>2;
十二:(事務控制)設置事務標記點,可以回滾到你設置的標記點
update student set age = '88' where id = '2';
savepoint mark01;
delete from student where id = '2';
savepoint mark02;
rollback to mark01;
commit;
十三:提交
Commit;
十四:設置標記點:
Savepoint 標記點名稱;
十五:回滾到你設置的標記點
rollback to標記點名稱;
十六:查詢一定范圍的記錄
select * from student where age in('20','23');說明:年齡在20到23之間的記錄不包含20和23
select * from student where age between 20 and 23; 年齡在20到23之間的記錄
select * from student where age not between 20 and 23; 年齡不在20到23之間的記錄
select * from student where age>20 and age<40;
select * from student where age>20 or age<40;
select * from student where not age>22
十七:模糊查詢
select * from student where age like '%2_%'
十八:intersect操作符返回兩個表公共的記錄
select * from student01
intersect
select * from student;
十九:MINUS 操作符返回從第一個查詢結果中排除第二個查詢中出現的行
select * from student
minus
select * from student01;
二十:集合操作符
select * from student
union
select * from student01;
二十一:集合操作符
select * from student
union all
select * from student01;
二十二:字符函數:把小寫轉化成大寫
select upper('sun') from dual;
二十三:數學函數:取絕對值
select abs(-30) from dual;
二十四:取出數據庫總記錄
select count(*) from student;
二十五:取平均數
select avg(age) from student;
二十六:取某個字段中最大值
select max(age) from student;
二十六:取某個字段中最小值
select min(age) from student;
二十七:取某個字段中所有數據的和
select sum(age) from student;
二十八: ROWID的操作
select rowid from student;
declare
num varchar(30);
test student%rowtype;
begin
select rowid into num from student where id = '1';
dbms_output.put_line(num);
end;
二十九:查看用戶詳細信息
select * from dba_users where username = 'haha';
三十:連接數據庫
connect haha/haha@oracledb01
connect + 用戶名/密碼@本地net服務名
三十一:創建表
create table student01(
stu_id number,
stu_name varchar(50),
stu_age number
)
三十二:增加表字段
格式:Alter table 表名 Add 字段名 字段類型
實例:alter table student01 add adddate date
三十三:修改表字段(未調試通)
alter table student01 alter column adddate varchar(50)
三十四:刪除表中的字段
alter table student01 drop column adddate
三十五:刪除表
drop table student01
三十六:創建表并加上主鍵或者某字段不為空
create table student01(
stu_id int primary key,
stu_name varchar(50) not null,
stu_age number
)
三十七:對數據庫的備份,即:導出和導入
導出:
exp username/userpwd@oracledb file=d:\xx.dmp
導入:
imp username/userpwd@soracledb file=d:\xx.dmp full=y
三十八:創建自動增長數據無重復的列
比如說:一個表主鍵id每次新增記錄時id號自動加一并且不會和現有記錄發生沖突
先看例子:
先創建一個表(worker):
create table worker(
work_id number primary key not null,
work_name varchar2(100) not null,
work_age number,
work_sex char(10)
)
在這張表里主鍵是work_id我們想每次增加一個記錄時主鍵自動填充并是唯一的一個數
這就需要創建一個序列:
再創建一個序列(workerId):
create sequence workerId;
實現自動填充唯一記錄主要是讓序列自動填充的
現再可以測試是否是正確的了,我們新增記錄看看:
insert into worker values (workerId.Nextval,'張三','23','男');
commit;
^_^好像是對了。
三十九:使用客戶端連接遠程oralce數據庫服務器
Connect 用戶名/密碼@219.233.196.26
sqlplus 用戶名/密碼@//IP地址:端口/SID號
四十:顯示數據庫中所有表
Select * from tab
四十一:刪除用戶命令
Drop user lyz cascade;
四十二:插入數據庫記錄時字段為日期類型
to_date('2008-4-4 12:2:2','YYYY-MM-dd hh24:mi:ss')
insert into " table" values (1,'test',12,to_date('2008-3-3 12:2:2','YYYY-MM-dd hh24:mi:ss'));
四十三:查詢oralce數據庫時間字段,顯示時分秒
select to_char(UpdTime,'HH24:MI:SS') from RunCur_Video
四十四:怎么察看oracle有多少個連接
select * from v$license; 可以查看你是否設了連接限制等,同時也能很清楚的顯示有多少session