DDL_数据库模式定义语言

2014年11月22日 15:53:24

DDL 定义  define
概念:是用于描述数据库中要存储的现实世界实体的语言。一个数据库模式包含该数据库中所有实体的描述定义。
             这些定义包括结构定义、操作方法定义等。
数据库对象    (创建,修改,删除,重命名)
  1. 表         table
  2. 视图      view
  3. 序列      sequence
  4. 索引      index
  5. 同义词  synonym

关键字:
  • CREATE
  • ALTER
  • DROP
  • TRUNCATE
  • COMMENT
  • RENAME
需要满足:
  •   数据完整性
  •   实体完整性      每行唯一  PK
  •   域完整性        合法范围内的值
  •   引用完整性      引用的值应与另一关联
  •   用户定义完整性   自定义规则 age score

约束用于实施数据完整性 
  •   PK  主键
  •   FK  外键
  •   UK  唯一键
  •   CK  检查
  •   NN  非空

  1. -- 创建 create
  2. -- 约束 表级、列级
  3. create table bigKind(
  4. bigKIndNo char(3),
  5. bigKindName varchar2(40) not null, -- varchar2(40 byte|char) -- 非空约束 not null
  6. constraint bigKind_pk_bigKindNo primary key(bigKindNo),
  7. constraint bigKind_UK_bigKindName unique(bigKindName)
  8. );
  9. create table smallKind(
  10. --smallKindNo char(5) primary key, -- 系统名称
  11. --smallKindName varchar2(50) not null unique
  12. smallKindNo char(5) constraint smallKind_pk_smallkindNo primary key,
  13. smallKindName varchar2(50) constraint namenotNull not null
  14. constraint smallKind_uk_smallkindName unique,
  15. bigKindNo char(3) not null constraint smallKInd_FK_bjgKindNo references bigKind(bigKindNo)
  16. );
  17. create table smallKind(
  18. smallKindNo char(5),
  19. smallKindName varchar2(50),
  20. bigKindNo char(3) not null,
  21. constraint sk_pk_skno primary key(smallKindNO),
  22. constraint sk_nn_skname check(smallKindName is not null), -- check约束
  23. constraint sk_uk_skname unique(smallkindName),
  24. constraint sk_fk_bk_bkNo foreign key(bigKindNo) references bigKind(bigKindNO)
  25. );
  26. create table product(
  27. proNo char(10),
  28. proName varchar2(100) not null,
  29. price number(10, 2) not null,
  30. store number not null,
  31. info varchar2(4000),
  32. constraint product_pk_proNo primary key(proNo),
  33. constraint product_ck_price check(price > 0), -- check(列取值范围) not null
  34. constraint product_ck_store check(store >= 0)
  35. );
  36. -- 修改 alter
  37. alter table product add smallKindNO char(5) not null
  38. constraint pro_fk_smkNo references smallKind(smallKindNo);
  39. alter table product add smallKindNO char(5) not null;
  40. alter table product modify smallKindNo char(10); -- 只可改变长度
  41. -- 修改类型 非空等 dropadd
  42. alter table product modify smallKindNo
  43. constraint pro_fk_smkNo references smallKind(smallKindNo); -- 增加约束
  44. alter table product drop constraint pro_fk_smkNo; -- 去除约束
  45. alter table product enable constraint pro_fk_smkNo; -- 禁用、启用约束 disable enable
  46. alter table product drop column smallKindNo;
  47. -- 去除 drop
  48. drop table bigKind;
  49. drop table smallKind;
  50. -- 重命名 rename
  51. rename sss to ss;
  52. BBS论坛
  53. kind 论坛模块 admin
  54. kindNo userName password
  55. kindName
  56. post 论坛帖子 Users 用户
  57. postId userId
  58. title userName
  59. content password
  60. postTime timestamp sex info
  61. userId 必须登录发帖
  62. Reply 帖子回复
  63. replyId
  64. content
  65. postId
  66. userId userId定义表忘了定义 加列夹约束 初始设置为not null -> 可登陆也可匿名
  67. -- 系统约束名可至数据字典中查找
  68. user_constraints
  69. user_tables ...
  70. select * from user_constraints where table_name = 'PRODUCT';
