码迷,mamicode.com
首页 > 数据库 > 详细

数据库设计

时间:2016-08-24 18:52:01      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

 

范例:数据库创建脚本

-- 删除数据表

DROP TABLE member PURGE ;

-- 创建数据表

CREATE TABLE member (

mid NUMBER  ,

name VARCHAR2(50) NOT NULL ,

address VARCHAR2(200) ,

zipcode VARCHAR2(6) ,

telphone VARCHAR2(20) ,

email VARCHAR2(50) ,

CONSTRAINT pk_mid PRIMARY KEY(mid)

) ;

 

范例:数据库创建脚本

-- 删除数据表

DROP TABLE customer_product_purcase PURGE ;

-- 创建数据表

CREATE TABLE customer_product_purcase (

customerid VARCHAR2(3) ,

name VARCHAR2(30) NOT NULL ,

location VARCHAR2(30) NOT NULL ,

productname VARCHAR2(30) NOT NULL ,

unitprice NUMBER ,

quantity NUMBER ,

CONSTRAINT pk_customerid_productname PRIMARY KEY(customerid,productname)

) ;

-- 增加测试数据

INSERT INTO customer_product_purcase (customerid,name,location,productname,unitprice,quantity)

VALUES (‘C01‘,‘李兴华‘,‘朝阳‘,‘佳洁士‘,8,3) ;

INSERT INTO customer_product_purcase (customerid,name,location,productname,unitprice,quantity)

VALUES (‘C02‘,‘马云涛‘,‘海淀‘,‘高露洁‘,6.5,2) ;

INSERT INTO customer_product_purcase (customerid,name,location,productname,unitprice,quantity)

VALUES (‘C03‘,‘董鸣楠‘,‘西城‘,‘舒肤佳‘,5.7,5) ;

INSERT INTO customer_product_purcase (customerid,name,location,productname,unitprice,quantity)

VALUES (‘C04‘,‘王月清‘,‘朝阳‘,‘夏士莲‘,19.8,1) ;

INSERT INTO customer_product_purcase (customerid,name,location,productname,unitprice,quantity)

VALUES (‘C05‘,‘周艳军‘,‘宣武‘,‘雕牌‘,2.3,9) ;

-- 出现问题的数据

INSERT INTO customer_product_purcase (customerid,name,location,productname,unitprice,quantity)

VALUES (‘C01‘,‘李兴华‘,‘朝阳‘,‘高露洁‘,6.5,20) ;

INSERT INTO customer_product_purcase (customerid,name,location,productname,unitprice,quantity)

VALUES (‘C02‘,‘马云涛‘,‘海淀‘,‘佳洁士‘,8,30) ;

 

范例:使用第二范式进行修改

-- 删除数据表

DROP TABLE purcase PURGE ;

DROP TABLE customer PURGE ;

DROP TABLE product PURGE ;

-- 创建数据表

CREATE TABLE customer (

customerid VARCHAR2(3) ,

name VARCHAR2(30) NOT NULL ,

location VARCHAR2(30) NOT NULL ,

CONSTRAINT pk_customerid PRIMARY KEY(customerid)

) ;

CREATE TABLE product (

productid VARCHAR2(3) ,

productname VARCHAR2(30) NOT NULL

unitprice NUMBER NOT NULL ,

CONSTRAINT pk_productid PRIMARY KEY(productid)

) ;

CREATE TABLE purcase (

customerid VARCHAR2(3) ,

productid VARCHAR2(3) ,

quantity NUMBER ,

CONSTRAINT fk_customerid FOREIGN KEY(customerid) REFERENCES customer(customerid) ON DELETE CASCADE ,

CONSTRAINT fk_productid FOREIGN KEY(productid) REFERENCES product(productid) ON DELETE CASCADE

) ;

-- 增加测试数据 —— 顾客表记录

INSERT INTO customer(customerid,name,location) VALUES (‘C01‘,‘李兴华‘,‘朝阳‘) ;

INSERT INTO customer(customerid,name,location) VALUES (‘C02‘,‘马云涛‘,‘海淀‘) ;

INSERT INTO customer(customerid,name,location) VALUES (‘C03‘,‘董鸣楠‘,‘西城‘) ;

INSERT INTO customer(customerid,name,location) VALUES (‘C04‘,‘王月清‘,‘朝阳‘) ;

INSERT INTO customer(customerid,name,location) VALUES (‘C05‘,‘周艳军‘,‘宣武‘) ;

-- 增加测试数据 —— 商品表记录

INSERT INTO product(productid,productname,unitprice) VALUES (‘P01‘,‘佳洁士‘,8) ;

INSERT INTO product(productid,productname,unitprice) VALUES (‘P02‘,‘高露洁‘,6.5) ;

INSERT INTO product(productid,productname,unitprice) VALUES (‘P03‘,‘舒肤佳‘,5.7) ;

INSERT INTO product(productid,productname,unitprice) VALUES (‘P04‘,‘夏士莲‘,19.8) ;

INSERT INTO product(productid,productname,unitprice) VALUES (‘P05‘,‘雕牌‘,2.3) ;

INSERT INTO product(productid,productname,unitprice) VALUES (‘P06‘,‘中华‘,2.6) ;

INSERT INTO product(productid,productname,unitprice) VALUES (‘P07‘,‘汰渍‘,7.3) ;

-- 增加测试数据 —— 购买记录

INSERT INTO purcase(customerid,productid,quantity) VALUES (‘C01‘,‘P01‘,3) ;

INSERT INTO purcase(customerid,productid,quantity) VALUES (‘C02‘,‘P02‘,2) ;

INSERT INTO purcase(customerid,productid,quantity) VALUES (‘C03‘,‘P03‘,5) ;

