日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Oracle创建表、修改表、删除表、约束条件语法

發(fā)布時間:2024/8/26 综合教程 31 生活家
生活随笔 收集整理的這篇文章主要介紹了 Oracle创建表、修改表、删除表、约束条件语法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、 使用create關(guān)鍵字創(chuàng)建表

--(1)創(chuàng)建新表
use 數(shù)據(jù)庫(在那個數(shù)據(jù)庫中建表)
create table 表名
(
字段名1(列名) 數(shù)據(jù)類型 列的特征,
字段名2(列名) 數(shù)據(jù)類型 列的特征(NOT NULL),
......
)

--(2)創(chuàng)建帶有主鍵約束的表語法
create table 表名
(
字段名1(列名) 數(shù)據(jù)類型 列的特征,
字段名2 數(shù)據(jù)類型 列的特征(NOT NULL),
......
primary key(主鍵列字段)

)

--(3)利用現(xiàn)有的表創(chuàng)建表 -- 注意:僅復(fù)制Oracle數(shù)據(jù)表結(jié)構(gòu):采用的是子查詢方式
create table 新表
as
select * from 舊的表 where 1=2

--(4)利用現(xiàn)有的表的結(jié)構(gòu)創(chuàng)建新表 -- 注意:僅復(fù)制Oracle數(shù)據(jù)表結(jié)構(gòu):采用的是子查詢方式
create table 新表
select 字段1,字段2... from 舊的表 where 條件(舊的表字段滿足的條件)

--(5)利用現(xiàn)有的表的結(jié)構(gòu)創(chuàng)建新表 -- 注意:復(fù)制Oracle數(shù)據(jù)表數(shù)據(jù)
create table 新表
as
select * from 舊的表 where 1=1

--(6)利用現(xiàn)有的表的結(jié)構(gòu)創(chuàng)建新表 -- 注意:復(fù)制Oracle數(shù)據(jù)表數(shù)據(jù)
create table 新表
as
select 字段1,字段2... from 舊的表 where 條件(舊的表字段滿足的條件)

--(7)將查詢結(jié)果插入另一張表
insert into 另一張表
select * from 要查詢的表 where 條件(要查詢的表的列符合什么條件)

二、修改表

alter table 表名 add constraint 主鍵名 primary key(字段1,字段2,……); //更改主鍵約束

alter table 表名 drop primary key;                    //刪除表主

alter table 表名 add constraint 唯一約束名 unique(字段1,字段2……); // 增加唯一約束

alter table 表名 add constraint CK_INFOS_GENDER check(gender=’男’ or gender=’女’)   //更改條件約束

alter table 表名 add constraint CK_INFOS_AGE(age>=0 and age<=50)

alter table 表名 modify 字段名 字段類型 default 默認值;   //更改字段類型

alter table 表名 add 字段名 字段類型;       //增加字段類型

alter table 表名 drop column 字段名;       //刪除字段名

alter table 表名 rename column 列名 to 列名   //修改字段名

rename 表名 to 表名              //修改表名


三、刪除表

truncate table 表名   //刪除表中的所有數(shù)據(jù),速度比delete快很多,截斷表

delete from table 條件 // 刪除表中的數(shù)據(jù)

drop table 表名    //刪除表

四、Oracle中約束條件

每個標(biāo)題都默認表先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,'');

--錯誤的數(shù)據(jù),name為null的數(shù)據(jù)會收到約束限制,無法插入。

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');

--主鍵重復(fù)了,插入失敗

3.unique 唯一約束,值不能重復(fù)(空值除外)
電話不重復(fù)

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');

--電話重復(fù),操作失敗

4.check 條件約束,插入的數(shù)據(jù)必須滿足某些條件
例如限制年齡的輸入

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);

如果使用了以上的表直接創(chuàng)建,則插入下面的記錄有效:

insert into book(bid,name,pid) values(1001,'oracle',15);

以上的代碼沒有任何錯誤但是沒有意思,因為一本書應(yīng)該只屬于一個人,所以在此處的pid應(yīng)該對應(yīng)person表中的記錄。
此時就需要外鍵,修改book表結(jié)構(gòu)

create table book(bid number primary key not null,name varchar2(30), pid number references person(pid)) on delete cascade

--on delete cascade級聯(lián)刪除

--建立約束:book_pid_fk,與person表中pid為主-外鍵關(guān)系

--constraint book_pid_fk foreign key(pid) references person(pid));

insert into book(bid,name,pid) values(1001,'oracle',15);

6.級聯(lián)刪除
接上面5的案例,如果一個人的人員信息沒有了,那么此人所擁有的書還應(yīng)該存在么?
正常來說,如果person中的一條信息沒有了,則對應(yīng)的book中的數(shù)據(jù)也應(yīng)該同時消失。
在之前的結(jié)構(gòu)上執(zhí)行delete語句,刪除person表中一條記錄:

delete from person where pid=12;

提示刪除失敗:因為book中存在此項的關(guān)聯(lián),如果person表中的一條數(shù)據(jù)刪除了,則一定會影響到book表中數(shù)據(jù)的完成性,
所以不讓刪除。
如果非要刪除,則應(yīng)該刪除book表中的對應(yīng)數(shù)據(jù),之后再刪除person表中對應(yīng)的數(shù)據(jù)。
此時如果想完成刪除person表的數(shù)據(jù)同時刪除book表中的數(shù)據(jù),則必須使用級聯(lián)刪除。
在建立外建的時候必須指定級聯(lián)刪除(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的主--外鍵約束,要求帶級聯(lián)刪除

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;

練習(xí)作業(yè):
1.創(chuàng)建一張表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.創(chuàng)建一張學(xué)員興趣表hobby
id number(10)
hobby_name varchar2(20)
sid number --學(xué)生id
給sid字段添加外鍵約束,并且要帶級聯(lián)刪除

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;
-

總結(jié)

以上是生活随笔為你收集整理的Oracle创建表、修改表、删除表、约束条件语法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。