Installation, uninstallation and basic use

Oracle

1. Course objectives

【了解】Oracle基本概念
【理解】Oracle的安装与卸载
【掌握】Oracle的基本使用

2. Overview

ORACLE database system is a set of software products with distributed database as the core provided by the American ORACLE company (Oracle). It is currently one of the most popular client/server (CLIENT/SERVER) or B/S architecture databases. ORACLE database is currently the most widely used database management system in the world. As a general database system, it has complete data management functions; as a relational database, it is a complete relational product; as a distributed database, it realizes distribution Type processing function. But all of its knowledge, as long as you learn ORACLE knowledge on one model, you can use it on various types of machines.

2.1 Oracle development history

• In 1978, Oracle 2 was born, it was developed using assembly language, but its appearance did not attract much attention

• In 1982, Oracle introduced Oracle 3, which was the first relational database that could run on large and minicomputers

•In 1997, Oracle Corporation launched Oracle 8 based on Java language

• In 1999, Oracle officially provided the world’s first Internet database Oracle8i

• In June 2001, Oracle Corporation released Oracle 9i

• On July 12, 2007, Oracle Corporation launched Oracle 11g. The "g" in Oracle 11g stands for "network"

2.2 Features of Oracle Database System

•Support large database, multi-user, high-performance transaction processing

• Good security

• Provides new distributed database capabilities

• Portability and compatibility

2.3 Oracle database storage structure

Insert picture description here

Data Block

Data block is the smallest unit of data management, that is, all I/O operations in Oracle are based on block. The size of the data block is an integer multiple of the block size of the operating system, the common size is 2KB or 4KB

Data Extent

The data interval is composed of physically and continuously stored blocks. It is the smallest unit of Oracle storage allocation. One or more blocks form a data interval

Data Segment

Several data intervals constitute a data segment

Table Space

In order to improve the efficiency of database server management and operation, Oracle 11g uses the virtual concept of "table space" to manage logical objects. Users can store logical objects of different natures in different table spaces.
In the Windows operating system, use folders. To classify and manage various files, the table space is equivalent to the folder of the Oracle database.

Each table space is composed of one or more data files, and a data file can only be associated with one table space

The relationship between account, table space, and table

An account can have permission to use multiple tablespaces, a tablespace can store multiple tables, and a table can only belong to one tablespace

Third, the uninstallation and installation of Oracle

3.1 Uninstallation of Oracle

1) Shut down all running Oracle services

Insert picture description here

2) Delete the corresponding registry information

Delete oracle registry information. Run regedit, delete the registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Oracle

Delete the oracle service. The location of the oracle service in the registry is:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Service
Delete all services beginning with the oracle character.

Delete the event log. The location in the registry is
HKEY_LOCAL_MACHINE\SYSTEM\CurrentContrilSet\Services\Eventlog\Application to
delete all keys beginning with the oracle character.

3) Delete the oracle environment variable in the environment variable

Delete the ORACLE environment variables, such as the "JSERV" variable and the oracle path in the Path variable

4) Delete oracle related menus and directories

  1. Delete the oracle menu.
  2. Delete the "program files\oracle" directory
  3. Restart Windows.
  4. Delete the oracle home directory.

3.2 Oracle installation

1) Unzip the installation package to the specified file (if it is the installation package downloaded from the official website, you need to unzip the two compressed files together)

Steps that are not in the screenshot below can be taken directly to the next step.

Insert picture description here

2) Click setup.exe to install

Insert picture description here

3) Follow the prompts to complete the configuration and installation of oracle

Uncheck Security Update Prompt

Insert picture description here

Choose to install server applications

Insert picture description here

Select advanced installation for detailed configuration

Insert picture description here

Select the installation location of the oracle management software, and the detailed location will be automatically generated afterwards

Insert picture description here

Modify the default character set of data stored in the database

Insert picture description here

Set a password for the default account (can be set uniformly or separately)

Insert picture description here

4) Unlock account and change password

By default, many accounts are locked and cannot be used, and need to be unlocked before they can be used

Insert picture description here

5) Use sqlplus to test whether you can connect to the database

Insert picture description here

The user enters sys as sysdba and enters the password

Insert picture description here

3.3 PLSQL Developer graphical tool installation

1) Unzip to the specified folder

Insert picture description here

2) Configure the configuration to connect to the current oracle data

Enter the specified folder to modify the .ora file

Insert picture description here

Modify the ip to the ip where the oracle database is located
and enter ipconfig in the DOS command to view the local IP

ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 10.50.6.110 )(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )

Four, the basic use of Oracle

4.1 DCL statement

4.1.1 Creation of table space

The role of table space

