oracle笔记

来源:互联网 发布:mac恢复垃圾箱删除文件 编辑:程序博客网 时间:2024/06/02 05:10
一、Oracle安装
  Oracle是数据库管理系统,全球应用最广的,Top100有80家使用此数据库。
  目前数据库技术:
   SQLServer,MySQL,DB2,Oracle
 1.口令要求
  长度:8位+
  规则:必须包含大写、小写英文字母、数字,如Disen666
 2.安装路径
 必须英文路径,且不带空格
 3.安装要求
  4g,20g硬盘  
二、用户管理
 1.链接Oracle
  1)管理员sysdba
     a)sqlplus / as sysdba;
     b)conn /as sysdba;
     c)conn sys/ Disen666;
     d)sqlplus sys/Disen666;
  2)sqlplus hr/hr;
  3)conn hr/hr
 2.创建用户
   创建用户名:create user用户名 identified by 口令;
    CMD> sqlplus/as user
    SQL>show user 
    SQL>create user disen identified by disen;
   授权的语法:
     grant 权限 to 用户名;
     grant create table to disen;
     grant create session to disen;
     disen用户链接Oracle数据库:
    SQL>conn Disen666 /Disen666;
 3.修改用户口令
   SQL> alter user disen identified by d666;
 4.锁定用户
  SQL>alter user disen account lock;
 5.用户解锁
  SQL>alter user disen account unlock;
 练习
 1.给hr用户进行解锁
  SQL>alter user hr account unlock;
 2.修改hr的口令为hr
  SQL>alter user hr identified by hr;
 3.查看hr下所有的表
 SQL>conn hr/hr;
 SQL>select table_name from user_tables;
三、表查询
 1.查看表结构
  SQL>desc employees;
 2.number数值类型
  NUMBER(6)长度为6位的整数
  NUMBER(8,2)长度为8位,小数点保留两位
  VARCHAR(20)可变长度字符类型,根据内容决定
 3.查看当前系统时间
  SQL>select sysdate from dual
  SQL>select to_char(sysdate,'yyyy-MM-dd hh24:mi:ss')from dual;
 4.查看所有员工的ID,姓名,工龄,岗位,部门,工资,按工龄的高低排序
  select EMPLOYEE_ID,FIRST_NAME,((sysdate-hire_date)/365)"工龄",JOB_ID,DEPARTMENT_ID,SALARY from employees
  order by hire_date;
 5.查询的语法
  select column1[[as]"别名"],column2[[as]"别名"],……
  from table_name[表别名]
  where查询条件
  group by 分组字段
  having 分组字段的条件
  order by排序字段ASC|DESC
 6.查询练习1
  1)查询80部门中工资大于6000的员工id,姓名,工资,入职时间
    SQL>select employee_id "员工编号",first_name||'-'||last_name"姓名",salary,hire_date from employees
        where department_id=80 and salary>=9000;
   扩展
   SQL>set line 1000//当前行显示多少个字符
   SQL>set wrap off//是否换行显示,off不换行
  2)查询80部门中最高工资、最低工资、平均工资、部门人数
    提示:max(),min(),avg(),count()
     SQL>select max(salary)"最高工资",min(salary)"最低工资",avg(salary)"平均工资,count(department_id) as 部门人数 
         from employees where department_id=80;
  3)查询80部门中最高工资的员工id,姓名,工资,入职时间
     select employee_id,first_name||' '||last_name "姓名",salary,hire_date from employees where department_id=80
     and salary=(select max(salary) from employees where department_id=80);
  4)查看所有员工的姓名,工资,奖金,入职年限,岗位,按照工资的高低排序
  提示:nvl(cl,value)判断cl是否为空,若为空则返回value值
    SQL>select first_name,salary,round(salary*nvl(commission_pct,0),2)"奖金",round((sysdate-hire_date)/365)"入职年限",job_id
        from employees
        order by salary desc;
