您的位置:首页 > 数据库 > Oracle

以前写过的一些oracle语句

2016-06-29 16:35 127 查看
这下以后用起来就方便了。可算是找到了

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: