午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    Oracle的約束和索引
    來源:易賢網(wǎng) 閱讀:954 次 日期:2014-08-25 11:02:42
    溫馨提示:易賢網(wǎng)小編為您整理了“Oracle的約束和索引”,方便廣大網(wǎng)友查閱!

    Oracle的約束

    * 如果某個約束只作用于單獨(dú)的字段,即可以在字段級定義約束,也可以在表級定義約束,但如果某個約束作用于多個字段,

    必須在表級定義約束

    * 在定義約束時可以通過CONSTRAINT關(guān)鍵字為約束命名,如果沒有指定,ORACLE將自動為約束建立默認(rèn)的名稱

    定義primary key約束(單個字段)

    create table employees (empno number(5) primary key,...)

    指定約束名

    create table employees (empno number(5) constraint emp_pk primary key,...)

    定義primary key約束(多個字段,在表級定義約束)

    create table employees

    (empno number(5),

    deptno number(3) not null,

    constraint emp_pk primary key(empno,deptno)

    using index tablespace indx

    storage (initial 64K

    next 64K

    )

    )

    ORACLE自動會為具有PRIMARY KEY約束的字段(主碼字段)建立一個唯一索引和一個NOT NULL約束,定義PRIMARY KEY約束時可以為它的索引

    指定存儲位置和存儲參數(shù)

    alter table employees add primary key (empno)

    alter table employees add constraint emp_pk primary key (empno)

    alter table employees add constraint emp_pk primary key (empno,deptno)

    not null約束(只能在字段級定義NOT NULL約束,在同一個表中可以定義多個NOT NULL約束)

    alter table employees modify deptno not null/null

    unique約束

    create table employees

    ( empno number(5),

    ename varchar2(15),

    phone varchar2(15),

    email varchar2(30) unique,

    deptno number(3) not null,

    constraint emp_ename_phone_uk unique (ename,phone)

    )

    alter table employees

    add constraint emp_uk unique(ename,phone)

    using index tablespace indx

    定義了UNIQUE約束的字段中不能包含重復(fù)值,可以為一個或多個字段定義UNIQUE約束,因此,UNIQUE即可以在字段級也可以在表級定義,

    在UNIQUED約束的字段上可以包含空值.

    foreign key約束

    * 定義為FOREIGN KEY約束的字段中只能包含相應(yīng)的其它表中的引用碼字段的值或者NULL值

    * 可以為一個或者多個字段的組合定義FOREIGN KEY約束

    * 定義了FOREIGN KEY約束的外部碼字段和相應(yīng)的引用碼字段可以存在于同一個表中,這種情況稱為"自引用"

    * 對同一個字段可以同時定義FOREIGN KEY約束和NOT NULL約束

     定義了FOREIGN KEY約束的字段稱為"外部碼字段",被FORGIEN KEY約束引用的字段稱為"引用碼字段",引用碼必須是主碼或唯一碼,包含外部碼的表稱為子表,

    包含引用碼的表稱為父表.

    A:

    create table employees

    (.....,

    deptno number(3) NOT NULL,

    constraint emp_deptno_fk foreign key (deptno)

    references dept (deptno)

    )

    如果子表中的外部碼與主表中的引用碼具有相同的名稱,可以寫成:

    B:

    create table employees

    (.....,

    deptno number(3) NOT NULL

    constraint emp_deptno_fk references dept

    )

    注意:

    上面的例子(B)中not null后面沒有加逗號,因為這一句的contraint是跟在那一列deptno后面的,屬于列定義,所以都無需指明列。而A例中的是表定義,需要指明那一列,所以要加逗號,不能在列后面定義,還可以寫成:

    create table employees

    (empno char(4),

    deptno char(2)not null constraint emp_deptno_fk references dept,

    ename varchar2(10)

    )

    表定義contraint的只能寫在最后,再看兩個例子:

    create table employees

    (empno number(5),

    ename varchar2(10),

    deptno char(2) not null constraint emp_deptno_fk references dept,

    constraint emp_pk primary key(empno,ename)

    )

    create table employees

    ( empno number(5),

    ename varchar2(15),

    phone varchar2(15),

    email varchar2(30) unique,

    deptno number(3) not null,

    constraint emp_pk primary key(empno,ename),

    constraint emp_phone_uk unique (phone)

    )

    添加foreign key約束(多字段/表級)

    alter table employees

    add constraint emp_jobs_fk foreign key (job,deptno)

    references jobs (jobid,deptno)

    on delete cascade

    更改foreign key約束定義的引用行為(delete cascade/delete set null/delete no action),默認(rèn)是delete on action

    引用行為(當(dāng)主表中一條記錄被刪除時,確定如何處理字表中的外部碼字段):

    delete cascade : 刪除子表中所有的相關(guān)記錄

    delete set null : 將所有相關(guān)記錄的外部碼字段值設(shè)置為NULL

    delete no action: 不做任何操作

    先刪除原來的外鍵約束,再添加約束

    ALTER TABLE employees DROP CONSTRAINT emp_deptno_fk;

    ALTER TABLE employees ADD CONSTRAINT emp_deptno_fk FOREIGN KEY(deptno) REFERENCES dept(deptno) ON DELETE CASCADE;

     check約束

    * 在CHECK約束的表達(dá)式中必須引用到表中的一個或多個字段,并且表達(dá)式的計算結(jié)果必須是一個布爾值

    * 可以在表級或字段級定義

    * 對同一個字段可以定義多個CHECK約束,同時也可以定義NOT NULL約束

    create table employees

    (sal number(7,2)

    constraint emp_sal_ck1 check (sal > 0)

    )

    alter table employees

    add constraint emp_sal_ck2 check (sal < 20000)

    刪除約束

    alter table dept drop unique (dname,loc) --指定約束的定義內(nèi)容

    alter table dept drop constraint dept_dname_loc_uk --指定約束名

    刪除約束時,默認(rèn)將同時刪除約束所對應(yīng)的索引,如果要保留索引,用KEEP INDEX關(guān)鍵字

    alter table employees drop primary key keep index

    如果要刪除的約束正在被其它約束引用,通過ALTER TABLE..DROP語句中指定CASCADE關(guān)鍵字能夠同時刪除引用它的約束

    利用下面的語句在刪除DEPT表中的PRIMARY KEY約束時,同時將刪除其它表中引用這個約束的FOREIGN KEY約束:

    alter table dept drop primary key cascade

    禁用/激活約束(禁用/激活約束會引起刪除和重建索引的操作)

    alter table employees disable/enable unique email

    alter table employees disable/enable constraint emp_ename_pk

    alter tabel employees modify constraint emp_pk disable/enable

    alter tabel employees modify constraint emp_ename_phone_uk disable/enable

    如果有FOREIGN KEY約束正在引用UNIQUE或PRIMARY KEY約束,則無法禁用這些UNIQUE或PRIMARY KEY約束,

    這時可以先禁用FOREIGN KEY約束,然后再禁用UNIQUE或PRIMARY KEY約束;或者可以在ALTER TABLE...DISABLE

    語句中指定CASCADE關(guān)鍵字,這樣將在禁用UNIQUE或PRIMARY KEY約束的同時禁用那些引用它們的FOREIGN KEY約束,如:

    alter table employees disable primary key cascade

    約束數(shù)據(jù)字典

    all_constraints/dba_constraints/user_constraints 約束的基本信息,包括約束的名稱,類型,狀態(tài)

    (約束類型:C(CHECK約束),P(主碼約束),R(外部碼約束),U(唯一碼約束))

    all_cons_columns/dba/user 約束對應(yīng)的字段信息

    更多信息請查看IT技術(shù)專欄

    更多信息請查看數(shù)據(jù)庫
    易賢網(wǎng)手機(jī)網(wǎng)站地址:Oracle的約束和索引
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機(jī)號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報警專用圖標(biāo)