zoukankan      html  css  js  c++  java
  • [学习笔记] Oracle基础增删改查用法

    查询

    select *|列名|表达式 from 表名 where 条件 order by 列名
    
    select t.* from STUDENT.STUINFO t where t.stuname = '李四';
    select t.stuid,t.classno,t.stuaddress,t.grade from STUDENT.STUINFO t where t.stuname = '李四';
    select t.* from STUDENT.STUINFO t where t.classno = 'C201801' ORDER BY T.AGE ASC;
    

    备份查询数据

    create table 表名 as select 语句
    
    create table student.stuinfo2 as select * from student.stuinfo;
    select * from student.stuinfo2;
    

    插入

    insert into 表名(列名1,列名2,列名3.....) values(值1,值2,值3.....);
    
    insert into STUDENT.STUINFO (STUID, STUNAME, SEX, AGE, CLASSNO, STUADDRESS, GRADE, ENROLDATE, IDNUMBER)
    values ('SC201801005', '龙七', '1', 26, 'C201801', '福建省厦门市XXX号', '2018', to_date('01-09-2018', 'dd-mm-yyyy'), '3503021992XXXXXXXX');
    select * from student.stuinfo t where t.stuid='SC201801005';
    

    插入查询结果

    INSERT INTO 表名 SELECT 子句
    
    delete from student.stuinfo t where t.stuid in (select b.stuid from student.stuinfo2 b);
    insert into student.stuinfo select * from student.stuinfo2;
    select * from student.stuinfo;
    

    更新

    update 表名 set 列名1=值1,列名2=值2,列名3=值3... where 条件
    
    update student.stuinfo t set t.age = '24', t.idnumber = '3503021994XXXXXXXX' where t.stuname = '张三';
    commit;
    select * from student.stuinfo t where t.stuname='张三';
    

    通过查询结果更新

    update 表1 set 列名=(select 列名 from 表2 where 表1.列名=表2.列名) 
           where exists (select 1 from 表2 where 表1.列名=表2.列名)
    
    update student.stuinfo t 
    set (age, idnumber) = (select age, idnumber from student.stuinfo2 b where b.stuid = t.stuid) 
    where exists (select 1 from student.stuinfo2 b where b.stuid = t.stuid and b.stuname = '张三');
                
    select *from student.stuinfo t where t.stuname='张三';
    

    删除

    delete from 表名 where 条件
    
    delete from stuinfo t where t.stuname='张三';
    

    截断表

    truncate table 表名
    
    truncate table stuinfo2;
    

    删除和截断的区别

    • TRUNCATE 是 DDL 命令,命令执行完就提交,删除的数据不能恢复; DELETE 命令是 DML 命令,命令执行完需提交后才生效,删除后的数据可以通过日志文件恢复。
    • TRUNCATE 的执行速度比 DELETE 速度快很多。
    • TRUNCATE 会重置索引,DELETE 不会。
    • DELETE 会触发触发器,TRUNCATE 不会。
    • DELETE 的原理是一条一条从表中删除数据,并将删除操作当做事务记录在数据库日志中,可以回滚;TRUNCATE 是一次性将数据页删除,不能回滚。
  • 相关阅读:
    替换URL传递的参数
    执行SQl语句得到xml结果集
    table中文本太长换行
    org.xml.sax.SAXNotRecognizedException
    WAMP+CMSeasy快速搭建学校网站
    推荐几个web前台开发的小工具
    来园子里注册啦
    C++ Virtual的背后
    Games101观后补充笔记
    Lua语法入门
  • 原文地址:https://www.cnblogs.com/danhuang/p/12442564.html
Copyright © 2011-2022 走看看