oracle修改表字段约束条件,Oracle创建表、修改表、删除表、约束条件语法
三、刪除表
truncate table 表名 //刪除表中的所有數據,速度比delete快很多,截斷表
delete from table 條件 ? // 刪除表中的數據
drop table 表名 ? ?? //刪除表
四、Oracle中約束條件
每個標題都默認表先drop table后create table
1.not null 非空約束
create table person(?pid number,?name varchar2(20) not null);
insert into person(pid,name) values(1,'zhangsan');
--成功插入
insert into person(pid,name) values(1,'');
--錯誤的數據,name為null的數據會收到約束限制,無法插入。
2.primary key 主鍵約束
create table person(?pid number primary key,?name varchar2(20));
insert into person(pid,name) values(1,'zhangsan');
--成功插入
insert into person(pid,name) values(1,'lisi');
--主鍵重復了,插入失敗
3.unique 唯一約束,值不能重復(空值除外)
電話不重復
create table person(?pid number primary key,?name varchar2(20),?tel varchar2(20) unique);
insert into person(pid,name,tel) values(1,'zhangsan','1234567890');
--成功插入
insert into person(pid,name,tel) values(2,'lisi','1234567890');
--電話重復,操作失敗
4.check 條件約束,插入的數據必須滿足某些條件
例如限制年齡的輸入
create table person(?pid number primary key,?name varchar2(20),?tel varchar2(20) not null unique,?age number check(age between 0 and 150));
insert into person(pid,name,tel) values(1,'zhangsan','1234567890',30);
--成功插入
insert into person(pid,name,tel) values(2,'lisi','1111111',160);
--年齡輸入錯誤
5.foreign key 外鍵
例如:有以下一種情況:一個人有很多本書
create table book(bid number primary key not null,name varchar2(30),pid number);
如果使用了以上的表直接創建,則插入下面的記錄有效:
insert into book(bid,name,pid) values(1001,'oracle',15);
以上的代碼沒有任何錯誤但是沒有意思,因為一本書應該只屬于一個人,所以在此處的pid應該對應person表中的記錄。
此時就需要外鍵,修改book表結構
create table book(?bid number primary key not null,?name varchar2(30),? pid number references person(pid)) on delete cascade
--on delete cascade級聯刪除
--建立約束:book_pid_fk,與person表中pid為主-外鍵關系
--constraint book_pid_fk foreign key(pid) references person(pid));
insert into book(bid,name,pid) values(1001,'oracle',15);
6.級聯刪除
接上面5的案例,如果一個人的人員信息沒有了,那么此人所擁有的書還應該存在么?
正常來說,如果person中的一條信息沒有了,則對應的book中的數據也應該同時消失。
在之前的結構上執行delete語句,刪除person表中一條記錄:
delete from person where pid=12;
提示刪除失敗:因為book中存在此項的關聯,如果person表中的一條數據刪除了,則一定會影響到book表中數據的完成性,
所以不讓刪除。
如果非要刪除,則應該刪除book表中的對應數據,之后再刪除person表中對應的數據。
此時如果想完成刪除person表的數據同時刪除book表中的數據,則必須使用級聯刪除。
在建立外建的時候必須指定級聯刪除(on delete cascade)
create table book(?bid number primary key not null,?name varchar2(50),? pid number CONSTRAINT book_pid_fk FOREIGN KEY(pid) REFERENCES person(pid) on delete cascade);
為新表添加約束條件
--person
create table person(?pid number,?name varchar2(30),?tel varchar2(50),?age number);
--book
create table book(?bid number,?name varchar2(30),?pid number);
以上兩張表中沒有任何約束,下面通過alter命令為表添加約束
1.為兩個表添加主鍵
person表pid為主鍵
alter table person add constraint person_pid_pk primary key(pid);
book表bid為主鍵
alter table book add constraint book_bid_pk primary key(bid);
2.為person表中的tel添加唯一約束
alter table person add constraint person_tel_uk unique(tel);
3.為person表中的age添加檢查約束
alter table person add constraint person_age_ck check(age between 0 and 150);
4.為book表中的pid添加與person的主--外鍵約束,要求帶級聯刪除
alter table book add constraint person_book_pid_fk foreign key(pid) references person(pid) on delete cascade;
7.刪除約束
alter table person drop constraint unique(tel);
alter table book drop constraint person_book_pid_fk;
8.啟用約束
alter table book enable constraint person_book_pid_fk;
9.禁用約束
alter table book disable constraint person_book_pid_fk;
練習作業:
1.創建一張表student
id number
name varchar2(20)
age number(5)
tel varchar2(15)
給id字段添加主鍵約束
給name字段添加非空約束
給age字段添加check約束(18-35)
給tel添加唯一非空約束
create table student(?id number primary key,?name varchar2(20) not null,?age number(5) check(age between 18 and 35),?tel varchar2(15) unique not null);
2.創建一張學員興趣表hobby
id number(10)
hobby_name varchar2(20)
sid number --學生id
給sid字段添加外鍵約束,并且要帶級聯刪除
create table hobby(?id number(10),?hobby_name varchar2(20),?sid number references student(id) on delete cascade);
3.刪除掉student表中tel字段的唯一約束(先寫出查看該表約束的sql)
select constraint_name,constraint_type from all_constraints where user_table=upper('student');alter table student drop constraint unique(tel);
4.手動添加student表中tel字段的唯一約束(約束名為:my_constraint_1)
alter table student add constraint my_constraint_1 unique(tel);
5.禁用約束my_constraint_1
alter table student disable constraint my_constraint_1;
6.啟用約束 my_constraint_1
alter table student enable constraint my_constraint_1;
-
總結
以上是生活随笔為你收集整理的oracle修改表字段约束条件,Oracle创建表、修改表、删除表、约束条件语法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: $.post把表单对象传递过去_Form
- 下一篇: 信息学奥赛一本通 1967:【14NOI