7.多表查询
  1)等值条件
  有n个表连接,必须存在n-1个等子条件
  1—1)查询所有人的ID,姓名,岗位,部门名称
  提示:employees,departments,jobs
     SQL>select employee_id,first_name,job_id,department_name from employees e,departments d where e.department_id=d.department_id;
  1-2)查询110部门的人员ID,姓名,岗位描述,工资,部门名称
    select employee_id,first_name,job_title,salary,department_name from employees e,departments d,jobs
    where e.department_id=d.department_id
    and e.job_id=jobs.job_id
    and e.department_id=110;
  2)自连接查询
 一张表中存在两个字段,其中一个字段的取值来源于另外一个字段。
  employees:employees_id,manager_id
   2-1)查询所有员工的ID,姓名,工资,主管id,主管姓名,主管工资,主管入职时间
   SQL>select e.employee_id,e.first_name,e.salary,e.hire_date,
       m.employee_id,m.first_name,m.salary,m.hire_date
       from employees e,employees m
       where e.manager_id=m.employee_id;
四、多表查询之join
   语法:
   select 表1,字段1,表2,字段2
   from 表1 join 表2
   on (等值条件)
   where条件
   order by 字段...
   1.查询70部门员工的id,姓名,工资,部门名称,且按部门排序
   SQL>select employee_id,first_name,salary,department_name
       from employees e
       join departments d
       on(e.department_id=d.department_id)
       where e.department_id=70;
       order by department_name;
   2.查询所有员工的id姓名,salary,部门名称,包含没有部门的员工
   没有部门员工:SQL> select employee_id,first_name
                      from employees
                      where department_id is null;


       SQL>select employee_id,first_name,salary,department_name
           from employees e
           left outer join departments d
           on(e.department_id=d.department_id)
           order by department_name;
   3.查询所有部门的员工id,姓名,salary,部门名称,包含没有员工的部门
     SQL>select rownum,employee_id,first_name,salary,d.department_id,department_name
         from employees e
         right outer join departments d
         on(e.department_id=d.department_id)
         order by department_id;


   4.查询所有部门的员工的id,姓名,工资,部门名称,包含所有部门和员工
     SQL>select rownum,employee_id,first_name,salary,d.department_id,department_name
         from employees e
         full outer join departments d
         on (e.department_id=d.department_id)
         order by department_id;
