oracle學習總結3

來源:互聯網
上載者:User

標籤:

1:ddl語句
建立一張學生表

create table stu(
id number(6) not null,
name varchar2(20),
age number(3),
gender char(1) default ‘m‘,
grade number(1),
class varchar2(20) constraint stu_class_fk foreign key references class(id),
email varchar2(50)
);

create table class(
id number(4),
name varchar2(30),
constraint class_id_pk primary key(id)
);

default ‘m‘ 該欄位預設是m,如果insert語句沒有向該欄位插入值,那麼預設是m


2:約束類型五種:
not null 非空約束,就是在向表中插入資料時,必須給值。
unique 唯一性限制式,插入資料時,該欄位的值不能夠重複
主鍵 primary key:非空約束,唯一性限制式,邏輯上面有意義。
外鍵 foreign key:參考另外一張表的某個欄位(該欄位必須是主鍵),插入資料時,必須在被參考欄位資料範圍內。
如果被參考欄位資料不存在,則插不進去。
被參考欄位所在的表不能別刪除,在參考表沒有被刪除前。
check

註:主鍵概念:能夠唯一表示一行記錄的欄位

3:表級約束和欄位級約束

欄位級約束,只能作用於一個欄位:
create table stu(
id number(6) constraint stu_id_uni unique, --給id欄位唯一性限制式
name varchar2(20)
);

表級約束,如果是兩個欄位組合約束:
create table stu(
id number(6),
name varchar2(20),
email varchar2(50),
constraint stu_uni unique(name,email) --給name和email這兩個欄位組合加約束
);


4:修改表結構 alter
--向表中添加一個欄位
alter table stu add (addr varchar2(20));
--修改表中的欄位
alter table stu modify (addr varchar2(50));
--刪除表中的欄位
alter table stu drop (addr);


5:修改表中的約束 constraint
alter table stu drop constraint stu_class_fk; --刪除表中的外鍵約束
alter table stu add constraint stu_class_fk foreign key(class) references class(id);


6:修改表的名字
rename old_name to new_name;

 

7:oracle,資料字典表,目前使用者下的所有表都在dictionary中
select * from dictionary;
只有兩個欄位:table_name comments


--使用者表user_tables,該使用者下所有的表都在這個表中
--使用者視圖user_view
--使用者索引user_indexes
--使用者約束user_constraint


8:建立索引index
索引可以提高查詢效率,但是插入資料時效率降低,因為在插入資料的同時還要插入索引表
在給欄位設定主鍵或者唯一性限制式時,索引會自動建立。
create index idx_stu_id on stu(id); --建立索引
drop index idx_stu_id; --刪除索引

缺點:
1:不輕易建索引,建大量索引會佔用資料表空間。
2:降低修改資料的效率,插入資料時還要插入索引表。


9:視圖view 虛表
概念:就是一張虛表,一條select語句、

create view v$_stu as select * from stu; --建立一張視圖
drop view v$_stu; --刪除一張視圖

優點:
1:方便查詢,簡化查詢。
2:保護表中相關資料不被查看,保護隱私。

不足:
維護成本升高,如果修改原來的表結構,那麼視圖中的資料也要做相應的修改。
修改視圖中的資料,實際上修改的就是原表中的資料。


10:序列
create sequence seq1;
insert into stu values(seq1.nextval,‘‘,‘‘);--插入索引,一般給主鍵添加值

create sequence seq2 start with 1 increment by 1; --從1開始,步長為1

mysql:auto increment;

 

oracle學習總結3

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.