数据库语言
一、DDL
1、创建表空间
--1.基本语法:
create tablespace 表空间名 datafile '磁盘目录\数据文件名.dbf' size 大小;
--示例:
create tablespace stx4(表空间名) datafile +'C:\stxstx\stx+.dbf' +size 128m(给表空间的大小,128兆);(英文版的分号结束)
--2.创建用户
create user fjh1(用户名) identified by fjh1(密码) default tablespace stx4 (表空间名);
--3.用户授权 授dba权限
grant dba(权限名称) to fjh1(用户名);
2、创建表
--创建表
create table student(
id number,
gender char,
name varchar2(32),
creation_time timestamp
);
--查看表结构
desc 表明;
desc student;
oracle 数据类型:以11g为例
https://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements001.htm#SQLRF30020
二、DML
1、插入数据
--基本语法
insert into 表名[列名1,列名2···列名n] values(值1,值2····值n);
--示例1
insert into student(id, gender, name, creation_time) values(1, '1', 'frank',
current_timestamp);
--示例2
insert into student values(4, '1', 'frank4', current_timestamp);
--注意事项
--1.如果没有指定列,那么就是给所有的列赋值。
--2.字段和值的顺序是一一对应的。
--3.如果插入的值是字符类型的,要用英文单引号引起来。
--4.如果插入部分字段,那么必须写明要插入的字段名称。
--基本查询
select * from student;
--设置自动提交
--1. PL/SQL: 工具--> 首选项--->窗口类型--->SQL窗口
--2.命令行设置:查看 show autocommit; 设置 set autocommit on;
PL/SQL 设置自动提交
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GKwH60V1-1635229126616)(C:\Users\方健紅\Desktop\SUANGTI\JAVA\Typora\缓存\笔记图片\PLSQL 设置自动提交.png)]
2.删除数据
--基本语法
delete from 表名 [条件];
--示例1
delete from student;
--示例2:按条件删除
delete from student where id = 1;
--删除表中所有的数据,第二种方式:建议熟悉delete、 truncate 的区别。查看P307。
truncate table student(表名)
3.更新数据
--基本语法
update 表名 set 列名1 = 值1,列名2 = 值2,列名3 = 值3 [where 条件];
--示例
update student set name = '方健紅' where id = 1;
三、DQL
1.基本查询
--基本语法
select 列名1,列名2,···列名n from 表名;
--示例
select * from emp;
--查询指定列名:
select ename,sal from emp;
--查询工资总和 nvl(列名,0);
select ename,sal,comem,sal+ nul(comm,0) nvl(字段名),如果字段为空 from emp;
--起别名:as关键字(as可以省略)
select ename,sal,comem,sal+ nul(comm,0) as '总和'from emp;
--查询工资在2500以上的员工
select * from emp where sal >2500;
--查询工资介于2500~5000之间的员工
select * from emp where sal between 2500 and 5000;(包含上下限)
--查询有奖金的员工信息
select * from emp where comm is not null;
--查询姓名中包含’天‘字(like字符串匹配)
select * from emp where ename like '%天%';
-- 查询姓张的员工:
select * from emp where ename like '张%';
--查询员工姓名是两个字的 两个下划线
select * from emp where ename like '__’;
2.排序
--基本语法(默认升序,asc;降序,desc)
select * from emp(表名) order by 列名 ,asc/desc;
--示例:
select * from emp order by sal;
--多列排序:
select * from emp order by sal desc;
selece ename,sal,comem,sal+ nul(comm,0) as '总和'from emp order by 总和 desc;
3.聚合函数
---count(列名),count为计数
---count(*)*为避免空行,会把空行也记录进去
---count(列名)不会记录空行
select count(*) from emp;
---count不可以统计空值例如:
select count(comm) from emp;
---count 中去重,例如
select count(distinct(job)) from emp;
---sum 求和
select sum(sal) from emp(表名);
---avg 平均值
select avg(sal) from emp;
---max 最大值
select max(sal) from emp;
---min 最小值
select min(sal) from emp;
---同时求平均值,最大值:
select avg(sal),max(sal) from emp;
4.分组查询
---基本语法,关键字 group by
select 列名,聚合函数 from 表名 group by 列名;
---实例
select job(岗位), count(1) from emp group by job;
---统计每个岗位的平均工资人数
select job,avg(sal),count(1) from emp group by job;
------统计每个岗位的平均工资人数,按平均工资倒叙排序
select job,avg(sal),count(1) from emp group by job order by avg(sal) desc;
-- 在上一个需求的基础上,排除掉老板的工资(工资少于5000)
select job, avg(sal) as sal, count(1) from emp where sal < 5000 group by job
order by sal desc;
---统计每个岗位的平均工资人数,按平均工资倒叙排序,要求统计岗位
select job,avg(sal) as sal,count(1) from emp
group by job
having count(1) > 2
order by sal desc;
---理解where 和having 的区别
where :分组之前的筛选
having:分组之后的再次筛选
5.联表查询
-- SQL-99
select 列名1, 列名2,... 列名n from 表1 inner join 表2 on 条件;
-- 示例:
select ename, dname from emp inner join dept on emp.deptno = dept.deptno;
-- SQL-89
select 列名1, 列名2,... 列名n from emp, dept where emp.deptno = dept.deptno;
-- 示例:
select ename, dname from emp, dept where emp.deptno = dept.deptno;
-- 起别名
select e.ename, d.dname, e.deptno
from emp e, dept d
where e.deptno = d.deptno;
6.补充
-- 1, 主键关键字
//普通主键
create table test1(
id number primary key,
name varchar(16)
);
//高级主键
constraint 主键名称 primary key;
-- 示例:
create table test2(
id number constraint pk_id primary key,
name varchar(16)
);
-- 2, timestamp默认值
create table test3(
id number primary key,
name varchar(16),
creation_time timestamp default current_timestamp
);
-- 插入指定日期:
insert into test3
(id, name, creation_time)
values(2, 'frank2',
to_date('2021-04-01 14:25:09', 'yyyy-MM-dd HH24:mi:ss'));
-- 3,序列的创建
create sequence seq_test1(序列名字)
start with 1 increment by 1;
-- 使用序列
insert into test1(id, name)
values(seq_test1.nextval, 'frank');
-- 查看序列当前值:
select seq_test1.currval from dual;
.