国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Oracle > 正文

Oracle數據庫的字段約束創建和維護示例

2024-08-29 14:00:02
字體:
來源:轉載
供稿:網友

創建Oracle數據庫的字段約束:

  1. 非空約束
  2. 唯一約束
  3. 對字段的取值的約束
  4. 默認值
  5. 外鍵約束
create table tab_class(  class_id number primary key,  class_name varchar2(10) not null unique ); 
create table tab_stu( stu_id number,  --學生姓名,不能為空,不能重復 stu_name varchar2(20) not null unique,  --學生姓名只能是male或female stu_gender varchar2(6) not null check(stu_gender='male' or stu_gender='female'),  --學生年齡只能在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)  ); 

維護已經創建好的約束:

  1. 可添加或刪除約束,但不能直接修改。
  2. 可使約束啟用和禁用。
  3. 非空約束必須使用MODIFY子句增加。
  4. 為表增加主鍵約束:
--維護約束 --創建約束 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); 

添加檢查約束:

--添加一個字段 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; 

復合約束,聯合主鍵,也就是兩個字段的組合成一個主鍵

--聯合主鍵 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); 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到oracle教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泰州市| 农安县| 涟水县| 桐梓县| 崇信县| 九江市| 镇远县| 崇明县| 黄平县| 盘锦市| 保康县| 鹤庆县| 平阴县| 馆陶县| 赤峰市| 永平县| 徐州市| 赤峰市| 民和| 眉山市| 临猗县| 长宁县| 荥经县| 江西省| 镇沅| 本溪市| 鹿泉市| 永泰县| 大埔县| 建阳市| 山东| 新丰县| 乐陵市| 兰溪市| 卓尼县| 南和县| 霞浦县| 吕梁市| 商河县| 资源县| 宜宾市|