oracle入门10g

oracle安装自动生成两个用户分别是:sys/change_on_install、system/manager,最大区别是system没有create database的权限。

sqlplus登录

1.

#sqlplus / as sysdba

2.

#sqlplus /nolog

>conn /as sysdba

>conn sys/password as sysdba

3.

#sqlplus username/passwd@tnsname

sqlplus常用命令

>conn username/passwd@tnsname --连接

>disc --断开连接

>show user --显示当前用户名

>exit --断开连接并退出sqlplus

>@ a.sql --执行sql脚本

>start a.sql --执行sql脚本

>edit a.sql --编辑sql脚本

>spool b.sql --屏幕输出内容到b.sql

>spool off --关闭输出

>show linesize --显示行宽度

>set linesize 100 --设置行宽度

>show pagesize --设置页大小

>set pagesize 100 --设置页大小

--可以类似设置其他环境变量

用户管理

>create user abc identified by cba --创建abc/cba

>alter user abc identified by a123 --修改abc/a123

>drop user abc cascade --带表删除数据库abc

-------example1----------------------------------

create user ming identified by m1;

conn / as sysdba;

grant connect to ming; --授权connect权限

conn ming/m1;

conn scott/tiger;

grant select on scott.emp to ming with grant option;

revoke select on emp from ming; --回收select权限

-------example1 end-----------------------------

表管理

常用数据类型:char, varchar2, number, number(5,2), number(5), date, timestamp, blob, .......

---------example2---------------------

create table student(sid number(4),name varchar2(20),sex char(2),birthday date,salary number(7,2));

create table class(cid number(2),cname varchar2(40));

alter table student add (cid number(2)); --为student表增加cid字段

alter table student modify (name varchar2(30)); --修改字段类型,不能有记录

alter table student drop column salary; --删除字段

rename student to stu; --重命名

drop table student; --删除表

insert into stu values(1,'小明','男','01-10月-88',100); --为所有字段添加数据

alter session set nls_date_format='yyyy-mm-dd'; --修改日期格式

insert into stu(sid,name,sex,birthday) values(2,'小红','女','1990-01-01'); --选择字段添加数据

select * from stu where salary is not null;

update stu set salary=20 where sid=2;

delete from stu; --删除所有记录

savepoint a; --创建保存点a

rollback to a;--恢复到保存点a

delete from stu where sid=1;

truncate table stu; --清空表

---------example2 end----------------

查询

--------example3---------------------

conn scott/tiger;

desc emp;

select * from dept;

set timing on; --打开显示操作时间的开关

create table users(id varchar2(10),name varchar2(20),passwd varchar2(30));

insert into users values('001','aaa','123456');

insert into users(id,name,passwd) select * from users; --自我复制记录

select count(*) from users;

select ename,sal,job from emp;

select distinct ename,sal,job from emp; --去重

select depno,job,sal from emp where ename='SMITH';

select sal*13+nvl(comm,0)*13 "年薪",ename,comm from emp;

select ename ||' is a '|| job from emp; --字串连接

select * from emp where sal>=200 and sal<=300;

select * from emp where ename like '_S%';

select * from emp where mgr is null;

select * from emp where empno in(1234,5678,4321);

select * from emp order by deptno, sal desc;

clear; --清屏

select max(sal),min(sal),avg(sal),sum(sal),count(sal) from emp group by deptno,job having avg(sal)<2000;

select e.ename,e.sal,d.dname from emp e,dept d where e.deptno=d.deptno; --多表查询

--子查询

select * from emp where deptno=(select deptno from emp where ename='SMITH'); --子查询结果为单值

select * from emp where job in(select distinct job from emp where deptno=10); --子查询结果为多值

--------example3 end---------------

事务

事务用于保证数据的一致性,由一组相关的dml语句组成,该组语句要么全做,要么全不做。

当执行事务操作时,oracle会在被作用的表上加锁,防止其他用户修改表的结构。

