Oracle

1.SQL语句
1. select查询语句

格式: select (*|列名1,列名2,......) form 表名
desc 表名 查看表结构
column 列名 format a10;设置输出格式

	1.
		varchar()   只对汉字和全角等字符占两字节,数字,英文字符等都是一个字节。
		varchar2()  把空串等同于null处理,而varchar仍按照空串处理。最多可以存255个字符
		number      存放数字
		clob        用来存储大量文本数据
		blob        用来存储大量二进制数据的
		
	2.
		2.1 数字列进行数计算
			1.查询每个用户年工资
				select id,last_name,salary*12 from e_emp;
				
		2.2 给列/字段起别名
			select 列名1 [as] 别名,列名2 as 别名 from 表; 
		
		2.3 拼接
			2.3.1 ||
				select salary||'$' from s_emp;
			2.3.2 concat(值1,值2)
				select concat(salary,$) from s_emp;

		2.4 去除NULL值
			nvl(需要去除null值的列,默认值);
				NVL(eExpression1, eExpression2)
				若表达式1的值为null,则值替换为表达式2
			nvl2(需要去除null值的列,非null值的默认值,null值的默认值);
				NVL2(eExpression1,eExpression2,eExpression3)
				若表达式1为null,则显示表达式3的值,否则返回表达式2 的值
		
		2.5 去除重复元素
			select distinct 列名 from 表名;
2.排序和查询(限制)条件
	1. 排序
		order by 列名1 [desc/asc] ,列名2 [desc/asc] ....
		order by 列下标 [desc/asc] .....  下标从1开始
		asc升序 默认    desc 降序
	注意:出现在select查询语句末尾
	
	
	2. 查询条件
			where 条件表达式1 and/or 条件表达式2 .....
			
			1. 条件表达式
				比较符号:<  >  <=  >=  
				不等符号:!=  <>  ^=
				等于符号:=
				特殊关键字:
					1. 列 between 值1 and 值 2;
					2. 列 in (值1,值2,.....)
					3. like  模糊查询
					      列  like ' '
					      where last_name like 'P%';    值  区分大小写
							 %   任意多个字符
							 _   任意单个字符
							 符号转义  :where name like (%/%%) escape('/')
					4. is null
						is not null
					5. not
						not in
						not like
						is not null
						
			2. 逻辑表达式
				and/or
				select id,last_name from s_emp where salary>500 and salary<1000;
3. 单值函数
单值函数
		字符函数
			lower(列名/字符串)                    转化为小写
			upper()                              转化为大写
			initcap()                            首字母为大写
			length()                             获取字符串长度
			substr(列名/字符串,start,length)    截取字符串
			nvl/2()
			concat()
			sysdate                              获取当前日期
			
		数字函数                 
			round(列/数字,[数字])              四舍五入
			正数为小数点后几位,负数为小数点前几位             
			trunc(列/数字,[数字])              截取
			mod(列/数字,列/数字)               取模/取余
			
				select round(3.1545,2) from dual;	

		日期函数
			sysdate                            当前日期
			months_between(date1,date2)        计算相隔月份
			add_months(date,month)             增加月份
			next_day(date,周几)                获取当前日期的下一个周几 
			last_day(date)                     该月份最后一天
			                               
			
			select months_between(sysdate,sysdate-2) from dual;

		转化函数
			to_char
				fm 去空格
				L  改写本地货币符号
				select to_char (salary,'L9,999,999.00') from s_emp;
			to_number
			to_date(‘字符日期’,‘日期格式‘)				
		
多值函数  group by
		  avg sum max min 
		  count
4.多表查询
                                    (where条件数 = table数-1)
select * from table1,table2 where table1.id1 = table2.id;
1. 等值连接
2. 不等值连接
3. 外连接 
		1. 左外连接  left [outer]  join
				select id,last_name,salary from s_emp s,s_dept  d where e.dept_id = d.id(+);
				select id,last_name,salary from s_emp s left outer join s_dept d on e.dept_id = d.id;
		2. 右外连接  right [outer]  join
				select id,last_name,salary from s_emp e right outer join s_dept  d on e.dept_id = d.id;
				select id,last_name,salary from s_emp s,s_dept  d where e.dept_id(+) = d.id;
		3. 全连接  funll [outer] join
				select id,last_name,salary from s_emp e full outer join s_dept  d on e.dept_id = d.id;
				
4. 自连接
5.组函数
1. group by 分组
		位于where后或是from后
		
