sql入门


create user xianxian   identified  by xianxian
--DCL
grant connect  to xianxian  -- Connect 登录角色
grant resource to xianxian  -- resource 操作数据权限
grant create any view to xianxian  --给用户创建视图的权限
---以上用系统管理员身份登录(DBA操作)

--建表结构Data define Language--DDL
create table myuser
(--字段
username varchar2(12),
password varchar2(12)
)

select u.username from myuser u

create table  department
(
departId int primary key,
departName varchar2(12),
remark  varchar2(12)
)
--CRUD(增删改查专业术语)--DML
--增加
insert into department(departId,departName,remark)values(100,'人事部','很厉害的')
insert into department(departId,departName,remark)values(101,'技术部','牛的很')
--修改
update department t set t.departname='软件部' where t.departname='人事部'
--删除
delete from department t where t.departid=100;
--查询
select * from department



SQL--Standard Query Language标准查询语言
--DML
--DDL
--DCL


--创建序列对象,从100开始,自增长,每次增1,没有最大值,不循环
create sequence seq_department increment by 1 start with  100 nomaxvalue nocycle

--需要伪表dual
select seq_department.NextVal from dual--首先执行NextVal,在此回话中定义
select seq_department.CURRVAL from dual
--事物回滚体现了数据的一致性,对数据便于维护
insert into department(departId,departname) values(seq_department.NextVal,'设计部')
select * from department


drop table employee;
create table employee(
empid number primary key ,
empname varchar2(15),
salary number(5,2),  --整数加小数部分 最大为5位,小数位为两位
hiredate date,
deptid  number
)
select * from employee;
insert into employee values(7,'武器大师',null,sysdate,112);
select  deptid,sum(salary),round(avg(nvl(salary,0)),2),count(*) from employee group by deptid order by avg(nvl(salary,0)) desc;
--distinct  唯一(去重)
---nvl() 使分组函数无法忽略空值  --avg(nvl(salary,0))
--%代表任意个数的字符   --%req%: req为子串的通配符
--to_date('1993-06-18','YYYY-MM-DD')  --字符串转换成日期型用于添加数据
--to_char(hiredate,'MM-DD-YYYY') ---date类型转换成字符串 用于查询

select to_char(sysdate,'DD-MM-YYY') from dual

--根据ASCII表 可以对字符串进行排序
select * from employee order by empname desc --降序
select * from employee order by salary,hiredate  --升序

--用到group by   select 中不能出现在多值 ;如有多值,在group by 后也要加上这个字段
select deptid,empname from employee group by deptid,empname
--where 字句要先于group by子句执行  group by 之后进行过滤 使用 having  avg(salary) >...
---in 表示范围  ,等于列表中的任何一个
-- < all(select...)
-- < any(select...)

           select deptid,salary from employee  group by deptid ,salary having  salary>(select deptid, avg(salary) from employee group by deptid)
           select deptid,empname,salary from employee  group by deptid,empname,salary
           group by
           --2--查询工资大于其所在部门的平均工资的员工信息
           select empname,salary from employee oe where salary >(select avg(salary)
           from employee  ie where ie.deptid=oe.deptid)
           
           
           --3--查询每个部门的最大工资(列出部门号和该部门最大工资)。
           --这些部门需满足该条件,平均工资大于部门号为XX(自己定)的部门的平均工资
           select deptid ,max(salary) from employee group by deptid having avg(salary)>(select avg(salary) from employee where deptid=111)
           --4--查询同一天进公司(雇佣日期相同)的员工中工资最高的员工信息(包括姓名、雇佣日期、 工资)
          select * from employee oe where salary=(select max(salary)
          from employee ie group by  hiredate having oe.hiredate=ie.hiredate)
       
         
           --商品种类表
         create table  shangpingzhonglei(
           id number primary key,
           spzlname varchar2(15)
          )
           --商品表
        create table shangping(
          spid number primary key,
          spcid number,
          spname varchar2(15),
          spprice number(5,2),
          spnum number,
          spallsum number(5,2)
           )
           
   --5--查询每个商品类别下所有商品数量,要求显示类别名和对应数量
   select zl.spzlname,count(s.spcid)from
   shangping s,shangpingzhonglei zl
    where zl.id=s.spcid  group by zl.spzlname
   --6--查询每个商品类别下所有商品的总金额,要求显示类别名和对应的总金额
   select zl.spzlname,sum(s.spallsum)
   from shangping s,shangpingzhonglei zl
   where zl.id=s.spcid group by zl.spzlname
   --7--查询所有商品类别下总金额最高的那个类别的数据,要求显示类别名和对应总金额
    --写法1
     select sl.spzlname,r.sumprice from
      (select s.spcid id,sum(s.spallsum) sumprice from  shangping s group by s.spcid
       having sum(s.spallsum)>=
       all(select sum(s.spallsum) from shangping s group by s.spcid)) r
       inner join shangpingzhonglei sl on r.id=sl.id
    --写法2
    
    --写法3
    select sl.spzlname,sum(s.spallsum) from shangping s
     inner join shangpingzhonglei sl on s.spcid=sl.id
     group by s.spcid,sl.spzlname
     having sum(s.spallsum)>=
     all(select sum(s.spallsum) from shangping s group  by s.spcid)
    
    select * from shangpingzhonglei
   --1--查询没有任何员工的部门信息
   select * from department  where departid not in(
   select deptid from employee where deptid is not null)
   
   ---内联接 .. from employye e  inner join   department d on e.deptid=d.departid;
   select e.empname,d.departid,d.departname from employee e
   inner  join department d on e.deptid=d.departid;
   ---外联接 :
   --左外联接,(left [outer] join ..on..),(oracle特定语法: 。。。。。where e.deptid=d.departid(+);)
    select e.empname,d.departid,d.departname from employee e
    left outer join department d on e.deptid=d.departid;
     --右外联接(right [outer] join ..on..),(oracle特定语法: 。。。。。where e.deptid(+)=d.departid;)
    select e.empname,d.departid,d.departname from employee e
    right outer join department d on e.deptid=d.departid;
    
     --全外联接(满外联接)(full outer join ..on..)
    select e.empname,d.departid,d.departname from employee e
    full outer join department d on e.deptid=d.departid;
   
   ----交叉联接(笛卡尔积)(无关联条件)(记录数,左边和右边的乘积)cross join
   select e.empname, d.departid, d.departname from employee e
   cross join department d;
   
  --添加foreign key约束
  alter table employee add  constraint FK_EMP_DEPARTID foreign key(deptid)
  references department(departid)
 
  -- ON DELETE CASCADE(父项删除,子项强制删除)  ON DELETE set null(父项删除,子项设为null)
   alter table employee add  constraint FK_EMP_DEPARTID foreign key(deptid)
  references department(departid)
   ON DELETE CASCADE
   --添加unique约束
   alter table employee add constraint emp_empname unique(empname)
  --添加 not null 约束
  alter  table employee   modify salary  number not null;
   --查询数据字典视图(查询约束)
    select constraint_name,constraint_type,search_condition from user_constraints  where table_name='EMPLOYEE'
    
    --无效化约束
    ---激活约束
    ---创建视图
    create or replace view emp_view
    as
    select e.empname,d.departname from employee e
    inner join department d
     on e.deptid=d.departid;
     --
     select * from emp_view;
     --删除视图
     drop view emp_view;
     --exists的用法
     select empname,Salary from employee outer where   exists(select empid from employee where Salary>500 and outer.empid=empid);

你可能感兴趣的:(primary,password,identified,department,系统管理员)