五、常用函数
  1.字符函数
    length(s)
    substr(s,n,len)截取字符,从n位置开始截取len长度的字符
    concat(s1,s2)链接s1和s2两个字符,类似
    lower(s)小写字符转换
    upper(s)大写字符转换
    initcap(s)首字符大写
    instr(s,s1,n,m)从s的位置开始查找s1在s中第m次出现的位置
    replace(s,s1,s2)将s中s1部分替换成s2
   SQL>select length('abcdef') "Len" from dual;
   SQL>select substr('Disen hi',2,2) "Result" from dual;
   SQL>select upper(concat('hi','disn')) "rssult" from dual;
   SQL>select instr('abcdefabcbbcabcddfadc','abc',1,4) "Result" from dual;
   SQL>select replace('hi,disen','hi','hello') "Result" from dual;
   1-1)查询50部门的员工id,姓名,要求姓名全大写显示
     SQL>select employee_id,upper(first_name)
         from employees
         where department_id=50;
   1-2)查询岗位为SA_REP的员工姓名,工资,部门名称
     SQL>select first_name,salary,department_name
         from employees e join departments d
         on (e.department_id=d.department_id)
         where job_id=upper('sa_rep');//'SA_REP';
   1-3)查询姓名中包含'la'的所有员工的id,姓名,工资,岗位
    提示:模糊查询->字段 like'%la%'     ‘_la’,通配符:% 任意长度的任意字符,_一个任意字符
      SQL>select employee_id,first_name,salary,job_id
          from employees
          where lower(fisrt_name) like '%la%';
   1-4)查询姓名中的第二个字符是'l'的所有员工的ID,姓名,工资,岗位
      SQL>select employee_id,first_name,salary,job_id
          from employees
          where lower(first_name) like'_l%';   
   1-5)查询姓名以'al'开头的员工姓名,工资,岗位,同时将'al'替换成'Di'进行显示
      SQL>select replace(lower(first_name),'al','Di'),salary,job_id
          from employees 
          where lower(first_name) like 'al%';
  2.数值函数
    数值->字符 转换  to_char(99,[format])
    字符->数值 转换  to_number('99.85',[format])

    9:任意数字,指定位置不存在数值时,则不显示
    0:任意数字,指定位置不存在数值时,则显示为0
    ,: 千位符号
    .: 小数点
    $: 美元符号
    L: 本地货币符号
    SQL>select to_char(1998.56,'L9,999.0') "Result" from dual;
    SQL>select to_char(11998.56,'L99,999.0') "Result" from dual;
   2-1)将'1,998.6'转成数值,在增加20000
     SQL>select to_number('¥1,998.6','L9,999.0')+2000 "Result" from dual;
     SQL>select '¥1,998.6'+2000 "Result" from dual;--错误,必须对数值的字符转换为数值再计算
   2-2)查询所有员工id,姓名,岗位,且工资要求格式化输出,同时按工资高低排序
    SQL>select employee_id,first_name,to_char(salary,'L99,999.00') "Salary",job_id
        from employees
        order by salary desc;
   3.日期函数
     日期->字符 to_char(date,[format]);
     字符->日期 to_date('',[format]);
     format:
     yyyy-MM-dd  年-月-日
     dd 月中的日
     dddd年中的日
     day 星期几
     hh12 12小时制
     hh24 24小时制
     mi 分钟
     ss 秒
    SQL->select to_char(sysdate,'yyyy-MM-dd hh24:mi:ss day ddd') "Result" from dual;
         add_months(date,n) 给指定的时间增加六个月,返回六个月后的日期
       next_day(date,'星期一')返回下个星期几
       last_day(date)返回指定时间月的最后一天的日期
          SQL->select last_day(sysdate) from dual;
     3-1)查询2015年10月的最后一天的日期
      SQL->select last_day(to_date('2015-10','yyyy/mm')) "Result" from dual;
      SQL->select last_day('01-10月-15') "Result" from dual;
      注意:如果字符日期格式与系统的日期格式相同,则会自动转换成日期类型
     扩展:
          修改系统的日期格式,默认系统时间日期格式:dd-Month-yy
      SQL->alter session set nls_date_format='yyyy-MM-dd';
      SQL->alter session set nls_date_format='yyyy-Month-dd';
      SQL->select sysdate from dual;
      SQL->select add_months(sysdate,2)"实现小目标" from dual;
      SQL->select add_months(sysdate,2)"实现小目标",to_char(1300,'L9,999.0') "小目标" from dual;
   4.通用函数
     nvl(s1,s2)若s1不为空,返回s1,反之返回s2
     nvl(s1,s2,s3...)返回第一个不为空的字符
     case  分支
     case 列|表达式 when 条件1 then 值1
               when 条件2 then 值2
  when 条件3 then 值3
  else 值4
  end "别名"
     4-1)目标1300,如果收入500,返回"不满意",收入800,返回"尚可",收入1300,返回"满意",
         收入2000,返回"高兴",收入10000,返回"意外",其他收入,返回"不可能"
       SQL->select to_char(10000,'L99,999.0')"收入",
            case 3000 when 500 then '不满意'
            when 800 then '尚可'
   when 1300 then '高兴'
   when 10000 then '意外'
   else '不可能'
            end"结果"
            from dual;


        decode(列|表达式,条件1,值1,条件2,值2,条件3,值3)别名
        SQL->select to_char(800,'L99,999.0')"收入"
             decode(800,500,'不满意',800,'尚可',1300,'满意',2000,'高兴',10000,'意外','不可能') "结果"
             from dual;
    4-2)查询岗位SA-REP、IT_PROG,ST_MAN,HR_REP的员工id,工资,岗位,奖金
        并对SA-REP岗位的奖金增加5%,对IT_PROG岗位的奖金增加10%,对ST_MAN岗位的奖金增加12%,其他岗位不变
        SQL->select employee_id,salary,job_id,salary*nvl(commission_pct,0) "奖金"
        from employees
        where upper(job_id) in('SA_REP','IT_PROG','ST_MAN','HR_REP');
        SQL->select employee_id,salary,job_id,
             decode(job_id,'SA_REP',salary*(nvl(commission_pct,0)+0.05),
              'IT_PROG',salary*(nvl(commission_pct,0)+.1),
 'ST_MAN',salary*(nvl(commission_pct,0)+.12),salary *nvl(commission_pct,0)) "奖金"
 from employees
 where upper(job_id) in ('SA_REP','IT_PROG','ST_MAN','HR_REP');
