oracle数据库相关知识总结

TNS服务启动
C:\Documents and Settings\new>lsnrctl start


使用system登陆
sqlplus
conn system as sysdba
口令回车


解锁scott用户,将密码改成tiger
SQL> alter user scott identified by tiger account unlock;
SQL> conn scott/tiger


创建表
create table tableName
(
colName TYPE,
colName TYPE,
.....
);



删除表
drop table tableName;

修改表
alter table tableName modify 列名 新属性


清空回收站
purge recyclebin;


约束:1.表中必须要有主键。primary key 不能重复不能为空(主键是能索引到一条唯一记录)
unique: 唯一,列不能有重复的内容
   not null: 不能为空


1 create table S2
2 (
3 id number(4),
4 name varchar2(20),
5 constraint pk_S2 primary key(id),
6 constraint uni_S2 unique(name)
7* )


去掉表中的约束,需要提供约束名
alter table S2 drop constraint pk_S2;


> create table stuScore
(
id number(4),
score number(4,1),
constraint fk_ss foreign key(id) references stuName(id)
);


外键StuScore.id引用stuName.id主键


学生姓名,学号,系,系主任,学科,成绩

学号,姓名,系
学号,学科,成绩
系,系主任


create table S2
(
id number,
age number check ( age> 0 and age<100)
);
check 用来检查数据是否复合条件,符合条件才允许insert

//为表添加一列
alter table S2 add name varchar2(20);
//删除一列
alter table S2 drop column name;
//更改存在的列长度
alter table S2 modify name varchar2(10);

类型:
char/varchar2/number/date/systimestamp(毫秒)


select *** from *** where ****;
//执行顺序:from -> where -> select


null+任何数字运算=null


聚合函数(分组函数),只有一个结果
1. select count(*) from emp; 查寻行数
2. select sum(sal) from emp; 求列的和
3. select max(sal) from emp; 求列的最大值
4. select min(sal) from emp; 求列的最小值
5. select avg(sal) from emp; 求列的平均值

distinct 排除重复的列

单函数
1.mod() 取余运算
2.round() 四舍五入
select round(10.987654,2) from dual; 保留2位小数
3.trunc: 不四舍五入,直接舍去小数
select trunc(123.456,2) from dual;
4.bitand:按位与
5.lower: 转成小写字符
6.upper: 转成大写字符
7.initcap: 把单词首字母大写
8.trim: 两边的空格剔除
9.to_number() 字符串转换成数字
to_char()
to_date()
10.空值替换 nvl(列名,0)
select ename,(sal+nvl(comm,0)) from emp;

select ... from .... where ..... groupby .... having .....

先筛选再分组再筛选


查寻emp表中deptno不为20的,根据deptno分组,查寻平均工资大于2000的部门号及部门的工资总和


切记:分组的列才能在select 上显示出来,分组函数的结果也可以显示,其它均不能显示


id name address job sex height weight
1 zhangsan jilin student m 178 160
2 hanmeimei liaoning teacher f 165 118
3 lilei jizhou javapg m 180 172
4 huhansan jilin student f 177 180
5 liudehua jizhou singer m 170 120

1.查寻地址在jizhou的最胖的人体重
select max(weight) from myclass where address='jizhou'
2.不要liaoning的按性别分组,查平均身高。
select sex,avg(height) from myclass where address<>'liaoning' group by sex;
3.查寻体重超过自己身高的人的信息
select * from myclass where weight>height;
4.查寻地址不以j开头的工作有哪些
select job from myclass where address not like 'j%';
5.计算男女生性别比
select (select count(*) from myclass where sex='m') / (select count(*) from myclass where sex='f')
from dual

order by asc默认升序desc降序
select .... from ..... where . .... group by ..... having ..... order by
1.from -> where -> group by -> having -> select -> order by



子查寻(嵌套查寻)
select 子查寻
SQL> select (select count(*) from emp where deptno=10) / (select count(*) from emp ) from dual;


from 子查寻
select * from (select * from emp);
select * from (select * from emp where deptno=20) where sal>2000;




where 子查寻
SQL> select * from emp where sal > (select avg(sal) from emp);
select * from emp where sal in (select avg(sal) from emp);


练习:查寻每一个部门的员工的最高工资的人是谁
SQL> select ename,deptno from emp
where (deptno,sal) in (select deptno,max(sal) from emp group by deptno);

having子查寻
练习:查寻部门平均工资高于公司平均工资的部门信息
SQL> select deptno,avg(sal) from emp group by deptno having avg(sal) > (select avg(sal) from emp);


