最近有学习Oracle就又重新温习了一下sql数据类型及sql命令,当然我们在学习Oracle的同时应该已经接触过MySql数据库,具有了一定的java基础。所以对Oracle型的数据类型及命令也会很快的理解,它们之间并没有多大的区别,这篇文章里有很多的示例,大家可以跟着实践一下,通俗易懂。
当然(楼主)也只是一个java编程菜鸟,才刚刚入门而已,不敢说什么经验之谈。
只是希望能够提高自己的编写能力和养成定期学习总结的良好习惯,承蒙各位看官抬爱观看,权当在下是抛砖引玉、借花献佛了。
废话说了很多(也不全是废话喽),下面正文开始,敬请观看:
SQL数据类型
1、数字类型:不分整数和小数,number:相当于java里边的double
number(最大长度):用来表示整数。
number(最大长度,小数位数):用来表示小数。
例如:number(7,2)
12345.22——>正确,可以存储
123456.22——>错误,有效数字超过7位无法存储
12345.22222——>正确,小数部分会四舍五入的被截算,实际存储的是12345.22
2、字符串类型:不分字符和字符串
varchar2(最大长度):变长的字符类型,会根据实际值调整存放空间,空间可变,最多可存储4000个字节。
char(num):直接为值分配num个空间,最大可存储2000个字节。
注:中文在数据库里存放时一个汉字一般占两个字节。
3、布尔类型
number(1):当取值为1(或者为非0)时表示为真,当取值为0时表示为假。
char(1):当取值为’1’时表示为真,当取值为’0’时表示为假。
4、时间类型
date:年月日时分秒
timestamp:时间戳,年月日时分秒,比date表示的精度更高,最小能精确到纳秒。
5、大数据类型
clob:字符型大对象(character large object)
blob:二进制型大对象(binary large object)
数据定义语言(DDL)
创建数据库与表结构
建表语法:
create table 表名(
列名 数据类型(长度) constraint 约束名 约束类型,
列名 数据类型(长度) 约束类型1 约束类型2 ……,
……
列名 数据类型(长度) 约束类型
);
合法标识符:由字母、数字、_、$、#组成,其中数字不能出现在开始位置,不区分大小写。
约束:
- primary key:主键约束,表示唯一并且非空,一张表里最多只能有一个主键。
- not null:非空约束,要求该列值非空。
- unique:唯一约束,要求表里的该列值是唯一的。
- check(给定约束条件):自定义约束,表示只有符合条件的数据才是有效数据。
注:每一列可同时加上多个约束,多个约束之间用空格隔开。
- references tabname(colname) on delete cascade:外键约束,外键一般要求指向另外一个表的主键或者唯一键。
注:习惯上的约束命名:表名_列名_约束类型简称
若建表的时候没有给约束名,那么oracle系统会自动起一个约束名称。
蓝色部分代表可省略。
示例:
创建学生表和班级表,体现学生归属在某个班级下:
Student(id,name,sex,email,mobile)
Class(id,name)
-------------------------------------------------------------------------------------------------------------
create table class(
id number(7) primary key,
name varchar2(20) not null
);
create table student(
id number(7) constraint student_id_pk primary key,
name varchar2(20) constraint student_name_nn not null,
sex char(1) default ‘m’ check(sex in(‘m’,’f’)),
email varchar2(40) constraint student_email_uk unique constraint
student_email_ck check(email like ‘%_@_%._%’),
mobile char(11) check(length(mobile)=11),
class_id number(7) references class(id) on delete cascade
);
更新数据库表结构
alter table
- 添加列
alter table 表名 add(列名 数据类型 【默认值】 【约束】,列名 数据类型……);
- 删除列
alter table 表名 drop(列名1,列名2,……);
- 修改列名
alter table 表名 rename column 旧名 to 新名;
- 修改列
alter table 表名 modify(列名 【数据类型】 【默认值】 【约束】);
- 添加约束
alter table 表名 add 约束类型(列名);
- 删除约束
alter table 表名 drop constraint 约束名;
示例:
- 为学生表添加年龄ages(数据类型为varchar2,约束为非空)、微信wechat
alter table student add(
ages varchar2(10) not null,
wechat varchar2(20)
);
- 删除微信列
alter table student drop(wechat);
- 修改列名ages为age
alter table student rename column ages to age;
- 修改age为number数据类型
alter table student modify(age number(3));
- 为age添加约束条件,让其不能为负数
alter table student add check(age>0);
- 添加外键约束
ALTER TABLE emp
ADD CONSTRAINT fk_depno
FOREIGN KEY(depno) REFERENCES dep(id)
ON DELETE CASCADE;
删除表
drop table tabname cascade constraint;
示例:
创建一张测试表,并且将其删除
create table test(
id number(7) primary key,
name varchar2(20) not null
);
drop table test;
数据操作语言(DML)
向数据库中插入数据(insert into)
- 全表插入:
insert into tabname values(v1,v2,……);
注:values里值的个数、类型、顺序必须和表里的列保持一致。
例:insert into student values (1, 'lucy', 'm', '[email protected]', '13501234345', null, 18);
- 选择列插入:
insert into tabname(需要插入的列名,多个列名之间用逗号分隔) values(v1,v2,……);
注:values里值的个数、类型、顺序必须和给定的列匹配。
例:insert into STUDENT (ID, NAME, SEX, EMAIL, MOBILE, CLASS_ID, AGE)
values (2, 'alice', 'm', '[email protected]', '13501234344', null, 20);
- 利用子查询插入:(讲完查询之后再演示)
insert into tabname(列名,……) select(列名,……) from tabname1;
例:insert into emp (id,name) select id,name from student;
更新数据库中的数据
update tabname set colname=值,colname=值,…… where 条件;
例:修改学生表中姓名为lucy的年龄为20
update student set age=20 where name='lucy';
修改员工表中部门为空的员工的部门为测试部
update emp set dep=’测试部’ where dep is null;
删除数据库中的记录
delete from tabname where 条件;
例:删除学生表中年龄大于20的数据
delete from student where age>20;
数据查询语言(DQL)
简单查询语句
1、查询所有列
select * from tabname;
例:select * from student;
2、查询指定列
select colname,colname,…… from tabname;
例:select id,name,age from student;
3、查询指定行(过滤查询)
select * from tabname where 条件1 and 条件2……;
select * from tabname where 条件1 or 条件2……;
- 精确查询
条件:colname=值
例:(1)查询名字是lucy的学生
SQL—>select * from student where name='lucy';
(2)查询名字是lucy并且性别为女的学生
SQL—>select * from student where name='lucy' and sex=’f’;
(3)查询名字是lucy或是alice的学生
SQL—>select * from student where name='lucy' or name=’alice’;
- 模糊查询
条件:colname like 操作符和值
注:%代表0到多个字符,_代表一个字符
例:(1)查询名字里含有字母l的学生
select * from student where name like ‘%l%’;
(2)查询名字里第二个字母为l的学生
select * from student where name like ‘_l%’;
- 在where中使用in
条件:colname in (值,值,……)
例:查询名字是lucy和alice的学生
select * from student where name in(‘lucy’,’alice’);
- 查询字段内容为空/非空的语句
is null/is not null
例:(1)查询手机号为空的学生
select * from student where mobile is null;
(2)查询手机号不为空的学生
select * from student where mobile is not null;
- 使用算术表达式
例:先创建一张员工表(主键,姓名,薪资,部门),然后插入一些数据,最后查询每位员工的年薪
create table emp(
id number(7) primary key,
name varchar2(20) not null,
salary number(7,2),
dep varchar2(20)
);
insert into emp values(11,'zhangsan',5000,'测试部');
insert into emp values(22,'lisi',6000,'开发部');
insert into emp values(33,'wangwu',4000,'测试部');
select salary*12 from emp;
- 将查询结果按照指定字段排序
order by
例:(1)将学生按照年龄升序排序
select * from student order by age asc;
(2)将学生按照年龄降序排序
select * from student order by age desc;
注:null是最大值。
- 去重distinct
例:(1)查询公司中的各种职位
select distinct dep from emp;
(2)查询公司中职位的个数
select count(distinct dep) from emp;
- case when
case when—表示一个判断,根据条件判断返回什么样的值。
语法:以case开头,中间为when……then……(可以写很多个,表示多个条件),以end结尾。
特点:只有一个值会被返回,使用else时,必须保证前边有一个或多个when,多个条件后返回值的类型应是一致的。
例:select name,salary,
case when salary>5000 then ‘high’
when salary between 3000 and 5000 then ‘middle’
else ‘base’
end as salary_level
from emp order by salary;
复杂语句
- 数据分组(max,min,avg,sum,count)
例:(1)查询员工表中最大的工资数,最小的工资数,平均工资以及工资和
select max(salary),min(salary),avg(salary),sum(salary) from emp;
(2)查询员工表中工资最高的员工信息
select * from emp where salary=(select max(salary) from emp);
(3)查询所有员工的数量
select count(*) from emp;
- group by(用于对查询结果的分组统计)和having子句(用于限制分组显示结果)
例:(1)查询各个部门之间最大的工资和平均工资
select dep,max(salary),avg(salary) from emp group by dep;
(2)查询部门平均工资小于4000的平均工资和部门中的最低工资
select dep,avg(salary),min(salary) from emp group by dep having avg(salary)<4000;
group by 的简单说明:
group by 一般和聚合函数一起使用才有意义,比如 count 、sum、avg等,使用group by的两个要素:
(1) 出现在select后面的字段要么是聚合函数(组函数)中的,要么就是group by中的。
例:select dep,max(salary),avg(salary),name from emp group by dep;(是不合法的)
(2) 要筛选结果可以先使用where再用group by或者先用group by 再用having。
注:只有出现在group by里的列名才能单独出现在查询结果select里。
如果group by里的列使用了单行函数,那么select里的这一列也必须使用同样的函数。
例:select dep,max(salary),avg(salary) from emp group by upper(dep);(是不合法的)
如果分组,只有组的共性才能够出现在order by。
例:select dep,max(salary),avg(salary) from emp group by dep order by name;(是不合法的)
没有出现在group by里的列,只有应用了组函数以后,才能出现在select里。
注:单行函数和组函数
如果根据函数返回的查询结果是一行或多行可将函数分为单行函数和组函数。
单行函数和多行函数的根本区别在于行数上的不同,单行函数对表或视图进行水平方向(横向)计算,所产生的结果对于表的每行均有一个值,多行函数对表或视图进行垂直方向(纵向)计算,对产生的结果是一个单值。
having和where的区别:
where出现在group by之前,数据以行为单位存在,所以where只能对行进行判断。
having出现在group by之后,数据以组为单位存在,所以having只能对组的共性判断。
例:select dep,max(salary) from emp group by dep having dep=’测试部’;
总结:
select……from……where……group by……having……order by
select六条子句执行顺序:
①from—确定数据来源
②where—将满足where条件的数据放入查询结果
③group by—对where条件查询回来的结果进行分组
④having—对分好的组条件判断,留下有用的组
⑤select—根据select里的显示要求,对有用数据进行统计计算
⑥order by—对统计好的数据排序
- 多表查询
例:(前提)此时将员工的部门提取出来,创建一张部门表,那么员工归属于某个部门通过外键来进行关联。
create table dep(
id number(7) primary key,
name varchar2(20) not null
);
insert into dep values(1,'测试部');
insert into dep values(2,'开发部');
insert into dep values(3,'财务部');
alter table emp rename column dep to depno;
update emp set depno=null;
alter table emp modify(depno number(7));
-
- 查询员工姓名、工资、部门名称
select e.name,e.salary,d.name from emp e,dep d where e.depno=d.id;
-
- 查询工资在5000到6000之间的员工姓名、工资、部门名称
select e.name,e.salary,d.name from emp e,dep d where e.depno=d.id and e.salary between 5000 and 6000;
注:between and(包含边界值)
- 自连接(指同一张表的连接查询)
例:(前提)为员工表中添加管理者id这一列
alter table emp add(mgr number(7));
查询员工的姓名以及他的管理者的姓名
select e1.name,e2.name from emp e1,emp e2 where e1.mgr = e2.id;
- 子查询(嵌入到其他sql语句中的select语句,也叫嵌套查询)
①单行子查询
例:查询表中与lucy同部门的人员名字
select name from emp where depno = (select depno from emp where name=’lucy’);
②多行子查询
例:查询工资低于5000的员工所属部门信息
select * from dep where id in(select depno from emp where salary<5000);
- 合并数据
①union:合并多条查询语句的结果,合并过程中去掉重复的记录,只保留一条
例:select * from emp where depno in(1,2)
union
select * from emp where depno in(2,3);
②union all:合并多条查询语句的结果,合并过程中保留重复记录
例:select * from emp where depno in(1,2)
union all
select * from emp where depno in(2,3);
③minus:求差集,用第一个select命令里的结果集减去第二个select命令里的结果集
例:select * from emp where depno in(1,2)
minus
select * from emp where depno in(2,3);
④intersect:求交集
例:select * from emp where depno in(1,2)
intersect
select * from emp where depno in(2,3);
到这里这期总结及分享就告一段落了,也希望大家有所收获,下期再会!