一、 使用create關鍵字創建表 --(1)創建新表use 資料庫(在那個資料庫中建表)create table 表名(欄位名1(列名) 數據類型 列的特征,欄位名2(列名) 數據類型 列的特征(NOT NULL),......)--(2)創建帶有主鍵約束的表語法create table 表名(字 ...
一、 使用create關鍵字創建表
--(1)創建新表use 資料庫(在那個資料庫中建表)
create table 表名
(
欄位名1(列名) 數據類型 列的特征,
欄位名2(列名) 數據類型 列的特征(NOT NULL),
......
)
--(2)創建帶有主鍵約束的表語法
create table 表名
(
欄位名1(列名) 數據類型 列的特征,
欄位名2 數據類型 列的特征(NOT NULL),
......
primary key(主鍵列欄位)
) --(3)利用現有的表創建表 -- 註意:僅複製Oracle數據表結構:採用的是子查詢方式
create table 新表
as
select * from 舊的表 where 1=2
--(4)利用現有的表的結構創建新表 -- 註意:僅複製Oracle數據表結構:採用的是子查詢方式
create table 新表
select 欄位1,欄位2... from 舊的表 where 條件(舊的表欄位滿足的條件)
--(5)利用現有的表的結構創建新表 -- 註意:複製Oracle數據表數據
create table 新表
as
select * from 舊的表 where 1=1
--(6)利用現有的表的結構創建新表 -- 註意:複製Oracle數據表數據
create table 新表
as
select 欄位1,欄位2... from 舊的表 where 條件(舊的表欄位滿足的條件)
--(7)將查詢結果插入另一張表
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 表名 //刪除表中的所有數據,速度比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');
--成功插入
nsert 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_1alter table student disable constraint my_constraint_1;6.啟用約束 my_constraint_1alter table student enable constraint my_constraint_1;
-