Oracle數(shù)據(jù)庫(kù)的字段約束創(chuàng)建和維護(hù)示例
創(chuàng)建Oracle數(shù)據(jù)庫(kù)的字段約束:
- 非空約束
- 唯一約束
- 對(duì)字段的取值的約束
- 默認(rèn)值
- 外鍵約束
create table tab_class( class_id number primary key, class_name varchar2(10) not null unique );
create table tab_stu( stu_id number, --學(xué)生姓名,不能為空,不能重復(fù) stu_name varchar2(20) not null unique, --學(xué)生姓名只能是male或female stu_gender varchar2(6) not null check(stu_gender='male' or stu_gender='female'), --學(xué)生年齡只能在18到60之間 stu_age number check(stu_age >18 and stu_age <60), --郵箱可以不填寫(xiě),填寫(xiě)的話不能相同 stu_email varchar2(30) unique, stu_address varchar2(30), --外鍵約束 class_id number not null references tab_class(class_id) );
維護(hù)已經(jīng)創(chuàng)建好的約束:
- 可添加或刪除約束,但不能直接修改。
- 可使約束啟用和禁用。
- 非空約束必須使用MODIFY子句增加。
- 為表增加主鍵約束:
--維護(hù)約束 --創(chuàng)建約束 create table tab_check( che_id number, che_name varchar2(20) ); --為表增加主鍵約束 alter table tab_check add constraints tab_check primary key(che_id);
添加唯一約束
--添加唯一約束,tab_check_unique表示約束的名稱 alter table tab_check add constraints tab_check_unique unique(che_name);
添加檢查約束:
--添加一個(gè)字段 alter table tab_check add che_age number; --添加檢查約束 alter table tab_check add constraints tab_check_age check(che_age>18 and che_age<60);
刪除約束:
--刪除主鍵約束 alter table tab_check drop constraints tab_check;
禁用約束:
--禁用約束 alter table tab_check disable constraints tab_check;
啟用約束
--啟用約束 alter table tab_check enable constraints tab_check;
復(fù)合約束,聯(lián)合主鍵,也就是兩個(gè)字段的組合成一個(gè)主鍵
--聯(lián)合主鍵 create table tab_person( tab_firstname varchar2(10), tab_lastname varchar2(10), tab_gender varchar2(5), primary key(tab_firstname,tab_lastname) );
為表添加外鍵約束:
alter table tab_stu add constraints tab_stu foreign key(class_id) references tab_class(class_id);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
oracle數(shù)據(jù)庫(kù)中選擇桌面類和服務(wù)器類的區(qū)別詳解
oracle就是ORACLE公司的數(shù)據(jù)庫(kù)產(chǎn)品,以高性能著稱,下面這篇文章主要給大家介紹了關(guān)于oracle數(shù)據(jù)庫(kù)中選擇桌面類和服務(wù)器類區(qū)別的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05關(guān)于使用PLSQL Developer時(shí)出現(xiàn)報(bào)錯(cuò)ora-12514的問(wèn)題
這篇文章主要介紹了關(guān)于使用PLSQL Developer時(shí)出現(xiàn)報(bào)錯(cuò)ora-12514的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11sqlplus 命令登錄 Oracle數(shù)據(jù)庫(kù)的多種方法
這篇文章主要介紹了sqlplus 命令登錄 Oracle數(shù)據(jù)庫(kù)的兩種方法,方式一通過(guò)sql*plus 命令窗口,方式2:通過(guò) cmd 窗口,每種方式給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09Oracle數(shù)據(jù)庫(kù)中文顯示???????解決辦法
這兩天剛剛使用Oracle,出現(xiàn)了好多不愉快的事情,下面分享一下我的一點(diǎn)經(jīng)歷,這篇文章主要給大家介紹了關(guān)于Oracle數(shù)據(jù)庫(kù)中文顯示???????的解決辦法,需要的朋友可以參考下2024-04-04詳解azure 云上準(zhǔn)備oracle11g的vnc安裝環(huán)境
本篇文章主要介紹了詳解azure 云上準(zhǔn)備oracle11g的vnc安裝環(huán)境,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Oracle數(shù)據(jù)庫(kù)密碼文件的使用與維護(hù)
Oracle數(shù)據(jù)庫(kù)密碼文件的使用與維護(hù)...2007-03-03