ps:  使用modify修改属性类型时,只能修改该类型的长度,不能更变类型,如果要改变类型,需要先删除drop 再添加add


作业:
创建表
管理员登录
设置模块
选择某一模块名称 发帖 - 用户登录 用户名
查看帖子 - 帖子编号 对帖子 回复 - 登录|未登录
查看所有帖子
分页查看 - 按最新帖最前面 帖子编号 帖子标题 发帖者 发帖时间(转换为习惯结构 不需要秒) 回复数
关注前5 回帖数
管理员
某一回复有问题 删除
某一帖子有严重问题 删除
有部分小问题 修改
用户登录
修改密码
修改个人信息
  1. --------------- 论坛模块
  2. create table kind (
  3. kindno char(5),
  4. kindname varchar2(40)not null,
  5. constraint kind_PK_kindno primary key (kindno),
  6. constraint kind_UK_kindname unique (kindname)
  7. );
  8. ------------------ 用户表
  9. create table users(
  10. userid char (10),
  11. username varchar2(50) not null,
  12. password number(8,0) not null,
  13. sex varchar2(5)not null,
  14. info varchar2(500) ,
  15. constraint users_PK_userid primary key (userid),
  16. -- constraint users_cK_sex check (sex = '男' || sex = '女')
  17. );
  18. ------------------ 论坛帖子表
  19. create table post(
  20. postID char(20),
  21. title varchar2(50) not null,
  22. content varchar2(500) not null,
  23. postTime date not null,
  24. userId char(10) not null,
  25. kindno char(5)not null,
  26. constraint post_PK_postId primary key (postid),
  27. constraint post_PK_title unique (title),
  28. constraint post_PK_userid foreign key (userid)references users(userid),
  29. constraint post_PK_kindno foreign key (kindno)references kind(kindno)
  30. );
  31. drop table post;
  32. ----------------论坛回帖表
  33. create table reply(
  34. replyID char(20),
  35. content varchar2(500) not null,
  36. postid char(20)not null,
  37. userid char(10) not null,
  38. constraint reply_PK_replyID primary key (replyID)
  39. -- constraint post_PK_userid foreign key (userid)references users(userid)
  40. );
  41. drop table reply;
  42. ---userId userId定义表忘了定义 加列夹约束
  43. alter table reply modify userid
  44. constraint reply_FK_userid references users(userid);
  45. ---初始设置为not null -> 可登陆也可匿名 (如何使用disable禁用not null)
  46. alter table reply drop column userid;
  47. alter table reply add userid char(10)
  48. constraint reply_FK_userid references users(userid);
  49. --- admin
  50. create table admins(
  51. username varchar2(40),
  52. password varchar2(10)not null,
  53. constraint admins_PK_username primary key (username)
  54. );
  55. drop table admins;
  56. -----------------------------------------------------------------------------
  57. insert into admins values('admin', '123456'); --插入管理员数据
  58. --管理员登录
  59. select * from admins where username = 'admin';
  60. select password from admins where password = '123456';
  61. --设置模块
  62. insert into kind values('01','学习');
  63. insert into kind values('02','运动');
  64. insert into kind values('03','社会');
  65. insert into kind values('04','军事');
  66. insert into users values('0001','张三','123456','男','张三,中国人最耳熟能详的名字。');--插入用户数据
  67. insert into users values('0002','李四','1234','女','李四,张三的女朋友。');
  68. --选择某一模块名称 发帖
  69. select userid from users where username = '张三' and password = '123456'; --登录
  70. insert into post values('0001','空指针异常 ','可能: 数据源错误,比如数据库名或IP错误','12-11月-14 12.53.00.000000000 下午' ,(select userid from users where username = '张三' and password = '123456') ,'01');
  71. insert into post values('0002','不能执行查询 ','可能: 数据库中表的问题,比如列名不存在 ','12-11月-14 11.42.00.000000000 下午' ,(select userid from users where username = '张三' and password = '123456') ,'02');
  72. insert into post values('0003','表名或列名不存在 ','可能:表不存在或者没有插入数据到表中','12-10月-14 12.33.00.000000000 下午' ,(select userid from users where username = '张三' and password = '123456') ,'03');
  73. insert into post values('0004','非法表达式开始 ','可能:丢失括号 .','17-01月-14 06.53.00.000000000 下午' ,(select userid from users where username = '李四' and password = '1234') ,'04');
  74. insert into post values('0005','找不到符号 ','可能:没导入包','15-11月-14 08.43.00.000000000 下午' ,(select userid from users where username = '李四' and password = '1234') ,'03');
  75. --查看帖子
  76. select *
  77. from post
  78. where postid = '0001';
  79. --登录状态对帖子回复
  80. insert into reply values('0001','我们要在程序书写过程中尽量解决这个问题','0001','0001');
  81. insert into reply values('0002','一楼说的对!','0001',null);
  82. insert into reply values('0003','一楼说的有问题!','0001','0001');
  83. insert into reply values('0004','三楼说的对!','0001',null);
  84. insert into reply values('0005','这个表真的好烦啊~~!','0002','0002');
  85. insert into reply values('0006','五楼说的对!','0002',null);
  86. insert into reply values('0007','难道一定要这样么','0002','0002');
  87. insert into reply values('0008','七楼你太厉害了!','0003',null);
  88. insert into reply values('0009','我们要在程序书写过程中尽量解决这个问题','0004','0002');
  89. insert into reply values('0010','一楼说的对!','0004',null);
  90. --查看所有帖子
  91. 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 回复数
  92. from post p , users u ,kind k , reply r
  93. where p.userid = u.userid and k.kindno = p.kindno and r.postid(+) = p.postid
  94. group by p.postid , p.title , p.content , p.posttime , u.username , k.kindname
  95. order by p.posttime asc;
  96. -- 分页数据查询 - rownum as name
  97. --分页查看 - 按最新帖最前面 帖子编号 帖子标题 发帖者 发帖时间(转换为习惯结构 不需要秒) 回复数
  98. select postid as 序号 ,title as 标题 , content as 内容 , to_char(posttime, 'yyyy-mm-dd HH24:mi') as 发帖时间 , username as 发帖者 , kindname as 板块
  99. from(select rownum as num, postid ,title , content , posttime, username , kindname
  100. from(select p.postid , p.title , p.content , p.posttime, u.username , k.kindname
  101. from post p , users u ,kind k
  102. where p.userid = u.userid and k.kindno = p.kindno
  103. order by p.posttime asc))
  104. where num >= 4 and num <= 6;
  105. ---关注前5 回帖数
  106. select *
  107. 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 回复数
  108. from post p , users u ,kind k , reply r
  109. where p.userid = u.userid and k.kindno = p.kindno and r.postid(+) = p.postid
  110. group by p.postid , p.title , p.posttime , u.username
  111. order by count(r.postid) desc)
  112. where rownum <=5;
  113. ---管理员
  114. --管理员
  115. --某一回复有问题 删除
  116. --某一帖子有严重问题 删除
  117. --有部分小问题 修改
  118. delete reply where replyid = '0010';
  119. delete reply where postid = '0004';
  120. delete post where postid = '0004';
  121. --用户登录
  122. --修改密码
  123. --修改个人信息
  124. select password from users where userid = '0001';
  125. update users set password = '1234567' where userid = '0001';
  126. update users set info = '无个人信息' where userid = '0001';--------------- 论坛模块
  127. create table kind (
  128. kindno char(5),
  129. kindname varchar2(40)not null,
  130. constraint kind_PK_kindno primary key (kindno),
  131. constraint kind_UK_kindname unique (kindname)
  132. );
  133. ------------------ 用户表
  134. create table users(
  135. userid char (10),
  136. username varchar2(50) not null,
  137. password number(8,0) not null,
  138. sex varchar2(5)not null,
  139. info varchar2(500) ,
  140. constraint users_PK_userid primary key (userid),
  141. -- constraint users_cK_sex check (sex = '男' || sex = '女')
  142. );
  143. ------------------ 论坛帖子表
  144. create table post(
  145. postID char(20),
  146. title varchar2(50) not null,
  147. content varchar2(500) not null,
  148. postTime date not null,
  149. userId char(10) not null,
  150. kindno char(5)not null,
  151. constraint post_PK_postId primary key (postid),
  152. constraint post_PK_title unique (title),
  153. constraint post_PK_userid foreign key (userid)references users(userid),
  154. constraint post_PK_kindno foreign key (kindno)references kind(kindno)
  155. );
  156. drop table post;
  157. ----------------论坛回帖表
  158. create table reply(
  159. replyID char(20),
  160. content varchar2(500) not null,
  161. postid char(20)not null,
  162. userid char(10) not null,
  163. constraint reply_PK_replyID primary key (replyID)
  164. -- constraint post_PK_userid foreign key (userid)references users(userid)
  165. );
  166. drop table reply;
  167. ---userId userId定义表忘了定义 加列夹约束
  168. alter table reply modify userid
  169. constraint reply_FK_userid references users(userid);
  170. ---初始设置为not null -> 可登陆也可匿名 (如何使用disable禁用not null)
  171. alter table reply drop column userid;
  172. alter table reply add userid char(10)
  173. constraint reply_FK_userid references users(userid);
  174. --- admin
  175. create table admins(
  176. username varchar2(40),
  177. password varchar2(10)not null,
  178. constraint admins_PK_username primary key (username)
  179. );
  180. drop table admins;
  181. -----------------------------------------------------------------------------
  182. insert into admins values('admin', '123456'); --插入管理员数据
  183. --管理员登录
  184. select * from admins where username = 'admin';
  185. select password from admins where password = '123456';
  186. --设置模块
  187. insert into kind values('01','学习');
  188. insert into kind values('02','运动');
  189. insert into kind values('03','社会');
  190. insert into kind values('04','军事');
  191. insert into users values('0001','张三','123456','男','张三,中国人最耳熟能详的名字。');--插入用户数据
  192. insert into users values('0002','李四','1234','女','李四,张三的女朋友。');
  193. --选择某一模块名称 发帖
  194. select userid from users where username = '张三' and password = '123456'; --登录
  195. insert into post values('0001','空指针异常 ','可能: 数据源错误,比如数据库名或IP错误','12-11月-14 12.53.00.000000000 下午' ,(select userid from users where username = '张三' and password = '123456') ,'01');
  196. insert into post values('0002','不能执行查询 ','可能: 数据库中表的问题,比如列名不存在 ','12-11月-14 11.42.00.000000000 下午' ,(select userid from users where username = '张三' and password = '123456') ,'02');
  197. insert into post values('0003','表名或列名不存在 ','可能:表不存在或者没有插入数据到表中','12-10月-14 12.33.00.000000000 下午' ,(select userid from users where username = '张三' and password = '123456') ,'03');
  198. insert into post values('0004','非法表达式开始 ','可能:丢失括号 .','17-01月-14 06.53.00.000000000 下午' ,(select userid from users where username = '李四' and password = '1234') ,'04');
  199. insert into post values('0005','找不到符号 ','可能:没导入包','15-11月-14 08.43.00.000000000 下午' ,(select userid from users where username = '李四' and password = '1234') ,'03');
  200. --查看帖子
  201. select *
  202. from post
  203. where postid = '0001';
  204. --登录状态对帖子回复
  205. insert into reply values('0001','我们要在程序书写过程中尽量解决这个问题','0001','0001');
  206. insert into reply values('0002','一楼说的对!','0001',null);
  207. insert into reply values('0003','一楼说的有问题!','0001','0001');
  208. insert into reply values('0004','三楼说的对!','0001',null);
  209. insert into reply values('0005','这个表真的好烦啊~~!','0002','0002');
  210. insert into reply values('0006','五楼说的对!','0002',null);
  211. insert into reply values('0007','难道一定要这样么','0002','0002');
  212. insert into reply values('0008','七楼你太厉害了!','0003',null);
  213. insert into reply values('0009','我们要在程序书写过程中尽量解决这个问题','0004','0002');
  214. insert into reply values('0010','一楼说的对!','0004',null);
  215. --查看所有帖子
  216. 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 回复数
  217. from post p , users u ,kind k , reply r
  218. where p.userid = u.userid and k.kindno = p.kindno and r.postid(+) = p.postid
  219. group by p.postid , p.title , p.content , p.posttime , u.username , k.kindname
  220. order by p.posttime asc;
  221. -- 分页数据查询 - rownum as name
  222. --分页查看 - 按最新帖最前面 帖子编号 帖子标题 发帖者 发帖时间(转换为习惯结构 不需要秒) 回复数
  223. select postid as 序号 ,title as 标题 , content as 内容 , to_char(posttime, 'yyyy-mm-dd HH24:mi') as 发帖时间 , username as 发帖者 , kindname as 板块
  224. from(select rownum as num, postid ,title , content , posttime, username , kindname
  225. from(select p.postid , p.title , p.content , p.posttime, u.username , k.kindname
  226. from post p , users u ,kind k
  227. where p.userid = u.userid and k.kindno = p.kindno
  228. order by p.posttime asc))
  229. where num >= 4 and num <= 6;
  230. ---关注前5 回帖数
  231. select *
  232. 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 回复数
  233. from post p , users u ,kind k , reply r
  234. where p.userid = u.userid and k.kindno = p.kindno and r.postid(+) = p.postid
  235. group by p.postid , p.title , p.posttime , u.username
  236. order by count(r.postid) desc)
  237. where rownum <=5;
  238. ---管理员
  239. --管理员
  240. --某一回复有问题 删除
  241. --某一帖子有严重问题 删除
  242. --有部分小问题 修改
  243. delete reply where replyid = '0010';
  244. delete reply where postid = '0004';
  245. delete post where postid = '0004';
  246. --用户登录
  247. --修改密码
  248. --修改个人信息
  249. select password from users where userid = '0001';
  250. update users set password = '1234567' where userid = '0001';
  251. update users set info = '无个人信息' where userid = '0001';