exists 如果子查寻有结果返回则为真
not exists如果子查寻没有结果返回则为真


连表查寻:连接关系主键外键
内连接:等值,不等值,自连接
外连接:左连,右连接,全外连接


学号,姓名,系(学号主键,外键)
学号,学科,成绩(学号外键、)
系,系主任 (系主键)


先做主键表再做外键表
SQL> create table xi
2 ( xi_name varchar2(10),
3 xi_zr varchar2(10),
4 constraint pk_xi primary key(xi_name)
5 );


SQL> create table stu
2 ( stu_id number,
3 stu_name varchar2(10),
4 xi_name varchar2(10),
5 constraint pk_stu primary key(stu_id),
6 constraint fk_stu foreign key(xi_name) references xi(xi_name)
7 );


SQL> create table score
2 (
3 stu_id number,
4 subj varchar2(10),
5 scor number(4,1),
6 constraint fk_score foreign key(stu_id) references stu(stu_id)
7 );


insert into xi values('waiyu','zhangsan');
insert into xi values('jisuanji','lisi');
insert into xi values('zhengfa','wangwu');


insert into stu values(1,'haner','waiyu');
insert into stu values(2,'tom','zhengfa');
insert into stu values(3,'jerry','jisuanji');
insert into stu values(4,'moto','waiyu');
insert into stu values(5,'nokia','waiyu');
insert into stu values(6,'iphone','jisuanji');
insert into stu values(7,'alibaba','jisuanji');
insert into stu values(8,'taobao','zhengfa');
insert into stu values(9,'dongdong','zhengfa');
insert into stu values(10,'dingding','waiyu');


insert into score values(1,'english',98);
insert into score values(1,'computer',76);
insert into score values(1,'math',56);
insert into score values(2,'math',88);
insert into score values(2,'computer',98);
insert into score values(3,'english',44);
insert into score values(3,'math',77);
insert into score values(3,'computer',39);
insert into score values(4,'computer',49);
insert into score values(4,'math',49);
insert into score values(5,'math',79);
insert into score values(5,'english',69);


commit;(注意操作之后要提交)


笛卡尔(各种交叉连接)
(标准版)select * from A cross join B on;
(非工业化) select * from A , B where;


内连接
select * from A join B on 条件
select stu.stu_id,stu_name,scor from stu join score on
stu.stu_id=score.stu_id and subj='math' and scor>60;

SQL> select * from A left join B on A.id=B.id;


ID NAME ID SCORE
---------- ---------- ---------- ----------
1 a 1 88
2 b 2 99






SQL> select * from A right join B on A.id=B.id;


ID NAME ID SCORE
---------- ---------- ---------- ----------
1 a 1 88
2 b 2 99
3 100


rownum伪列:如果想用rownum去判断必须包括1的值
否则只能起别名,再嵌套一层子查寻
select * from (select rownum r,s.scor from (select stu.stu_id,stu.stu_name,scor from stu join score
on stu.stu_id=score.stu_id and subj='math' order by scor desc) s ) ss where ss.r>=2 and ss.r<=4;

delete from 表名 where 条件
删除表中符合条件的内容,如果不加where则清空表内容

update 表名 set 列='值' where 条件
更新表中符合条件的某列的内容

1.查询wangwu系主任所在的系的学生都有谁
2.查询学生成绩总和最高的学生是谁
3.查询学生成绩总和最高的学生所在的系是哪个系
4.查询共几个学生一门成绩也没有
5.查询各系的学生数量
6.查询各科的成绩平均分
7.查询有挂科的学生信息
8.查询所有成绩都及格的学生的系主任是谁
9.根据系分组,按总分的高低给所有学生成绩排序(降序)
10.根据科目分组,查看每科的最高分和最低分(没成绩的排队)

事物oracle:上次提交到本次提交之间的所有操作叫一个事物
create table / drop table /alter table
insert into
insert into..
update ....
delete ....
commit; 提交


rollback;//回滚到本事物之初TNS服务启动
C:\Documents and Settings\new>lsnrctl start


使用system登陆
sqlplus
conn system as sysdba
口令回车


解锁scott用户,将密码改成tiger
SQL> alter user scott identified by tiger account unlock;
SQL> conn scott/tiger


创建表
create table tableName
(
colName  TYPE,
        colName  TYPE,
        .....
);



删除表
drop table tableName;

修改表
alter table tableName modify 列名 新属性


清空回收站
purge recyclebin;


