zoukankan      html  css  js  c++  java
  • Oracle学习系列1

    两个服务必须启动:
    	OracleOraDb10g*TNListener 和 OracleService***
    
    使用sqlplusw先进行环境的设置
    	set linesize 300    ;
    	set pagesize 30     ;
    
    编辑sql命令:
    	ed a.sql
    	执行 @a
    
    切换用户:
    	conn User/passwd   [as sysdba|sysoper ]
    
    	conn system/manager
    	conn sys/change_on_install  as sysdba
    
    显示用户:
    	show user;
    	
    获取所有表的名字:
    	select * from tab;
    	
    查看表的结构:
    	desc emp;  //emp 表
    	
    清空屏幕
    	clear scr
    	
    ****************************************************************
    
    SQL语句:
    
    	简单查询   限定查询   单行查询
    	
    	所需表:scott用户下的表 --  雇员表,部门表,奖金表,薪水表
    	
    	
    查询语句的基本格式
    
    使用DISTINCT 关键字去掉重复的查询列
    
    使用限定查询
    
    对查询结果进行排序
    
    掌握Oracle中提供的各主要单行函数
    
    ***********************************************************************************************
    
    
    SQL包含DML(数据操作语言)、DDL(数据定义语言)、DCL(数据控制语言)
    
    简单查询 :
    	select  {distinct } *|具体的列  别名  from 表  
    	
    	select * from emp;
    	select empno,ename, job  from emp;
    	
    	指定查询返回列的名称 ,为列起个别名
    		select  empno 编号 ,ename 姓名 ,job 工作 from emp;
    	
    	要求查询所有工作:
    		select job from emp;   //出现重复值
    		sel distinct job from emp;  //消除重复列。但是在消除重复列时,如果要同时查询多列,则必须保证所有列都重复才能消除
    	查询雇员的编号和工作:
    		select ename, job   from emp;
    	
    	查询雇员的编号,姓名,工作,显示格式为:编号是7369的雇员,姓名是:SMITH,工作是:CLERK   :
    	
    		select   '编号是:' || empno ||' 的雇员,姓名是:||'ename ',工作是:' || gob  from emp;
    	
    	要求求出每个雇员的姓名及年薪:
    		select ename , sal*12 from emp;
    		select ename , sal*12  income  from emp;  // 别名回避中文
    		
    ***********************************************************************************************
    
    限定查询(where子句):
    
    	格式:	select  {distinct } *|具体的列  别名  
    			from 表  
    			{ where 条件s }
    			
    	查询出工资大于1500的所有雇员信息:
    		select * 
    		from emp
    		where  sal>1500;
    		
    	查询每月可以得到奖金的雇员信息:
    		select * 
    		from emp
    		where comm IS NOT NULL; //comm字段不为null  
    		
    	查询无奖金的雇员:
    		select * 
    		from emp;
    		where comm IS NULL;
    		
    	查询出基本工资大于1500,同时可以领取奖金的所有雇员信息:
    		select * 
    		from emp
    		 where sal>1500 and comm IS NOT NULL;
    		 
    	 查询出基本工资大于1500,或者可以领取奖金的所有雇员信息:
    		select * 
    			from emp
    				where sal>1500 or comm IS NOT NULL;
    				
    	 查询出基本工资不大于1500,同时不可以领取奖金的所有雇员信息:
    	 	select *
    			from emp
    				where  not ( sal>1500 or comm IS NOT NULL ) ; //通过()表示一组条件
    		
    	 查询基本工资大于1500,但是小于3000的全部雇员信息:
    	 	select * 
    			from emp
    				where  sal>1500  and sal <3000 ;
    	****SQL专门制定范围的查询的过滤语句:between  min and  max (包含等于的功能)*****
    		select *
    			from emp
    				where sal between 1500 and 3000 ; //between and  等价于sal>=1500 and sal<=3000
    		
    	 查询出1981年雇佣的全部雇员信息:
    	 	select * 
    			from emp
    				where  hiredate between ‘ 1-1月 -81 ‘and  ’31-12月 -81 ‘ ;//日期表示加上’‘
    				
    		 **结论:between ...and...查询除了支持数字之外,还支持日期的查询(日期实际上以数组的形式表示出来)**
    	 
    	 查询出姓名是smith的雇员的信息:
    	 	select *
    			from emp
    				where ename ='SMITH' ;  //Oracle中对大小写敏感,smith不能查询出
    			
    	查询出雇员编号7369,7499,7521的具体信息:
    	
    	   **查询的范围,可以用IN()操作符完成,还有NOT IN()**
    		select * 
    			from emp
    				where  emp IN( 7369,7499,7521 );
    				
    	查询出编号不是7369,7499,7521的具体信息:
    		select * 
    			from emp
    				where  emp NOT IN( 7369,7499,7521 );
    				
    	要求查询出姓名是SMITH,ALLEN,KING的雇员信息:
    		select *
    			from emp
    				where ename IN( 'SMITH','ALLEN','KING' );
    				
    		结论:IN操作符可以用在数字和字符串上,指定的额外的值不影响程序的运行
    		
    		
    模糊查找功能,SQL中使用LIKE语句,但要注意通配符:% -->匹配任意长度的内容
    										  _ -->匹配一个长度的内容
    	
    	查询出所有雇员姓名中第二个字母中包含“M”的雇员信息:
    		select * 
    			from emp
    				where ename like '_M%';
    				
    	查询出雇员姓名中包含字母“M”的雇员信息:
    		select *
    			from emp
    				where ename like '%M%' ;
    				
    	查询出在1981年雇佣的雇员信息:
    		select * 
    		 	from emp
    				where hiredate like '%81%';
    				
    	查询雇员编号不是7369的雇员信息:
    		select * from emp where empno <>7369 ;
    		
    		select * from emp where enpno !=7369 ;
    				
    	
    ***********************************************************************************************
    
    对查询的结果进行排序(order by子句)(重点) -->排序操作永远放在SQL语句最后执行
    
    		格式:	select  {distinct } *|具体的列  别名  
    			from 表  
    			{ where 条件s }
    			{ order by 排序字段1,排序字段2  ASC|DESC } //默认低->高(升序)
    			
    	要求按照工资由低到高排序:
    		select *
    			from emp
    				order by sal ;
    				
    	
    	要求查询出10部门的所有雇员信息,信息按照工资降序排序,若工资相等,按照雇佣日期升序排序
    		select * 
    			from emp
    				where deptno =10
    					order by sal desc, hiredate (asc) ;//asc可不写
    					
    ***********************************************************************************************
    		
    单行函数(重点)
    		
    	格式:function_name( column | expression , [ arg1,arg2,...] )
    	
    	分类:
    		字符函数:接收字符输入并且返回字符或数值
    		数值函数:接收数值输入并返回数值
    		日期函数:对日期数据进行操作
    		转换函数:从一种数据类型转换成另一宗数据类型
    		通用函数:NVL函数,DECODE函数
    	
    	字符函数:
    			专门处理字符的,例如将大小写转换
    			
    		将小写字符转换成大写字符
    			select upper('smith') from dual ;// dual是张数据表
    		
    		查询smith雇员的所有信息:
    			select * 
    				from emp	
    					where ename = upper('smith') ;
    					
    		lower()函数:
    			select lower('SMITH') from dual ; //将字符串全部变为小写
    			
    		initcap()函数:
    			select initcap('hello, world') 
    				from dual ;  //将单词的第一个字母大写
    				
    				将雇员表中的雇员姓名变为开头字母大写
    					select initcap(ename) 
    						from emp;
    						
    		<字符串可以使用’||‘ 连接之外,还可以使用concat()函数进行连接操作>
    		select concat('hello' , 'world') from dual ; //不如’||‘好用
    		
    		字符串截取:substr()
    			select substr('hello',1,3) from dual ; //index从(0/1)开始  结果:hel 
    		
    		字符串长度:length()
    			select length('hello') from dual ; //  结果:5
    
    		内容替换:replace()
    			select replace('hello','l','x') from dual ; // 结果:hexxe
    		
    		要求显示所有雇员的姓名及姓名的后三个字符:
    			tips:先求整个长度-2,再截取	
    			select ename ,substr(ename, length(ename)-2) 
    				from emp;
    		or:
    			select ename ename, substr(ename ,-3,3)
    				from emp;
    				
    ***********************************************************************************************
    数值函数:
    	四舍五入: round()
    	截断小数点: trunc()
    	取余:	mod
    	
    	执行四舍五入操作:
    		select round(789.536) from dual ; // result:790
    		
    		保留两位小数:
    			select round(789.536,2) from dual ; // result:790.54
    		对整数进行四舍五入的进位:
    			select round(789.536,-2) from dual ; // result:800
    
    	验证trunc()函数:
    		select trunc(789.536) from dual ;//result : 789
    		select trunc(789.536,-2) from dual ;//result : 700
    		
    	使用mod()进行取余操作:
    		select mod(10,3) from dual ;//result : 1
    		
    	
    ***********************************************************************************************
    
    日期函数:
    		当前日期: select sysdate  from dual ;
    		
     规则:	日期-数字=日期        
    		日期+数字=日期
    		日期-日期=数字(天数)
    		
     函数:	months_between() :求给定日期范围的月数
     
     		add_months(): 在指定日期上加指定的月数,求出之后的日期
    		
    		next_day( date, char): 指定时间的下一个星期几(由char指定)所在的日期
    		
    		last_day(): 返回指定日期对应月份的最后一天
    	----------------------------------------------------
    	
    	显示10部门雇员进入公司的星期数:
    		tip: sysdate -hiredate = days /7 = weeks
    		
    		select  empno, ename,round( (sysdate - hiredate)/7 ) weeks 
    			from emp ;
    		
    	验证months_between():
    		select empno, round( ename, months_between(sysdate, hiredate) ) months
    			from emp;
    			
    	验证add_months():
    		select add_months(sysdate, 4) from dual ;
    		
    	验证next_day():
    		select next_day(sysdate, '星期一') from dual ;//当前系统日期的下一个周一
    		
    	验证last_day():
    		select last_day( sysdate ) from dual ;
    		
    		
    ***********************************************************************************************
    
    转换函数 (重点)
    	函数:
    		to_char(): 转换成字符串
    		to_number(): 转换成数字
    		to_date():  转换成日期
    		
       ===to_char()====
    	 	查询所有雇员的雇员编号,姓名,雇佣日期:
    			select enpno, ename, hiredate from emp ;
    		
    			将年,月,日进行分开,可以用to_char()函数进行拆分,
    					必须指定通配符	 yyyy-mm-dd
    				select enpno, ename, to_char( hiredate ,'yyyy') year,
    									 to_char(hiredate, 'mm') months,
    									 to_char(hiredate, 'dd') days
    						from emp;
    	
    		 to_char()函数具有日期显示转换的功能:
    		 
    			 select empno, ename, to_char(hiredate ,'yyyy-mm-dd')   //1994-05-04
    		 		from emp ;
    	     	 select empno, ename, to_char(hiredate ,'fmyyyy-mm-dd')  //1994-5-4
    		 		from emp ;	
    		
    		to_char()具有分隔数字功能:  (表示$:美元    L:本地货币)
    			select empno, ename,to_char(sal, '$99.999') 
    				from emp;    // sal   $1,600
    				
    ====to_number()=====
    	将字符串转变为数字
    		select to_number('123')+to_number('123') from dual ; //246
    
    ====to_number()=====
    	将字符串变为date类型
    		select to_date('2009-02-16','yyyy-mm-dd')  from dual ;
    		
    	
    ***********************************************************************************************
    
    通用函数:
    	NVL() :  将指定的null值变为指定的内容
    	
    	
    	DECODE() :DECODE(value,if1,then1,if2,then2,if3,then3,...,else),表示如果value 等于if1时,		  DECODE函数的结果返回then1,...,如果不等于任何一个if值,则返回else。
    			decode(value ,值1,返回值1,值2,返回值2,值3,返回值3,值4,返回值4,缺省值 )
    	
    	求出年薪的时候应该加上奖金,格式:(sal+comm)*12:
    		select empno, ename,  (sal+comm)*12 from emp ; //错误,有些雇员的comm为null
    		
    		select empno, ename, (sal+NVL(comm, 0)*12 ) income
    			from emp ;
    	 	
    		<对需要进行计算时,必须对null使用NVL()函数进行转换操作>
    		
    	验证decode()函数:
    		select decode(1, 1,'内容为1',2,'内容为2',3,'内容为3',4,'内容为4')
    			from dual ;   // result :内容为为1
    			
    		雇员的工作:业务员clerk, 销售人员salesman   经理 manager  分析员analyst  总裁president
    			
    		要求查询出雇员的编号,姓名,雇佣日期及工作,将工作替换成以上的信息:
    		
    			select empno 雇员编号, ename 雇员姓名, hiredate 雇佣日期,
    					decode(job, 'clerk','业务员','salesman','销售人员','manager','经理','analyst','分析师','president','总裁') 职位
    					from emp ;
    					
    ***********************************************************************************************
    
    SUMMARY
    	1,oracle主要用户:
    		超级管理员:sys/change_on_install
    		普通管理员:system/manage
    		普通用户:scott/tiger
    		
    	2,sqlplusw的一些常用命令:
    		设置行显示数量:set linesize 300
    		设置页显示数量:set pagesize 30
    		ed a.sql以及@a
    		连接:conn  user/passwd  as sysdba
    		
    	3,SQL基础语法的格式
    	4,单行函数,decode()函数最重要
    		
    

     

  • 相关阅读:
    【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
    Educational Codeforces Round 73 (Rated for Div. 2)F(线段树,扫描线)
    【PAT甲级】1042 Shuffling Machine (20 分)
    【PAT甲级】1041 Be Unique (20 分)(多重集)
    【PAT甲级】1040 Longest Symmetric String (25 分)(cin.getline(s,1007))
    【PAT甲级】1039 Course List for Student (25 分)(vector嵌套于map,段错误原因未知)
    Codeforces Round #588 (Div. 2)E(DFS,思维,__gcd,树)
    2017-3-9 SQL server 数据库
    2017-3-8 学生信息展示习题
    2017-3-5 C#基础 函数--递归
  • 原文地址:https://www.cnblogs.com/askDing/p/5463158.html
Copyright © 2011-2022 走看看