oracle数据库——简单表操作

oracle 数据库——简单表操作

表是数据库中最常操作的数据对象,本文将从以下几个方面对表进行基本的总结概括:

  • 表的创建语法
  • 简单的表查询
    • select 子句
    • from子句
    • where子句
    • order by排序
    • group by分组
  • 补充参考
    • oracle通配符

数据表的创建

create [ [global] temporary|table|schema ] table_name
(
column1 datatype1 [default exp1] [column1 constraint],
column2 datatype2 [default exp2] [column2 constraint]
.........
[table constraint]  --constraint 约束名 约束(列名)--
)
[on commit (delete | preserve) rows]    --事务级临时表|会话级临时表--
[organization {heap|index|external...}] --表类型--
[partition by ...(...)]                 --为分区及子分区信息--
[tablespace tablespace_name]            --表空间名--
[logging|nologging]                     --是否保留重做日志--
[compress|nocompress]                   --是否压缩--

default exp1设置列的默认值,可选;column1 constraint添加列级别的约束;table constraint添加表级别的约束。创建简单的学生信息表:

–利用列级别的约束–

  create table students(
  id number(12) primary key,
  name varchar(4) not null,
  age number(2)  check(age>0 and age<25),
  sex varchar(2) ,
  classNum number(3),
  address varchar2(20)
  )

– 利用表级别的约束–

create table students_1(
  id number(12) ,
  name varchar(4) not null,
  age number(2)  check(age>0 and age<25),
  sex varchar(2) ,
  classNum number(3),
  address varchar2(20))
  constraint id_pk primary key (id)
  )

简单的表查询

select column1,column2,…..from [schema.]table_name
where conditions ;

简单的查询操作由select子句,from子句,where条件子句构成。

select子句

1.select子句从表中选取列,可以为列设置列别名

 select age student_age from.....`

2.列类型为number或者date类型的可以对其进行算术运算;

 select age*1.1 new_age from....查询年龄扩大1.1倍的数据;

3.distinct修饰列名,去除查询结果中的重复值;

select distinct  classNum,avg(age) avg_age from.....
查询每个班级的平均年龄

from子句

1.from 后跟表名,对于非当前方案的表对象,需要表明表空间名也即是schema.table_name;

2.与select子句类似,查询的表可以使用表别名,通常这样做可以使引用到表的名称时更简洁:

    select * from students s where s.id=001;

where子句

where子句后跟查询条件,查询结果根据查询条件进行过滤,最终将过滤后的结果显示出来;

1.条件表达式构成查询条件:=,!=,<,>,like;
需要注意的是这里的“=”就是算术式中的相等判断符号,与赋值无关;like是匹配运算符,与通配符结合使用,可达到模糊查询的效果。

2.用连接运算符(and | or)连接多个查询条件;

 select *from students s where s.age=18 and s.sex='female'

3.null值,某行记录中某个字段数据为空也即是没有记录数据,此种情况的查询需运用is nullis not null判断某列值是否为空:

select *from students s where s.address is not null/is null;`

order by子句

oracle数据库中的排序功能可以对查询结果根据某个字段进行升序(ascending)排列或者是降序(descending)排列,排序有以下几点需要注意:

1.根据某个字段或者是某几个字段进行排序:

 select *from students s order by age desc,classNum [asc];

–按照第一个字段排序时值相等则按照第二个字段进行排序,以此类推–

2.默认使用升序排列

select * from students s order by age;

3.–对字符串类型列进行排序时根据的是字符的ASCII编码进行排序;

select * from students s order by name;

group by子句

group by子句用于对查询结果进行分组,例如对学生表中的数据按照班级进行分组:

select * from students s group by classNum;

group by 子句使用通常注意以下几点:
1.通常与统计函数(count、min、max、sum、avg)结合使用:

select avg(age)avg_age,classNum from students s group by classNum;--计算学生表中各班级的平均年龄--

2.与order by 排序类似,group by 分组子句同样可以进行多列分组:

select * from students s group by classNum,address;
--当按班级分组之后可以在主分组范围内按照地址进行二次分组

3.使用运算符rollup,cube运算符在查询结果中附加一行汇总信息:

select * from students s group by rollup(id);

having子句

having子句主要有两种用法:
1.与group by 子句联用进一步对分组查询结果进一步筛选:

select avg(age) avg_age,classNum from students s group by classNum having avg_age>10;

2.不与group by 子句联用时与where功能相同

补充参考

oracle通配符

%:代表0个,1个或多个任意字符;
_:代表任意一个字符;
?:用来表示确切的未知字符;
#:用来表示确切的阿拉伯数字,0到9;
[a-d]:用来表示字符范围,在这里是从a到d。

猜你喜欢

转载自blog.csdn.net/sandyxin5208/article/details/78965285