六、子查询
    子查询即可以作为表达式或数据来源在查询语句中使用
    1)查询大于80部门最高工资的员工id,姓名,工资,岗位,部门id
      SQL->select employee_id,first_name,salary,job_id,department_id
           from employees
           where salary > (select max(salary) from employees where department_id=80);
    2)查询80部门工资最高的员工id,姓名,工资,岗位
      SQL->select employee_id,first_name,salary,job_id
           from employees
           where salary = (select max(salary) from employees where department_id=80)
           and department_id=80;
  
  
  
  
  
  
    3)查询工龄大于12年的所有员工的id,姓名,工资,岗位,工资要求格式化输出
      SQL->select e.employee_id,first_name,to_char(salary,'$99,999.0'),y.years "工龄",job_id
           from employees e
           join (select employee_id,round((sysdate-hire_date)/365) "YEARS"
           from employees order by hire_date) y 
           on(e.employee_id=y.employee_id)
           where y.years>12;
      SQL->select employee_id,first_name,salary,round((sysdate-hire_date)/365) "YEARS"
           from employees
           where employee_id in 
          (select employee_id
          from
          (select employee_id,round((sysdate-hire_date)/365) "YEARS"
          from employees order by hire_date)
          where years>12);
    4)查询工资在80部门的最高工资和90部门的最高工资之间的员工id,工资,入职时间,岗位
      SQL->select employee_id,salary,to_char(hire_date,'yyyy-MM-dd hh24:mi:ss')"HIRE_DATE",job_id
           from employees
           where salary between(select max(salary) from employees where department_id=80)
           and (select max(salary) from employees where department_id=90);
七、增加(插入)、更新(修改)、删除及事务处理
   1.插入语句
    语法1:insert into 表名[(字段1,字段2,...)]
     value(字段1值,字段2值,...);
    语法2:insert into 目的表名[(字段1,字段2,...)]
     select 字段1,字段2,... from 源表名 where...
    1-1)插入一位员工到120部门,员工的姓名为"disen",编号666,邮箱8888@qq.com,入职时间为当天,岗位为IT_PROG
       SQL->insert into employees(employee_id,last_name,email,hire_date,job_id,department_id)
            values(666,'disen','8888@qq.com',sysdate,upper('it_prog'),120);
       SQL->select employee_id,last_name,email,hire_date,job_id from employees where department_id=120;
   2.事务
     commit 提交事务,之前进行的DML操作都生效
     rollback 回滚事务,之前进行的DML操作都无效
     savepoint 还原点名,在DML操作后,定义一个回滚的事件点
     rollback to 还原点名,回滚事务到还原点名之后,还原点名之前的DML不受影响
    扩展:在以下事件发生时,系统会自动提交事务
     1)当执行DDL或DCL时
     2)会话session(窗口)关闭时
     3)执行commit时
     4)执行trunk截断表时
   3.插入三位员工,使用sql脚本方式
     在d:\insert_emps.txt->insert_emps.sql
     SQL->start d:\insert_emps.sql 开始执行
   4.删除语句
    语法:
     delete from 表名
     where 条件;
     4-1)删除姓名(last_name)是disen的员工
      SQL->select last_name from employees where last_name='disen';
      SQL->delete from employees where last_name='disen';
      SQL->savepoint delDisen;
   5.修改语句
    语法:
    update 表名
    set 字段1=value1,字段value2,...
    where 条件
    5-1)将120部门的员工工资改成12000,绩效百分比市值为10%
    SQL->update employees set salary=12000,commission_pct=.1
         where department_id=120;
    5-2)查询120部门员工的id,姓名,工资,奖金,实际工资
     SQL->select employee_id,last_name,salary,commission_pct*salary"奖金",salary*(1+commission_pct) "实际工资"
          from employees
          where department_id=120;
    5-3)将zxp的奖金的百分比提升到15%,工资增加200
     SQL->savepoint updateSY;
     SQL->update employees
          set commission_pct=.15,salary=salary+200
          where first_name='zxp';
    5-4)将120部门员工的奖金小于1200的员工的奖金增加200
       SQL->savepoint up Salary;
       SQL->update employees 
            set commission_pct=round((salary*commission_pct+200)/salary,2)
            where department_id=120
            and(salary*commission_pct)<=1200;
    5-5)将入职年限超过12年的员工工资增加1000,且岗位是FI_MGR
       SQL->update employees
            set salary=salary+1000
            where(sysdate-hire_date)/365>=12
            and job_id='FI_MGR';
       SQL->select employee_id,last_name,salary,hire_date
            from employees
            where(sysdate-hire_date)/365>=12
            and job_id='FI_MGR';
