- 表的創(chuàng)建
表創(chuàng)建標(biāo)準(zhǔn)語法:
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr] , …);
--設(shè)計要求:建立一張用來存儲學(xué)生信息的表,表中的字段包含了學(xué)生的學(xué)號、姓名、年齡、入學(xué)日期、年級、班級、email等信息,
--并且為grade指定了默認(rèn)值為1,如果在插入數(shù)據(jù)時不指定grade得值,就代表是一年級的學(xué)生
--DML是不需要commit的,隱式事務(wù)
create table student
(
stu_id number(10),
name varchar2(20),
age number(2),
hiredate date,
grade varchar2(10) default 1,
classes varchar2(10),
email varchar2(50)
);
-- 注意日期格式要轉(zhuǎn)換,不能是字符串,varchar2類型要用引號,否則出現(xiàn)類型匹配
--DML 需要收到commit
insert into student values(20211114,'zhangsan',22,to_date('2021-11-14','YYYY-MM-DD'),'2','1','123@qq.com');
insert into student(stu_id,name,age,hiredate,classes,email) values(20211114,'zhangsan',22,to_date('2021-11-14','YYYY-MM-DD'),'1','1234@qq.com');
select * from student;
-- 給表添加列,添加新列時不允許為not null,因為與舊值不兼容
alter table student add address varchar(100);
-- 刪除列
alter table student drop column address;
--修改列
alter table student modify(email varchar2(100));
正規(guī)表設(shè)計使用power disinger
--表的重命名
rename student to stu;
-- 表刪除
drop table stu;
**
在刪除表的時候,經(jīng)常會遇到多個表關(guān)聯(lián)的情況(外鍵),多個表關(guān)聯(lián)的時候不能隨意刪除,使用如下三種方式:
- cascade(級聯(lián)):如果A,B,A中的某一個字段跟B表中的某一個字段做關(guān)聯(lián),那么再刪除表A的時候,需要先將表B刪除
- set null方式:在刪除的時候,把表的關(guān)聯(lián)字段設(shè)置成空
- restrict方式:只有當(dāng)依賴表中沒有一個外鍵值與要刪除的主表中主鍵值相對應(yīng)時,才可執(zhí)行刪
除操作
2.表的約束(constraint)
約束:創(chuàng)建表時,指定的插入數(shù)據(jù)的一些規(guī)則
約束是在表上強(qiáng)制執(zhí)行的數(shù)據(jù)校驗規(guī)則
Oracle 支持下面五類完整性約束:
1). NOT NULL 非空約束 ---- 插入數(shù)據(jù)時列值不能空
2). UNIQUE Key 唯一鍵約束 ----限定列唯一標(biāo)識,唯一鍵的列一般被用作索引
3). PRIMARY KEY 主鍵約束 ----唯一且非空,一張表最好有主鍵,唯一標(biāo)識一行記錄
4). FOREIGN KEY 外鍵約束---多個表間的關(guān)聯(lián)關(guān)系,一個表中的列值,依賴另一張表某主鍵或者唯一鍵
-- 插入部門編號為50的,部門表并沒有編號為50的,報錯
insert into emp(empno,ename,deptno) values(9999,'hehe',50);
5). CHECK 自定義檢查約束---根據(jù)用戶需求去限定某些列的值,使用check約束
-- 添加主鍵約束/not null約束/check約束/唯一鍵約束
create table student
(
stu_id number(10) primary key,
name varchar2(20) not null,
age number(3) check(age>0 and age<126),
hiredate date,
grade varchar2(10) default 1,
classes varchar2(10),
email varchar2(50) unique,
deptno number(2),
);
-- 添加外鍵約束
create table stu
(
stu_id number(10) primary key,
name varchar2(20) not null,
age number(3) check(age>0 and age<126),
hiredate date,
grade varchar2(10) default 1,
classes varchar2(10),
email varchar2(50) unique,
deptno number(2),
FOREIGN KEY(deptno) references dept(deptno)
);
-- 創(chuàng)建表時沒添加外鍵約束 也可以修改 其中fk_0001為外鍵名稱
alter table student add constraint fk_0001 foreign key(deptno) references dept(deptno);

- 表的索引(相當(dāng)于目錄)
索引是為了加快對數(shù)據(jù)的搜索速度而設(shè)立的。索引是方案(schema)中的一
個數(shù)據(jù)庫對象,與表獨立存放
索引的作用:在數(shù)據(jù)庫中用來加速對表的查詢,通過使用快速路徑訪問方法
快速定位數(shù)據(jù),減少了磁盤的I/O
索引也是存貯在磁盤中的
索引是有數(shù)據(jù)結(jié)構(gòu)的B+樹
局部性原理:時間局部性和空間局部性
磁盤預(yù)讀:
索引創(chuàng)建有兩種方式:
自動創(chuàng)建:當(dāng)在表上定義一個PRIMARY KEY 或者UNIQUE 約束條件時,Oracle數(shù)據(jù)庫自動創(chuàng)建一個對應(yīng)的唯一索引
手動創(chuàng)建:用戶可以創(chuàng)建索引以加速查詢
組合索引:多個列組成的索引
--索引:加快數(shù)據(jù)剪碎
create index i_ename on emp(ename);

--當(dāng)創(chuàng)建某個字段索引后,查詢某個字段會自動使用到索引
select * from emp where ename = 'SMITH';
--刪除索引 索引名稱也是唯一的
drop index i_ename;
一些概念:
回表:
覆蓋索引
組合索引
最左匹配