Oracle学习

Oracle教程

Oracle教程所有案例所需的表结构是利用Oracle技术圈自己设计的一套简单版的学生信息系统的表结构。相关表结构的关系图如下:
在这里插入图片描述

-- Create table
create table STUINFO
(
  stuid      VARCHAR2(11) not null,
  stuname    VARCHAR2(50) not null,
  sex        CHAR(1) not null,
  age        NUMBER(2) not null,
  classno    VARCHAR2(7) not null,
  stuaddress VARCHAR2(100) default '地址未录入',
  grade      CHAR(4) not null,`在这里插入代码片`
  enroldate  DATE,
  idnumber   VARCHAR2(18) default '身份证未采集' not null
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table STUINFO
  is '学生信息表';
-- Add comments to the columns 
comment on column STUINFO.stuid
  is '学号';
comment on column STUINFO.stuname
  is '学生姓名';
comment on column STUINFO.sex
  is '学生性别';
comment on column STUINFO.age
  is '学生年龄';
comment on column STUINFO.classno
  is '学生班级号';
comment on column STUINFO.stuaddress
  is '学生住址';
comment on column STUINFO.grade
  is '年级';
comment on column STUINFO.enroldate
  is '入学时间';
comment on column STUINFO.idnumber
  is '身份证号';
-- Create/Recreate primary, unique and foreign key constraints 
alter table STUINFO
  add constraint PK_STUINFO primary key (STUID)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
   
  -- Create table
create table CLASS
(
  classno        VARCHAR2(7) not null,
  classname      VARCHAR2(50),
  monitorid      VARCHAR2(11),
  monitorname    VARCHAR2(50),
  headmasterid   VARCHAR2(8),
  headmastername VARCHAR2(50),
  classaddress   VARCHAR2(50),
  enterdate      DATE
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table CLASS
  is '班级信息表';
-- Add comments to the columns 
comment on column CLASS.classno
  is '班级号';
comment on column CLASS.classname
  is '班级名称';
comment on column CLASS.monitorid
  is '班长学号';
comment on column CLASS.monitorname
  is '班长姓名';
comment on column CLASS.headmasterid
  is '班主任教师号';
comment on column CLASS.headmastername
  is '班主任姓名';
comment on column CLASS.classaddress
  is '班级地址';
comment on column CLASS.enterdate
  is '录入时间';
-- Create/Recreate primary, unique and foreign key constraints 
alter table CLASS
  add constraint PK_CLASS primary key (CLASSNO)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255;
 
 
-- Create table
create table COURSE
(
  courseid   VARCHAR2(9) not null,
  schyear    VARCHAR2(4),
  term       VARCHAR2(4),
  coursename VARCHAR2(100)
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table COURSE
  is '课程表';
-- Add comments to the columns 
comment on column COURSE.courseid
  is '课程id';
comment on column COURSE.schyear
  is '学年';
comment on column COURSE.term
  is '学期';
comment on column COURSE.coursename
  is '课程名称';
-- Create/Recreate primary, unique and foreign key constraints 
alter table COURSE
  add constraint PK_COURSE primary key (COURSEID)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
   
  -- Create table
create table STUCOURSE
(
  selectid   VARCHAR2(18) not null,
  stuid      VARCHAR2(11),
  courseid   VARCHAR2(9),
  schyear    VARCHAR2(4),
  term       VARCHAR2(4),
  redo       VARCHAR2(1),
  selectdate DATE
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table STUCOURSE
  is '学生选课表';
-- Add comments to the columns 
comment on column STUCOURSE.selectid
  is '选课id';
comment on column STUCOURSE.stuid
  is '学号';
comment on column STUCOURSE.courseid
  is '课程id';
comment on column STUCOURSE.schyear
  is '年度';
comment on column STUCOURSE.term
  is '学期';
comment on column STUCOURSE.redo
  is '是否重修';
comment on column STUCOURSE.selectdate
  is '选课时间';
-- Create/Recreate primary, unique and foreign key constraints 
alter table STUCOURSE
  add constraint PK_STUCOURSE primary key (SELECTID)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255;
 
 
-- Create table
create table SCORE
(
  scoreid  VARCHAR2(18) not null,
  stuid    VARCHAR2(11),
  courseid VARCHAR2(9),
  score    NUMBER,
  scdate   DATE
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table SCORE
  is '学生成绩表';
-- Add comments to the columns 
comment on column SCORE.scoreid
  is '学生成绩id';
comment on column SCORE.stuid
  is '学生学号';
comment on column SCORE.courseid
  is '课程id(年度+上下学期+课程序列)';
comment on column SCORE.score
  is '成绩';
comment on column SCORE.scdate
  is '成绩录入时间';
-- Create/Recreate primary, unique and foreign key constraints 
alter table SCORE
  add constraint PK_SCORE primary key (SCOREID)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

Oracle建表语句

常用的Oracle列字段的数据类型如下:

**数据类型 ****类型解释 **
VARCHAR2(length)字符串类型:存储可变的长度的字符串,length:是字符串的最大长度,默认不填的时候是1,最大长度不超过4000。
CHAR(length)字符串类型:存储固定长度的字符串,length:字符串的固定长度大小,默认是1,最大长度不超过2000。
NUMBER(a,b)数值类型:存储数值类型,可以存整数,也可以存浮点型。a代表数值的最大位数:包含小数位和小数点,b代表小数的位数。例子:number(6,2),输入123.12345,实际存入:123.12 。number(4,2),输入12312.345,实际春如:提示不能存入,超过存储的指定的精度。
DATA时间类型:存储的是日期和时间,包括年、月、日、时、分、秒。例子:内置函数sysdate获取的就是DATA类型
TIMESTAMP时间类型:存储的不仅是日期和时间,还包含了时区。例子:内置函数systimestamp获取的就是timestamp类型
CLOB大字段类型:存储的是大的文本,比如:非结构化的txt文本,字段大于4000长度的字符串。
BLOB二进制类型:存储的是二进制对象,比如图片、视频、声音等转换过来的二进制对象
select * from stuinfo;

alter table stuinfo add constraint pk_stuinfo_stuid primary key(STUID);

alter table stuinfo add constraint ch_stuinfo_age check (age>0 and age<50);

alter table stuinfo add constraint ch_stuinfo_sex check (sex='1' or sex='2');
alter table stuinfo add constraint ch_stuinfo_GRADE check (grade>='1900' and grade<='2999');


insert into stuinfo values('SC201801001','张三','1','19','C201801','陕西省西安市XXX号','2018',to_date('01-09-2018', 'dd-mm-yyyy'),'6102332000XXXXXXXX');

insert into STUINFO (STUID, STUNAME, SEX, AGE, CLASSNO, STUADDRESS, GRADE, ENROLDATE, IDNUMBER)
values ('SC201801005', '龙七', '1', 26, 'C201801', '福建省厦门市XXX号', '2018', to_date('01-09-2018', 'dd-mm-yyyy'),
 '3503021992XXXXXXXX')

代码解析:
(1)处: not null 表示学号字段(stuid)不能为空。
(2)处:default 表示字段stuaddress不填时候会默认填入‘地址未录入’值。
(3)处:表示表stuinfo存储的表空间是users,storage表示存储参数:区段(extent)一次扩展64k,最小区段数为1,最大的区段数不限制。
(4)处:comment on table 是给表名进行注释。
(5)处:comment on column 是给表字段进行注释。

查询

select命令结构:order by :查询结果按某个字段进行排序,默认是升序,desc是降序, asc是升序。

select *|列名|表达式  from  表名  where  条件  order  by  列名 asc

案例3:查询班级“C201801”所有同学信息,按年龄进行升序展示:

select t.* from STUDENT.STUINFO t  where t.classno = 'C201801'  ORDER BY  T.AGE ASC

备份查询数据命令结构:

create  table  表名 as  select 语句

备份查询数据命令结构:

create table 表名 as select 语句

案例 4:备份学生信息表(stuinfo)的数据:

create table  stuinfo_2018  as select  *  from  student.stuinfo ;select  *   from  student.stuinfo_2018;

增加数据

语法结构如下:

  INSERT INTOSELECT  子句

更新

update命令结构:

update  表名  set  列名1=1,列名2=2,列名3=3.....  where 条件

案例1、更新学生“张三”的年龄和身份证信息:

UPDATE STUINFO t set t.age = '21',t.idnumber = '4132332001XXXXXXXX',t.STUADDRESS='广西省南宁市XXX号'
 where t.STUNAME = '王五';
 COMMIT; --刷新

update 利用另外一张表关联更新本表数据的命令结构如下:

update1  set  列名=select  列名  from2  where1.列名=2.列名)      where  exists (select  1  from2  where1.列名=2.列名)

案例2、利用备份表stuinfo_2018更新回学生“张三”的年龄和身份证:

UPDATE STUINFO t
SET (age, idnumber) = (
   SELECT age, idnumber
   FROM STUINFO_2018 b
   WHERE b.STUID = t.STUID
)
WHERE EXISTS (
   	SELECT 1
   	FROM stuinfo_2018 b
   	WHERE b.stuid = t.stuid
   		AND b.STUNAME = '张三'
   );

删除

delete命令结构:

delete  from  表名  where  条件
命令解析:

1、当delete from不加where条件时,表示是把表中的数据全部删除。

案例1、删除学生信息表(stuinfo)中学生“张三”的数据:

delete  from  stuinfo t  where  t.stuname='张三';
truncate命令

truncate命令也是数据删除命令,他是直接把Oracle表数据一次删除的命令,truncate命令是一个DDL命令,不同于delete是DML命令。

truncate命令结构:

truncate  table  表名;

转载于https://www.oraclejsq.com/article/010100137.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值