本篇文章給大家分享的是有關(guān)Oracle數(shù)據(jù)庫(kù)中怎么創(chuàng)建字段約束,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
拉薩網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),拉薩網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為拉薩上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的拉薩做網(wǎng)站的公司定做!
非空約束
唯一約束
對(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), --郵箱可以不填寫,填寫的話不能相同 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);
以上就是Oracle數(shù)據(jù)庫(kù)中怎么創(chuàng)建字段約束,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)名稱:Oracle數(shù)據(jù)庫(kù)中怎么創(chuàng)建字段約束
本文地址:http://chinadenli.net/article30/gieeso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、、網(wǎng)站營(yíng)銷、定制開發(fā)、自適應(yīng)網(wǎng)站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)