clip_image002

常见的数据库

小型数据库 access(微软)

中型数据库 mysql(瑞典MySql AB) sql server(美微软) informix(美IBM)

大型数据库 sybase(美sybase) oracle(美oracle) db2(美IBM)

Oracle基本操作

一、Oracle数据库基本命令

1.SQL->show user; 显示用户 2.SQL->conn scott/tiger;用户连接 3.SQL->disc; 断开连接 4. SQL->passw; 修改密码 5. SQL->exit; 退出 6. SQL->start D:\test.sql; 运行D盘下test.sql脚本7. SQL->edit D:\test.sql; 编辑D盘下test.sql脚本

8. SQL->spool D:\test.sql;

SQL->select * from emp;

SQL->spool off;

二、Oracle管理用户

1.创建用户(以DBA身份)

SQL->create user name identified by password(以字母开头)

注意:新建用户默认是没有任何权限的,需要DBA赋予权限(1、系统权限:对数据库的相关权限;2、对象权限:用户对其他用户数据对象(表、视图等)操作的权限)

2. grant赋予用户权限

SQL->grant connect to username;(把角色connect赋予username(dba 、resource))

SQL->grant select on emp to username;(emp表的用户scott或sys、system把select权限赋予username)

username 用户可以进行select操作了,SQL->select * from scott.emp;(查询emp表的内容)

SQL->grant select on emp to username;(把全部权限赋予)

3.revoke回收用户权限

SQL->revoke select on emp from username; (scott收回用户username对用户scott表emp的select权限(必须是scott赋予username的权限))

4.权限的传递

对象权限:SQL->grant select on emp to username with grant option;

系统权限:SQL->grant select on emp to username with admin option;

5.给用户修改密码

SQL->password username;(password scott ;sys修改scott密码)

SQL->password;(scott修改自己密码)

6.使用profile管理用户口令

账户锁定

SQL->create profile lockname limit failed_login_attempts 3 password_lock_time 2;(以dba身份)创建配置文件,名字为lockname。

SQL->alter user username profile lockname;

账户解锁: SQL->alter user username account unlock;

删除profile :SQL->drop profile lockname;

Oracle表的管理

一、表和列的命名规则

· 必须以字母开头

· 不能使用oracle的保留字

· 只能使用A-Z,a-z,0-9,$,#等

· 长度限制为30个字符内

二、oracle的数据类型

1.字符型

char() 定长 特点:查询速度快,效率高 ;浪费空间 最大:char(2000)

varchar2()可变长度 特点:节省空间,效率低 最大varvhar(4000)

clob()字符型大对象 最大4G

2.数字类型

number 可以表示整数和小数

number(5,2)一个小数有5位有效数,2位小数-999.99~999,99

number(5)一个5位整数-99999~99999

3.日期类型

date 包含年月日时分秒

图片类型

blob 二进制数据 可以存放图片、声音

三、表的操作

1.创建表

SQL> create table student (

2 sID number(4),

3 sName varchar2(20),

4 sSex char(2),

5 sBirthday date,

6 sPay number(7,2) 区别于sql server 无逗号。

7 );

Table created

SQL> create table class(

2 cId number(2),

3 cName varchar2(20)

4 );

Table created

2.修改表

添加字段 SQL> alter table student add (cId number(2));

删除某个字段

SQL> alter table student drop column cId;

Table altered

查看表结构 SQL> desc student;