INSERT INTO purcase(customerid,productid,quantity) VALUES (‘C04‘,‘P04‘,1) ;

INSERT INTO purcase(customerid,productid,quantity) VALUES (‘C05‘,‘P05‘,9) ;

INSERT INTO purcase(customerid,productid,quantity) VALUES (‘C01‘,‘P02‘,20) ;

INSERT INTO purcase(customerid,productid,quantity) VALUES (‘C02‘,‘P01‘,30) ;

-- 提交事务

COMMIT ;

 

范例:编写数据库创建脚本

-- 删除数据表

DROP TABLE emp PURGE ;

DROP TABLE dept PURGE ;

-- 创建数据表

CREATE TABLE dept (

deptno NUMBER(2) ,

dname VARCHAR2(14) ,

loc VARCHAR2(13) ,

CONSTRAINT pk_deptno PRIMARY KEY(deptno)

) ;

CREATE TABLE emp (

empno NUMBER(4) ,

ename VARCHAR2(10),

job VARCHAR2(9),

sal NUMBER(7,2),

deptno NUMBER(2) ,

CONSTRAINT pk_empno PRIMARY KEY(empno) ,

CONSTRAINT fk_deptno REFERENCES dept(deptno)

);

-- 插入测试数据 —— dept

INSERT INTO dept VALUES (10,‘ACCOUNTING‘,‘NEW YORK‘);

INSERT INTO dept VALUES (20,‘RESEARCH‘,‘DALLAS‘);

-- 插入测试数据 —— emp

INSERT INTO emp(empno,ename,job,sal,deptno) VALUES (7839,‘KING‘,‘PRESIDENT‘,5000,10);

INSERT INTO emp(empno,ename,job,sal,deptno) VALUES (7369,‘SMITH‘,‘CLERK‘,800,20);

INSERT INTO emp(empno,ename,job,sal,deptno) VALUES (7566,‘JONES‘,‘MANAGER‘,2975,20);-

-- 提交事务

COMMIT ;

 

范例:编写数据库创建脚本

-- 删除数据表

DROP TABLE teacher_course PURGE ;

DROP TABLE student_course PURGE ;

DROP TABLE teacher PURGE ;

DROP TABLE student PURGE ;

DROP TABLE course PURGE ;

-- 创建数据表

CREATE TABLE teacher(

tid NUMBER ,

name VARCHAR2(30) NOT NULL ,

CONSTRAINT pk_tid PRIMARY KEY(tid)

) ;

CREATE TABLE student(

stuid NUMBER ,

name VARCHAR2(30) NOT NULL ,

age NUMBER(3) ,

CONSTRAINT pk_stuid PRIMARY KEY(stuid)

) ;

CREATE TABLE course(

cid NUMBER ,

cname VARCHAR2(30) NOT NULL ,

CONSTRAINT pk_cid PRIMARY KEY(cid)

) ;

CREATE TABLE teacher_course(

tid NUMBER ,

cid NUMBER ,

CONSTRAINT fk_tid FOREIGN KEY(tid) REFERENCES teacher(tid) ,

CONSTRAINT fk_cid1 FOREIGN KEY(cid) REFERENCES course(cid)

) ;

CREATE TABLE student_course(

cid NUMBER ,

stuid NUMBER ,

score NUMBER , 

CONSTRAINT pk_tid2 PRIMARY KEY(tid) ,

CONSTRAINT fk_cid2 FOREIGN KEY(cid) REFERENCES course(cid) ,

CONSTRAINT fk_stuid FOREIGN KEY(stuid) REFERENCES student(stuid)

) ;

-- 增加测试数据 —— 教师

INSERT INTO teacher(tid,name) VALUES (1001,‘李兴华‘) ;

INSERT INTO teacher(tid,name) VALUES (1002,‘马云涛‘) ;

-- 增加测试数据 —— 学生

INSERT INTO student(stuid,name,age) VALUES (678,‘庞浩然‘,20) ;

INSERT INTO student(stuid,name,age) VALUES (679,‘贾多多‘,25) ;

INSERT INTO student(stuid,name,age) VALUES (680,‘吴涛‘,22) ;

INSERT INTO student(stuid,name,age) VALUES (681,‘张蕊‘,18) ;

INSERT INTO student(stuid,name,age) VALUES (682,‘范欣‘,21) ;

INSERT INTO student(stuid,name,age) VALUES (683,‘刘婧‘,19) ;

-- 增加测试数据 —— 课程

INSERT INTO course(cid,cname) VALUES(50006,‘Oracle数据库‘) ;

INSERT INTO course(cid,cname) VALUES(50008,‘Android手机开发‘) ;

-- 增加测试数据 —— 教师授课

INSERT INTO teacher_course(tid,cid) VALUES (1001,50006) ;

INSERT INTO teacher_course(tid,cid) VALUES (1002,50008) ;

-- 增加测试数据 —— 学生上课

INSERT INTO student_course(cid,stuid,score) VALUES (50006,678,80) ;

INSERT INTO student_course(cid,stuid,score) VALUES (50006,679,89) ;

INSERT INTO student_course(cid,stuid,score) VALUES (50006,680,67) ;

INSERT INTO student_course(cid,stuid,score) VALUES (50006,681,95) ;

INSERT INTO student_course(cid,stuid,score) VALUES (50006,682,70) ;

INSERT INTO student_course(cid,stuid,score) VALUES (50008,678,70) ;

INSERT INTO student_course(cid,stuid,score) VALUES (50008,680,90) ;

INSERT INTO student_course(cid,stuid,score) VALUES (50008,683,82) ;

-- 提交事务

COMMIT ;

 

数据库设计

标签:

原文地址:http://www.cnblogs.com/doudouxiaoye/p/5804050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!