基于Oracle数据库的sql语句,有点乱

来源:互联网 发布:网站采集软件 编辑:程序博客网 时间:2024/05/20 06:28
order by后可以加
       1 列名
       2 别名
       3 select后面跟着的表达式
       4 select后面子项的标号
       5 不是select子项,但是是表中其它某一列(若列中包含null,按最大值处理)
       
where后面可加
       1.等式last_name='Magee'
       2.salary between 1525 and 1550;工资在1525-1550的闭区间内
       3.dept_id in (39,42,45) 列举dept_id能取到的值
       4.last_name like 'M%'  名字以M开头
       5.last_name like '\_%' escape '\'    名字以_开头,escape定义转义字符
       
单行函数
       lower(小写),upper(大写),concat(连接1,连接2),initcap(‘首字母大写输出’),substr(column,start,length)length下标从1开始
       
多表查询
     1 等值连接和不等值连接(内连接)  用等号连接
     2 外连接(左,右,全)  
     3 自连接
    
     左外连接     select e.last_name,d.name
                  from s_emp e
                  left outer join s_dept d
                  on e.dept_id = d.id;
                  
     右外连接          select e.last_name,d.name
                     from s_emp e
                     right outer join s_dept d
                     on e.dept_id = d.id;
                         
     全连接            select e.last_name,d.name
                    from s_emp e full outer join s_dept d
                    on e.dept_id = d.id;
                    
    并集   union
    补集   minus
    交集   intersect
    
多行函数  avg(salary),max(salary),min(salary),sum(salary)

      select dept_id,avg(salary)
      from s_emp
      where dept_id>40
      group by dept_id
      having dept_id>40
      
子查询
      select r,last_name
      from (select rownum r,last_name
      from s_emp
      where rownum<=20)  
      where r between 11 and 20;
      
替换变量
      默认值
     define varl = '''Smith'''
    
创建表
     create table customer(
        id number(7) primary key,
        name varchar2(25) not null,
        phone varchar2(20) unique,
        rep_id number(7) references s_emp(id)
        );
删除表:截断   
       drop table customer;
       
主键约束:既可以写在列后面(列级)
          也可以写在所有列之后(表级约束)
        列级
        create table customer(
        id number(7) primary key,
        name varchar2(25),
        phone varchar2(20)
        );
        表级
        drop table customer;        
        create table customer(
        id number(7),
        name varchar2(25),
        phone varchar2(20),
        primary key(id)
        );
外键约束:foreign key(emp_id) references s_emp(id)    
非空约束没有表级
唯一约束
    
    主键不一定是一个列
    多个列组成的主键成为联合主键     
    唯一:两个列组合的结果是唯一的
    非空:每一列都非空
    通常用在桥表
检查型约束
        check 限定某一列的值只能是某几个指定值,如果不是则报错
        
        create table student(
           gender varchar2(5)
           check(gender in('男','女'))
        );
        
        表可以通过子查询创建(复制表)
        create table emp1
        as
        select id,last_name,salary
        from s_emp;        
        
        复制表结构,没有数据    
        create table emp2
        as
        select *
        from s_emp
        where 1 = 0;        
            
        默认值
        create table student2(
          name varchar2(10) default '李狗蛋',
          age number(7)
        );    
插入数据
      insert into tab1ename() values();
         
        插入数据 使用子查询
       insert into emp2(id,last_name,salary)
       select id,last_name,salary
       from emp1
       where salary>1000    
更新UPDATE
       update emp1
       set salary = 5000
       where last_name = 'Biri';    
删除delete
       delete from emp1
       where salary = 5000;
       对于更新和删除来说
       如果操作的是外键
       1 更新外键列 如果更新后的值,在引入表中不存在
      delete:DML删除数据但不清空表空间
      truncate:DDL删除数据清空表空间
       1删除数据,如果删除的那列,被当作外键那么删除不了,需要把所有外键行先删除,再删除被引用行,建议在创建表的时候 添加级联删除
       create table my_emp(
           id number(7) primary key,
           name varchar2(20) not null
       );
       
       create table my_order(
            id number(7) primary key,
            name varchar2(20) not null,
            emp_id number(7),
            foreign key(emp_id) references my_emp(id) on delete cascade        
       );
