-----给表一创建增长序列,
create sequence user_Id
minvalue 1
maxvalue 1000000000
start with 1
increment by 1
cache 20
order;
-----删除自增列
drop sequence user_Id;
-------------------------------------对新建的目的表判断,若存在即将删除重新建立新表-------------------------
------------若要删除已存在的表 在登录用户时必须是 超级用户权限登录操作
--注:
--1,若登录时选择 "链接为”Normal 后,在创建表时若要对系统已存在的表名使用一下方法进行删除,在执行查询时会发现该表还存在
--2,若登录时选择 "链接为”sysdba & system 超级权限 在删除 系统已存在的表名时,在执行查询时会发现该表已经彻底删除;且不存在!
--3. 尚若要使用 普通权限 操作 删除数据库时,方法: drop table table_name ;
-----创建表一
declare num number;
begin
select count(1) into num from all_tables where TABLE_NAME ='T_1';
if num=1 then execute immediate 'drop table T_1';
end if;
end;
------创建表二
declare num number;
begin select count(1) into num from all_tables where table_name='T_2';
if num=1 then execute immediate 'drop table T_2';
end if;
end;
-----创建表
create table T_1
(
user_Id int primary key,-------职员表示号
user_IdCard varchar2(40) unique check(length(user_IdCard )>0 and length(user_IdCard)<=18) not null,------职员身份证号
user_name varchar2(50) not null,-------职员年龄
user_sex varchar2(10)default('男') check (user_sex='男' or user_sex='女')not null,-------职员性别
user_brithy date not null,-----职员出生年月
user_age number not null,-------职员年龄
user_brithyAddress varchar2(60) not null,-------职员出生地址
user_NowAddress varchar2(60) not null,-------职员现住址
user_Edu varchar2(10) default('大专') not null,--------职员学历
user_temp varchar2(60)----备注
);
------------------------------------
-----表二自增长序列
create sequence Dep_Id
start with 1
increment by 1
nocycle ;
----删除已存在的自增序列
drop sequence Dep_Id
-------------
create table T_2
(
Dep_Id int not null,---部门标识号
user_Id int primary key references T_1 ,---职员表示号
user_name varchar2(50) not null,---职员名称
user_sex varchar2(10) default('男') not null,---职员性别
user_brithy date not null,----职员出生年月
user_InJobDate date not null,----时间入职
user_DepPart varchar2(50),----部门
user_post varchar2(50),----职位
user_monthlyPay decimal(9,2) check( user_monthlyPay between 1500.00 and 80000.00) not null,----月薪
user_temp varchar2(60)----备注
);
---------------------------增加数据记录------------------
----向表T_1中插入数据记录
insert into T_1 values(user_Id.Nextval,'421123198709100000','sa','男',sysdate,'22','湖北孝感','武汉',default,'数据测试');
insert into T_1 values(user_Id.Nextval,'421123198709100001','zj','男',TO_DATE('19870910','YYYY-MM-DD'),'22','湖北黄冈','武汉','本科','数据测试');
insert into T_1 values(user_Id.Nextval,'421123198709100002','admin',default,sysdate,'22','湖北武汉','武汉','本科','数据测试');
insert into T_1 values(user_Id.Nextval,'421123198709100003','guest','女',sysdate,'22','湖南','长沙','本科','数据测试');
insert into T_1 values(user_Id.Nextval,'421123198709100004','login','男',sysdate,'22','湖北','黄冈','本科','数据测试');
----向表T_2中插入数据记录
insert into T_2 values ('1','3','admin',default,TO_DATE('19870910','YYYY-MM-DD'),TO_DATE('20010821','YYYY-MM-DD'),'研发部','技术专家',38000.00,'数据测试');
insert into T_2 values ('1','2','zj',default,TO_DATE('19870910','YYYY-MM-DD'),TO_DATE('20101123','YYYY-MM-DD'),'研发部','技术专家',38000.00,'数据测试');
insert into T_2 values ('1','1','sa',default,TO_DATE('19870910','YYYY-MM-DD'),TO_DATE('20010821','YYYY-MM-DD'),'研发部','技术专家',38000.00,'数据测试');
insert into T_2 values ('2','4','guest',default,TO_DATE('19870910','YYYY-MM-DD'),TO_DATE('20101123','YYYY-MM-DD'),'质量管理','技术顾问',48010.10,'数据测试');
insert into T_2 values ('3','5','login',default,TO_DATE('19870910','YYYY-MM-DD'),TO_DATE('20101126','YYYY-MM-DD'),'测试部','测试技术员',2500.10,'数据测试');
-----下面两行数据是在 存储过程中向表 T_1 中增加的数据记录
insert into T_2 values ('1','6','QA',default,TO_DATE('19870910','YYYY-MM-DD'),sysdate,'研发部','技术专家',38000.00,'数据测试');
insert into T_2 values ('1','7','存储',default,TO_DATE('19870910','YYYY-MM-DD'),sysdate,'研发部','技术专家',38000.00,'数据测试');
-----------------------增加数据表的新字段列---------------------
--1.利用代码增加已有数据表的新字段
alter table T_1 add Email varchar2(20) default('[email protected]') check( Email like'%@%.%') ---向表中插入新列
--2.向表中增加各种约束
alter table T_1 add Tel varchar2(50)default(027-8888888)---向表中插入列和约束
------------若要删除新增列 在登录用户时必须是 普通用户权限登录操作
--注:
--1,若登录时选择 "链接为”sysdba & system 超级权限 在删除 新增的列时候会报 SYS 权限错误提示!
--2,登录时选择 "链接为”Normal 普通操作权限 删除 新增的列执行成功;
alter table T_1 drop column Email
-------------------------查询数据记录----------------------------
----查询数据表内的数据记录
select * from T_1;
----查询数据表内的数据记录
select * from T_2;
--------------等值查询--------
select * from T_1 where user_id=2;
---查询表二中 user_id 与表一中的 user_id 共有指定部门
select * from T_2 where user_id in(select user_id from T_1 where Dep_Id=1)
--查询表一中 user_id 与表二中的 user_id 共有指定部门
select * from T_1 where user_id in(select user_id from T_2 where Dep_Id=1)
--------------Like 模糊匹配操作符------
--(1, % :表示任意0 到多个字符;
--(2, _ : 表示任意单个字符。
---显示首字符为S 的员工的数据记录
select * from T_2 where user_name like 's%'
---显示数据记录中带有符为S 的员工信息
select * from T_2 where user_name like '%s%'
---显示数据记录第三个字母为 "m“的职工信息
select * from T_2 where user_name like '__m%'
---如何查找1982.1.1 后入职的员工
Select * from T_2 where user_InJobDate='2010-11-26';
---------不匹配查询-------
--1,显示除部门 1 一外的记录信息
select *from T_2 where Dep_Id !=1;
select *from T_2 where Dep_Id <>1;
--显示部门编号不等于1 所在 T_2表内的职工信息
select * from T_2 where user_id in(select user_id from T_1 where Dep_Id !=1)
--- or 运算符查询
select *from T_2 where Dep_Id =1 or Dep_Id =2
--- and 运算符查询
--- between......and 区间查询
--查询表 T_1 字段 部门 2 与 3 等于或包含在之间的数据记录消息
select *from T_2 where Dep_Id between 2 and 3
-----------------------修改数据记录---------------------
---修改字段的长度
alter table T_1 modify (Tel varchar2(30));
----修改表的名字
rename t to T_1 ;--将已存在的表名’t‘修改为’T_1’
---修改指定匹配范围的列
update T_2 set user_InJobDate=sysdate where user_Id=2
update T_2 set user_name='测试' where user_Id=2
----------------------排序数据记录-------------------
------增量排序
select * from T_2 order by user_Id asc
------递减排序
select * from T_2 order by user_Id desc
------查询时给表标识列取别名
select Dep_Id as 部门编号, user_DepPart as 部门名称 , user_name as 姓名 , user_sex as 年龄,user_brithy as 生日,user_post as 职位 ,user_monthlyPay as 月薪,user_temp as 备注 from T_2
select Dep_Id 部门编号, user_DepPart 部门名称 , user_name 姓名 , user_sex 年龄,user_brithy 生日,user_post 职位 ,user_monthlyPay 月薪,user_temp 备注 from T_2
-----查询表的部分数据
select Dep_Id , user_DepPart, user_name , user_sex ,user_brithy, user_post ,user_monthlyPay , user_temp from T_2
---列出受雇员工入职较晚的所有员工
select * from T_2 e where user_InJobDate>(select user_InJobDate from T_2 where user_Id=e.dep_id);
-------------------------------------------基本连接的应用
-------------------------------------------超连接查询的综合应用
---right / left / inner / full (join) …on 综合连接查询只需更改表与表之间的连接关键字即可
--使用方法为:select A.*,B.*,C.*….* from table_1 A left join table_2 on A.n_ID=B.n_ID right join table C on B.no=C.no and/where list=&>&<值 &between a and b
---------------使用right join 左连接查询
select B.Dep_Id ,B. user_DepPart,A.user_id,A.user_name,A.user_IdCard ,A.user_sex,A. user_age ,A.user_Edu ,B.user_InJobDate ,B.user_monthlyPay ,B. user_post
from T_1 A right join T_2 B on A.user_Id = B.user_Id ;
---right join接匹配条件查询
select B.Dep_Id ,B. user_DepPart,A.user_id,A.user_name,A.user_IdCard ,A.user_sex,A. user_age ,A.user_Edu ,B.user_InJobDate ,B.user_monthlyPay ,B. user_post
from T_1 A right join T_2 B on A.user_Id = B.user_Id where Dep_Id=1;
-------------使用 left join 右左连接查询
select B.Dep_Id ,B. user_DepPart,A.user_id,A.user_name,A.user_IdCard ,A.user_sex,A. user_age ,A.user_Edu ,B.user_InJobDate ,B.user_monthlyPay ,B. user_post
from T_1 A left join T_2 B on A.user_Id = B.user_Id ;
---left join 接匹配条件查询
select B.Dep_Id ,B. user_DepPart,A.user_id,A.user_name,A.user_IdCard ,A.user_sex,A. user_age ,A.user_Edu ,B.user_InJobDate ,B.user_monthlyPay ,B. user_post
from T_1 A left join T_2 B on A.user_Id = B.user_Id where Dep_Id=1;
-----------使用inner join 内连接查询
select B.Dep_Id ,B. user_DepPart,A.user_id,A.user_name,A.user_IdCard ,A.user_sex,A. user_age ,A.user_Edu ,B.user_InJobDate ,B.user_monthlyPay ,B. user_post
from T_1 A inner join T_2 B on A.user_Id = B.user_Id;
---inner join 匹配条件查询
select B.Dep_Id ,B. user_DepPart,A.user_id,A.user_name,A.user_IdCard ,A.user_sex,A. user_age ,A.user_Edu ,B.user_InJobDate ,B.user_monthlyPay ,B. user_post
from T_1 A left join T_2 B on A.user_Id = B.user_Id where Dep_Id=2;
-----------使用full join 全连接查询
select B.Dep_Id ,B. user_DepPart,A.user_id,A.user_name,A.user_IdCard ,A.user_sex,A. user_age ,A.user_Edu ,B.user_InJobDate ,B.user_monthlyPay ,B. user_post
from T_1 A full join T_2 B on A.user_Id = B.user_Id;
--full join 匹配条件查询
select B.Dep_Id ,B. user_DepPart,A.user_id,A.user_name,A.user_IdCard ,A.user_sex,A. user_age ,A.user_Edu ,B.user_InJobDate ,B.user_monthlyPay ,B. user_post
from T_1 A full join T_2 B on A.user_Id = B.user_Id where Dep_Id=3 ;
----------创建存储过程 1--------------
create or replace procedure P_1
(
p_id int ,
p_IdCard varchar2(40),
p_name varchar2(50),
user_sex varchar2(10),----性别
p_brithy date ,--出生年月
p_age number ,----年龄
p_brithyAddress varchar2(60) ,----出生地址
p_NowAddress varchar2(60) ,----现住址
p_Edu varchar2(10) ,-----学历
p_temp varchar2(60)
)
as---此处可以用 is
begin
insert into T_1 values(p_id int ,p_IdCard varchar2(40),p_name varchar2(50), user_sex varchar2(10),
p_brithy date ,p_age number ,p_brithyAddress varchar2(60) ,p_NowAddress varchar2(60) ,p_Edu varchar2(10) ,p_temp varchar2(60)
)
commit;
end;
------向存储过程中插入数据记录-------------
begin
insert into T_1 values(user_Id.Nextval,'421123198709100006','存储',default,TO_DATE('19870910','YYYY-MM-DD'),'22','湖北','WH',default,'数据测试');
commit;
end ;
----
begin
insert into T_1 values(user_Id.Nextval,'421123198709100007','QA','男',TO_DATE('19870910','YYYY-MM-DD'),'22','湖北','WH','大专','数据测试');
commit;
end ;
-------存储过程查询
select *from T_2;
select *from T_1;
select user_Id 序号,user_name 职工名 ,cast(user_monthlyPay as decimal(10,2))as 工资转换为货币类型 from T_2 order by user_Id desc
--------------------------------------------------数据分组 count,sum,max,min,avg,------------------------------------------------------
----------------------count 聚合函数的应用------
--统计表 T_2 中部门人数
select count(*)as 部门人数 from T_2;
-----统计表 T_1 中的总人数
select count(*)as 人数统计 from T_1;
--统计表一内的男员工人数
select count(*)as 男员工人数 from T_1 where user_sex='男'
-----------------------Sum(求和) 聚合函数的应用---
--统计表 T_2 中的
select sum(user_monthlyPay)as 月薪支出 from T_2;
----统计指定部门月薪
select sum(user_monthlyPay)as 月薪支出 from T_2 where Dep_Id=1;
--统计指定地区的员工,工资总和
select sum(user_monthlyPay)as 湖北区职工工资和 from T_2 where user_Id in (select user_Id from T_1 where user_NowAddress ='武汉')
--显示指定部门的人数和 工资开支
select count(*) as 部门人数, sum(user_monthlyPay)as 月薪总计 from T_2
-----------------------Max(最大值) 函数的应用
--查找月薪最高的职工部分消息记录
select user_Id as 员工序号,user_name as 员工姓名,user_sex as 性别 , user_monthlyPay as 月薪 from T_2 where user_monthlyPay in(select max(user_monthlyPay)from T_2)
--查找月薪最高的职工全部消息记录
select * from T_2 where user_monthlyPay=(select max(user_monthlyPay)from T_2)
--查找最近入职的职工消息记录
select * from T_2 where user_InJobDate=(select max(user_InJobDate )from T_2)
-----------------------Min(最小值) 函数的应用
--查找最先入职的职工消息记录
select * from T_2 where user_InJobDate=(select min(user_InJobDate )from T_2)
--查找月薪最低的职工消息记录
select * from T_2 where user_monthlyPay=(select min(user_monthlyPay)from T_2)
--显示早期和最近的入职时间段
select min(user_InJobDate )最早入职时间, max(user_InJobDate )最近入职时间 from T_2
-----------------------Avg(平均值) 函数的应用
--显示部门工资的平均值
select avg(user_monthlyPay)from T_2
--显示部门工资小于平均值的职工数据记录
select * from T_2 where user_monthlyPay<(select avg(user_monthlyPay)from T_2)
--显示 T_2 表中 最高,最低,平均 月薪
select max(user_monthlyPay)最高月薪, min(user_monthlyPay)最低月薪, avg(user_monthlyPay) as 平均月薪 from T_2
--月薪总和 除以 人员总数 求得 月薪平均值
select sum(user_monthlyPay)/count(user_Id) as 平均月薪 from T_2
----------------------group by 分组函数的应用
--按照部门编号,部门名称 进行人数分组汇总,并且进行降序显示数据记录
select Dep_id 部门编号, user_DepPart 部门,count( Dep_id)部门人数 from T_2 group by Dep_id, user_DepPart order by Dep_id asc
---------------------消除重复列值----------
--去掉重复的部门编号,并进行统计无重复部门编号总计
select count(distinct(Dep_Id))as 部门总计 from T_2;
--查看重复的部门编号记录
select *from T_2 where dep_id in(select distinct(dep_id) from T_2 where dep_id =1);
--查看无重复的部门编号记录
select *from T_2 where dep_id not in(select distinct(dep_id) from T_2 where dep_id =1);
-------------给工资格式化为货币格式
select user_Id 职工代号,user_name,'RM:'||user_monthlyPay||'¥' as 工资转换为货币类型 from T_2 order by user_Id asc
select user_name 职工名,'RM:'||cast(user_monthlyPay as decimal(10,5))||'¥' as 工资发放实额 from T_2
----该方法是原始表列名查询方法
select user_name ,'RM:'||user_monthlyPay||'¥' from T_2
--执行结果(第二列表头出现不美观性代码 如:'RM:'||USER_MONTHLYPAY||'¥')
---为了解决以上问题,建议给表列头取别名
--------------表列字段连接
--“||”字符串连接符
Select user_name||' '||'月薪'||'RM:'|| user_monthlyPay ||'¥' from T_2--将查询出的值和字符串连接在起一
----------------显示职工的姓名,工资,代扣税,奖金,应发工资信息
--1.职工姓名性别工资都是职工表中存在的字段,而代扣税,奖金,应发工资都是虚拟字段。代扣税计算公式:代扣税=工资*5.0/1000,奖金公式是:奖金=工资*8%,应发工资公式是:应发工资=工资-代扣税+奖金,
select user_Id as 员工序号,user_name as 员工姓名,user_sex as 性别 ,user_monthlyPay as 月薪,user_monthlyPay*5.0/1000 as 代扣税,user_monthlyPay*0.08 as 奖金,user_monthlyPay-(user_monthlyPay*5.0/1000)+(user_monthlyPay*0.08)as 应发工资 from T_2
order by user_Id asc ----desc(降序)-----按照员工序号进行增序排序
--------2.应发工资小数点后零太多,可以利用cast函数重新设置其精确度
select user_Id as 员工序号,user_name as 员工姓名,user_sex as 性别 ,user_monthlyPay as 月薪,cast(user_monthlyPay*5.0/1000as decimal(10,0))as 代扣税 ,cast(user_monthlyPay*0.08as decimal(10,0))as 奖金,
cast(user_monthlyPay-(user_monthlyPay*5.0/1000)+(user_monthlyPay*0.08)as decimal(10,0))as 应发工资 from T_2
--------------------------------------------------------------------------------
select * from scott.emp
-------查看当前星期几
select rtrim(to_char(sysdate,'DAY')) from dual;
-------------------------------------------------虚拟字段创建-------------------------------------
------------若要删除已存在的表 在登录用户时必须是 超级用户权限登录操作
--注:
--1,若登录时选择 "链接为”Normal 后,在创建表时若要对系统已存在的表名使用一下方法进行删除,在执行查询时会发现该表还存在
--2,若登录时选择 "链接为”sysdba & system 超级权限 在删除 系统已存在的表名时,在执行查询时会发现该表已经彻底删除;且不存在!
--3. 尚若要使用 普通权限 操作 删除数据库时,方法: drop table table_name ;
--------使用 sysdba & system 超级权限 删除已存在的表名方法
declare num number;
begin
select count(1) into num from all_tables where TABLE_NAME ='Test ';
if num=1 then execute immediate 'drop table Test ';
end if;
end;
-------使用 Normal 普通权限 删除已存在的表名方法
drop table Test
------建表--------
create table Test ----虚拟字段创建测试表
(
article__id int primary key not null,----物品标示号
article_name varchar2(50) not null, ---物品名称
article_price number, ---物品价格
article_remark varchar2(20) /* 虚拟列名及数据类型*/-----物品价格评语
---------
generated always as
(
case
when article_price >1500 then '昂贵'
when article_price <=1500 and article_price >1000 then '中等'
when article_price <=1000 and article_price >=500 then '一般'
else '基本'
end
) virtual /*虚拟列值函数内容*/
----------
);
insert into Test(article__id ,article_name ,article_price )values('1000','手机','2000');
insert into Test(article__id ,article_name ,article_price )values('1001','MP4','1200');
insert into Test(article__id ,article_name ,article_price )values('1002','移动硬盘','800');
insert into Test(article__id ,article_name ,article_price )values('1003','U盘','200');
--普通查询
select * from test
---查询优化
select article__id 用户编号,article_name as 物品名,'RM:'||article_price||'¥' as 现价, article_remark 物价评定 from Test
-----------------
-----------------------------------------------------------------------------------------
-------拷贝已存在的表
create table emp_bf as select * from Scott.emp ;
select * from emp_bf;----查询已拷贝成功的表
-----------关于数据库全部用户的信息
select * from dba_users
------ 列出所有员工的姓名及其直接上级的姓名
SELECT ename,(eselct ename from scott.emp where empno=e.mgr)上级姓名 from scott.emp e;
--4 列出受雇日期早于其直接上级的所有员工
select * from scott.emp e where hiredate<(select hiredate from scott.emp where empno=e.mgr);
--5 列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门
select dept.*,emp.* from scott.emp right join scott.dept on emp.deptno=dept.deptno;
alter user scott account unlock;
--1 列出至少有一个员工的所有部门
select * from scott.dept where deptno in (select distinct deptno from scott.emp);
select * from scott.bonus
--2 列出薪金比“SMITH”多的所有员工
select * from scott.emp where sal>(select sal from scott.emp where ename='SMITH');
--3 列出所有员工的姓名及其直接上级的姓名
SELECT ENAME,(select ename from emp where empno=e.mgr)上级姓名 from emp e;
Select ename,hiredate from scott.emp where hiredate='1-1-1981';
--不允许用户在"Sundays"(星期天)使用员工信息表
create or replace trigger not_on_sunday
before insert or update or delete on scott.emp
begin
if rtrim(to_char(sysdate, 'DAY')) = '星期天' then
raise_application_error(-20333, '星期天不允许操作');
end if;
end;
----------------
create or replace procedure addsal
as
begin
update scott.emp set sal=sal+100;
end;
--调用过程
begin
addsal;
end;
--还可以在命令行下调用
select * from scott.emp;
rollback;