oracle入門必備

來源:互聯網
上載者:User

標籤:

//、、、、、、、、、、、、、、、、建立資料表空間 \  賦予角色  \ 建立資料表  \  插入資料  \  建立序列  \ 添加註釋   、、、、、、、、、、、、、、、、、、、、、、、、、、、

--建立資料表空間  
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入門必備

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.