约束:1.表中必须要有主键。primary key 不能重复不能为空(主键是能索引到一条唯一记录)
       unique: 唯一,列不能有重复的内容
    not null: 不能为空


 1  create table S2
 2  (
 3     id number(4),
 4     name varchar2(20),
 5     constraint pk_S2 primary key(id),
 6     constraint uni_S2 unique(name)
 7* )


  去掉表中的约束,需要提供约束名
  alter table S2 drop constraint pk_S2;


> create table stuScore
  (
     id number(4),
     score number(4,1),
     constraint fk_ss foreign key(id) references stuName(id)
  );


  外键StuScore.id引用stuName.id主键


  学生姓名,学号,系,系主任,学科,成绩

  学号,姓名,系
  学号,学科,成绩
  系,系主任


  create table S2
  (
   id number,
   age number check ( age> 0 and age<100)
 );
 check 用来检查数据是否复合条件,符合条件才允许insert

  //为表添加一列
  alter table S2 add name varchar2(20);
  //删除一列
  alter table S2 drop column name;
  //更改存在的列长度
  alter table S2 modify name varchar2(10);

类型:
  char/varchar2/number/date/systimestamp(毫秒)


  select  *** from *** where ****;
  //执行顺序:from -> where -> select 


  null+任何数字运算=null


   聚合函数(分组函数),只有一个结果
  1. select count(*) from emp;  查寻行数
  2. select sum(sal) from emp;  求列的和
  3. select max(sal) from emp;  求列的最大值
  4. select min(sal) from emp;  求列的最小值
  5. select avg(sal) from emp;  求列的平均值
  
   distinct 排除重复的列

  单函数
   1.mod() 取余运算
   2.round() 四舍五入
     select round(10.987654,2) from dual; 保留2位小数
   3.trunc: 不四舍五入,直接舍去小数
     select trunc(123.456,2) from dual;
   4.bitand:按位与
   5.lower: 转成小写字符
   6.upper: 转成大写字符
   7.initcap: 把单词首字母大写
   8.trim:  两边的空格剔除
   9.to_number()  字符串转换成数字
     to_char()
     to_date()
   10.空值替换 nvl(列名,0)
     select ename,(sal+nvl(comm,0)) from emp;

    select  ...  from .... where .....  groupby .... having .....
    
    先筛选再分组再筛选


    查寻emp表中deptno不为20的,根据deptno分组,查寻平均工资大于2000的部门号及部门的工资总和


    切记:分组的列才能在select 上显示出来,分组函数的结果也可以显示,其它均不能显示
   
 
    id    name     address     job     sex   height  weight
     1     zhangsan   jilin     student  m     178     160
     2     hanmeimei  liaoning  teacher  f     165     118
     3     lilei      jizhou    javapg   m     180     172
     4     huhansan   jilin     student  f     177     180
     5     liudehua   jizhou    singer   m     170     120
  
     1.查寻地址在jizhou的最胖的人体重
select max(weight) from myclass where address='jizhou'
     2.不要liaoning的按性别分组,查平均身高。
select sex,avg(height) from myclass where address<>'liaoning'  group by sex;
     3.查寻体重超过自己身高的人的信息
select * from myclass where weight>height;
     4.查寻地址不以j开头的工作有哪些
select job from myclass where address not like 'j%';
     5.计算男女生性别比