八、五种约束
    1.主键 primary key,表示主键的值不能为空,且是唯一的
    2.外键 foreign key references,外键字段值的来源于references引用主表的主键
    3.非空 not null,非空字段的值不能为null
    4.唯一 unique,唯一列值在整个表中就唯一的
    5.检查 check,列值必须符合check的条件
   约束的数据字典:
    user_constraints
    user_cons_columns;
   1)查询employees表的所有约束的名称,表名,字段名,类型
     SQL->desc user_constraints;
     SQL->select c.constraint_name,c.table_name,cc.column_name,c.constraint_type
          from user_constraints c join user_cons_columns cc
          on(c.constraint_name=cc.constraint_name)
          where c.table_name=upper('employees');
    扩展:
    设置列的显示宽度
      col 列名 format a20; 20代表列宽
      SQL->col column_name format a15;
      SQL->col constraint_name format a20;
   2)将120部门的所有员工的主管id改为555,操作是否成功,为什么?
    SQL->update employees
         set manager_id=555
         where department_id=120;  
    第 1 行出现错误:
    ORA-02291: 违反完整约束条件 (HR.EMP_MANAGER_FK) - 未找到父项关键字
    解决方法
    禁用EMP_MANAGER_FK约束
     SQL->alter table employees
          disabled constraint emp_manager_fk;


     SQL->select c.constraint_name,c.status,cc.column_name,c.constraint_type
          from user_constraints c join user_cons_columns cc
          on(c.constraint_name=cc.constraint_name)
          where c.table_name=upper('employees');
 
   3)查询主管id为555的员工的id,工资,姓名,邮箱
    SQL->select employee_id,salary,last_name,email
         from employees
         where manager_id=666;
  4)将主管id为555的员工的主管id设置为空
    SQL->update employees
         set manager_id=null
         where manager_id=666;
   SQL->commit;
   SQL->alter table employees
        disabled constraint emp_manager_fk;
九、DML
   1.创建表
      oracle数据类型:
 varchar2(len) 可变数据类型
 char(len) 固定长度的字符类型
 date
 number(m,n) 带小数点的数值类型
 number(m) 整型数值
