用來產生主鍵的值(本身控制主鍵值,很容易重複,用sequence能夠自動增加產生主鍵值)sql
序列能夠產生1.0*10^38個數數據庫
create sequence 序列名; (和創建表同樣)緩存
用序列放入的主鍵值絕對不會重複oracle
序列名。nextval(使用序列值的下一個數) 3d
序列名。currval(使用當前序列數,基本不會用到)blog
一張表只能用一個序列,不能用兩個序列,不然可能重複排序
能夠兩張表用一個序列,可是這樣數會比較亂索引
(1) 建立表(以前要先刪表)內存
create table testseq( id number primary key, name varchar2(30), salary number, );
(2) 建立序列rem
create sequence testseq_id;
(3) 使用序列,向表中插入數據
insert into testseq values( testseq_id, nextval, 'test'|| testseq_id.currval, 1000*testseq_id.currval, );
drop sequence 序列名 ;(和刪除表同樣,不存在就說刪不到)
drop sequence testseq_id;
drop sequence testseq_id;
加速查詢速度
底層經過樹狀結構組織數據,消耗了大量的空間和時間來加速查詢。消耗的是創建索引的時間,一旦索引創建完畢,查詢就很快。組織數據的樹狀結構消耗的空間很大。
索引就像書的目錄,正常的查找數據像在書中一頁一頁查詢數據量越大,索引的優點越大
ps:set timing on 如此設置後就會統計每條操做的時間
假設有3億條數據,要查詢其中一條數據:
不用索引:
用oracle(配置爲2G內存,i3處理器時)
查詢一條數據大概須要60*8=480s
用索引:0.01s
對惟一性的字段,系統會自動創建索引,叫惟一性索引
非惟一性索引創建:在表的某個字段上建立索引
正經常使用法:
create index 索引名 on 表名(要建立索引的字段名);
快速建表(非正經常使用法)
create table 表名 as select id,first_name name,salary from 查詢的表名
(即建的表的數據和查詢的數據同樣)
drop index 索引名;
數據庫腳本(xxx.sql)裏是怎樣寫的?(大小寫不敏感)
drop sequence 序列名;
drop table 表名 cascade constraint /* 解除關聯*/ ;
create sequence 序列名 屬性;
create table 表名(字段以及約束);
insert into 表名 values( 匹配表的要求的要增長的數據 );
commit ;
alter table 子表名 add constraint 子表名_加外鍵的字段名_fk foreign key(加外鍵的字段名)reference(父表名_被加外鍵的字段名);
Prompt table and sequences created and populated.set feedback on