Common database operations Oralce

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/HS2529077916/article/details/101165117

Common database operations Oralce

The overall structure of a database

  1. Database (database)

    Database is in accordance with the data structure to organize, store and manage data warehouse

  2. Tablespaces (table space)

    Table space is a logical division of the database, a table space can only belong to a database. All database objects are stored in the specified table space. But the object is stored in the main table, so called table space.

  3. Segments (段)

    Organizational structure is an important segment table space, the space segment is a collection of generic data files occupy space, or database objects used; segment can have the table segment, index segment, rollback segments, temporary segments and caching section and so on.

  4. Extents (panel)

    Logical unit is a database storage space allocation, it is composed of continuous data blocks. The first segment is composed of one or more extents. When all of the intermediate section of the space has been fully used, oracle for the segment assigned a new range

  5. DataBlook (data block)

    oralce file management data storage unit, the smallest unit of I / O is used by the database, which may be different from the size of a standard operating system I / O block size

2 Create a database

Table 3

3.1 Create a table

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

3.2 Delete table

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

3.3 modify the table

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

Data in Table 4

4.1 Adding Data

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

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

4.2 to delete data

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

delete from studentinfo where studentid=1;

4.3 modify the data

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

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

4.3 query data

Is a key part of the database is no longer here to speak up

4.4 Special Edition ---- constraints

The constraints (five kinds (primary key, foreign key, non-empty, the only check)):

Restrictions Explanation
primary key The primary key constraint
foreign key Foreign key constraint
not null/ null Non-empty constraint / null constraint
check Check constraints
unique default 'Default' The only constraint
  • primary key

    • Add constraint to build the table

      --注释 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)
      );
      
      
    • After adding constraints to build the table

      --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);
      
    • Drop Primary

      --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

    Generally the foreign key name "fK_" at the beginning,

    • Add constraint to build the table

      --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)
      );
      
    • After adding constraints to build the table

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

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

    • Add constraint to build the table

      create table student(
          id number(8,0),
          name varchar2(20) not null,
          constraint pk_id primary key(id)
          --注释 constraint 约束名 primary key(约束列)
      );
      
    • After adding constraints to build the table

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

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

    • Add constraint to build the table

      --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)
      );
      
    • After adding constraints to build the table

      --alter table 表名 add constraint 列名 check(检查条件);
      alter table student add constraint age check(age>=15 and age<=25);
      
    • Delete Check Constraints

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

    • Add constraint to build the table

      --constraint unique_列名 unique(列名)
      create table student(
       id number(8,0) primary key,
       name varchar2(20),
       age number(3,0),
       constraint unique_name unique(name)
      );
      
    • After adding constraints to build the table

      --alter table 表明 add constraint unique_列名 unique(列名);
      alter table student add constraint unique_name unique(name);
      
    • Drop the unique constraints

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

Note 5

Every time the database changes are required in the final surface of the department, submit, save the changes to Barbara this data into the database

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

Guess you like

Origin blog.csdn.net/HS2529077916/article/details/101165117