语法:
 create table 表名(column1 datetype(len)[约束][默认值],
                       (column2 datetype(len)[约束][默认值],
                       (column3  datetype(len)[约束][默认值],...)
[as 子查询]
     1-1)创建emp_high表,此表用于保存每个部门最高薪资的员工id,姓名,工资,入职时间,岗位
      SQL->create table emp_high(employee_id number(6),name varchar2(20),salary number(8,2),hire_date date,job_id varchar2(10));
1-2)修改emp_high表的salary数据长度为8位,小数点保留两位
SQL->alter table emp_high
modify(salary number(8,2));
SQL->select * from emp_high;
          SQL->insert into emp_high
               select employee_id,last_name,salary,hire_date,job_id
      from employees e
      join (select department_id,max(salary) "MAXSALARY" from employees group by department_id) eh 
               on(e.department_id=eh.department_id)
          where e.salary=eh.maxsalary;
     1-3)修改emp_high表中name列名为employee_name
        SQL> alter table emp_high
             rename column name to employee_name;
    1-4)将emp_high表中的employee_id的列名改成id,类型长度改为3位
       修改列类型语法:
       alter table 表名
       modify(列名 datetype[约束][默认值]);
     修改列名的语法:
       alter table 表名
  rename column 旧列名 to 新列名;
  SQL->alter table emp_high
       modify (employee_id number(8));//如果数据类型的长度比之前要小时,此列是空列。
  SQL->alter table emp_high
  rename column employee_id to id;
1-5)查询emp_high中每个员工所在部门名称,岗位,姓名
 SQL->select department_name,e.job_id,employee_name
      from employees e,departments d,emp_high n
  where e.employee_id=n.id
      and e.department_id=d.department_id;
SQL->select department_name,e.job_id,employee_name
     from emp_high eh join employees e
 on(eh.id=e.employee_id)
 join departments d
          on(e.department_id=d.department_id);
    1-6)将emp_high表中的job_id和salary两个字段删除
     删除列的语法:
       alter table 表名
       drop column 列名;
SQL->alter table emp_high
     drop column job_id;
SQL->alter table emp_high
     drop column salary;
1-7)删除emp_high表
      删除表的语法:
       drop table 表名;
     SQL->drop table emp_high;
  2.表和约束
    2-1)创建班级表、学生表、课程表、成绩表
班级表 t_cls(id,title)
学生表 t_stu(id,name,age,sex,tel,cls_id)
课程表 t_course(id,title,pct)  pct学分
成绩表 t_score(id,stu_id,course_id,score)
SQL->start d:/chuangjian.sql
2-2)查询所有的学生的姓名,成绩,班级名
SQL->select s.name,cs.title,c.title,sc.score
from t_stu s join t_cls cs
on (s.cls_id=cs.id)
join t_score sc on(s.id=stu_id)
join t_course c on(sc.course_id=c.id)
order by s.name;
2-3)向学生表添加组长ID列(mgr_id),取值为学生id
语法:
alter table 表名
add (列名 datetype(length)[约束][默认值]);
    SQL->alter table t_stu
    add mgr_id number(12) unique;
SQL->update t_stu
    set mgr_id='201503063240'
where id='201503063240';
十、View视图
  1.创建视图的语法:
create view 视图名[(列名1,列名2,...)]
as 子查询;
删除视图:
drop view 视图名;
   1-1)查询80部门员工id,姓名,工资,岗位和部门名称,并将查询结果以视图的方式保存。
    SQL->create view depart_80 as
    select employee_id,last_name,salary,job_id,department_name
from employees e join departments d
on (e.department_id=d.department_id)
where e.department_id=80;
SQL->select * from depart_80;

SQL->create or replace view depart_80(id,name,salary,job_id,d_name)
    as
    select employee_id,last_name,salary,job_id,department_name
from employees e join departments d
on (e.department_id=d.department_id)
where e.department_id=80;
    1-2)查询SA_REP岗位员工的id,姓名,入职时间,工资,奖金,实发工资和部门名称,
   按照入职时间早晚排序,并将查询结果以视图的方式保存。    
    SQL->create or replace view v_sa_rep
     as
 select employee_id,first_name,to_char(hire_date,'yyyy-MM-dd') "HIRE_DATE",
 to_char(salary,'L99,999.0')"SALARY",
 salary*nvl(commission_pct,0) "COMMISSON",
 salary*(1+nvl(commission_pct,0)) "RSALARY",department_name
 from employees e join departments d
         on(e.department_id=d.department_id)
 where job_id=upper('sa_rep')
         order by hire_date;
