首页 技术 正文
技术 2022年11月15日
0 收藏 544 点赞 4,565 浏览 5061 个字

接上篇博客介绍Oracle基本概要。以下将介绍数据库的操作指令。

Sql*plus经常使用命令

连接命令

1、 conn[ect]

使用方法 connusername/password@网路服务名[as sysdba/sysoper] 当用特权用户身份连接时,必须带上as sysdba或者是as sysoper

文件操作命令

1、start 和@

执行sql脚本      案例sql >@ d:\a.sql或者sql>start d:\a.sql

2、 edit

该命令能够编辑制定的sql脚本  Sql>edit d:\a.sql

3、spool

该命令能够将sql*plus屏幕上的内容输出到指定文件里去。案例:slq>spool d:\b.sql 并输入sql>spool off

显示和环境变量设置

能够用来控制输入的各种格式,set show 假设希望永久的保存相关设置。能够去改动glogin.sql脚本

1、 linesize 设置显示行的宽度。默认是80个字符

Sql>show linesize

Sql>set linesize 90

Pagesize  设置每页显示的行数目。默认是14 Set pagesize 8

2、创建用户

Create user 一般具有dba的权限才干够使用

Sql>create user xiaoming identified bym123   名字以字母开头

3、 改动password  自己password sql >password username  System 能够改动

4、删除用户  自己删自己不可。drop user ;怎样删除的用户,已经创建了表。那么须要在删除时带一个參数cascade

5、用户管理

为新用户加入登录权限:连接 sql>conn username

赋权限 sql>grant connect to xiaoming

权限:系统权限。用户对数据库的相关权限,登录,改动password等; 对象权限用户对其它用户的数据对象訪问的权限。

角色:自己定义角色;提前定义角色;Dba角色

Resourse 随意在表空间建表