视图
         是从一个或多个表(或视图)中导出的行或列的子集

图的优点:
  •       视图可用来检索表中所选的列. 
  •       用视图创建简单的查询,可容易地检索需要频繁调看的结果.
  •       视图可用来从多个表中检索数据.
  •       用户或用户组可根据视图里指定的准则来访问数据

视图可在不需要的时候被除去,而不影响数据库.
    简单视图
              从单个表中导出数据.      简单视图可以进行性DML(增、删、改、查)操作
    复杂视图  --  只读
              从多个表导出数据.   复杂视图DML易出错,一般  with read only
  --通过视图显示数据 
                          可以使用SELECT语句通过视图访问数据。

当通过视图访问数据时,Oracle服务器执行以下步骤:
  •       它从数据字典表里回复视图定义的材料.
  •       它检查基表的访问权限.
  •       它把在视图上的查询转换为在基表上的等价操作.
  1. CREATE [FORCE|NOFORCE] VIEW view_name [(alias [, alias]…)]
  2. AS SQLQuery
  3. WITH READ ONLY [CONSTRAINT constraint_name]]; -- 创建
  4. CREATE OR REPLACE VIEW view_name -- 修改
  5. drop view view_name -- 删除
  6. rename view_name to newview_name -- 重命名
  7. create or replace view chinaUNStudents
  8. as select stuNo, name, sex, age, address from student
  9. where majorno in (select majorno from major
  10. where facultyno in (select facultyno from faculty
  11. where schoolcode = (select schoolcode from school
  12. where schoolname = '中国大学')))
  13. order by score desc;
  14. -- 创建视图
  15. -- select .. from 视图
  16. select * from students;
  17. drop view students;
  18. rename students to chinaUNStudents;
  19. select * from chinaUNStudents;
  20. -- 简单视图 DML
  21. update chinaUNStudents set sex = '女', age = 21 where stuno = 'S0001';
  22. delete from chinaunstudents where stuno = 'S0002';
  23. -- insert into chinaunstudents values('S6736', '周生生', '男', 20, '中国南京'); -- 看视图有无包含基表非空列
  24. -- 外联部 数据 体育部数据 -- 为不同权限定义视图
  25. create or replace view wlbView
  26. as select * from student where unionno = '01'
  27. with read only; -- 只读 无法DML
  28. create or replace view spView
  29. as select * from student where unionno = '03'
  30. with read only;
  31. select * from wlbview;
  32. select * from spview;
  33. -- delete from spview where stuNo = 'S9999';
  34. -- 复杂视图
  35. create or replace view unionStus
  36. as select u.unionno, u.unionname, s.stuno, s.name
  37. from student s, studentunion u
  38. where s.unionno(+) = u.unionno
  39. with read only;
  40. select * from unionstus;



