以前写过的一些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 ...
随机推荐
- [C语言 - 9] typedef
关键字typedef typedef 操作符可以看做是普通变量和类型之间的转换开关!! 例如 typedef int Integer;//定义了一种类型 int Integer;//定义了一个变量 ...
- ucGUI例程收藏
ucGUI 几个重要例程Demo 按钮的定制 #include <stddef.h> #include <string.h> #include "WM.h&quo ...
- 经典代码-C宏 #转字符串【瓦特芯 笔记】
在调试C语言程序时,有时需要打印宏的名字.可以通过定义宏,宏名字的数组来获得. 例如: #include <stdio.h> #define MACRO_STR(x) {x, #x} ty ...
- Linux下移植pjsip,使用QT开发
1.移植pjsip env:fedora14 arm-linuc-gcc:gcc version 4.5.1 (ctng-1.8.1-FA) #./configure \ CC=arm-linux-g ...
- PC 端微信扫码注册和登录
一.前言 先声明一下,本文所注重点为实现思路,代码及数据库设计主要为了展现思路,如果对代码效率有着苛刻要求的项目切勿照搬. 相信做过微信开发的人授权这块都没少做过,但是一般来说我们更多的是为移动端的网 ...
- matlab中读取txt数据文件(转)
根据txt文档不同种类介绍不同的读取数据方法 一.纯数据文件(没有字母和中文,纯数字) 对于这种txt文档,从matalb中读取就简单多了 例如test.txt文件,内容为“17.901 -1.111 ...
- C#学习笔记(十四):GC机制和弱引用
垃圾回收(GC) 垃圾回收即Garbage Collector,垃圾指的是内存中已经不会再使用的对象,通过收集释放掉这些对象占用的内存. GC以应用程序的root为基础,遍历应用程序在Heap上动态分 ...
- hdu 5565 Clarke and baton 二分
Clarke and baton Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
- lightOJ 1030(期望)
题意:有一个迷宫是1×n的格子,一个人每到一个格子就能够把这个格子内的金子所有拿走,刚開始站在第1个格子,然后開始掷骰子得到点数x,他就要从当前位置走到加x的位置.假设发现位置是大于n的就又一次掷骰子 ...
- MVC风格
MVC风格 点击了解很多其它软件体系结构风格 §模型-视图-控制器风格常被简称为MVC风格 §组件:模型.视图.控制器 §连接件:显式调用.隐式调用.其它机制(比如:Http协议) 工作机制: Mod ...