第一节
1. 数据类型:
字符类型:char(标准通用拉丁字符),nchar(汉字等其他字符),varchar2(长度可变字符),nvarchar2,long;
数字类型:number(通用),integer,float
日期和时间:date,timestamps(分秒,时区)
行:rowid(逻辑地址),urowid(逻辑地址,内存地址);二进制:raw(size)(原始二进制数据),long raw,blob(二进制大型对象;最大4G字节),clob(字符大型对象),nclob,bfile;
2.oracle WEB管理页面:localhost:5560/isqlplus; localhost:5500/em
3.net设置远程测试:tnsping datebasename;
远程连接:sqlplus name/password@datebasename;
4.创建表空间:
create tablespace test
datafile 'test.dbf' size 10m autoextend on next 2m
maxsize unlimited
logging
permanent
extent management local autoallocate
blocksize 8k
segment space management manuaL;//段空间
5.创建用户并连接:
create user "TEST" identified by "TEST"
default tablespace TEST
temporary tablespace TEMP
quota unlimited on TEST
quota unlimited on TEMP
grant "connect" to test//分配基本权限。
conn test/test;
解锁:alter user scott account unlock;
7.sql脚本的执行:@路径/filename.sql;其他:in/not in,between..and..,is null/is not null,like,exists/not exists;
Eg:select count(distinct c1) as 种类 from t1 where c1 like ‘%l%’(模糊查询如m_n)(c1 between 10 and 100) group by c1 order by c1 desc,c2 desc(不写就默认asc)17.声明主键和check:一.create table t1(c1 primary key)
三:create table t1(emp_no number not null , constraint pk_emp primary key(emp_no));
为已经存在的表增加主键:alter table t1 add constraint pk_emp2 primary key (c1);
创建check: create table t1(budget number(7),constraint CHK_PROJ check(budget>10000 and budget<1000000))18.创建外键: create table t1(dept_no varchar2(4) not null, constraint fk_emp foreign key (dept_no) references t2(dept_no),
对已经存在表创建外键:alter table t1 add constraint foreign_work_emp foreign key(c1) references t2(c1);
删除一个外键alter table t1 drop constraint foreign_work_emp;删除记录:delete from t1 where;truncate table t1;drop table t1;
21.合并查询:intersect(select * intersect select *),union(去掉空),union all(包括空),minus(减集);
when 'p1' then case when enter_dateto_date('1998-10-1','yyyy-mm-dd')then '等两年'//时间非字段时间型
else '两室一厅'end
when 'p2' then case when enter_dateto_date('1998-10-1','yyyy-mm-dd')
then '看情况再分' else ' 一室一厅'end
end as 分房情况from works_on;
if then end if,loop end loop,declare type *,while( )loop end loop,case when then end
declare icount number;
begin
icount :=0;
for mycount in reverse 0..10//mycount 为自定义变量
---while(icount<100)
Loop icount :=icount+10;
---exit when(icount>100);
if(mycount>5) then
dbms_output.put_line('result='||icount);//流程输出语句
end if;
end loop;
end; //(set serverout on才能输出)
28.创建函数:
create or replace function get_maxe_empno return varchar2 is
tmpvar varchar2(20);//局部变量声明
begin //可执行语句
select max(emp_no) into tmpvar from employee;//把取出的值赋给一个变量
return tmpvar;//函数的重要特征就是通过return语句来传递参数
end get_maxe_empno;
29.过程:
create or replace procedure sp_test
(fname in varchar2, update_count out int)//参数列表是过程的特征 is
cursor emp_cursor//定义游标
is
select emp_no from employee where employee.emp_fname=fname;//输入in
begin // 可执行语句
update_count:=0;
for emp_rec in emp_cursor
loop
update employee set emp_fname=emp_lname,emp_lname=fname
where employee.emp_no=emp_rec.emp_no;update_count:=update_count+1;
end loop;commit;//控制语句,强制执
end;//过程可以有in/out变量
查看过程:
select distinct(name) from user_source where type='PROCEDURE'
select text from all_source where name='SP_TEST' order by line;
使用过程:
declare fname varchar2(200);
update_count number;
begin
fname:='John';update_count:=0;
sp_test(fname,update_count);commit;dbms_output.put_line(update_count);
end;
30.触发器:
create or replace trigger tgr_emp_change
after insert or update or delete on employee
referencing new as n old as o// :new为引用最新列值; :old为引用以前列值;
for each row
begin
if inserting then
insert into emp_update_info(emp_no,oper_type) values(:n.emp_no,1);
end if;
if updating then // updating为触发器的操作符;
insert into emp_update_info(emp_no,oper_type) values(:o.emp_no,3);
end if;
end;
建表:create table emp_update_info(emp_no varchar2(20) not null,oper_type integer);
31.声明包:
create or replace package pkg_test as
function get_max_empno return varchar2;//子程序说明,公共对象声明
procedure sp_test(fname in varchar2,update_count out int);
end pkg_test;
创建包体:create or replace package body pkg_test as // is也行
function get_max_empno return varchar2 as //公共类型与对象声明
tmpvar varchar2(20);//公共类型和对象声明,
begin // 语句体中是初始化语句,特殊用途
select max(emp_no) into tmpvar from employee; return tmpvar;
end get_max_empno;
//子程序主体部分
procedure sp_test(fname in varchar2,update_count out int) is
cursor emp_cursor is
select emp_no from employee where employee.emp_fname = fname;
begin
update_count := 0;
for emp_rec in emp_cursor
loop
update employee set emp_fname = emp_lname, emp_lname = fname
where employee.emp_no = emp_rec.emp_no;
update_count := update_count + 1;
end loop;
commit;//强制执行
end;
end pkg_test;
32.数据库设计:业务规则和流程—实体和关系—ER图(一对一,一对多,出现多对多进行拆分)—SQL语句—视图和索引—储存过程和触发器—维护。
第二节
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, /*qq 改为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 whe
第三节
create index [index_name] on [table_name]( "column_name ")
表:
建表:
create table user_list
(
user_name varchar2(23),
user_age int
);
查看表结构:
desc user_list;
添加字段:
alter table user_list
add (userno int );
修改字段:
alter table user_list
modify ( user_name varchar2(50));
删除字段:
alter table user_list
drop column user_age;
对表添加数据:
insert into user_list(user_name ,userno) values('gouchao' ,10001);
insert into user_list values('xuyongchao',10003);
insert into user_list values('gouchao' ,S_userno.Nextval);
对表修改数据 :
update user_list
set user_name ='gouli' where user_name ='gouchao';
对表删除数据:
delete from user_list where userno = 10001;
删除表格:
drop table user_list;
查询:
查询所有记录:
select * from user_list;
select userno from user_list;
select userno ID from user_list;
select user_list.userno from user_list;
select a.userno from user_list a ;
查询所有记录的某些字段:
select empno ,ename from emp;
查询某些字段的不同记录:
select distinct job from emp;
单条件 的查询:
=
select * from emp where job='CLERK';
!=
select * from emp where job!='CLERK';
>
select * from emp where sal > 1600;
select * from emp where sal > all(select sal from emp where job='MANAGER');
喜欢我的文章,请关注我的公众号