alter table 表
[ add 新列 資料類 [ 完整性約束 ] ]
[ drop 完整性約束 ]
[ alter column列 資料類 ];[例8]向student表增加「入學時間」列,其資料型別為日期型。
alter table student add s_entrance date;
不論基本表中原來是否已有資料,新增加的列一律為空值。
[例9]將年齡的資料型別由字元型(假設原來的資料型別是字元型)改為整數。
alter table student alter column sage int;
[例10]增加課程名稱必須取唯一值的約束條件。
alter table course add unique(cname);drop table 表[restrict| cascade];
restrict:刪除表是有限制的。
欲刪除的基本表不能被其他表的約束所引用
如果存在依賴該錶的物件,則此表不能被刪除
cascade:刪除該錶沒有限制。
在刪除基本表的同時,相關的依賴物件一起刪除[例11] 刪除student表
drop table student cascade ;
基本表定義被刪除,資料被刪除
表上建立的索引、檢視、觸發器等一般也將被刪除[例12]若表上建有檢視,選擇restrict時表不能刪除
create view is_student
as select sno,sname,sage
from student
where sdept='is';
drop table student restrict;
--error: cannot drop table student because other
objects depend on it[例12]如果選擇cascade時可以刪除表,檢視也自動被刪除
drop table student cascade;
--notice: drop cascades to view is_student
select * from is_student;
--error: relation " is_student " does not exist建立索引的目的:加快查詢速度
誰可以建立索引
dba 或 表的屬主(即建立表的人)
dbms一般會自動建立以下列上的索引
primary key
unique
誰 維護索引
dbms自動完成
使用索引
dbms自動選擇是否使用索引以及使用哪些索引
rdbms中索引一般採用b+樹、hash索引來實現
b+樹索引具有動態平衡的優點
hash索引具有查詢速度快的特點
採用b+樹,還是hash索引 則由具體的rdbms來決定
索引是關聯式資料庫的內部實現技術,屬於內模式的範疇
create index語句定義索引時,可以定義索引是唯一索引、非唯一索引或聚簇索引
語句格式
create [unique] [cluster] index 索引
on 表(列[次][,列[次] ]…);[例13] create cluster index stusname
on student(sname);
在student表的sname(姓名)列上建立乙個聚簇索引
在最經常查詢的列上建立聚簇索引以提高查詢效率
乙個基本表上最多只能建立乙個聚簇索引
經常更新的列不宜建立聚簇索引[例14]為學生-課程資料庫中的student,course,sc三個表建 立索引。
create unique index stusno on student(sno);
create unique index coucno on course(cno);
create unique index scno on sc(sno asc,cno desc);
student表按學號公升序建唯一索引
course表按課程號公升序建唯一索引
sc表按學號公升序和課程號降序建唯一索引drop index 索引;
刪除索引時,系統會從資料字典中刪去有關該索引的
描述。[例15] 刪除student表的stusname索引
drop index stusname;
資料庫和表的建立與刪除
使用sql命令實現資料庫,關係表的建立與 刪除 建立選課表時,選課表中主鍵是sno,cno並且此表中的sno,cno與學生表和課程表中一致。建立學生資料庫 create database 學生資料庫 建立學生表 student create table student sno char 4 prim...
建立資料庫 表以及索引
這樣做就可以建立乙個資料庫 create database 資料庫名稱這樣做就可以建立乙個資料庫中的表 create table 表名稱 列名稱1 資料型別,列名稱2 資料型別,本例演示如何建立名為 person 的表,有四個列。列名是 lastname firstname address 以及 a...
資料庫 索引操作(索引的建立,刪除,查詢)
1 建立索引 create index 索引名 on 表名 列名 2 刪除索引 drop index 索引名 3 建立組合索引 create index 索引名 on 表名 列名1,列名2 4 查詢索引 根據索引名,查詢表索引字段 select from user ind columns where...