修改字段长度 SQL> alter table student modify (cId number(5);

删除表 SQL> drop table student;

修改表名字 SQL>rename student to stu;

3.添加数据

SQL>insert into student values(1,'小明','男','11-5月-2011',12345.1,12);

1 row inserted

日期格式默认为DD-MON-YY 天-月份-年

可以修改默认日期格式

SQL> alter session set nls_date_format ='yyyy-mm-dd';

Session altered

修改一个字段

SQL>update student set sSex='男' where sId='2';

1 row updated

添加空值

SQL> insert into student (sId,sname,sbirthday) values (4,'小明',NULL);

1 row inserted

查询时注意:SQL> select * from student where sBirthday is null;

删除数据 SQL>delete from student;删除所有记录,表结构还在,如果再删除之前设置回滚点还可以回滚。

回滚操作

SQL>savepoint a;

SQL>delete from student;

SQL>rollback to a;

· SQL>drop table student;删除表结构和数据

· SQL>delete from student where sId=1;删一条记录

· SQL>truncate table student;删除所有记录,表结构还在,速度快对于大型表适用

oracle 表的基本查询

SQL> set timing on;显示执行时间

SQL> desc dept; 查看表结构

一、单表查询

取消重复行

SQL> select distinct deptno ,job from emp;

NULL处理

SQL>select sal+nvl(comm,0) from emp;当comm为null时变为0

SQL Server中为select sal+isnull(comm,0) from emp;

where子句

SQL>select ename,hiredate from emp where hiredate>'1-1月-1989';

like语句

SQL>select ename,sal from emp where ename like 'S%';

%:零个或多个字符

_:任意单个字符

逻辑操作符 or and

order by 子句

SQL>select * from emp order by sal;默认为升序

SQL>select * from emp order by sal asc;升序

SQL>select * from emp order by sal desc;降序

使用列的别名排序

SQL>select ename,(sal+nvl(comm,0))*12 as "年薪" from emp order by "年薪" desc;

Oracle复杂查询

max(),min(),avg(),sum(),count()

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

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

SQL>select avg(sal),max(sal),deptno from emp group by deptno having avg(sal)>2000 order by avg(sal) asc;

二、多表查询

1.笛卡尔积

SQL>select e.ename,e.sal,d.dname from emp e,dept d where e.deptno=d.deptno;

2.自连接

自连接指在同一张表的连接查询

SQL>select worker.ename,boss.ename from emp worker,emp boss where worker.mgr=boss.empno;

3.子查询(嵌套查询)

嵌入在其他sql语句中的select语句。

单行子查询

SQL>select * from emp where deptno=(select depno from emp where ename='SMITH');

多行子查询

SQL>select * from emp where job in (select distinct job from emp where deptno = 10 );

多行子查询中使用all

SQL>select ename,sal,deptno from emp where sal>all (select sal from emp where deptno=30);

等价

SQL>select ename,sal,deptno from emp where sal>(select max(sal) from emp where deptno=30);

多行子查询中使用any

SQL>select ename,sal,deptno from emp where sal>any (select sal from emp where deptno=30);

等价

SQL>select ename,sal,deptno from emp where sal>(select min(sal) from emp where deptno=30);

多列子查询

SQL>select ename,sal from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH');

SQL>select a2.ename,a2.sal,a2.deptno,a1.mysal from emp a2,(select deptno,avg(sal) mysal from emp group by deptno) a1 where a2.deptno=a1.deptno and a2.sal>a1.mysal;

注意表取别名是不加as,列可以。

SQL> select a2.ename,a2.sal,a2.deptno,a1.mysal from emp a2 join (select deptno,avg(sal) as mysal from emp group by deptno) a1 on a2.deptno= a1.deptno where a2.sal>a1.mysal ;

4.分页查询

SQL> select * from (select a.*,rownum rn from (select * from emp) a where rownum<=10 ) where rn>=6;

SQL> select * from (select a.*,rownum rn from (select ename,sal from emp order by sal) a where rownum<=10 ) where rn>=6;

5.查询结果创建新表

SQL>create table mytable (id,ename,sal) as select empno,ename,sal from emp;

合并查询

集合操作符union

union all

intersect

minus 差集

6.创建新的数据库

数据库配置助手Database Configuration Assistant

posted on 2011-11-07 23:44  笨蛋一休  阅读(414)  评论(0编辑  收藏  举报