標籤:
//、、、、、、、、、、、、、、、、建立資料表空間 \ 賦予角色 \ 建立資料表 \ 插入資料 \ 建立序列 \ 添加註釋 、、、、、、、、、、、、、、、、、、、、、、、、、、、
--建立資料表空間
create tablespace new_tabspace
datafile ‘E:\File_Orc\File\A.DBF‘
size 100m
create tablespace haha
datafile ‘E:\File_Orc\File\B.DBF‘
size 50m
--刪除空間並
刪除物理檔案
drop tablespace haha including contents and datafiles
--建立使用者
create user zhangsan
identified by 123
default tablespace new_tabspace
--給使用者賦予許可權
grant connect,resource to zhangsan
grant dba to zhangsan
--建立資料表
--主人表
create table master(
id number(5) not null primary key,
name nvarchar2(50) not null
)
--插入資料
insert into master values(1,‘aa‘)
insert into master values(2,‘bb‘)
select * from master
--刪除所插入的資料
delete from master
--建立序列
create sequence master_seq
start with 1 --從1開始
increment by 1 --每次增加1
nomaxvalue --無最大值
cache 10 --每次增長10
--插入資料
insert into master values(master_seq.nextval,‘張三‘)
insert into master values(master_seq.nextval,‘李四‘)
--查看序列的當前值和下一個值
select master_seq.currval from dual
select master_seq.nextval from dual
--給主人表添加註釋
comment on table master is ‘寵物‘
comment on column master.id is ‘主人ID‘
//、、、、、、、、、、、、、、、、、、、 給表添加約束 、、、、、、、、、、、、、、、
oracle建資料表條件約束主要有以下幾大類:
NN:NOT NULL 非空約束
UK:UNIQUE KEY 唯一約束
PK:PRIMARY KEY 主鍵約束
FK:FOREIGN KEY 外鍵約束
CK:CHECK 條件約束
//建立表的時候添加約束
create table pet(
id number primary key, --主鍵約束
usrername nvarchar2(50) not null, --非空約束
email varchar2(30) unique, --唯一約束
sal number(5) check(sal>1500), --核查約束
status char(1) default 1 not null, --check約束
master_ID number(5) references pet_type(id) --外鍵約束
)
--一些其它的相關操作
--修改資料表中某個欄位的約束(以唯一約束來說)
alter table pet add constraint UN_name unique(username)
--添加列
alter table 表名 add 新列列名 列資料類型 [default 0 not null] (添加列預設值為0)
--刪除列
alter table 表名 drop 列名
--修改列
alter table 表名 alter column 列名 新添加的資料類型 (修改列)
oracle入門必備