提交事务commit。

回退事务rollback,必需在没有commit前做,如果没有commit而exit了,则自动提交哦。

设置只读事务:set tansaction read only

导入导出表

>show parameter; --显示初始化参数

####导出表####

#exp userid=scott/tiger@ora1 tables=(emp,dept) file=/usr/local/tmp/e1.dmp

#exp userid=scott/tiger@ora1 tables=(emp) file=e2.dmp rows=n //导出表结构

#exp userid=scott/tiger@ora1 tables=(emp) file=e3.dmp direct=y //直接导出方式

####导出数据库####

#exp userid=system/manager@ora2 full=y inctype=complete file=e4.dmp

####导入表####

#imp userid=scott/tiger@ora3 tables(emp,dept) file=e1.dmp;

#imp userid=scott/tiger@ora3 tables(emp,dept) file=e1.dmp touser=scott;

#imp userid=scott/tiger@ora3 tables(emp,dept) file=e1.dmp rows=n; //导入表结构

#imp userid=scott/tiger@ora3 tables(emp,dept) file=e1.dmp ignore=y; //如果对象存在只导入内容

####导入数据库####

#imp userid=system/manager full=y file=e2.dmp

管理表空间和数据文件

表空间是数据库的逻辑组成部分。从物理上,数据库存放在数据文件中;从逻辑上,数据库存放在表空间中,表空间由一个或多个数据文件组成。

>create tablespace data1 datafile '/usr/local/tmp/data1.dbf' size 20m uniform size 128k; --创建区大小为128k空间大小为20m的表空间

>create table tab1(id number(4),name varchar(20)) tablespace data1; --使用表空间

>alter tablespace data1 offline; --脱机

>alter tablespace data1 online; --在线

>alter tablespace data1 read only; --只读

>alter tablespace data1 read write; --读写

>select * from all_tables where tablespace_name='data1';  --查看表空间包括的所有表

>select tablespace_name,table_name from user_tables where table_name='emp'; --查看emp表属于哪个表空间

>drop tablespace 'data1' including contents and datafiles; --删除表空间

>alter tablespace data1 add datafile '/usr/local/tmp/data1_1.dbf' size 20m; --新增数据文件的方式扩展表空间

>alter tablespace data1 '/usr/local/tmp/data1.dbf' resize 100m; --增加数据文件大小的方式扩展表空间

>alter tablespace data1 '/usr/local/tmp/data1.dbf' autoextend on next 10m maxsize 500m; --自动扩展表空间

>select tablespace_name from dba_tablespaces; --查看所有表空间信息

>select file_name,bytes from dba_data_files where tablespace_name='data1'; --查看表空间含有哪些数据文件

约束

约束用于确保数据库数据满足特定的商业规则。oracle中的约束包括:not null、unique、primary key、foreign key、check五种。

>alter table stu modify name not null; --修改约束

>alter table stu add constraint aaa unique(sid); --添加约束aaa

>alter table stu drop constraint aaa; --删除约束aaa

>alter table stu drop primary key cascade; --消除主从关系删除主键

>create table emp2(id number(12) constraint pk_emp primary key, name varchar2(20) not null unique); --列级约束

>create table emp3(id number(10), name varchar(20), constraint pk_emp primary key (id,name)); --表级约束

索引

索引是用于加速数据读取的数据对象。

>create index idx_id on stu(sid); --创建单列索引idx_id

>create index idx_a on stu(sid,name); --创建多列索引

>drop index idx_id; --删除索引

视图

视图是一个虚拟表,其内容由查询定义。

>create view myview as select * from emp where sal<1000; --创建视图myview

>create view myview2 as select emp.empno,emp.ename,dept.dname from emp,dept where emp.deptno=dept.deptno;

>drop view myview; --删除视图

补充

查看所有表名:select table_name from user_tables;

原文地址:https://www.cnblogs.com/feilv/p/4071444.html