Oracle数据库操作基本语法
创建表
SQL>create table classes(
classId number(2),
cname varchar2(40),
birthday date
);
添加一个字段
SQL>alter table student add (classId number(2));
修改字段长度
SQL>alter table student modify(xm varchar2(30));
修改字段的类型/或是名字(不能有数据)
SQL>alter table student modify(xm char(30));
删除一个字段
SQL>alter table student drop column sal;
修改表的名字
SQL>rename student to stu;
删除表
SQL>drop table student;
插入所有字段数据
SQL>insert into student values (‘001’,’salina’,’女’,’01-5月-05’,10);
修改日期输入格式
SQL>alter session set nls_date_format = ‘yyyy-mm-dd’; //临时生效,重启后不起错用
SQL>insert into student values (‘001’,’salina’,’女’,to_date(’01-5 -05’,’yyyy-mm-dd’),10);
SQL>insert into student values (‘001’,’salina’,’女’,to_date(’01/5 -05’,’yyyy/mm/dd’),10);
插入部分字段
SQL>insert into student (xh,xm,sex) values(‘001’,’lison’,’女’);
插入空值
SQL>insert into student (xh,xm,sex,birthday) values(‘021’,’BLYK’,’男’,null);
一条插入语句可以插入多行数据
SQL> insert into kkk (Myid,myname,mydept) select empno ,ename,deptno from emp where deptno=10;
查询空值/(非空)的数据
SQL>select * from student where brithday is null(/not null);
修改(更新)数据
SQL>update student set sal=sql/2 where sex =’男’;
更改多项数据
SQL> update emp set (job,sal,comm)=(select job,sal,comm from emp where ename='SMITH') where ename='SCOTT';
删除数据
- 1. 保存还原点
SQL>savepoint aa;
- 2. 删除数据
【1】 SQL>delete from student; //删除表的数据
【2】 SQL>drop table student; //删除表的结构和数据
【3】 SQL>delete from student where xh=’001’; //删除一条记录
【4】 SQL>truncate table student; //删除表中的所有记录,表结构还在,不写日志,无法扎找回的记录,速度快
查看表结构
SQL>desc student;
查询指定列
SQL>select sex,xh,xm from student;
如何取消重复
SQL>select distinct deptno,job from student;
打开显示操作时间的开关
SQL>set timing on;
为表格添加大的数据行(用于测试反应时间)
SQL>insert into users (userid,username,userpss) select * from users;
统计表内有多少条记录
SQL>select count(*) from users;
屏蔽列内相同数据
SQL>select distinct deptno,job from emp;
查询指定列的某个数据相关的数据
SQL>select deptno,job,sal from emp where ename=’smith’;
使用算数表达式
SQL>select sal*12 from emp;
使用类的别名
SQL>select ename “姓名”,sal*12 as “年收入” from emp;
处理null(空)值
SQL>select sal*13+nvl(comm,0)*13 “年工资”,ename,comm from emp;
连接字符串(||)
SQL>select ename || ‘is a’ || job from emp;
Where子句的使用
【1】SQL>select ename,sal from emp where sal>3000; //number的范围确定
【2】SQL>select ename,hiredate from emp where hiredate>’1-1月-1982’; //日期格式的范围确定
【3】SQL>select ename,sal from emp where sal>=2000 and sal<=2500; //组合条件
Like操作符:’%’、’_’
SQL>select ename,sal from emp where ename like ‘S%’; //第一个字符【名字第一个字符为S的员工的信息(工资)】
SQL>select ename,sal from emp where ename like ‘__O%’; //其它字符【名字第三个字符为O的员工的信息(工资)】
批量查询
SQL>select * from emp where in(123,456,789); //查询一个条件的多个情况的批量处理
查询某个数据行的某列为空的数据的相关数据
SQL >select * from emp where mgr is null;
条件组合查询(与、或)
SQL>select * from emp where (sal>500 or job=’MANAGER’) and ename like ‘J%’;
Order by 排序
【1】SQL>select * from emp order by sal (asc); //从低到高[默认]
【2】SQL>select * from emp order by sal desc; //从高到低
【3】SQL>select * from emp order by deptno (asc),sal desc; //组合排序
【4】SQL>select ename,sal*12 “年薪” from emp order by “年薪” (asc);
SQL> select ename,(sal+nvl(comm,0))*12 as "年薪" from emp order by "年薪";
资料分组(max、min、avg、sum、count)
SQL>select max(sal),min(sal) from emp;
SQL>select ename,sal from emp where sal=(select max(sal) from emp); //子查询,组合查询
SQL> select * from emp where sal>(select avg(sal) from emp); //子查询,组合查询
SQL> update emp set sal=sal*1.1 where sal<(select avg(sal) from emp) and hiredate<'1-1月-1982'; //将工资小于平均工资并且入职年限早于1982-1-1的人工资增加10%
Group by 和 having 子句
//group by用于对查询出的数据进行分组统计
//having 用于限制分组显示结果
SQL>select avg(sal),max(sal),deptno from emp group by deptno; //显示每个部门的平均工资和最低工资
SQL>select avg(sal),max(sal),deptno from emp group by deptno; //显示每个部门的平均工资和最低工资
SQL> select avg (sal),max(sal),deptno from emp group by deptno having avg(sal)>2000;
SQL> select avg (sal),max(sal),deptno from emp group by deptno having avg(sal)>2000 order by avg(sal);
多表查询
笛卡尔集:规定多表查询的条件是至少不能少于:表的个数-1
SQL> select a1.ename,a1.sal,a2.dname from emp a1,dept a2 where a1.deptno=a2.deptno;
SQL> select a1.dname,a2.ename,a2.sal from dept a1,emp a2 where a1.deptno=a2.deptno and a1.deptno=10; //显示部门编号为10的部门名、员工名和工资
SQL> select a1.ename,a1.sal,a2.grade from emp a1,salgrade a2 where a1.sal between a2.losal and a2.hisal;
SQL> select a1.ename,a1.sal,a2.dname from emp a1,dept a2 where a1.deptno=a2.deptno order by a1.deptno; //多表排序
SQL> select worker.ename,boss.ename from emp worker,emp boss where worker.mgr=boss.empno; // 自连接(多表查询的特殊情况)
SQL> select worker.ename,boss.ename from emp worker,emp boss where worker.mgr=boss.empno and worker.ename='FORD';
子查询
SQL> select * from emp where deptno=(select deptno from emp where ename='SMITH');
SQL> select distinct job from emp where deptno=10;
SQL> select * from emp where job in (select distinct job from emp where deptno=10);
// 如何查询和部门10的工作相同的雇员的名字、岗位、工资、部门号。
SQL> select ename ,sal,deptno from emp where sal>all (select sal from emp where deptno=30);//如何查询工资比部门30的所有员工的工资高的员工的姓名、工资和部门号
SQL> select ename ,sal,deptno from emp where sal>(select max(sal) from emp where deptno=30);
SQL> select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH');
内嵌视图
//当在from子句中使用子查询的时候,必须给子查询指定别名
SQL>select a2.ename,a2.sal,a2.deptno,a1.mysal from emp a2,(select deptno,avg(sal) (as ) mysal from emp group by deptno) a1 where a2.deptno=a1.deptno and a2.sal>a1.mysal;
分页
SQL >select a1.*,rownum rn from (select * from emp) a1;//orcle为表分配的行号
SQL> select * from (select a1.*,rownum rn from (select * from emp) a1 where rownum<=10) where rn>=6;
//查询内容的变化
- 所有的改动(指定查询列)只需更改最里面的子查询
- (排序)只需更改最里面的子查询
子查询(用查询结果创建新表)
SQL> create table mytable (id,name,sal,job,deptno) as select empno,ename,sal,job,deptno from emp;
合并查询
union(求并集), union all , intersect(取交集), minus (差集)
SQL> select ename,sal,job from emp where sal>2500;
SQL> select ename,sal,job from emp where job='MANAGER';
SQL> select ename,sal,job from emp where sal>2500 union select ename,sal,job from emp where job='MANAGER'; // union(求并集)
事务
SQL>commit; //事务 (第一次创建,第二次提交)当退出数据库时,系统自动提交事务
SQL>savepoint a1; //创建保存点 (保存点的个数没有限制)
SQL>rollback to aa; //使用保存点回滚到aa
SQL>rollback; //回滚到事务创建开始
只读事务
SQL>set transaction read only
Java中的事务
Ct.setAutoCommit(false); //设置事务自动提交为否
Ct.commit(); //提交事务
字符函数
lower(char)将字符串转换为小写的格式
upper(char)将字符串装换为大写的格式
length(char)返回字符串的长度
substr(char,m,n)取字符串的子串
SQL>select lower(ename) from emp;
SQL>select ename from emp where length(ename)=5;
参考网址:http://www.cnblogs.com/huangf714/p/5876166.html
Oracle数据库操作基本语法的更多相关文章
- 基于OCILIB的oracle数据库操作总结及自动生成Model和Dao的工具
基于OCILIB的oracle数据库操作总结 1. 类图 2. 基类BaseOCIDao的设计与实现 BaseOCIDao.h头文件 #pragma once /* ----- ...
- C# Oracle数据库操作类实例详解
本文所述为C#实现的Oracle数据库操作类,可执行超多常用的Oracle数据库操作,包含了基础数据库连接.关闭连接.输出记录集.执行Sql语句,返回带分页功能的dataset .取表里字段的类型和长 ...
- (转)C# Oracle数据库操作类
原文地址:http://www.cnblogs.com/haibing0107/p/6143922.html using System;using System.Data;using System.C ...
- C# Oracle数据库操作类
using System; using System.Data; using System.Collections.Generic; using System.Configuration; using ...
- 循序渐进Python3(十二) --2-- web框架之django简单实现oracle数据库操作
在 Django 中构建 Oracle 数据库支持的 Web 应用程序 了解如何配置 Django 以便与 Oracle 数据库交互,并使用 ORM 进行数据库连接. 产能在软 ...
- Oracle数据库操作
本例使用oracle数据库,使用PL/SQL可视化工具: --查询员工表数据 (emp为pl/sql自带的表,也可自己新建)select * from emp; --创建表空间create table ...
- Java从入门到精通——数据库篇之JAVA中的对Oracle数据库操作
在Java中对Oracle数据库的操作分为两种:一.查询.二.非查询. 下面是我对其进行总结: 一.查询数据 /** * 根据用户代码查询 * @param userId * @return 如果存在 ...
- Oracle数据库操作---入门(一)
由于工作原因,近期可能会开始多复习一些数据库相关的知识了,想深入了解的,也可以一起复习.学习~ 前期先巩固一下基础操作,后期会一点点的加深向运维方向深入.开篇主要介绍一些数据库理论知识,不感兴趣的可以 ...
- Oracle数据库操作类及连接方法
创建DataBaseAccess引用库下面包括DBConnectionString.cs,DBType.cs ,SysName.cs 三个类 DBConnectionString: using Sys ...
随机推荐
- HashMap 与 ConcurrentHashMap 在初始化不同大小容量时,实际分配的空间情况
HashMap.java int capacity = 1; int initialCapacitys[] = {1,2,3,4,5,6,7,8,9,10,11,13,15,16,17,2 ...
- java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 不能使用 '(未知的)';文件已在使用中
最近查看程序输入的日志时发现一个java连接Access的程序时不时的抛出: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driv ...
- 函数式编程(九)——map,filter,reduce
编程方法论: 面向过程:按照一个固定的流程去模拟解决问题的流程 函数式:编程语言定义的函数 + 数学意义的函数 y = 2*x + 1 函数用编程语言实现 def fun(x): return 2*x ...
- es6数组的复制
数组是复合的数据类型,直接复制的话,只是复制了指向底层数据结构的指针,而不是克隆一个全新的数组. const a1 = [1, 2]; const a2 = a1; a2[0] = 2; a1 // ...
- 在windows上部署使用redis出现的问题及解决方法
在windows上部署使用Redis出现问题的解决方法 原文链接:https://www.cnblogs.com/herry52/p/5938902.html 下载Redis 在Redis的官网下载 ...
- Linux系统下yum镜像源环境部署记录
之前介绍了Linux环境下本地yum源配置方法,不过这个是最简单最基础的配置,在yum安装的时候可能有些软件包不够齐全,下面说下完整yun镜像源系统环境部署记录(yum源更新脚本下载地址:https: ...
- SQL SERVER与C#数据类型对照表
分类 SQL SERVER类型 类型说明 C#类型 精确数字 bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) ...
- Feature Selection Can Reduce Overfitting And RF Show Feature Importance
一.特征选择可以减少过拟合代码实例 该实例来自机器学习实战第四章 #coding=utf-8 ''' We use KNN to show that feature selection maybe r ...
- php 配置文件不加载的一次配置
最近在一台服务器上安装配置了5.6和7.1两个版本的php,之前是5.6的.使用都正常,7.1是后期安装的. 问题: 在7.1的程序目录中命名的配置文件为:php7-9026.ini,同时安装并在配置 ...
- 标准遗传算法(二进制编码 python实现)
代码地址:https://github.com/guojun007/binary_sga 种群初始化: binary_sga/population_init/population_init.py #种 ...