序列
  •      是以有序的方式创建唯一整数值的数据库对象。
  •     可以用作表的主键或唯一键。
  •     可以减少编写序列生成代码所需的应用代码的工作量
注意点:
  1. alter 修改时  不能修改 起始值(start with  num)
  2. 使用序列时,就算语句失败,序列取值语言仍然会执
  1. CREATE SEQUENCE sequence_name
  2. INCREMENT BY x -- 值变化
  3. START WITH x -- 起始值
  4. [MAXVALUE x | NOMAXVALUE] -- 最大值
  5. [MINVALUE x | NOMINVALUE] -- 最小值
  6. [CYCLE | NOCYCLE] -- 循环
  7. [CACHE | NOCACHE]; -- 缓存
  8. create sequence stuSeq
  9. start with 1
  10. increment by 1;
  11. create sequence postSeq
  12. start with 100
  13. increment by 2;
  14. -- 获取序列值
  15. select stuSeq.nextVal from dual; -- 下一个值
  16. select stuSeq.currval from dual;
  17. select postSeq.nextVal from dual;
  18. -- 序列 解释SQL运行出错 也执行一次
  19. insert into student values(lpad(stuSeq.nextVal, 5, '0'), '苦参碱', '', '', '', '', '',
  20. 'M0005', '', '', '', '', '');
  21. insert into course values(lpad(stuSeq.nextVal, 3, '0'), 'JavaME', '');
  22. -- 修改
  23. alter sequence postSeq
  24. --start with 300 - wrong
  25. increment by 1;
  26. --minvalue 200 <= 起始值
  27. --maxValue 100 >= 当前值
  28. -- 删除
  29. drop sequence ps;
  30. -- 重命名
  31. rename postSeq to ps;