2. 组函数
	 2.1 avg  求平均值 
	 2.2 count  计算有多少少条数据 
	 2.3 max  最大值 	 
	 2.4 min  最小值	 
	 2.5 sum  求和
	 2.6 stddev  标准差
	 2.7 variance  方差
  
  用法:  1. 直接写组函数
		  只能出现在select
	    2. 组函数 + group by
	    3. 多表查询  
			select 中的列如果没有被组函数修饰,必须出现在group by 子句后面,否则编译不能通过
	    4. group by + having			
			having出现在group by之后,having 不能单独出现,只能出现在group by后面
			select dept_id,avg(salary)
		 	from s_emp
		 	group by dept_id
		 	having avg(salary)>1500;
6. 子查询 嵌套查询
即一个select查询中嵌套另一个select查询
 eg:	select last_name,salary
		 	from s_emp
		 	where salary=(
		 	select max(salary)
		 	from s_emp
		);
7.数据建模和数据库设计
1. 软件开发的步骤大致分为:
	1. 需求分析
	2. 系统设计
	3. 编码实现
	4. 系统测试
	5. 运行维护

    1:1   :在任意一方建立外键 
    1:N   :在多的一方需要建立外键
    N:M   :需要借助第三张表 ,桥表(桥接作用)
		    学生 student
		    课程 course                                 
		    桥表 stu_course(id,stu_id,course_id)
8. 建表
1. 格式:
		create table 表名 (
			列名1  数据类型  列级约束,
			列名2  数据类型  列级约束,
			......
			列名3  数据类型  列级约束,
			
			表级约束1,
			......
		);
		列级约束 :not null
		标记约束:联合。。。
		
	删除表:drop table 表名;

  eg:	create table student(
			id number primary key,
			name varchar2(20) not null,
			phone number unique not null,
			gender varchar2(5) check(gender in('f','m')) not null
		);
		
2.   数据类型:
		number,char/varchar/varchar2,date,clob.blob

3. 约束(5种):
		主键约束     primary key
		外键约束     foreign key
		不为空约束   not null
		唯一约束     unique
		检查约束     check
	eg:
		create table s_user(
		   id number primary key,
		   name varchar2(20) not null
		);

		create table s_order(
			 id number primary key,
			name varchar(20),
			user_id number references s_user(id)
		);
		
		create table s_order(
			 id number,
			name varchar(20),
			user_id number, 
			primary key(id),
			foreign key(user_id) references s_user(id)
		);
9.DML语句
SQL语句种类:
 	查询语句:select
 	DML语言:数据操纵语言 ==> 事务,会创建事务
		 	insert  undate  delete
	DDL语句:数据定义语言  自动提交事务
			create  drop  truncate
			alter
	DCL语句:数据控制语言
			grant  revoke
	事务控制语言:
			commit  rollback  savepoint
	
事务特征ACID:
		1.原子性:
				同时成功同时失败  
		2.一致性:
				事务执行的结果必须使数据库从一个一致性状态编程另一个一致性状态
		3. 隔离性:
				事务操作应该相互独立
		4. 持久性:
				事务所作的影响是持久性的
1. insert 格式
		1. insert into 表名(列名1,列名2,....)
		values(值1,值2,....)
		
		2. insert into 表名(列名1,列名2,....)
			    sql语句

2. update  
		格式:update 表名 set 列名1=值1,列名2=值2 [where条件表达式]; 		

3. delete
		格式:delete from 表名 [where 条件表达式];  

4. truncate
		格式:truncate table 表名
			  不可回滚
			  delete from 表名
			  可回滚
	
5. alter 修改
	 	1. 修改表:
			 	alter table 表名;
			 	添加列
			 	alter table 表名 add 列名 数据类型 列级约束;
			 	删除列
			 	alter table 表名 drop cloumn 列名;
			 	修改列
			 	alter table 表名 modify (列名 数据类型 列级约束)
			 	添加约束:添加的是表级约束
			 	alter table 表名 add constraint 约束名 约束;
			 	删除约束:
			 	alter table 表名 drop constraint 约束名 约束;
		2. 

6. 

事务控制语句:
1. commit  提交
2. rollback  [to 保存点名字]   回滚  
3. savepoint 保存点
		在语句中设置保存点,使用rollback to 保存点名称  回退到保存点
1. 序列 sequence
1. 作用:为主键提供值,非空唯一
		eg: stuId 主键 学号
		
2. 语法:create sequence 序列名;
3. 获取值:序列名.nextval    获取下一个值
		   序列名.currval    获取当前值
4. 删除序列:drop sequence 序列名;
5. 查看当前用户所有序列:select sequnce_NAME from user_sequences
2. 视图 view
1.  语法: create or replace view 视图名 select语句;
		 eg: create or replace view my_view as select id,last_name from s_emp;
3. 索引 index
自动创建索引:主键,unique,
用户自定义索引:
创建索引:
	create index 索引名 on 表名(列名);
删除索引:
	drop index 索引名;
产看当前用户所有索引:
	select index_name from user_indexs;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值