You can use table space to limit the size
of database files. Use table space to store data files on different disks to improve IO performance and facilitate data backup and recovery.
Create table space syntax:
CREATE TABLESPACE tablespacename
DATAFILE'filename' [SIZE integer [K| M]]
[AUTOEXTEND [OFF|ON]];

create tablespace myspace
datafile 
  'e:/my01.dbf' size 5 M,
  'd:/my02.dbf' size 10 M
-- 创建表时,为表指定表空间
 create table person
 (
    pid number ,
    pname varchar2(20) ,
    page number(3),
    birthday date,
    address varchar2(20) default '不详'
 )tablespace myspace;

4.1.2 Account creation

There can be multiple users in the same database at the same time, and each user manages its own database objects. Such as database tables, indexes, views, etc. .
The CREATE USER command in Oracle is used to create a new user. Each user has a default table space and a temporary table space. If not specified, Oracle sets SYSTEM as the default tablespace and TEMP as a temporary tablespace.

CREATE USER ahx
IDENTIFIED BY 123456
DEFAULT TABLESPACE myspace;

The newly created account is locked by default and has no connection permission, it needs to be unlocked and authorized

将指定用户解锁
alter user ahx account unlock;

4.1.3 Role authority authentication

The commonly used system predefined roles in Oracle are as follows.

CONNECT: Temporary users, especially those users who do not need to create tables, are usually given this role.
RESOURCE: More reliable and formal database users can grant this role, and can create tables, triggers, procedures, etc.
DBA: database administrator role, has the highest authority to manage the database. A user with a DBA role can revoke any other users or even other DBA permissions, which is very dangerous, so do not easily grant this role to some unimportant users.
Assign permissions or roles to users

The GRANT command is used to assign permissions or roles to users, and the REVOKE command is used to revoke permissions and roles for users

Authorization

分配权限或角色语法:GRANT [<权限> | <角色>] TO <用户>;
将指定用户授权
grant connect,dba,resource to ahx
grant create session,create table to ahx;--为用户分配权限
grant connect to ahx;--为用户分配角色
grant resource to ahx;
--grant connect,resource to ahx;--为用户分配角色

--为ahx分配查询scott账号下的emp表的权限
 -- grant select|insert|update|delete on 表 to 用户
 grant select on scott.emp to ahx
 --以xiaoming账号登录查询
 select * from scott.emp

When granting permissions, you can assign individual permissions to the specified account, or you can assign roles to directly have the permissions owned by the role

Cancel authorization

撤销权限和角色语法:REVOKE [<权限> | <角色>] FROM <用户>;

4.2 DDL statements

Since Oracle does not have the concept of a library, creating a table space is a statement for building a database. Oracle's ddl statement often refers to a statement that creates a table or modifies the columns of a table.

4.2.1 CREATE TABLE statement

Except for the data type, it has the same syntax as the MySQL table creation statement, and oracle allows check constraints (generally handed over to the server for processing)

---创建表: create table 表名(列名1 数据类型 [约束])
create table dept
(
   deptno number(6) primary key,
   deptname varchar2(50) not null,
   deptnum  number(5)  check (deptnum>=0),
   loc varchar(100) default '郑州' 
)

create table employee
(
   empId number(10) primary key,
   empName varchar2(50) not null,
   empAge number(3) check(empAge>=18 and empAge<=60),
   deptno number(6) references dept(deptno)
)

Oracle does not have the concept of primary key auto-increment. If you need primary key auto-increment, you can solve it by sequence

4.2.2 ALTER TABLE statement

Operate on the columns of an existing table

increase

语法: alter table tableName add columnName dataType;

alter table employee add empTel varchar2(11);

modify

语法: alter table tableName modify columnName dataType;

alter table employee modify empTel varchar2(14);

delete

语法: alter table tableName drop column columnName;

alter table employee drop column empTel;

Add constraints to columns

语法:alter table tableName add constraint constraintName constraintType(columnName)

alter table dept add constraint un_deptname unique(deptname);

View constraints from the view USER_CONS_COLUMNS

select constraint_name,column_name from user_cons_columns

4.2.3 Sequence

SEQUENCE is a named sequential number generator, which can generate a series of sequential integers in a serial manner

The main purpose of the sequence:

  • Primary key and foreign key value application requirements
  • Serial number application requirements
  • Sequence generation and definition content

Sequence syntax:

CREATE SEQUENCE sequnce_name
[START WITH n1] //Specify the first sequence number to be generated (starting from n1)
[INCREMENT BY n2] //Use to specify the interval between sequence numbers, the default value is 1
[{MAXVALUE n3 | NOMAXVALUE}] //Specify the maximum value that the sequence can generate
[{MINVALUE n4 | NOMINVALUE}] //Specify the minimum value that the sequence can generate
[{CACHE n5 | NOCACHE}] //Use to specify the pre-allocation in the cache The number of serial numbers, the default is 20
[{CYCLE | NOCYCLE}] //Used to specify whether to cycle after reaching the maximum or minimum of the sequence
[ORDER]; //Used to specify to generate serial numbers in order to ensure that the sequence is unique And orderly

