Oralce数据库常见的操作

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/HS2529077916/article/details/101165117

Oralce数据库常见的操作

1 数据库的整体结构

  1. Database (数据库)

    数据库是按照数据结构来组织、存储和管理数据的仓库

  2. Tablespaces (表空间)

    表空间是数据库的逻辑划分,一个表空间只能属于一个数据库。所有的数据库对象都存放在指定的表空间中。但主要存放的对象是表, 所以称作表空间。

  3. Segments (段)

    段是表空间的重要组织结构,段是指占用数据文件空间的通称,或数据库对象使用的空间的集合;段可以有表段、索引段、回滚段、临时段和高速缓存段等。

  4. Extents (盘区)

    是数据库存储空间分配的一个逻辑单位,它由连续数据块所组成。第一个段是由一个或多个盘区组成。当一段中间所有空间已完全使用,oracle为该段分配一个新的范围

  5. DataBlook (数据块)

    oralce 管理数据文件中存储空间的单位,为数据库使用的I/O的最小单位,其大小可不同于操作系统的标准I/O块大小

2 创建数据库

3 表

3.1 创建表

CREATE TABLE 表名 (
字段名 字段类型  约束条件,
字段名 字段类型  约束条件
);
--添加注释 
--COMMENT ON COLUMN 表名.字段名 IS '注释内容';
comment on column student.name is '学生姓名';

3.2删除表

--删除表的结构,彻底删除该表
DROP TABLE 表名;

3.3修改表

--修改表明
ALTER TABLE ()表名 RENAME TO ()表名;

4 表中数据

4.1添加数据

--表中添加数据
INSERT INTO 表名(字段名1,字段名2,字段名3..) VALUES(1(字段名1),2(字段名2),3(字段名3),..)

insert into student(id,name,sex,age) values(1,'曾华','男',16);

4.2删除数据

--删除数据
DELETE FROM 表名 WHERE 条件;

delete from studentinfo where studentid=1;

4.3修改数据

--修改数据
UPDATE 表名 SET (要修改)字段名 = (新的)WHERE 条件;

update  studentinfo set  studentsex='女' where studentid=1;

4.3查询数据

是数据库中关键部分就不再这里讲了

4.4 特别篇 ----约束条件

约束条件(五种(主键、外键、非空、唯一、检查)):

约束条件 解释
primary key 主键约束
foreign key 外键约束
not null/ null 非空约束/空约束
check 检查约束
unique default ‘默认值’ 唯一约束
  • primary key

    • 建表时添加约束

      --注释 constraint 约束名 primary key(约束列)
      create table student(
          id number(8,0),
          name varchar2(20),
          constraint pk_id primary key(id)
      );
      --或者
      create table student(
          id number(8,0) primary key,
          --如果此处这样定义主键,则主键名称系统自己定义设置 
          --id number(8,0) constraint pk_id primary key;
          name varchar2(20)
      );
      
      
    • 建表后添加约束

      --alter table 表名 add constraint 主键名 primary key(要设为主键的列名);
      create table student(
          id number(8,0),
          name varchar2(20)
      );
      alter table student add constraint pk_id primary key(id);
      
    • ​ 删除主键

      --1. alter table 表名 drop constraint 主键名
      	alter table student drop constrait pk_id;
      --2. alter table 表名 drop primary key
      	alter table student drop primary key;
      
  • foreign key

    一般外键名称为”fK_”开头,

    • 建表时添加约束

      --constraint 外键名(一般外键名称为”fK_”开头) foreign key (要设为外键的列名) references 主表名(主表中该列列名)
      create table dept(
          deptid number(8,0) primary key,
          deptname varchar2(20),
          constraint fk_deptid foreign key(deptid) references dept(deptid)
      );
      
    • 建表后添加约束

      --alter table 从表名 add constraint 外键名称 foreign key(要设为外键的列名) references 主表名(主表中该列列名);
      alter table student add constraint fk_deptid foreign key(deptid) references dept(deptid);
      
    • 删除外键

      --alter table "表名" drop constraint "外键名"
      alter table dept drop constraint fk_deptid;
      
  • not null

    • 建表时添加约束

      create table student(
          id number(8,0),
          name varchar2(20) not null,
          constraint pk_id primary key(id)
          --注释 constraint 约束名 primary key(约束列)
      );
      
    • 建表后添加约束

      --alter table 表名 modify 列名 not null/null;
      --如果表中已经存在null,就不能更改其为not null约束
      alter table student modify name not null;
      
    • 删除 非空约束

      --alter table 表名 modify 列名 null;
      --删除非空,就是将其设为空
      alter table student modify name null;
      
  • check

    • 建表时添加约束

      --constraint 列名 check(检查约束的条件);
      create table student(
       id number(8,0) primary key,
       name varchar2(20),
       age number(3,0),
       constraint name check (age>=15 and age<=25)
      );
      
    • 建表后添加约束

      --alter table 表名 add constraint 列名 check(检查条件);
      alter table student add constraint age check(age>=15 and age<=25);
      
    • 删除检查约束

      --alter table 表名 drop constraint 列名;
      alter table student drop constraint age;
      
  • unique

    • 建表时添加约束

      --constraint unique_列名 unique(列名)
      create table student(
       id number(8,0) primary key,
       name varchar2(20),
       age number(3,0),
       constraint unique_name unique(name)
      );
      
    • 建表后添加约束

      --alter table 表明 add constraint unique_列名 unique(列名);
      alter table student add constraint unique_name unique(name);
      
    • 删除唯一性约束

      --alter table 表明 drop constraint unique_列名;
      alter table student drop constraint unique_name;
      

5 注意

​ 每一次对数据库进行改动的时候,都需要在最后面的部门,进行提交,这样数据的变动的才能真真的保存到数据库中

--commit  是一次性提交到数据库保存,不commit就不会真正存储到数据库中。
--rollback 是回滚操作,代表的意思就是不commit就可以回滚到上一次操作

猜你喜欢

转载自blog.csdn.net/HS2529077916/article/details/101165117