SQL-> drop view v_sa_rep; 


  2.on delete cascade /on delete set null 主外键级应用
    2-1)创建用户表和用户日志表,并设置用户表和日志表主外键关系,同时考虑级联删除。

用户表:t_user(id,name,passwd,nick_name,u_level,u_roles)
日志表:t_log(id,content,user_id,log_date,log_ip)
 SQL->create table t_user
      (id number(8) primary key,
  name varchar2(20) not null,
  passwd varchar(20) not null,
   nick_name varchar2(20),
u_level number(2) default 1,
u_roles varchar2(10) default 'comm',
constraint user_name_un unique(name)
);

 SQL->create table t_log
(id number(8) primary key,
content varchar2(200),
user_id number(6) references t_user(id) on delete casecade,
log_date date default sysdate,
log_ip varchar2(20)
);
--on delete casecade 当user_id的记录从t_user删除时,那么t_log表中的所有user_id的记录会被全部删除
--on delete set null 当user_id的记录从t_user删除时,那么t_log表中的所有user_id的记录会被全部设置为null。
SQL->alter table t_log
    drop constraint SYS_C0011176;--删除t_表的SYS_C0011175约束

SQL->alter table t_log
modify(user_id number(6) references t_user(id) on delete casecade);

2-2)查询t_log表的所有约束的名称,类型,表名,列名,状态
 提示:user_constraints(table_name,constraint_name,type,status),
       user_cons_columns(table_name,constraint_name,column_name)
 SQL->create or replace view v_log_cons as
      select c.constraint_name,c.constraint_type,c.table_name,cc.column_name,c.status
  from user_constraints c join user_cons_columns cc
  on (c.constraint_name=cc.constraint_name)
  where c.table_name=upper('t_log');
  
SQL->col column_name format a20;
SQL->select * from v_log_cons;
2-3)添加用户(1,'disen','888'),添加日志(1,'login',1),(2,'query goods',1),(3,'add goods',1)
  SQL->insert into t_user(id,name,passwd) values(1,'disen','888');
       insert into t_log(id,content,user_id) values(1,'login',1);
insert into t_log(id,content,user_id) values(2,'query goods',1);
insert into t_log(id,content,user_id) values(3,'add goods',1);
   SQL->select name,content,log_date from t_user u join t_log l on(u.id=l.user_id);
SQL->delete from t_user where id=1;
SQL->alter table t_log
drop constraint SYS_C0011176;--删除t_log表的SYS_C0011176约束

SQL->alter table t_log
    modify(user_id number(10) references t_user(id) on delete set null);
 
  3.序列
      创建序列的语法:
       create sequence 序列名
       increment by 步长
       start with 起始值
       maxvalue 最大值
       minvalue 最小值
       cycle|nocycle
       cache n|nocache;
       使用序列:
      在插入语句中:序列名.nextval
      序列的数据字典:user_sequences
        SQL->select sequence_name,increment_by,min_value,to_char(max_value),last_number
from user_sequences;
3-1)为t_user表和t_表创建自增序列,起始位1,步长1,最大值999999
SQL->create sequence user_seq
    increment by 1
start with 1
maxvalue 999999
nocycle
nocache;
3-2)使用user_seq序列作为user的id,向用户表插入两条记录。
  SQL->insert into t_user(id,name,passwd) values(user_seq.nextval,'disen','888');
       insert into t_user(id,name,passwd) values(user_seq.nextval,'sy','666');
注意:当DML事务回退时,序列不回退。
4.复习java
   1)数据类型
字符:char,String
数值:
  整数: bype,short,int,long
  精度:float,double
  布尔:boolean(true,false)
2)数据类型的引用类
 char-> Character
 byte->Byte
 int->Integer
 long->Long
 float->Float
 double->Double