事务:一组操作命令(DCL,DDL   DML,select)
                          
      代表业务逻辑中的一个行为,这个行为可能需要多个sql语句才能执行完    
      ACDI:原子性,隔离性,一致性,永久性
      原子性:事务必须是原子工作单元;对于其数据修改,要么全都执行,要么全都不执行
      一致性:事务在完成时,必须使所有的数据都保持一致状态   
      隔离性:由并发事务所作的修改必须与任何其它并发事务所作的修改隔离
      持久性:事务完成之后,它对于系统的影响是永久性的

commit  事务提交 ,能将事务修改的数据持久保存到数据库中
                   如果不提交事务,事务只能在当前会话中看到
      1 exit 退出sqlplus
      2 DDL语句会触发事务提交
    不同处理方式commit
      1 Oracle 的sqlplus中需要手动提交
      2 jdbc 自动提交事务
      3 mysql 的客户端需要手动提交事务
      4 navicat 通用的数据可视化窗口,自动提交
      
rollback 事务的回滚(也代表事务结束):没用提交的事务,可以回滚到原始状态

savepoint 保存回滚点:将事务回退到某个回滚点状态下
     update my_emp
     set name = '李狗蛋'
     where id = 5;
     savepoint p1;
添加新的列
     alter table my_emp
     add(
       salary number(7) unique
     );    
删除表中某一列
     alter table my_emp
     drop column salary;
    
更改表中某一列
     可以更改列的数据类型,更改not null约束
     如果列中已有null值,则不能添加not null约束
     alter table my_emp
     modify(
        id number(9)
     );
    
添加约束
     create table tes(
        id number(7) primary key,
        name varchar2(20)
     );
     alter table tes
     modify(name varchar2(15) not null);
删除约束
     
    
视图:视图是表的映像
    create view v_emp
    as
    select id,last_name,salary
    from s_emp;
    
    简单视图:直接通过单表条件查询
    允许直接在视图中增删改查修改数据,且会关联到原表
    更改原表中数据也会关联到视图中
    
    复杂视图:通过group by或者连接查询
    在视图上只能进行 查询工作
    原表中进行增删改会影响视图
    create or replace view v_emp
    as
    select
    
    视图无法修改表结构alter
    
    将视图设置为只读(只用于简单视图,复杂视图本来就是只读)
    create  view v_emp
    as
    select id,last_name,salary
    from s_emp
    with read only:设置为只读
    
    with check option:通过条件筛选获得的,添加这条语句就不能修改条件列的内容
    drop view
    
索引 :查询的数据较少,但数据缺很多,比如大海中找绣花针
    相当于给原列添加了 目录(描述了某个范围的值,在那个位置)
    创建索引 就是一句建议的话,用不用还是oracle判断1,基于速度,2.基于代价,性价比高的时候
  1.自动添加索引
    primary key和唯一列 默认有索引
  2.手动添加索引
    create index indexname
    on table(column)

注释:
    comment on table 表名
    is '注释‘;
    
序列:oracle特有
    create sequence seqname
    increment by n   步长
    start with n   起始位置
    {maxvalue n}   最大取值
    {minvalue n}   最小取值
    cycle   循环
    cache n   一次可以去多个值


    拿创建的序列值
    select seqname.nextval from dual;
    主键自动生成
    insert into tab1ename(id,name)
    values(seqname.nextval,'hello');

    mysql:auto_increment
    
1.给用户权限
    create user scott
    create session 创建会话权限
    create table 创建表的权限
    create sequence 创建序列的权限
    create view 创建视图的权限
    create procedure 创建程序单元plsql
    
    2.给权限
    grant create table to user
    给用户创建表的权限
    
    grant connect,resource,dba to username;
    dba:拥有全部权限,是系统最高权限,只有dba才可以创建数据库结构
    resource:只可以创建实体,不可以创建数据库结构
    connect:只能登陆oracle,不可以创建实体,数据库
    
    3.回收权限
    revoke 权限名 from 用户/角色/public
    
    4.修改密码:alter user username identified by password
    
    5.导入导出数据
    导出briup@briup$ exp userid=briup/briup full=y file=inner_notify.dmp
    导入briup@briup$ imp userid=briup/briup full=y file=inner_notify.dmp