第一篇:Oracle05表操作和約束(范文模版)
一、DDL數(shù)據(jù)定義語言 1.創(chuàng)建表:create table 表名要求:
1.必須以字母開頭
2.必須在1-30個字符之間
3.必須只能包含A-Z,a-z,0-9,$,_,# 4.必須不能和用戶定義的其他對象重名 5.必須不能是Oracle的保留字 例如: 方式一:
create table student(stu_no number(11), name varchar(32), gender char(5), age number(3)default 19//為字段設(shè)置默認值);Create table test1(ID char(11), Name carchar2(24), Age number(3)default 19);方式二:子查詢建表
Create table user_info As
select first_name,salary,dept_id From s_emp;2.刪除表:drop table 例如,drop table studentzxf;3.修改表:alter table Create table tal(Stu_no number(11), Name varchar2(32));1.增加一列
Alter table tal add age number(3);2.增加多列
Alter table tal add(address varchar2(32),birthday date);3.刪除一列
Alter table tal drop column address(列名);4.刪除多列
Alter table tal drop(age,birthday);5.邏輯刪除列
Alter table tal set unused column name;Alter talbe tal drop unused columns;//在數(shù)據(jù)庫不繁忙的時候用 Oracle在設(shè)計的時候,沒有考慮可逆性 6.修改一列
Alter table tal modify stu_no number(20);//將原來的長度為11 修改為長度為20 修改列時注意:
考慮現(xiàn)有數(shù)據(jù)的安全性(包括數(shù)據(jù)的類型,長度),修改長度是 只能往大改 如果表中有數(shù)據(jù),不可以修改類型,只有當(dāng)表中沒數(shù)據(jù)了才能修改類型 修改多列
Alter tabale tal modify(stu_no number(11)not null,name char(20));7.重命名列
Alter table tal rename column stu_no to s_no;8.更改約束
Alter table tal add constraint tal_pk primary key(s_no);9.刪掉約束
Alter table test4 drop constraint tal_pk;10.重命名表名
Rename tal to 新表名 11.添加外鍵約束
ALTER TABLE tal ADD CONSTRAINT STUDENT_FK FOREIGN KEY(dept_no)REFERENCES dept(dept_no);
二、DML數(shù)據(jù)操縱語言
1.插入數(shù)據(jù):insert 例如: insert into Student values(1,’Jack’);//按表中的自然順序逐個插入數(shù)據(jù)
insert into student(stu_no,name)values(1,’Jack’);//指定字段插入值 2.刪除數(shù)據(jù):delete from[tablename]where… 例如: delete from testdefault where age = 19;3.修改數(shù)據(jù):update[tablename]set a = exp1 where b = exp2 如果沒有限定條件,則會修改整張表中的數(shù)據(jù),即修改整列。例如: update Student set age = 24 where name = ‘jack’;也可一次性寫入多條insert語句一次執(zhí)行完畢。也可加載腳本(.sql文件)命令@+路徑/...sql 4.truncate截斷表
delete刪除時會產(chǎn)生日志信息的。
truncate刪除數(shù)據(jù)時不生成日志信息,所以效率快,但也不可逆。truncate截斷后會釋放表空間,而delete并不釋放表空間。
三、約束
考慮數(shù)據(jù)庫的完整性約束 三方面:1.實體完整性
2.參照完整性
3.用戶自定義完整性 5種約束:
Not null 非空約束
(nk)例:Create table stu_info(S_no char(11)not null, S_name varchar2(24)not null, S_age number(3));Unique 唯一約束(uk)(只保證唯一,不保證不為空,多條記錄的值都可以為空,因為Null和null是不相等的,不會提示違反了唯一性)方式一:(列級約束)例:Create table stu_info(S_no char(11)unique not null, S_name varchar2(24)not null, S_age number(3));方式二:(表級約束)例:Create table stu_info(S_no char(11)not null, S_name varchar2(24)not null, S_age number(3), Constraint stu_info_uk unique(S_no,S_name));//約束名為stu_info_uk Primary key 主鍵約束(pk)唯一且非空 方式一:(列級約束)例:create table test2(C1 number(3)constraint test2_pk primary key, C2 number(3));方式二:(表級約束)例:create table test2(C1 number(3), C2 number(3), Primary key(c1,c2));c1 , c2 組合起來是唯一的,c1,c2單獨看不一定是唯一的 Atler table tablename add constraint 主鍵名稱primay key
表級約束可以約束很多列,可以定義聯(lián)合主鍵,而列級約束是做不到的。
Foreign key 外鍵約束(fk)
要求引用另一張表中已存在的值(主鍵、唯一鍵)
例:create table parent(C1 number(3)primary key, C2 number(3));Create table child(C1 number(3)primary key, C2 number(3)references parent(c1));先建父表再建子表。
不能隨便刪除,刪除時只能先刪子表記錄再刪父表;刪除表時同理。插入數(shù)據(jù)時先給父表插,再給子表插。
Drop table parent cascade cinstraint;//刪除表時,如果想先刪除父表,子表不變 例:create table parent(C1 number(3)primary key, C2 number(3));Create table child(C1 number(3)primary key, C2 number(3)references parent(c1)on delete cascade);//刪除記錄時切斷關(guān)系,刪除了子表中的記錄再刪除父表
級聯(lián)刪除:世紀是先刪除子表的記錄,再刪除父表的記錄。表都存在。學(xué)生表:學(xué)號 姓名 性別
成績表:成績編號 學(xué)號 成績 課程編號 課程表:課程編號 課程名稱
外鍵:
成績表里面學(xué)號應(yīng)該是學(xué)生表
成績表里面的課程編號應(yīng)該是課程表里面的課程編號
Create table student(sno number primary key, name varchar2(10),sex varchar2(2))Create table score(scno number primary key,sno number not null,sco number(5,2),cno number not null references course(cno))Create table course(cno number primary key,name varchar2(20));Alter table score add constrain fk_stuandscore foreign key(sno)references student(sno);Check 檢查約束(ck)方式一:(列級約束)
例:create table test3(C1 number(3)check(c1<150), C2 number(3)check(c2 is not null));方式二:(表級約束)
例:Create table test3(C1 number(3), C2 number(3), Check(c1 = c2));
第二篇:oracle表空間管理及操作實用案例
Oracle
表空間管理及操作詳解(附帶案例)
一:表空間概述
表空間是數(shù)據(jù)庫的邏輯組成部分,從物理上講,數(shù)據(jù)庫數(shù)據(jù)存放在數(shù)據(jù)文件中;數(shù)據(jù)庫邏輯上講是由一個或者多個表空間組成,數(shù)據(jù)文件則是存放在表空間中,表空間有一個或者多個數(shù)據(jù)文件組成即表空間有數(shù)據(jù)文件構(gòu)成的。
數(shù)據(jù)庫,表空間,段,區(qū),塊,按大小排列依次是:數(shù)據(jù)庫---》表空間-----》段-----》區(qū)----》塊。
1.建立表空間的作用:
(1.)控制數(shù)據(jù)庫占用的磁盤空間。(2).dba可以將不同的數(shù)據(jù)類型部署到不同的位置,這樣有利于提高i/o性能,同時有利與備份和恢復(fù)等管理操作。
二:管理表空間和數(shù)據(jù)文件。
(1)建立表空間create tablespace;一般情況下建立表空間是特權(quán)用戶或者dba來執(zhí)行的,如果其他用戶來創(chuàng)建表空間,則用戶建必須要具有create tablespace的系統(tǒng)權(quán)限。(2)建立數(shù)據(jù)表空間
在建立數(shù)據(jù)庫后,為便于管理表,最好建立自己的表空間。
create tablespace sp02 datafile 'd:testsp02.dbf' size 20m uniform size 128k 說明:執(zhí)行完上述命令后,會建立名稱為sp02的表空間,并為該表空間建立名稱為sp02.dbf的數(shù)據(jù)文件,區(qū)的大小為128k。(3.)在表空間建表
create table tablesp02(ID number(5),name varchar2(20))tablespace sp02(4)顯示當(dāng)前用戶擁有哪些表。
select * from tab;
(5).知道表空間名,顯示該表空間包括的所有表。
select * from all_tables where tablespace_name='表空間名'。
注:擁有權(quán)限不同的用戶在進行以上操作得到的結(jié)果可能不同。System可以查看得到某空間的所擁有表。(比如users表空間)(6)知道表名,查看該表屬于那個表空間。
select TABLESPACE_NAME, TABLE_NAME from user_tables where TABLE_NAME='EMP';(7)知道表名,查看該表屬于那個表空間,屬于哪個用戶。
select owner,TABLESPACE_NAME, TABLE_NAME from all_tables where TABLE_NAME='EMP';(8)改變表空間的狀態(tài) 使表空間可讀寫
alter tablespace 表空間名 read write;
刪除表空間,一般情況下由特權(quán)用戶或者dba來操作。具有drop tablespace系統(tǒng)權(quán)限。
drop tablespace ‘表空間’includeing contens and datafiles;
說明:includeing contens表示刪除表空間時,刪除該空間的所有數(shù)據(jù)庫對象而datafiles表示將數(shù)據(jù)庫文件也刪除。(9)改變表空間的狀態(tài)(1)使用空間脫機
alter tablespace 表空間名 offline(2)使用空間聯(lián)機
alter tablespace表空間名online(3)只讀表空間
建立表空間時,表空間可以讀寫,如果不希望在表空間上執(zhí)行update,delete,insert操作。那么可以將表空間修改為只讀。alter talbespace sp01 read only;
三:移動數(shù)據(jù)文件:
(1)確定數(shù)據(jù)文件所在的表空間(知道表屬于哪個表空間)Select tablespace_name from dba_data_files where file_name='D:TEST