3)Java命名规范:驼峰命名
a)类名:首字母大写,随后的每个词组的第一个字母大写。
b)属性(成员变量/字段)名:首字母小写,随后的每一个字母大写。
c)方法名:同属性命名方式。
eg.将"123"转成数值后加上100,打印结果
核心代码:
String ns="123";
int number=Integer.parseInt(ns);
number=number+100;//number+=100;
System.out.println("number="+number);
4)Java类
a)静态块
b)构造方法
c)成员变量
d)成员方法
使用类中的成员时,必须先实例化。
5)集合ArrayList,Arrays排序
Collection(List->ArrayList,Set->HashSet) Map->HashMap,TreeMap
在bean包下:alt+[insert]->Class->User:
 ID
          PASSWD
          NICK_NAME
          U_LEVEL
          U_ROLSE
 ->生成setter和getter方法
 ->生成构造方法
 ->生成实例化方法(单例模式)
二、java
1.数据类型
"123.56"->int :Integer.parseInt("123");
"123.56"->float :Float.parseFloat("123.56");
67.78->String :String.valueOf(67.78); "67.78"
2.语法
class,method,访问类型(public,default,proteted,private)
if-elseif-else

int a=100;
switch(a){
case 90:
System.out.println("a");
case 99:
System.out.println("c");
case 100:
System.out.println("d");
default:
System.out.println("e");
}
结果:d
      e
成绩>=60及格,>=80良,>=90优,switch语句实现
switch(score){
case 60:
case 70:
System.out.println("及格");break;
case 80:
System.out.println("良");break;
case 90:
case 100:
System.out.println("优");break;
default:
System.out.println("差");break;
}
while,do-while
for(int i=0,i<=10;i++){
}
3.数组,初始化
int[] numbers={2,34,5,6,7};
int[] n2=new int[4];
int len=n2.length;
change(n2);
System.out.println(int[] abc){
abc=12;//当前执行的位置
}
4.类对象
三大特性:封装,继承,多态
多态:
1)父类的引用指向子类的对象
2)子类->父类:自动转换
  父类->子类:强制转换  instanceof
 class User{
private String name;
public User(){
this.name="disen"
}
public void setName(String name){
 
}
public void getName(){
return this.name;
}
 }
 public static void main(String[] args){
 User user=new User();
 User u2=new User("SY");
 changName(u2);
 System.out.println(user.getName());
 System.out.println(u2.getName());
 }
 public static void changeName(User user);
  user.setName("YanYan");
 }
  










开库
root
1.su - oracle//使用oracle
2.//打开监听器
3.sqlplus / as sysdba
4.sql>startup
5.alter system register
6.!lsnrctl status
7.vi /etc/hosts//修改ip




clear//清屏
su - oracle
sqlplus  / as sysdba
create user zhang identified by zhang;
conn zhang/zhang;
conn / as sysdba
grant create session to zhang;
sqlplus zhang/zhang;
select *from tab;
grant create table to zhang;
create table t(id int,name char(10));
grant select on emp to zhang;
revoke select onemp form zhang;


delclare 
integer
char
varchar2


oerr ora 12899//查错


select nullif(1,0) from dual;//1
select nullif(0,0) from dual;//null


date
timestamp
exception
begin 
exception
end;






关系型数据库
*数据是以二维表的形式来存储
*列不可再分
coll coll2 coll3 coll4
coll.1
coll.2
第一范式:
*只要建立了列不可再分的二维表,就属于第一范式。
第二范式:
*二维表中存在一列,该列的值没有重复,可以唯一地区分每一行记录。
第三范式:
*在符合第二范式
SQL
select * from emp where empno=7788
optimizer




*简单查询
数据来源一个表
*复杂查询
数据来源于多个表,或者包含多个查询
*多表连接查询
*复合查询
union,intersect,minus
*多表连接查询
*非关联子查询
select * from emp where sal>(select from emp where ename="jones");


select ename,a.deptno,sal,avgsal from emp a,(select deptno,AVG(sal) avgsal from emp group by deptno) b where a.deptno=b.deptno and sal>avgsal;
关联子查询
select ename,o.deptno,sal from emp o where sal>(select avg(sal) from emp 1 where i.deptno=o.deptno);
原创粉丝点击