以前写过的一些oracle语句
这下以后用起来就方便了。可算是找到了
Orcl 数据库服务 启动项:
OracleDBConsoleorcl
OracleOraDb10g_home1iSQL*Plus
OracleOraDb10g_home1TNSListener
OracleServiceORCL
都被我配置了延迟启动。。。这样好歹 能让我先做点儿 什么。
2016.2.2今天配置成了手动启动。
GOODS_ID GOODS_NAME GOODS_PRICE GOODS_CA GOODS_PROVIDER
-------- ---------- ----------- -------- ---------------------
1 shangpin1 50 weijin 33
插入语句。
SQL> insert into goods_tb values(2,'weijin',35,'yongpin',33);
-- 修改表结构。
SQL> alter table goods_tb modify(goods_name varchar2(10));
创建表:
Create table test_tb(
Test_id number(2),
Test_name varchar(20)
);
Select * from emp where sal>all(select sal from emp where deptno=20);
添加约束:
主键约束:
alter table employee_tb add constraint pk_employee_tb_id primary key (employee_id);
外键约束:
alter table employee_tb add constraint fk_employee_tb_id foreign key (department_id) references department_tb(department_id);
复合主键:
create table depart_pos(
department_id int not null,
position_id int not null,
primary key (department_id,position_id) #设定复和主键
);
数据库各项异常:
Ora-02290 违反检查约束条件
Ora-00291 未找到父项关键字
Ora-00298 父项关键字 不匹配
Ora-00904 标识符无效
ora-00911 无效字符 【插入语句当中本来是用英文逗号分割的,而我的用的是中文的。】
大部分情况下,此错误是由于引用了不存在的列名导致的。比如select name from Studtent 当studeng表中无name列时,系统就会报此错误。
-- 数据库 命名的规范问题 tb 下划线隔开 不能使用数字开头
-- 数据库也有数据类型 varchar2
-- 创建表格
-- 表格中的字段应该选择什么样 数据类型
-- 主键id 都选择 number int
-- char 数据的长度不会经常变化的时候 varchar2 变化的时候
create table emp_tb(
emp_id number,
emp_name varchar2(20),
emp_sex char(4),
emp_age number,
emp_sal number(9,2),
emp_time date
)
create table dept_tb(
dept_id number,
dept_name varchar2(20)
)
-- 删除表格
select * from stu_tb3;
drop table stu_tb3;
-- 修改表格
-- 表结构的修改
-- 添加一个字段
alter table emp_tb add dept_id number;
select * from emp_tb;
-- 修改字段 类型
-- 你要是 改字段类型 或者 是长度的时候 一定要谨慎
alter table emp_tb modify emp_age varchar2(10)
alter table emp_tb modify emp_age int
-- 删除字段
alter table emp_tb drop column emp_age;
-- 表数据的操作
select * from emp_tb;
insert into emp_tb (emp_id,emp_name,emp_time) values(3,'zhangsan','1-1月-2016');
update emp_tb set emp_name = 'wangwu' where emp_id = 2;
delete from emp_tb where emp_id = 2;
commit;
select * from emp_tb;
-- 在当前的事物上 这只一个保存点
savepoint a;
delete from emp_tb where emp_id = 2;
savepoint b;
delete from emp_tb;
select * from emp_tb;
commit;
rollback to a;
-- 表约束的操作
-- 约束
-- 目的: 保证我们数据表数据的完成性
-- 每个字段的数据类型和长度 也都一种约束
-- not null unique check primary key foregin key
-- 没有任何约束 不行的 至少 都得有一个 主键
-- 在创建表的同时 在字段后面 直接添加
create table stu_tb1(
stu_id number not null,
stu_name varchar2(20) unique
)
-- primary key 自身有三个内容 非空 唯一 索引
create table stu_tb2(
stu_id number primary key,
stu_name varchar2(20) unique
)
-- check
create table stu_tb3(
stu_id number primary key ,
stu_name varchar2(20),
stu_sex char(4) default '男',
stu_age number check (stu_age between 18 and 60)
)
drop table stu_tb3;
select * from stu_tb1;
-- 在创建表的同时 在所有字段后面 添加约束
create table stu_tb4(
stu_id number,
stu_name varchar(2),
constraint pk_stu_tb4_id primary key(stu_id),
constraint un_stu_tb4_name unique (stu_name)
)
-- 在创建表格以后 再去添加约束
select * from emp_tb;
alter table emp_tb modify emp_name varchar(20) not null
alter table emp_tb add
constraint pk_emp_tb_id primary key (emp_id)
--
alter table dept_tb add
constraint pk_dept_tb_id primary key (dept_id)
-- 外键约束
alter table emp_tb add
constraint fk_emp_tb_dept_id foreign key (dept_id)
references dept_tb(dept_id)
--数据查询
-- 基本查询
-- 查询所有字段
select * from emp;
-- 查询指定的字段
select empno,ename,job from emp;
select distinct job,empno from emp;
-- 给查询的字段 取别名
select empno as "编号",ename as empname from emp;
-- 查询每个员工 年薪
select empno,ename,sal*12 as "年薪" from emp;
-- mysql ifnull()
select empno,ename,(sal+nvl(comm,0))*12 as "年薪" from emp;
select * from emp
-- 条件查询
-- 如果查询工资高于3000 的员工的信息;
select empno,ename,job,sal from emp where sal > 3000;
select * from emp where hiredate > '1982/1/1';
select * from emp where sal between 2000 and 3000;
select * from emp where sal >=2000 and sal<=3000;
select * from emp where ename like '%A%'
select * from emp where ename like '__A%'
-- 复习
select * from emp
-- 排序查询
-- 聚合函数查询
-- 分组查询
-- 复习
select * from emp;
select deptno,job,sal from emp where ename='SMITH';
select (sal+nvl(comm,0))*12 from emp;
---
select * from emp where empno not in(7839,7844,123,456)
select * from emp where mgr is not null;
--查询工资高于500或者是岗位为MANAGER的雇员,
--同时还要满足他们的姓名首字母为大写的J?
select * from emp
where (sal>500 or job = 'MANAGER') and ename like 'J%'
select * from emp where 1=1 and order by sal;
-- from where select order by
select * from emp order by deptno ,sal desc ;
-- 按照年薪排序
select (sal+nvl(comm,0))*12 as "年薪" ,ename from emp
order by (sal+nvl(comm,0))*12 desc;
--分组函数
select max(sal),min(sal),avg(sal),sum(sal),count(*) from emp;
select count(empno) from emp;
-- 分组查询
-- 如果你要进行 分组查询 select 中 就只能 显示 分组字段 或者 分组函数
select deptno,max(sal),avg(sal) from emp where 1=1
group by deptno order by avg(sal);
-- from where group by having select order by
select deptno,max(sal),avg(sal) from emp where 1=1
group by deptno having avg(sal)>3000 order by avg(sal);
-- 最大工资的那个人叫啥
-- 条件子查询
select ename,sal from emp where sal > (select avg(sal) from emp);
-- 笛卡尔积现象
select * from emp;
select * from dept;
select * from emp e,dept d;
-- 关联查询
-- 内连接查询
select * from emp e,dept d where e.deptno = d.deptno;
select * from emp e inner join dept d on e.deptno = d.deptno
-- 外连接查询
-- 左外联
select * from emp e left join dept d on e.deptno = d.deptno;
--select * from emp e,dept d where e.deptno =* d.deptno;
-- 右外联
select * from emp e right join dept d on e.deptno = d.deptno;
-- 全外联
select * from emp e full join dept d on e.deptno = d.deptno;
-- 自连接查询
select e.empno,e.ename,e2.empno,e2.ename
from emp e inner join emp e2 on e.mgr = e2.empno
-- 工资的级别
select * from salgrade;
select e.ename,e.sal,s.grade from emp e,salgrade s
where e.sal between s.losal and s.hisal;
-- 子查询
-- 条件子查询
-- 单列子查询
-- 单列单行
select * from emp where sal = (select max(sal) from emp);
select * from emp where deptno =
(select deptno from emp where ename ='SMITH');
-- 单列 多行
select * from emp where job in(
select distinct job from emp where deptno = 20);
--- 多列子查询
select * from emp where (job,deptno) = (
select job,deptno from emp where ename = 'SMITH');
select * from emp where sal>(
select max(sal) from emp where deptno = 30);
select * from emp where sal > all(
select sal from emp where deptno = 30);
-- from 子句子查询
select * from (select empno,ename,sal from emp)
a where a.sal = 100;
-- 分页查询
select * from (
select e.*, rownum rn from emp e
order by empno desc) a where a.rn<11 and a.rn>5;
select * from (
select e.*, rownum rn from emp e where rownum<=10
order by empno desc) a where a.rn>5;
-- 索引 提高查询的效率
-- 主键来讲 默认就有索引
-- 单列索引
create index emp_tb_index_emp_name on emp_tb(emp_name)
-- 多列索引
create index emp_tb_index_emp_name2 on emp_tb(emp_name,emp_sex)
-- 索引是在数据库表使用的后期,用来对数据库查询的一个优化
-- 大表:字段比较多, 数据比较
-- 经常会被用在where 条件, 外键字段,
select * from emp_tb;
select index_name,table_name from user_indexes
where table_name='EMP_TB';
select * from user_ind_columns where index_name='PK_EMP_TB_ID'
-- 删除索引
drop index emp_tb_index_emp_name2
drop index pk_emp_tb_id
drop primary key emp_tb;
-- 视图
-- 减少程序员所写代码
-- 起到对象权限的分配作用
create view emp_age_view as
select a.empno,a.ename,a.sal from (
select e.*, rownum rn from emp e where rownum<=10
order by empno desc) a where a.rn>5;
select * from emp_age_view;
--
drop view emp_age_view;
-- 序列创建
create sequence emp_sequence
increment by 1----每次增加几个
minvalue 1----最小值为1
nomaxvalue----不限制最大值
start with 1----从1开始
cache 10----缓存
order;
--
select emp_sequence.nextval from dual;
select emp_sequence.currval from dual;
select * from dual;
select * from dept_tb;
insert into dept_tb values(emp_sequence.nextval,'教学部');
-- 触发器
-- 前置触发 还有后置 触发
-- insert update delete
-- 行级触发 列级触发
create or replace trigger dept_tb_insert
before insert on dept_tb
for each row
begin
select emp_sequence.nextval into:New.dept_id from dual;
end;
select * from emp_tb;
insert into emp_tb (emp_name,emp_sex,emp_sal)
values('刘能','男',5000)
insert into dept_tb (dept_name) values('教学部');
select * from dept_tb;
以前写过的一些oracle语句的更多相关文章
- 53个Oracle语句优化规则详解(转)
Oracle sql 性能优化调整 1. 选用适合的ORACLE优化器 ORACLE的优化器共有3种:a. RULE (基于规则) b. COST (基于成本) c. CHOOSE ...
- Oracle语句中IN和=的区别有哪些?
Oracle语句中IN和=的区别有: 1.首先应用范围不一样:in 可以理解为是范围内的选择:= 只有一个.例如: select sno, sname from t1 where sno in ('s ...
- [转载]T-SQL(Oracle)语句查询执行顺序
原文链接:http://blog.sina.com.cn/s/blog_61c006ea0100mlgq.html sql语法的分析是从右到左,where子句中的条件书写顺序,基本上对sql性能没有影 ...
- 性能测试常用Oracle语句
性能测试常用Oracle语句 显示数据库当前的连接数 select count(*) from v$process; 显示数据库最大连接数: select value from v$parameter ...
- oracle语句随笔
oracle语句随笔 dmp数据的导入. ; --创建用户 GRANT CONNECT,RESOURCE,DBA TO memsspc; --赋值权限 --cmd 中导入命令 IMP memsspc@ ...
- Oracle语句优化1
Oracle语句优化1 优化就是选择最有效的方法来执行SQL语句.Oracle优化器选择它认为最有效的 方法来执行SQL语句. 1. IS NULL和IS NOT ...
- SQL点滴10—使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比
原文:SQL点滴10-使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比 今天偶尔看到sql中也有with关键字,好歹也写了几年的sql语句,居然第一次接触,无知啊.看了一位博主的文章 ...
- oracle语句insert into select如何加后续插入条件
oracle语句insert into select如何加后续插入条件 2014-01-21 10:48匿名 分类:其他编程语言 | 浏览 2746 次 oracle中有批量插入语句insert i ...
- Oracle 语句中“||”代表什么啊?
Oracle 语句中“||”代表什么啊? Oracle 语句中“||”代表什么啊?跟ServerSQL中的字符串的连接符“+”是一个概念么? 1. 恩是的 是一个含义...select '1'||'2 ...
随机推荐
- java的math常用方法
鉴于java求整时欲生欲死,整理常用math如下: 1: java取整 a:floor向下取整 用法:Math.floor(num) Math.floor(1.9)//1 ...
- 现代程序设计 homework-07
现代程序设计 homework-07 这次作业是要阅读C++11的新特性,按照老师blog提供的链接稍微学习了一下,一下就是一些学习总结(或者说就是介绍)之类的:由于英文能力有限,并且很多中文资料也都 ...
- Java缓存学习之一:缓存
一.缓存 1.什么是缓存? 缓存是硬件,是CPU中的组件,CPU存取数据的速度非常的快,一秒钟能够存取.处理十亿条指令和数据(术语:CPU主频1G),而内存就慢很多,快的内存能够达到几十兆就不错了,可 ...
- build.gradle 使用tips
7.查看依赖 gradlew [你想查看的module]:dependencies >dependencies.txt 6.buildToolsVersion build tools版本号 co ...
- Ubuntu 下安装 Oracle Java
这只是一篇流水帐,记录如何安装Java. 在Ubuntu 下管理软件很方便,但安装的Java是opensdk.如果在某些条件下,需要安装Sun (Oracle)的Java,则需要自己手工安装. 一般情 ...
- Java设计模式系列之适配器模式
适配器模式的定义 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.(就类似于我们充电器的转接头将220V的电压转换成我们的手机端 ...
- DllImport属性详解
API函数是构筑Windows的基石, 是Windows编程的必备利器.每一种Windows应用程序开发工具都提供间接或者直接的方式调用Win32API,C#也不例外.使用Win32API的一个好处就 ...
- Java学习笔记(三):数组
数组声明 java语言中,数组是一种最简单的复合数据类型.数组是有序数据的集合,数组中的每个元素具有相同的数据类型,可以用一个统一的数组名和下标来唯一地确定数组中的元素. int arr1[]; in ...
- PHP实现基于Swoole简单的HTTP服务器
引用Swoole官方定义: PHP语言的异步.并行.高性能网络通信框架,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTa ...
- 让人眼花缭乱的 RSS 版本0.90、0.91、0.92、0.93、0.94、1.0 和 2.0
1.0的规范 http://web.resource.org/rss/1.0/spec 2.0的规范 http://cyber.law.harvard.edu/rss/rss.html 一个介绍什么是 ...