建表 sql>create table test(userID varchar2(30)

对象权限分类:Select,insert,update ,delete .alll ,createindex

授权:Grant select on emp to xiaoming

Sql>conn xiaoming /m1234

Sql>select * from scott.emp;

希望用户具有全部权限:Grant all on emp to xiaoming

收回权限:Revoke select on emp from xiaoming

对权限的维护,权限的传递

假设是对象权限,增加with grand option比如:    Grant select on emp to xiaoming with grantoption

创建用户

Sql>create user xiaohong identified bym123

Sql>grant connect on xiaohong

Sql>conn xiaoming /m123

Sql>grant select on scott.emp toxiaohong

假设是系统权限

Grant connet to xiaoming with admin option

xiaoming 能够将权限下分到还有一个人

使用 profile管理用户口令(是口令限制,资源限制的命令集合)

1、 账户锁定

登录次数的限定

Sql> create profile
lock_account()规则名称 limistfailed_login_attempts 3 password_lock_time 2(天);

Sql>alter user teaprofile lock_account;

2、 给用户解锁

Sql>alter user tea account unlock;

3、终止口令

Dba身份   Sql>create profile myprofile limitpassword_life_time10 password_grace_time2;

每10天改动password。2天的延迟

Sql>alter user tea profile myprofile.

口令历史:不能使用曾经的password:

建立profile:Sql>createprofile password_historylimitpassword_life_time
10password_grace _time2password_reuse_time 10    凝视:Password_reuse_time
10天后能够反复

4、删除profile

Sql>drop profilepassword_history[cascade](级联)

Oracle表的管理

1、数据类型

字符型: Char  定长 最大2000字符

Varchar2(20) 变长,最大4000字符

Clob(character large object)字符型大对象  最大4G

数字类型

Number 范围-0的-38次方,到10的38次方

Number(5,2)表示一个小数位有5位有效数,2位小数 范围-999.99-999.99

Number(5)表示一个五位整数   -99999-99999

日期类型

Date 包括年月日和时分秒   Timestamp 扩展

图片:Blob 二进制数据,能够存放图片、声音 4g

2、建表:

Sql> create table users (usernamechar(200),

Sql>create table classes(classId

Sql> dec

3、加入字段

Sql>alter table student add(classidnumber(2));

4、改动

Sql>alter table student modify(xmvarchar(30));

5、删除

Sql>alter table student drop column sal;

6、改动变名称

Sql>rename student to stu;

7、删除表

Sql>drop table student ;

8、加入数据

Insert into student values(‘A001’,’张三‘);默认时间格式 dd-mon-yy 09-6月-99

改动日期的默认格式 alter session setnls_date_fomart=’yyyy-mm-dd’

改动后加入数据

Insert into student values(‘a002’,’mike’,’男’,‘1905-05-06‘)。

9、插入部分字段

Insert into student(xh,xm,sex)values(‘A003’,’john’,’女”)。

10、插入空值

Insert into student (xh,xm,sex,birthday)values(‘a004’,’mat’,’男’,null)

11、改一个字段

Update student set sex=’女“

12、保存点:Sql> savapoint aa;Sql>rollback to aa;回滚

13、删除数据

Delete from student

删除全部记录,表结构还在,写日志,能够恢复,速度慢

Drop table student 删除表的结构和数据

Delete from student where xh=‘a001’; 删除一条数据

Truncate table student;

删除表中的全部记录,表结构还在,不谢日志。无法找回,速度快

14、查看表结构:Sql>desc dept

Oracle查询

1、怎样取消反复行

Select distinct deptno, job from emp;

2、复制

Sql>nstert into users(userid,username,userpss) select * from users

3、使用算数表达式

4、使用列的别名:Select ename “姓名” ,en fromemp;别名

Sql> select name from emp

5、怎样处理null

使用nvl(comm,0)假设comm是空值则用0来表示

6、怎样连接字符串(||)

Select ename || ‘is a’|| job form emp;

7、使用where、

Sql> select ename,sal from emp wheresal>3000; sal>=2000 and sal|<=2500

8、使用 like

Select ename,sal from emp where emp whereename like ’_ _o%’

9、在where条件中使用in

Select *from emp  where empno in(11,234,456);

10、使用is null 的操作符

Select * from emp where mgr is null;

11、使用逻辑操作符:使用order by。使用列的别名排序

12、分页查询

Group by 用于对查询结果分组统计

Having 子句用于限制分组显示结果

13、多表查询

按部门排序

Sql>select?。?,?。from empa1,dept a2 where a1.deptno= a2.deptno order by a.deptno;

假设是group by 字段必须包括在查询的字段里面

14、自连接:是指在同一张表的链接查询

Select worker.name.boss.name  from emp worker, emp boss where  worker.mgr = boss.empno workere.name =‘frode’;

15、单行子查询

Select *from emp where deptno =(select…);     数据在运行sql是从左到右运行

16、多行子查询

Select ename,sal,dept from emp wheresal>all(select sal form emp where deptno =30);

任一:Select ename,sal,dept from emp wheresal>any(select sal form emp where deptno =30);

17、多列子查询

Select * from emp where (deptno,job)=(selectdeptno,job from emp where name=’’);

18、分页查询

右子查询过来

Select a1.* ,rownum rn from(select * from emp) al;

一共同拥有三种方式

1.      rownum 分页

select * from emp

select * from (Select a1.* 。rownum rn from (select * from emp) alwhere rownum<=10) where rn>6;

2.      显示rownum

Select  al.* ,rownum  rn form emp

19、建表并将其它表数据导入到新表

Create table mytable (id,name ,sal,job,deptno) as select empno,ename,sal ,job ,deptbo from emp;

20、合并查询

1 union 取得两个结果集的并集,自己主动去掉反复行。

Select ename,sal ,job from emp wheresal>2500 union select ename ,sal,job from emp where job =’manager’;

2 union all   同Union ,但不取消反复行,并且不会排序

Select ename,sal ,job from emp wheresal>2500 union all select ename ,sal,job from emp where job =’manager’;

3 minus合并查询

获得两个结果集的差集,仅仅显示存在第一个集合中。而不存在第二集合中的数据

Select ename,sal ,job from emp wheresal>2500 minus select ename ,sal,job from emp where job =’manager’;

Orale视图

    特点:不占磁盘;不能加入索引。使用视图能够简化
复杂查询。提高安全性。

创建视图

Create view
视图名 as select
语句[with read only]

创建或改动视图

Create or replaceview
视图名 as select
语句[withread only]

删除视图

Drop view
视图名

以上是主要的操作命令,一開始认为挺多的,可是后来觉着这些东西跟SQL Server的SQL语句一样。

所以学习的东西就少了非常多。

只是这些东西挺多的,所以就懒下别记了。什么时候用什么再回来看看,孰能生巧。这些命令特别是数据库的查询,怎样写,将影响到数据的查询性能。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898