標籤:style color ar for strong sp 資料 on ad
主鍵約束;外鍵約束;唯一性限制式;檢查約束;預設值約束 -——————五大約束
一、 主鍵約束;
--建立表的主鍵約束
create table student (student_id number primary key,student_name varchar2(20),sudent_birthday date,student_address varchar2(50),student_phone varchar2(20))
--顯示命名主鍵約束
--第一種寫法:create table student1 (student_id number constraint pk_student primary key ,student_name varchar2(20),sudent_birthday date,student_address varchar2(50),student_phone varchar2(20))
第二種寫法: create table student1 (student_id number ,student_name varchar2(20),sudent_birthday date,student_address varchar2(50),student_phone varchar2(20) constraint pk_student primary key(student_id))
--建立多列主鍵
create table student1 (student_id number ,student_name varchar2(20),sudent_birthday date,student_address varchar2(50),student_phone varchar2(20) constraint pk_student primary key(student_id,student_name))
--查看使用者建立的所有約束
select * from user_constraints where table_name like ‘%STUD%‘
--建立無主鍵的表
create table student2 (student_id number ,student_name varchar2(20),sudent_birthday date,student_address varchar2(50),student_phone varchar2(20))
--為建立的表添加 主鍵約束
alter table student2 modify (student_id number primary key )
--建立無主鍵的表
create table student3 (student_id number ,student_name varchar2(20),sudent_birthday date,student_address varchar2(50),student_phone varchar2(20))
--為表添加多列主鍵
alter table student3 add constraint pk_studentForMutPrimarykey primary key (student_name,sudent_birthday,student_address)
--刪除主鍵 與列一樣,主鍵是表的一個對象,刪除表的主鍵與刪除列的文法非常相似。
alter table student3 drop primary key
--當然,如果將主鍵看做表的一 個對象,而且知道主鍵的名稱,那麼可以利用刪除約束的文法來刪除表的主鍵。如下:
alter table student3 drop constraint pk_studentForMutPrimarykey
--啟用/禁用主鍵
alter table student disable primary key ;
alter table student enable primary key ;
--重新命名主鍵
alter table student3 rename constraint pk_studentForMutPrimarykey to PK_STUDENT2
主鍵的應用情境
1. 對於完整性要求比較高的資料表都應該建立主鍵
2.對於經常按照某列進行查詢的資料表,應該考慮建立主鍵
3.考慮是否對外鍵有利
二、外鍵約束;
--建立表customers
create table customers (customer_id number primary key ,customer_name varchar2(50),customer_address varchar2(50),customer_phone varchar2(30),email varchar2(20),constrator varchar2(20));
--建立表orders
create table orders (order_id number primary key ,customer_id number,goods_name varchar(20),quantity number,unit varchar(10));
--建立外鍵約束
alter table orders add constraint fk_orders_customers foreign key (customer_id) references customers(customer_id)
--重新命名外鍵
alter table orders rename constraint FK_ORDERS_CUSTOMERS to FK_ORDERS
--啟用/禁用外鍵
alter table orders modify constraint FK_ORDERS disable
alter table orders modify constraint FK_ORDERS enable
--刪除外鍵
alter table orders drop constaint FK_ORDERS
三、唯一性限制式;
--建立唯一性限制式
create table users(user_id number primary key ,
user_name varchar2(50),user_address varchar2(50),user_phone varchar2(20),
email varchar2(20) unique,constractor varchar2(20))
--將表的某一列設定為 :唯一性限制式:
alter table users add constraint uq_phone unique (user_phone)
--查看唯一性限制式
select * from user_constraints where table_name=‘USERS‘
select * from user_cons_columns where table_name=‘USERS‘
--刪除唯一性限制式
alter table users drop constraint uq_phone
四、檢查約束;
--建立檢查約束
create table students (student_id number primary key ,student_name varchar2(10),
subject varchar2(20),score number constraint chk_score check(score between 0 and 100))
--複雜些的檢查約束
create table employees(
employee_id number primary key ,employee_name varchar2(10),grade varchar2(10),salary number,constraint chk_salary
check(
grade in(‘MANAGER‘,‘LENDER‘,‘STAFF‘) --grade 為‘MANAGER‘,‘LENDER‘,‘STAFF‘ 這三個中一個
and
(
grade=‘MANAGER‘ and salary<=8000 --若grade 為grade=‘MANAGER‘ and salary<=8000
or grade=‘LERDER‘ and salary<=5000 --若grade 為grade=‘LERDER‘ and salary<=5000
or grade=‘STAFF‘ and salary<=4000 --若grade=‘STAFF‘ and salary<=4000
)
)
)
--添加檢查約束:
alter table employees add constraint chk_name check(length(employee_name)<=4)
--刪除檢查約束
alter table employees drop constraint chk_name
五、預設值約束
oracle學習系列之三 (約束)