1、docker 安裝oracle11g步驟:
1:引入oracle源
docker pull oracleinanutshell/oracle-xe-11g
2:開始建立容器
docker run -h "oraclehost" --name "oracle" -d -p 9090:8080 -p 1521:1521 oracleinanutshell/oracle-xe-11g
3: docker ps
4:在終端中進入此容器
docker exec -it 94910105eb6f /bin/bash
5:使用普通身份登入
sqlplus system/oracle
6:建立使用者并配置設定權限
6.1 檢視使用者
select username,password from dba_users;
6.2 建立使用者:使用者名為 oracle;密碼為:oracle
create user oracle identified by oracle;
6.3 檢視是否有此使用者 oracle
select * from all_users;
6.4 給使用者賦予connect 和 resource 角色(connect: 保證資料庫可以連結;resource:該使用者可以使用資料庫的資源;create session:會話的權利)
grant connect,resource,create session to oracle;
6.5 建立表格添加,更改使用者表空間配額
grant unlimited tablespace to oracle;
6.6 删除使用者
drop user oracle;
7:開啟docker oracle服務
docker start oracle
2、oracle學習筆記01
192.168.52.135
三大部分
1.使用者與表空間
2.表與限制
3.查詢語句
全局資料庫名:orcl
密碼:root
圖形化管理工具 sql-plus
兩個使用者都可以使用:sys/system
sys as sysdba 使用管理者的身份登入資料庫
sql-plus
寫sql語句必須要分号結尾 但是指令不需要加;友善的話,統一使用分号。
清屏:
若在dos的視窗下sql/plus就要用:host cls 或是clear screen
show linesize : 檢視目前設定的sqlplus輸出的最大行寬
set linesize : 設定sqlplus輸出的最大行寬
使用者與表空間
使用者
登入 sql-plus
系統使用者:
四個系統使用者:
sys,system
權限 sys > system
sys登入必須使用 管理者 或者 系統操作人員權限 登入
sysman
作業系統管理器的人員,也是管理者級别的使用者
scott password: tiger (default)
oracle 創始人的一員
使用系統使用者登入:
使用system使用者登入
[username/password][@server][as sysdba][sysoper]
--- system/root @orcl as sysdba
--- system/root #本地直接
--- connect/conn sys/root #error ORA-28009: connection as SYS should be as SYSDBA or SYSOPER
--- connect/conn sys/root as sysdba
orcl就是自己設定的服務名
檢視登入使用者
--- show user; 檢視目前使用者
dba_users 資料字典 和普通的表類似
--- desc dba_users; #檢視資料字典屬性資訊
啟用scott使用者
預設被鎖定了
需要解鎖 使用該使用者
alter user username account unlock;
修改登入後 需更新密碼: root
表空間
表空間概述
了解表空間
資料庫與表空間
表空間實際上是資料庫的邏輯存儲空間,表空間可以了解為在資料庫當中開辟一個空間用來存放資料庫對象。
一個資料庫可以有多個表空間構成。
oracle很多優化都是與表空間實作的。
表空間與資料檔案
表空間有一個或多個資料檔案構成,大小可以由使用者自己定義。
我們建的表,資料庫當中對象都是存儲在表空間的資料檔案當中。
表空間的分類
永久表空間
資料庫當中永久存儲的資料,例如:資料庫表,視圖,存儲過程等
臨時表空間
存儲資料庫操作過程當中中間執行的過程,執行完畢自動會被釋放掉
UNDO表空間
儲存事務所修改的舊值,被修改之前的資料,實作復原操作
檢視使用者的表空間
利用4個資料字典
dba_tablespaces 系統管理者檢視的
user_tablespaces 普通使用者檢視的
dba_users 系統管理者檢視的 檢視使用者預設和臨時表空間
user_users 普通使用者檢視的 檢視使用者預設和臨時表空間
--- desc dba_tablespaces
--- select TABLESPACE_NAME from dba_tablespaces; #6個系統預設的
SYSTEM 存放sys使用者的表,視圖,存儲過程等資料庫對象,系統表空間
SYSAUX 作為Example表空間的輔助表空間
UNDOTBS1 存儲一些撤銷資訊的表空間 belong to UNDO表空間
TEMP 存儲sql語句處理的表和索引資訊的臨時表空間
USERS 存儲使用者建立的資料庫對象的永久表空間,和SYSTEM類似
EXAMPLE 用于安裝oracle11g事例的表空間
--- select TABLESPACE_NAME from user_tablespaces; #6個系統預設的
SYSTEM 存放sys使用者的表,視圖,存儲過程等資料庫對象,系統表空間
SYSAUX 作為Example表空間的輔助表空間
UNDOTBS1 存儲一些撤銷資訊的表空間 belong to UNDO表空間
TEMP 存儲sql語句處理的表和索引資訊的臨時表空間
USERS 存儲使用者建立的資料庫對象的永久表空間,和SYSTEM類似
EXAMPLE 用于安裝oracle11g事例的表空間
--- select DEFAULT_TABLESPACE from dba_users;
--- select TEMPORARY_TABLESPACE from dba_users;
#查詢system使用者的預設表空間
#查詢system使用者的臨時表空間 臨時表空間(預設是TEMP,可以自己建立)
--- select default_tablespace,temporary_tablespace from dba_users where username = 'SYSTEM'; (SYSTEM,TEMP)
--- select default_tablespace,temporary_tablespace from dba_users where username = 'SCOTT'; (USERS,TEMP)
設定使用者的預設或臨時表空間
alter user username
default|temporary TABLESPACE tablespace_name;
--- alter user system default tablespace users; #将使用者system的預設表空間修改為users
建立,修改,删除表空間
建立
create [temporary] tablespace tablespace_name
tempfile|datafile 'xx.dbf' size xx # datafile 就是表空間資料檔案的名字 [tablespace,tempfile]比對
--- create tablespace test1_ts datafile 'test1file.dbf' size 10m;
--- create temporary tablespace temptest1_ts tempfile 'tempfile.dbf' size 10m;
--- desc dba_data_files 檢視永久表空間的檔案資訊
--- select file_name from dba_data_files where tablespace_name='TEST1_TS';
C:\APP\ZHENGSHENG.CHEN\PRODUCT\11.2.0\DBHOME_1\DATABASE\TEST1FILE.DBF
--- desc dba_temp_files 檢視臨時表空間的檔案資訊
--- select file_name from dba_temp_files where tablespace_name='TEMPTEST1_TS';
C:\APP\ZHENGSHENG.CHEN\PRODUCT\11.2.0\DBHOME_1\DATABASE\TEMPFILE.DBF
修改(永久的)
修改表空間的狀态
設定聯機或脫機的狀态
alter tablespace tablespace_name online|offline;
建立完表空間預設是聯機狀态
--- alter tablespace test1_ts offline
--- select status from dba_tablespaces where tablespace_name = 'TEST1_TS';
--- alter tablespace test1_ts online
設定隻讀或可讀寫狀态(必須是聯機狀态)
alter tablespace tablespace_name read only|read write
預設表空間是 read write狀态
--- alter tablespace test1_ts read only;
--- select status from dba_tablespaces where tablespace_name = 'TEST1_TS'; #read only
--- alter tablespace test1_ts read write;
--- select status from dba_tablespaces where tablespace_name = 'TEST1_TS'; #online
修改資料檔案
增加資料檔案
alter tablespace tablespace_name
add datafile 'xx.dbf' size xx;
--- alter tablespace test1_ts add datafile 'test2_file.dbf' size 10m;
--- select file_name from dba_data_files where tablespace_name='TEST1_TS';
C:\APP\ZHENGSHENG.CHEN\PRODUCT\11.2.0\DBHOME_1\DATABASE\TEST1FILE.DBF
C:\APP\ZHENGSHENG.CHEN\PRODUCT\11.2.0\DBHOME_1\DATABASE\TEST2_FILE.DBF
删除資料檔案
alter tablespace tablespace_name
drop datafile 'filename.dbf';
不能删除表空間中第一個資料檔案,如果要删除的話,我們需要把整個表空間删掉
--- alter tablespace test1_ts drop datafile 'test2_file.dbf';
删除表空間
drop tablespace tablespace_name [including contents](括号加上同時删除資料檔案)
--- drop tablespace test1_ts including contents;
表與限制
表存儲在表空間當中
存儲資料的基本存儲機關
二維結構
行和列
資料類型
日期型
DATE(經常使用)
範圍:公元前4712/1/1--公元9999/12/31
可以精确到秒
TIMESTAMP
字元型
固定長度
char(n) max(n) = 2000
nchar(n) 按照unicode max(n) == 1000 存儲漢字比較多
可變長度
varchar2(n) max(n) = 4000
nvarchar2(n) 按照unicode max(n) == 2000 存儲漢字比較多
數值型
number(p,s) p:有效數字 s:小數點後的位數 經常使用
number(5,2) 有效數字五位,小數兩位
float(n) 存儲二進制數,二進制數的位數[1,126],轉換為十進制數需要乘以0.30103才可以得到
其他類型(大對象)
blob 最大存儲4G的資料 二進制形式存放
clob 最大存儲4G的資料 字元串形式存放
管理表
建立表
同一個使用者下表名唯一
create table table_name(
column_name datatype,
...
)
demo:
建立使用者資訊表(所需字段 字段的類型)
編号 使用者名 密碼 郵箱 注冊時間
create table userinfo(
id number(6,0),
username varchar2(20),
userpwd varchar2(20),
email varchar2(30),
regdate date
)
--- desc userinfo;
修改表(結構)
添加字段
alter table table_name
add column_name datatype;
--- alter table userinfo add remarks varchar2(500);
更改字段資料類型
alter table table_name
modify column_name datatype;
--- alter table userinfo modify remarks varchar2(400); #更改字段長度
--- alter table userinfo modify userpwd number(6,0); #更改資料類型
删除字段
alter table table_name
drop column column_name;
--- alter table userinfo drop column remarks;
修改字段名
alter table table_name
rename column old_column to new_column
--- alter table userinfo rename column email to new_email;
修改表名
rename table_name to new_table_name
--- rename userinfo to user_info
删除表
truncate table table_name #截斷表,删除表中所有資料,速度比delete快
drop table table_name 删除表結構
操作表的資料
添加資料
insert 語句
insert into table_name(column1,column2,...)
values (value1,value2,...)
操作執行個體
向表中所有字段添加值
--- insert into userinfo values(1,'國中生','123','[email protected]',sysdate);
向表中指定字段添加值
--- insert into userinfo(id,username,userpwd) values(2,'yyy','123');
向表中添加預設值
create table user_info(
id number(6,0),
regdate date default sysdate
);
--- insert into user_info(id) values(1)
--- alter table userinfo modify email default '無';
--- insert into userinfo(id) values(3);
--- insert into userinfo(id,email) values(4,'aaa')
複制表資料
在建表的時候複制
create table table_new
as
select column1,...|* from table_old;
--- create table usercopy as select id,username,userpwd from userinfo;
在添加時複制
insert into table_new
[(column1,...)]
select column1,...|* from table_old;
--- insert into usercopy select id,username,userpwd from userinfo;
--- insert into usercopy(id,username) select id,username from userinfo;
修改資料
update 語句
update table_name set column1=value1,... [where conditions]
删除資料
delete * from table_name [where conditions]
限制
限制作用
定義規則
確定完整性
非空限制
建立表時設定非空限制
create table table_name(
column_name datatype not null,
...
)
修改表時添加非空限制 (必須保證表屬性中無空資料)
alter table table_name
modify column_name datatype not null;
修改表時去除非空限制
alter table table_name
modify column_name datatype null;
主鍵限制
非空 唯一
一張表隻能設計一個主鍵限制
主鍵限制可以由多個字段構成(聯合主鍵或複合主鍵)
1.在建立表時設定主鍵限制
單一主鍵
create table table_name(
column_name datatype primary key,
...
);
聯合主鍵
create table userforce_pk(
id number(6,0),
username varchar2(20),
userpwd varchar2(20),
constraint pk_id_username primary key(id,username)
);
查找限制 字典:desc user_constraints
---
2.在修改表時添加主鍵限制
alter table table_name
add constraint constraint_name
primary key(column_name1,..);
--- alter table userinfo add constraint pk_id primary key(id);
3.更改限制的名稱
alter table table_name
rename constraint old_name to new_name
--- alter table userinfo rename constraint pk_id to id_pk;
4.删除主鍵限制
alter table table_name
disable|enable|drop constraint constraint_name;
drop primary key[CASCADE] #針對外鍵限制
--- alter table userinfo disable constraint id_pk;
--- 檢視限制狀态
--- select constraint_name,status from user_constraints where table_name = 'USERINFO';
外鍵限制
在建立表時設定外鍵限制
create table table1
(
column_name datatype REFERENCES table2(column_name),
...
);
# table1:從表 table2:主表
# 設定外鍵限制時,主表的字段必須是主鍵
# 主從表中相應的字段必須是同一個資料類型
# 從表中外鍵字段的值必須來自主表中的相應字段的值,或者為null值
--- create table C(id number(2,0) references A(id));
constraint constraint_name FOREIGN KEY(column_name) references table_name(column_name)[on delete cascade]
[on delete cascade] 級聯删除 主表當中一條資料被删除之後,從表當中使用了這條資料的字段所在的行也會被删除
--- create table C(
id number(2,0),
constraint fk_id foreign key(id) references A(id)
);
在修改表時添加外鍵限制
add constraint constraint_name FOREIGN KEY(column_name) references table_name(column_name)[on delete cascade]
--- alter table E add constraint fk_id_e foreign key(id) references A(id);
删除外鍵限制
alter table table_name
disable|enable|drop constraint constraint_name;
--- alter table E disable constraint fk_id_e;
--- alter table E drop constraint fk_id_e;
唯一限制
保證字段值的唯一性
唯一限制 vs 主鍵限制
1.主鍵字段值必須是非空的
2.唯一限制允許有一個空值
3.主鍵在每張表隻有一個,唯一限制可以有多個
在建立表時設定唯一限制
列級 vs 表級
create table table_name (
column_name datatype unique,
...
)
create table userinfo_A(
id varchar2(10) primary key,
username varchar2(20) unique,
userpwd varchar2(20)
);
表級:
constraint constraint_name unique(column_name)
在修改表時添加唯一限制
add constraint constraint_name unique(column_name)
删除唯一限制
alter table table_name
disable|enable|drop constraint constraint_name;
檢查限制
create table table_name
(
column_name datatype CHECK(expressions),
...
)
--- create table T_A (id number(2,0) check(id>10));
建立表時設定檢查限制
constraint constraint_name check(expressions)
在修改表的時候添加檢查限制
alter table table_name
add constraint constraint_name check(expressions);
删除檢查限制
alter table table_name
disable|enable|drop constraint constraint_name;