一、模式
1.模式的创建
create schema <模式名> authorization <用户名>
例如:为用户WANG定义一个学生-课程模式S-T
create schema "S-T" authorization wang;
2.模式的删除
drop schema <模式名> <cascade|restrict>
cascade删除模式的同时也把该模式的所有数据库对象删除
restrict 1.如果该模式下有下属对象就拒绝这个删除语句的执行
2.仅当该模式中没有任何下属的对象时才能执行。
例如:
DROP SCHEMA ZHANG CASCADE;
二、表
1.表的创建
create table <表名>
(<列名> <数据类型>[ <列级完整性约束条件> ]
[,<列名> <数据类型>[ <列级完整性约束条件>] ]
)
示例:
CREATE TABLE student
(Sno CHAR(9) PRIMARY KEY, /* 列级完整性约束条件,Sno是主码*/
Sname CHAR(20) UNIQUE, /* Sname取唯一值*/
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20)
);
primary key 和 unique就是完整性约束条件
2.表的删除
drop table <表名> [cascade|restrict]
注意:[ ]代表里面的东西是可有可无的 但<>就是必修要有的
RESTRICT:删除表是有限制的。
欲删除的基本表不能被其他表的约束所引用
如果存在依赖该表的对象,则此表不能被删除
CASCADE:删除该表没有限制。
在删除基本表的同时,相关的依赖对象一起删除
示例:
drop table student
drop table student cascade
drop table student restrict
3.表的修改
alter table <表名>
[ add[column] <新列名> <数据类型> [ 完整性约束 ] ]
[ add<表级完整性约束>]
[ drop[ column] <列名> [cascade| restrict] ]
[ drop constraint <完整性约束名>[ cascade| restrict] ]
[alter column <列名><数据类型> ]
示例:1.向student表增加“入学时间”列,其数据类型为日期型
alter table student add 入学时间 date
2.增加课程名称必须取唯一值的约束条件。
alter table course add unique(Cname);
3.将年龄的数据类型由字符型(假设原来的数据类型是字符型)改为整数。
alter table student alter column age int
三、索引
1.索引的建立
create [unique][cluster] index <索引名>
on <表名>(<列名>[<次序>][,<列名>[<次序>] ]…);
<表名>:要建索引的基本表的名字
索引:可以建立在该表的一列或多列上,各列名之间用逗号分隔
<次序>:指定索引值的排列次序,升序:ASC,降序:DESC。缺省值:ASC
UNIQUE:此索引的每一个索引值只对应唯一的数据记录
CLUSTER:表示要建立的索引是聚簇索引
示例:为学生-课程数据库中的Student,Course,SC三个表建立索引。Student表按sno(学号)升序建唯一索引,Course表按cno(课程号) 升序建唯一索引,SC表按学号升序和课程号降序建唯一索引
create unique index stu on Student(sno)
create unique index couon Course(cno asc)
create unique index s on sc(sno asc,cno desc)
2.索引的删除
drop index <索引名>
3.索引的修改
alter inde <旧索引> rename to <新索引>
例:将SC表的SCno索引名改为SCSno
alter index SCno rename to SCSno;