DDL_数据库模式定义语言
- 表 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';
- 视图可用来检索表中所选的列.
- 用视图创建简单的查询,可容易地检索需要频繁调看的结果.
- 视图可用来从多个表中检索数据.
- 用户或用户组可根据视图里指定的准则来访问数据
- 它从数据字典表里回复视图定义的材料.
- 它检查基表的访问权限.
- 它把在视图上的查询转换为在基表上的等价操作.
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;
DDL_数据库模式定义语言的更多相关文章
- mysql-数据库模式定义语言(DDL)
库的管理 /* 一.库的管理 创建.修改.删除 二.表的管理 创建.修改.删除 创建: create 修改: alter 删除: drop */ #一.库的管理 #.库的创建 /* 语法: creat ...
- 数据库基础之一--DDL(数据库定义语言),DCL(数据库控制语言)
Mysql是一个非常典型的C/S结构的应用模型,所以Mysql连接必须依赖于一个客户端或者驱动. 在linux中支持两种连接模式:TCP/IP模式和socket SQL语句的四部分: DDL:数据定义 ...
- 第一天,Mysql安装,DDL(数据库定义语言),DBA,DML(数据库操纵语言),导入外面的sql文件
把“D:\mysql-5.6.22-winx64\bin”添加到系统环境变量path中了,然后在任意目录可访问mysql等命令,这样如登录等操作就不需要进入MySQL安装目录才好执行! MySQL下载 ...
- DDL(数据库定义语言)(五)
一.数据定义语言(Data Definition Language)的基本操作 定义数据库.表等,包括CREATE语句.ALTER语句.DROP语句.CREATE语句用于创建数据库.数据表等,ALTE ...
- MySQL进阶11--DDL数据库定义语言--库创建/修改/删除--表的创建/修改/删除/复制
/*进阶 11 DDL 数据库定义语言 库和表的管理 一:库的管理:创建/修改/删除 二:表的管理:创建/修改/删除 创建: CREATE DATABASE [IF NOT EXISTS] 库名; 修 ...
- Oracle数据库 — DDL:数据定义语言
1.数据定义语言:用于定义数据库的结构,比如创建.修改或删除数据库对象: 包括: CREATE TABLE:创建数据库表:创建表的命名规则: 2.以字母开头:在 1–30 个字符之间:只能包含 A–Z ...
- oracle数据库语言(1)--数据定义语言
1.数据定义语言 (DDL)DATE DEFINITION LANGUAGE 作用是用于增删改 数据库对象 (1) 创建表格 CREATE TABLE EMP ( -------创建 名为 EMP ...
- 数据库与SQL语言
数据库(DB) :长期储存在计算机中.有组织.可共享的数据的集合. 特点:(1)数据按一定的数据模型组织.描述和储存:(2)较小的冗余度:(3)数据独立性较高:(4)易扩展:(5)可共享(不同用户可按 ...
- Sybase数据库常用sql语言
Sybase数据库常用sql语言 1,表备份: --table_name1:需要备份的表; table_name2:备份后的表 SELECT * into table_name2 from table ...
随机推荐
- Android文件选择器的实例分享
本文给大家讲解下Android文件选择器的使用.实际上就是获取用户在SD卡中选择的文件或文件夹的路径,这很像C#中的OpenFileDialog控件. 此实例的实现过程很简单,这样可以让大家快速的熟悉 ...
- Sales_item例子
Sales_item.h #ifndef SALES_ITEM_H #define SALES_ITEM_H #include<iostream> #include<string&g ...
- 高级I/O之记录锁
若两个人同时编辑一个文件,其后果将如何呢?在很多UNIX系统中,该文件的最后状态取决于写该文件的最后一个进程.但是对于有些应用程序(例如数据库),进程有时需要确保它正在单独写一个文件.为了向进程提供这 ...
- ajax生成html双引号问题
//动态创建列表 function createLists(result){ var len=result.length,i; for(i=0;i<len;i++){ $myLi = $(&qu ...
- Android(java)学习笔记91:泛型接口的概述和使用
package cn.itcast_06; /* * 泛型接口:把泛型定义在接口上 */ public interface Inter<T> { public abstract void ...
- 计算 MD5值
// // MD5Value.h // iOSEdu // // Created by littest on 16/2/26. // Copyright © 2016年 littest. All ri ...
- SQL Server 通配符 Wildcard character
SQL Server 通配符 Wildcard character % 任意长度的字符串,如:'%computer%' _ 单个字符,如:'_ean' [] ...
- nagios–配置文件
ngios的配置文件位于/etc/nagios目录下 # ll /etc/nagios 总用量 -rw-rw-r-- nagios nagios 3月 : cgi.cfg -rw-r--r-- roo ...
- windows下安装swoole。
服务器是用了Linux环境,所以安装swoole的过程只要看看文档就好了. 由于编写代码环境是在windows上,需要在windows上安装swoole.以便测试. 好了废话不多说,我们看官网文档解决 ...
- Adobe Edge Animate –使用EdgeCommons加载和播放音频
Adobe Edge Animate –使用EdgeCommons加载和播放音频 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. 在Edge中,可以new一 ...