DDL_数据库模式定义语言
2014年11月22日 15:53:24
DDL 定义 define
概念:是用于描述数据库中要存储的现实世界实体的语言。一个数据库模式包含该数据库中所有实体的描述定义。
这些定义包括结构定义、操作方法定义等。
数据库对象 (创建,修改,删除,重命名)
- 表 table
- 视图 view
- 序列 sequence
- 索引 index
- 同义词 synonym
关键字:
- CREATE
- ALTER
- DROP
- TRUNCATE
- COMMENT
- RENAME
需要满足:
- 数据完整性
- 实体完整性 每行唯一 PK
- 域完整性 合法范围内的值
- 引用完整性 引用的值应与另一关联
- 用户定义完整性 自定义规则 age score
约束用于实施数据完整性
- PK 主键
- FK 外键
- UK 唯一键
- CK 检查
- NN 非空
-- 创建 create-- 约束 表级、列级create table bigKind(bigKIndNo char(3),bigKindName varchar2(40) not null, -- varchar2(40 byte|char) -- 非空约束列 not nullconstraint bigKind_pk_bigKindNo primary key(bigKindNo),constraint bigKind_UK_bigKindName unique(bigKindName));create table smallKind(--smallKindNo char(5) primary key, -- 系统名称--smallKindName varchar2(50) not null uniquesmallKindNo char(5) constraint smallKind_pk_smallkindNo primary key,smallKindName varchar2(50) constraint namenotNull not nullconstraint smallKind_uk_smallkindName unique,bigKindNo char(3) not null constraint smallKInd_FK_bjgKindNo references bigKind(bigKindNo));create table smallKind(smallKindNo char(5),smallKindName varchar2(50),bigKindNo char(3) not null,constraint sk_pk_skno primary key(smallKindNO),constraint sk_nn_skname check(smallKindName is not null), -- check约束constraint sk_uk_skname unique(smallkindName),constraint sk_fk_bk_bkNo foreign key(bigKindNo) references bigKind(bigKindNO));create table product(proNo char(10),proName varchar2(100) not null,price number(10, 2) not null,store number not null,info varchar2(4000),constraint product_pk_proNo primary key(proNo),constraint product_ck_price check(price > 0), -- check(列取值范围) not nullconstraint product_ck_store check(store >= 0));-- 修改 alteralter table product add smallKindNO char(5) not nullconstraint pro_fk_smkNo references smallKind(smallKindNo);alter table product add smallKindNO char(5) not null;alter table product modify smallKindNo char(10); -- 只可改变长度-- 修改类型 非空等 drop再addalter table product modify smallKindNoconstraint pro_fk_smkNo references smallKind(smallKindNo); -- 增加约束alter table product drop constraint pro_fk_smkNo; -- 去除约束alter table product enable constraint pro_fk_smkNo; -- 禁用、启用约束 disable enablealter table product drop column smallKindNo;-- 去除 dropdrop table bigKind;drop table smallKind;-- 重命名 renamerename sss to ss;BBS论坛kind 论坛模块 adminkindNo userName passwordkindNamepost 论坛帖子 Users 用户postId userIdtitle userNamecontent passwordpostTime timestamp sex infouserId 必须登录发帖Reply 帖子回复replyIdcontentpostIduserId userId定义表忘了定义 加列夹约束 初始设置为not null -> 可登陆也可匿名-- 系统约束名可至数据字典中查找user_constraintsuser_tables ...select * from user_constraints where table_name = 'PRODUCT';
ps: 使用modify修改属性类型时,只能修改该类型的长度,不能更变类型,如果要改变类型,需要先删除drop 再添加add
作业: 创建表 管理员登录 设置模块 选择某一模块名称 发帖 - 用户登录 用户名 查看帖子 - 帖子编号 对帖子 回复 - 登录|未登录 查看所有帖子 分页查看 - 按最新帖最前面 帖子编号 帖子标题 发帖者 发帖时间(转换为习惯结构 不需要秒) 回复数 关注前5帖 回帖数 管理员 某一回复有问题 删除 某一帖子有严重问题 删除 有部分小问题 修改 用户登录 修改密码 修改个人信息
--------------- 论坛模块create table kind (kindno char(5),kindname varchar2(40)not null,constraint kind_PK_kindno primary key (kindno),constraint kind_UK_kindname unique (kindname));------------------ 用户表create table users(userid char (10),username varchar2(50) not null,password number(8,0) not null,sex varchar2(5)not null,info varchar2(500) ,constraint users_PK_userid primary key (userid),-- constraint users_cK_sex check (sex = '男' || sex = '女'));------------------ 论坛帖子表create table post(postID char(20),title varchar2(50) not null,content varchar2(500) not null,postTime date not null,userId char(10) not null,kindno char(5)not null,constraint post_PK_postId primary key (postid),constraint post_PK_title unique (title),constraint post_PK_userid foreign key (userid)references users(userid),constraint post_PK_kindno foreign key (kindno)references kind(kindno));drop table post;----------------论坛回帖表create table reply(replyID char(20),content varchar2(500) not null,postid char(20)not null,userid char(10) not null,constraint reply_PK_replyID primary key (replyID)-- constraint post_PK_userid foreign key (userid)references users(userid));drop table reply;---userId userId定义表忘了定义 加列夹约束alter table reply modify useridconstraint reply_FK_userid references users(userid);---初始设置为not null -> 可登陆也可匿名 (如何使用disable禁用not null)alter table reply drop column userid;alter table reply add userid char(10)constraint reply_FK_userid references users(userid);--- admincreate table admins(username varchar2(40),password varchar2(10)not null,constraint admins_PK_username primary key (username));drop table admins;-----------------------------------------------------------------------------insert into admins values('admin', '123456'); --插入管理员数据--管理员登录select * from admins where username = 'admin';select password from admins where password = '123456';--设置模块insert into kind values('01','学习');insert into kind values('02','运动');insert into kind values('03','社会');insert into kind values('04','军事');insert into users values('0001','张三','123456','男','张三,中国人最耳熟能详的名字。');--插入用户数据insert into users values('0002','李四','1234','女','李四,张三的女朋友。');--选择某一模块名称 发帖select userid from users where username = '张三' and password = '123456'; --登录insert into post values('0001','空指针异常 ','可能: 数据源错误,比如数据库名或IP错误','12-11月-14 12.53.00.000000000 下午' ,(select userid from users where username = '张三' and password = '123456') ,'01');insert into post values('0002','不能执行查询 ','可能: 数据库中表的问题,比如列名不存在 ','12-11月-14 11.42.00.000000000 下午' ,(select userid from users where username = '张三' and password = '123456') ,'02');insert into post values('0003','表名或列名不存在 ','可能:表不存在或者没有插入数据到表中','12-10月-14 12.33.00.000000000 下午' ,(select userid from users where username = '张三' and password = '123456') ,'03');insert into post values('0004','非法表达式开始 ','可能:丢失括号 .','17-01月-14 06.53.00.000000000 下午' ,(select userid from users where username = '李四' and password = '1234') ,'04');insert into post values('0005','找不到符号 ','可能:没导入包','15-11月-14 08.43.00.000000000 下午' ,(select userid from users where username = '李四' and password = '1234') ,'03');--查看帖子select *from postwhere postid = '0001';--登录状态对帖子回复insert into reply values('0001','我们要在程序书写过程中尽量解决这个问题','0001','0001');insert into reply values('0002','一楼说的对!','0001',null);insert into reply values('0003','一楼说的有问题!','0001','0001');insert into reply values('0004','三楼说的对!','0001',null);insert into reply values('0005','这个表真的好烦啊~~!','0002','0002');insert into reply values('0006','五楼说的对!','0002',null);insert into reply values('0007','难道一定要这样么','0002','0002');insert into reply values('0008','七楼你太厉害了!','0003',null);insert into reply values('0009','我们要在程序书写过程中尽量解决这个问题','0004','0002');insert into reply values('0010','一楼说的对!','0004',null);--查看所有帖子select p.postid as 序号 ,p.title as 标题 , p.content as 内容 , to_char(p.posttime, 'yyyy-mm-dd HH24:mi') as 发帖时间 , u.username as 发帖者 , k.kindname as 板块 ,count(r.postid) as 回复数from post p , users u ,kind k , reply rwhere p.userid = u.userid and k.kindno = p.kindno and r.postid(+) = p.postidgroup by p.postid , p.title , p.content , p.posttime , u.username , k.kindnameorder by p.posttime asc;-- 分页数据查询 - rownum as name--分页查看 - 按最新帖最前面 帖子编号 帖子标题 发帖者 发帖时间(转换为习惯结构 不需要秒) 回复数select postid as 序号 ,title as 标题 , content as 内容 , to_char(posttime, 'yyyy-mm-dd HH24:mi') as 发帖时间 , username as 发帖者 , kindname as 板块from(select rownum as num, postid ,title , content , posttime, username , kindnamefrom(select p.postid , p.title , p.content , p.posttime, u.username , k.kindnamefrom post p , users u ,kind kwhere p.userid = u.userid and k.kindno = p.kindnoorder by p.posttime asc))where num >= 4 and num <= 6;---关注前5帖 回帖数select *from (select p.postid as 序号 ,p.title as 标题 , to_char(p.posttime, 'yyyy-mm-dd HH24:mi') as 发帖时间 , u.username as 发帖者 ,count(r.postid) as 回复数from post p , users u ,kind k , reply rwhere p.userid = u.userid and k.kindno = p.kindno and r.postid(+) = p.postidgroup by p.postid , p.title , p.posttime , u.usernameorder by count(r.postid) desc)where rownum <=5;---管理员--管理员--某一回复有问题 删除--某一帖子有严重问题 删除--有部分小问题 修改delete reply where replyid = '0010';delete reply where postid = '0004';delete post where postid = '0004';--用户登录--修改密码--修改个人信息select password from users where userid = '0001';update users set password = '1234567' where userid = '0001';update users set info = '无个人信息' where userid = '0001';--------------- 论坛模块create table kind (kindno char(5),kindname varchar2(40)not null,constraint kind_PK_kindno primary key (kindno),constraint kind_UK_kindname unique (kindname));------------------ 用户表create table users(userid char (10),username varchar2(50) not null,password number(8,0) not null,sex varchar2(5)not null,info varchar2(500) ,constraint users_PK_userid primary key (userid),-- constraint users_cK_sex check (sex = '男' || sex = '女'));------------------ 论坛帖子表create table post(postID char(20),title varchar2(50) not null,content varchar2(500) not null,postTime date not null,userId char(10) not null,kindno char(5)not null,constraint post_PK_postId primary key (postid),constraint post_PK_title unique (title),constraint post_PK_userid foreign key (userid)references users(userid),constraint post_PK_kindno foreign key (kindno)references kind(kindno));drop table post;----------------论坛回帖表create table reply(replyID char(20),content varchar2(500) not null,postid char(20)not null,userid char(10) not null,constraint reply_PK_replyID primary key (replyID)-- constraint post_PK_userid foreign key (userid)references users(userid));drop table reply;---userId userId定义表忘了定义 加列夹约束alter table reply modify useridconstraint reply_FK_userid references users(userid);---初始设置为not null -> 可登陆也可匿名 (如何使用disable禁用not null)alter table reply drop column userid;alter table reply add userid char(10)constraint reply_FK_userid references users(userid);--- admincreate table admins(username varchar2(40),password varchar2(10)not null,constraint admins_PK_username primary key (username));drop table admins;-----------------------------------------------------------------------------insert into admins values('admin', '123456'); --插入管理员数据--管理员登录select * from admins where username = 'admin';select password from admins where password = '123456';--设置模块insert into kind values('01','学习');insert into kind values('02','运动');insert into kind values('03','社会');insert into kind values('04','军事');insert into users values('0001','张三','123456','男','张三,中国人最耳熟能详的名字。');--插入用户数据insert into users values('0002','李四','1234','女','李四,张三的女朋友。');--选择某一模块名称 发帖select userid from users where username = '张三' and password = '123456'; --登录insert into post values('0001','空指针异常 ','可能: 数据源错误,比如数据库名或IP错误','12-11月-14 12.53.00.000000000 下午' ,(select userid from users where username = '张三' and password = '123456') ,'01');insert into post values('0002','不能执行查询 ','可能: 数据库中表的问题,比如列名不存在 ','12-11月-14 11.42.00.000000000 下午' ,(select userid from users where username = '张三' and password = '123456') ,'02');insert into post values('0003','表名或列名不存在 ','可能:表不存在或者没有插入数据到表中','12-10月-14 12.33.00.000000000 下午' ,(select userid from users where username = '张三' and password = '123456') ,'03');insert into post values('0004','非法表达式开始 ','可能:丢失括号 .','17-01月-14 06.53.00.000000000 下午' ,(select userid from users where username = '李四' and password = '1234') ,'04');insert into post values('0005','找不到符号 ','可能:没导入包','15-11月-14 08.43.00.000000000 下午' ,(select userid from users where username = '李四' and password = '1234') ,'03');--查看帖子select *from postwhere postid = '0001';--登录状态对帖子回复insert into reply values('0001','我们要在程序书写过程中尽量解决这个问题','0001','0001');insert into reply values('0002','一楼说的对!','0001',null);insert into reply values('0003','一楼说的有问题!','0001','0001');insert into reply values('0004','三楼说的对!','0001',null);insert into reply values('0005','这个表真的好烦啊~~!','0002','0002');insert into reply values('0006','五楼说的对!','0002',null);insert into reply values('0007','难道一定要这样么','0002','0002');insert into reply values('0008','七楼你太厉害了!','0003',null);insert into reply values('0009','我们要在程序书写过程中尽量解决这个问题','0004','0002');insert into reply values('0010','一楼说的对!','0004',null);--查看所有帖子select p.postid as 序号 ,p.title as 标题 , p.content as 内容 , to_char(p.posttime, 'yyyy-mm-dd HH24:mi') as 发帖时间 , u.username as 发帖者 , k.kindname as 板块 ,count(r.postid) as 回复数from post p , users u ,kind k , reply rwhere p.userid = u.userid and k.kindno = p.kindno and r.postid(+) = p.postidgroup by p.postid , p.title , p.content , p.posttime , u.username , k.kindnameorder by p.posttime asc;-- 分页数据查询 - rownum as name--分页查看 - 按最新帖最前面 帖子编号 帖子标题 发帖者 发帖时间(转换为习惯结构 不需要秒) 回复数select postid as 序号 ,title as 标题 , content as 内容 , to_char(posttime, 'yyyy-mm-dd HH24:mi') as 发帖时间 , username as 发帖者 , kindname as 板块from(select rownum as num, postid ,title , content , posttime, username , kindnamefrom(select p.postid , p.title , p.content , p.posttime, u.username , k.kindnamefrom post p , users u ,kind kwhere p.userid = u.userid and k.kindno = p.kindnoorder by p.posttime asc))where num >= 4 and num <= 6;---关注前5帖 回帖数select *from (select p.postid as 序号 ,p.title as 标题 , to_char(p.posttime, 'yyyy-mm-dd HH24:mi') as 发帖时间 , u.username as 发帖者 ,count(r.postid) as 回复数from post p , users u ,kind k , reply rwhere p.userid = u.userid and k.kindno = p.kindno and r.postid(+) = p.postidgroup by p.postid , p.title , p.posttime , u.usernameorder by count(r.postid) desc)where rownum <=5;---管理员--管理员--某一回复有问题 删除--某一帖子有严重问题 删除--有部分小问题 修改delete reply where replyid = '0010';delete reply where postid = '0004';delete post where postid = '0004';--用户登录--修改密码--修改个人信息select password from users where userid = '0001';update users set password = '1234567' where userid = '0001';update users set info = '无个人信息' where userid = '0001';
视图
是从一个或多个表(或视图)中导出的行或列的子集
图的优点:
- 视图可用来检索表中所选的列.
- 用视图创建简单的查询,可容易地检索需要频繁调看的结果.
- 视图可用来从多个表中检索数据.
- 用户或用户组可根据视图里指定的准则来访问数据
视图可在不需要的时候被除去,而不影响数据库.
简单视图
从单个表中导出数据. 简单视图可以进行性DML(增、删、改、查)操作
复杂视图 -- 只读
从多个表导出数据. 复杂视图DML易出错,一般 with read only
--通过视图显示数据
可以使用SELECT语句通过视图访问数据。
当通过视图访问数据时,Oracle服务器执行以下步骤:
- 它从数据字典表里回复视图定义的材料.
- 它检查基表的访问权限.
- 它把在视图上的查询转换为在基表上的等价操作.
CREATE [FORCE|NOFORCE] VIEW view_name [(alias [, alias]…)]AS SQLQueryWITH READ ONLY [CONSTRAINT constraint_name]]; -- 创建CREATE OR REPLACE VIEW view_name -- 修改drop view view_name -- 删除rename view_name to newview_name -- 重命名create or replace view chinaUNStudentsas select stuNo, name, sex, age, address from studentwhere majorno in (select majorno from majorwhere facultyno in (select facultyno from facultywhere schoolcode = (select schoolcode from schoolwhere schoolname = '中国大学')))order by score desc;-- 创建视图-- select .. from 视图select * from students;drop view students;rename students to chinaUNStudents;select * from chinaUNStudents;-- 简单视图 DMLupdate chinaUNStudents set sex = '女', age = 21 where stuno = 'S0001';delete from chinaunstudents where stuno = 'S0002';-- insert into chinaunstudents values('S6736', '周生生', '男', 20, '中国南京'); -- 看视图有无包含基表非空列-- 外联部 数据 体育部数据 -- 为不同权限定义视图create or replace view wlbViewas select * from student where unionno = '01'with read only; -- 只读 无法DMLcreate or replace view spViewas select * from student where unionno = '03'with read only;select * from wlbview;select * from spview;-- delete from spview where stuNo = 'S9999';-- 复杂视图create or replace view unionStusas select u.unionno, u.unionname, s.stuno, s.namefrom student s, studentunion uwhere s.unionno(+) = u.unionnowith read only;select * from unionstus;
序列
- 是以有序的方式创建唯一整数值的数据库对象。
- 可以用作表的主键或唯一键。
- 可以减少编写序列生成代码所需的应用代码的工作量
注意点:
- alter 修改时 不能修改 起始值(start with num)
- 使用序列时,就算语句失败,序列取值语言仍然会执
CREATE SEQUENCE sequence_nameINCREMENT BY x -- 值变化START WITH x -- 起始值[MAXVALUE x | NOMAXVALUE] -- 最大值[MINVALUE x | NOMINVALUE] -- 最小值[CYCLE | NOCYCLE] -- 循环[CACHE | NOCACHE]; -- 缓存create sequence stuSeqstart with 1increment by 1;create sequence postSeqstart with 100increment by 2;-- 获取序列值select stuSeq.nextVal from dual; -- 下一个值select stuSeq.currval from dual;select postSeq.nextVal from dual;-- 序列 解释SQL运行出错 也执行一次insert into student values(lpad(stuSeq.nextVal, 5, '0'), '苦参碱', '', '', '', '', '','M0005', '', '', '', '', '');insert into course values(lpad(stuSeq.nextVal, 3, '0'), 'JavaME', '');-- 修改alter sequence postSeq--start with 300 - wrongincrement by 1;--minvalue 200 <= 起始值--maxValue 100 >= 当前值-- 删除drop sequence ps;-- 重命名rename postSeq to ps;
同义词
- 是数据库对象(表、视图和序列)的别名。
- 提供能在查询里被引用的对象的别名。
- 可以为表、视图、序列、过程或任何其他数据库对象创建同义词
create synonym sc for studentCourse;-- public | [private - 不写] public - DBAselect * from studentCourse;select * from sc;drop synonym sc;
索引:
- 数据库中有类似于书的包含内容的有序列表的索引。
- 借助于指针提供对表行的快速访问。
- 是一个存储按序排列的数据的一个单独的表。
- 只包含键值字段和一个指向表中行的指针(而不是整个记录)
使用索引的优点有:
- 查询执行快。
- 实施数据的唯一性。
- 多表检索数据的过程快。
使用索引的缺点有:
- 占用磁盘空间。
- 创建索引需要花费时间。
- 延长了数据修改的时间,因为要更新索引。
可创建两种类型索引:
- 唯一索引
- 非唯一索引
CREATE [UNIQUE] INDEX Index_NameON Table_Name (column1, column2,);-- table PK UK 自动加入unique索引create index stuNameIndexon student(name);select name from student where name = '张三三';
索引适用于:
- 取值范围很大的列。
- 有很多空值的列。
- 在WHERE子句或联接里使用的列。
- 预期查询返回的行数小于表的总行数的2%。
- drop index stuNameIndex;
2014年11月22日 20:06:16

浙公网安备 33010602011771号