select     (select count(*) from myclass where sex='m') / (select count(*) from myclass where sex='f') 
from dual

     order by  asc默认升序desc降序
   select   .... from ..... where . .... group by ..... having ..... order by 
   1.from -> where -> group by -> having -> select -> order by 


     
  子查寻(嵌套查寻)
  select 子查寻
  SQL> select (select count(*) from emp where deptno=10) / (select count(*) from emp ) from dual;


  from 子查寻
  select * from (select * from emp);
  select * from (select * from emp where deptno=20) where sal>2000;




  where 子查寻
  SQL> select * from emp where sal > (select avg(sal) from emp);
       select * from emp where sal in (select avg(sal) from emp);


  练习:查寻每一个部门的员工的最高工资的人是谁
   SQL> select ename,deptno from emp
        where (deptno,sal) in (select deptno,max(sal) from emp group by deptno);

   having子查寻
   练习:查寻部门平均工资高于公司平均工资的部门信息
   SQL> select deptno,avg(sal) from emp group by deptno having avg(sal) > (select avg(sal) from emp);


   exists 如果子查寻有结果返回则为真
   not exists如果子查寻没有结果返回则为真
   

   连表查寻:连接关系主键外键
   内连接:等值,不等值,自连接
   外连接:左连,右连接,全外连接


  学号,姓名,系(学号主键,外键)
  学号,学科,成绩(学号外键、)
  系,系主任 (系主键)


  先做主键表再做外键表
   SQL> create table xi
  2  (  xi_name varchar2(10),
  3     xi_zr  varchar2(10),
  4     constraint pk_xi primary key(xi_name)
  5  );


  SQL> create table stu
  2  (  stu_id number,
  3     stu_name varchar2(10),
  4     xi_name varchar2(10),
  5     constraint pk_stu primary key(stu_id),
  6     constraint fk_stu foreign key(xi_name) references xi(xi_name)
  7  );


  SQL> create table score
  2  (
  3    stu_id number,
  4    subj   varchar2(10),
  5    scor   number(4,1),
  6    constraint fk_score foreign key(stu_id) references stu(stu_id)
  7  );


  insert into xi values('waiyu','zhangsan');
  insert into xi values('jisuanji','lisi');
  insert into xi values('zhengfa','wangwu');


  insert into stu values(1,'haner','waiyu');
  insert into stu values(2,'tom','zhengfa');
  insert into stu values(3,'jerry','jisuanji');
  insert into stu values(4,'moto','waiyu');
  insert into stu values(5,'nokia','waiyu');
  insert into stu values(6,'iphone','jisuanji');
  insert into stu values(7,'alibaba','jisuanji');
  insert into stu values(8,'taobao','zhengfa');
  insert into stu values(9,'dongdong','zhengfa');
  insert into stu values(10,'dingding','waiyu');


  insert into score values(1,'english',98);
  insert into score values(1,'computer',76);
  insert into score values(1,'math',56);
  insert into score values(2,'math',88);
  insert into score values(2,'computer',98);
  insert into score values(3,'english',44);
  insert into score values(3,'math',77);
  insert into score values(3,'computer',39);
  insert into score values(4,'computer',49);
  insert into score values(4,'math',49);
  insert into score values(5,'math',79);
  insert into score values(5,'english',69);


  commit;(注意操作之后要提交)


  笛卡尔(各种交叉连接)
  (标准版)select * from A cross join B on;
  (非工业化) select * from A , B where; 


  内连接 
  select * from A join B on 条件
   select stu.stu_id,stu_name,scor from stu join score on
   stu.stu_id=score.stu_id and subj='math' and scor>60;
  
SQL> select * from A left join B on A.id=B.id;


        ID NAME               ID      SCORE
---------- ---------- ---------- ----------
         1 a                   1         88
         2 b                   2         99






SQL> select * from A right join B on A.id=B.id;


        ID NAME               ID      SCORE
---------- ---------- ---------- ----------
         1 a                   1         88
         2 b                   2         99
                               3        100


  rownum伪列:如果想用rownum去判断必须包括1的值
  否则只能起别名,再嵌套一层子查寻
   select * from  (select rownum r,s.scor from (select stu.stu_id,stu.stu_name,scor from stu join score
   on stu.stu_id=score.stu_id and subj='math' order by scor desc)  s ) ss where ss.r>=2 and ss.r<=4;

   delete from 表名 where 条件
   删除表中符合条件的内容,如果不加where则清空表内容

   update 表名 set 列='值' where 条件
   更新表中符合条件的某列的内容

1.查询wangwu系主任所在的系的学生都有谁
2.查询学生成绩总和最高的学生是谁
3.查询学生成绩总和最高的学生所在的系是哪个系
4.查询共几个学生一门成绩也没有
5.查询各系的学生数量
6.查询各科的成绩平均分
7.查询有挂科的学生信息
8.查询所有成绩都及格的学生的系主任是谁
9.根据系分组,按总分的高低给所有学生成绩排序(降序)
10.根据科目分组,查看每科的最高分和最低分(没成绩的排队)

  事物oracle:上次提交到本次提交之间的所有操作叫一个事物 
           create table / drop table /alter table
           insert into 
           insert into..
           update ....
           delete ....   
           commit; 提交


       rollback;//回滚到本事物之初

java 程序连接oracle数据库代码:

public class Test {
public static void main(String[] args) throws ClassNotFoundException {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:myoracle","scott","tiger");
if(conn!=null)
System.out.println("成功");
else
System.out.println("失败");
} catch (SQLException e) {
e.printStackTrace();
}
}
}












你可能感兴趣的:(oracle,sql,数据库,oracle数据库相关知识总结)