同义词
  •    是数据库对象(表、视图和序列)的别名。 
  •    提供能在查询里被引用的对象的别名。 
  •    可以为表、视图、序列、过程或任何其他数据库对象创建同义词
  1. create synonym sc for studentCourse;
  2. -- public | [private - 不写] public - DBA
  3. select * from studentCourse;
  4. select * from sc;
  5. drop synonym sc;


索引:
  •       数据库中有类似于书的包含内容的有序列表的索引。 
  •       借助于指针提供对表行的快速访问。
  •       是一个存储按序排列的数据的一个单独的表。
  •       只包含键值字段和一个指向表中行的指针(而不是整个记录)
   使用索引的优点有:
  1.       查询执行快。
  2.       实施数据的唯一性。
  3.       多表检索数据的过程快。
   使用索引的缺点有:
  1.       占用磁盘空间。
  2.       创建索引需要花费时间。
  3.       延长了数据修改的时间,因为要更新索引。
  可创建两种类型索引:
  •          唯一索引
  •        非唯一索引 
      
  1. CREATE [UNIQUE] INDEX Index_Name
  2. ON Table_Name (column1, column2,);
  3. -- table PK UK 自动加入unique索引
  4. create index stuNameIndex
  5. on student(name);
  6. select name from student where name = '张三三';

    索引适用于: 
  •       取值范围很大的列。
  •       有很多空值的列。
  •       在WHERE子句或联接里使用的列。
  •       预期查询返回的行数小于表的总行数的2%。
  •         drop index stuNameIndex;


2014年11月22日 20:06:16






















posted @ 2016-09-21 15:53  无丑不成戏如人生  阅读(635)  评论(0编辑  收藏  举报