0.oracle卸载
windows系统下彻底清除oracle
1.删除oracle注册表信息.运行regedit,删除注册表项 HKEY_LOCAL_MACHINE\\SOFTWARE\\Oracle 2.删除oracle服务.oracle服务在注册表中的位置是: HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Service 删除所有以oracle字符打头的服务.
3.删除事件日志.注册表中的位置是
HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentContrilSet\\Services\\Eventlog\\Application 删除所有以oracle字符打头的键.
4.删除ORACLE环境变量,如\变量和Path变量中的oracle路径 5.删除oracle菜单.
6.删除\目录 7.重启WINDOWS. 8.删除oracle主目录.
1常见命令
Oracle的内置用户
超级用户:sys-->on_change_install,系统管理员sysdba,系统操作员sysoper 系统管理员:system-->manager,系统管理员sysdba,系统操作员sysoper sys和system的区别:能否创建数据库 conn system/manager as sysdba 示例用户:scott-->tigger
注意:scott安装完成后可能处于冻结锁定状态 为账号解锁(必须拥有解冻的权限) alter user 用户名 account unlock; 为账号更改密码
alter user 用户名 identified by 密码;
1.打开sqlplus
开始-->运行-->cmd -->sqlplus 用户名/密码 [as 身份] 身份:sysdba,sysoper,normal
eg: sqlplus system/xasxt as sysdba
2.conn[ect] 用户名/密码 [as sysdba]:连接数据库 3.disconn[ect] :断开连接 4.clear scr[een] 清屏
5.show user 显示当前登录用户
6. select * from tab;查看当前用户的表
7.desc 表名; 查看表结构
8.创建账号: create user 用户名 identified by 密码 9.为账号分配权限获取角色
系统权限允许用户执行某些数据库操作,如创建表就是一个系统权限 grant 权限/角色 to 用户名
grant create session,create table,create seqence to xiaoming;--为用户分配权限 grant connect to xiaoming;--为用户分配角色 grant resource to xiaoming;
10.移除权限
revoke create session, create table from xiaoming;
对象权限允许用户对数据库对象(如表、视图、序列等)执行特定操作 grant select,update,insert,delete on scott.emp to xiaoming; revoke select on scott.emp from xiaoming; 11. drop user 用户名 [cascade] 12.ed:编辑 13.spool d:/xxx.txt xxxxx
spool off
14.执行文件的中sql语句 \文件路径\eg: @d:1.txt
isqlplus:http://localhost:5500/em
2表管理基本的SQL语句
1.创建Person2表,并将person表中的数据插入到Person2中: --create table person2 as select * from person; 在已存在的Person2表中复制Person表中的数据: --insert into person2 select * from person;
新增:
insert into person(pid,pname,page,birthday) values(1,'aa',23,'1-1月-1988') 修改
update person set pname='bb' where pid=1; 删除:
delete from person where pid=1
2.创建表
create table person (
pid number primary key, pname varchar2(20) not null, page number(3), birthday date )
/
3.添加约束
主键约束:primary key
唯一约束:unique 非空约束:not null, Check约束:check 外键约束:foreign key 默认值:default create table person3 (
pid number primary key, pname varchar2(20) unique,
page number(3) check(page between 1 and 150),//page number(3) check(page>=1 and page<=150),
birthday date not null )
create table person5 (
pid number ,
pname varchar2(20) , page number(3),
birthday date,
address varchar2(20) default '不详'
)
alter table person4 add constraint pk_pid primary key (pid);
alter table person4 add constraint ck_page check (pid between 1 and 150);
create table classinfo(
classId number(4) primary key, className varchar2(10) not null )
create table stuinfo(
stuId number(4) primary key, stuName varchar2(10) not null,
classId number(4) --references classinfo(classId) )
alter table stuinfo add constraint fk_xx foreign key (classId) references classinfo(classId);
4.为表添加列
alter table person4 add address varchar2(50); 5.更改列的大小
alter table person4 modify pname varchar2(30); 6.查看约束
select constraint_name,table_name from user_constraints;
7.删除约束
alter table person4 drop constaint ck_pid;
8.表空间 作用:
1.可以使用表空间限制数据库文件的大小
2.利用表空间将数据文件存放到不同的磁盘上,提高IO性能,利于数据的备份和恢复
-表空间 --段(Segment) ---区(Extent) ----块(Block)
创建表空间:
CREATE TABLESPACE tablespacename DATAFILE ‘filename’ [SIZE integer [K|M]] [AUTOEXTEND [OFF|ON]];
create tablespace myspace datafile
'e:/my01.dbf' size 5 M, 'd:/my02.dbf' size 10 M
更改表空间
1.为表空间添加数据文件
alter tablespace myspace add datafile 'e:/cc.dbf' size 5m; 2.更改数据文件的大小
alter database datafile 'e:/bb.dbf' resize 5m;
3.设置数据文件自动增长(每次自动增长2M,最大为10M)
alter database datafile 'e:/cc.dbf' autoextend on next 2M maxsize 10M;
9.创建表时,为表指定表空间 create table person4 (
pid number ,
pname varchar2(20) , page number(3),
birthday date,
address varchar2(20) default '不详' )tablespace myspace;
grant create tablespace,alter tablespace to xiaoming;
10.查询表空间
select tablespace_name from user_tablespaces;
11.查询sp01表空间下的表
select table_name from all_tables where tablespace_name='sp01';
3课堂练习,基本查询
scott/tigger
--1 查询当前用户下的所有表 select * from tab;
--2 查询雇员表中所有信息 select * from emp;
--3 查询雇员编号,姓名,工作,工资 select empno,ename,job,sal from emp
--4 查询雇员编号,姓名,工作,工资,并显示中文(为列起别名)
select empno as 编号,ename as 姓名,job as 工作,sal as 工资 from emp --5 消除重复列,查询雇员工作种类 select distinct job from emp
--6 字符串连接操作(||)
--查询雇员编号,姓名,工作.按以下格工显示:编号:7369,姓名:Smith,工作:Clerk select '编号:'||empno,'姓名:'||ename,'工作:'||job from emp --7 查询列支持四则运算(年薪=(工资+奖金)*12) --查询雇员编号,姓名,工作,年薪
select empno,ename,job,(sal+nvl(comm,0))*12 from emp nvl(comm,0)==>如果comm值为空,取值0
--8 Where条件查询
-- 查询工资大于1500的所有雇员 select * from emp where sal>1500 --查询可以得到奖金的所有雇员
select * from emp where comm is not null
--查询工资大于1500或可以得到奖金的雇员 select * from emp where sal>1500 or comm is not null --查询工资大于1500并且可以领取奖金的雇员
select * from emp where sal>1500 and comm is not null --查询工资不大于1500或者不可以领取奖金的雇员 select * from emp where sal<=1500 or comm is null --查询工资在1500到3000的所有雇员信息 select * from emp where sal>=1500 and sal<=3000 select * from emp where sal between 1500 and 3000 --查询在1981年雇用的员工信息
select * from emp where hiredate like '?%' --查询雇员姓名中第二个字母为\的雇员 select * from emp where ename like '_M%'