create sequence ms
   start with 1
   increment by 1
   maxvalue 5
   minvalue 1
   nocycle
   cache 10;

use

Get the value of the next sequence through nextval

select ms.nextval from dual;

Get the value of the current sequence through currval

select ms.currval from dual;

Use of sequence

--创建序列
create sequence ms
start with 1  --从1开始
increment by 1 --序列的间隔(每次变化的数值)
maxvalue 10000000 --最大值
minvalue 1   --最小值
nocycle --不循环
cache 10 

 select * from dual
 select my_seq.nextval from dual
 select my_seq.currval from dual
   
 select * from employee
   --Oracle中没有主键自增的概念,但可以通过自定义序列来实现
   insert into employee(empId,empName,empAge,deptno) values(ms.nextval,'zhangsan',20,10)

Modify sequence

#更改序列
ALTER SEQUENCE ms MAXVALUE 5000 CYCLE;
#删除序列
DROP SEQUENCE ms;

4.3 DML statement

The basic oracle statement uses sql, so it is basically the same as the dml statement used when learning mysql

4.3.1 insert increase

insert into 表名(列名1,列名2,列名3)values(1,2 ,3)

Since there is no primary key self-increment, the sequence is used for data id generation when adding

4.3.2 update

update 表名 set 列名1=1, 列名2=2[where 条件]

4.3.3 delete delete

delete from 表名 [where 条件]

Basic query statement exercise

--使用scott账号练习基本查询语句
--1 查询当前用户下的所有表
select * from tab;
--2 查询雇员表中所有信息
select * from emp;
--3 查询雇员编号,姓名,工作,工资
select empno,ename,job,sal from emp
--4 查询雇员编号,姓名,工作,工资,并显示中文(为列起别名)
select empno as 编号,ename as 姓名,job as 工作,sal as 工资 from emp
--5 消除重复列,查询雇员工作种类
select distinct job from emp
--6 字符串连接操作(||)
--查询雇员编号,姓名,工作.按以下格工显示:编号:7369,姓名:Smith,工作:Clerk
select '编号:'||empno,'姓名:'||ename,'工作:'||job from emp
--7 查询列支持四则运算(年薪=(工资+奖金)*12)
--查询雇员编号,姓名,工作,年薪 
select empno,ename,job,(sal+nvl(comm,0))*12 from emp
nvl(comm,0)==>如果comm值为空,取值0

--8 Where条件查询
-- 查询工资大于1500的所有雇员
select * from emp where sal>1500
--查询可以得到奖金的所有雇员
select * from emp where comm is not null
--查询工资大于1500或可以得到奖金的雇员
select * from emp where sal>1500 or comm is not null
--查询工资大于1500并且可以领取奖金的雇员
select * from emp where sal>1500 and comm is not null
--查询工资不大于1500或者不可以领取奖金的雇员
select * from emp where sal<=1500 or comm is null
--查询工资在1500到3000的所有雇员信息
select * from emp where sal>=1500 and sal<=3000
select * from emp where sal between 1500 and 3000
--查询在1981年雇用的员工信息
select * from emp where hiredate like '%81%'
--查询雇员姓名中第二个字母为"M"的雇员
select * from emp where ename like '_M%'
--查询雇员工资中带8这个数字的
select * from emp where sal like '%8%'
--查询编号是7369,7499,7521,7799的雇员信息
select * from emp where empno=7369 or empno=7499 or empno=7521 or empno=7799
select * from emp where empno in(7369,7499,7521,7799)
--查询雇员编号不是7369,7499,7521,7799的所有雇员信息
select * from emp where empno not in(7369,7499,7521,7799)
--查询雇员编号为7369的雇员信息
select * from emp where empno =7369
--查询雇员编号不为7369的雇员信息
select * from emp where empno !=7369
select * from emp where empno <>7369

--查询雇员信息,按工资由低到高排序
select * from emp order by sal asc
--查询雇员信息,按工资由高到低排序
select * from emp order by sal desc

--操作集合:
--union:将两个记录合并,去掉重复项 
select distinct deptno from emp union select deptno from dept;
--union:将两个记录合并,不去掉重复项 
select distinct deptno from emp union all select deptno from dept;
--intersect:取两个集合的交集
select distinct deptno from emp intersect select deptno from dept;
--minus:去掉交集(集合A-(集合A和集合B的交集))
select deptno from dept minus select distinct deptno from emp ;

Guess you like

Origin blog.csdn.net/qq_53449032/article/details/115298607