Oracle Notes (16) Database Design Paradigm

Database design paradigms are an important concept, but this level of importance is for reference only. Using the database design paradigm can make the data table better save data, because no matter how reasonable the design is, if the amount of data is large, there will definitely be performance problems. Therefore, in the development, the only thing that can be called a design collection - try to avoid multi-table association queries in future programs when designing.

1. First Normal Form

The so-called first normal form means that the data columns in the data table cannot be further divided.

For example, now we have the following data table:

CREATE TABLE member (
  mid NUMBER PRIMARY KEY,
  name VARCHAR2(200) NOT NULL,
  contact VARCHAR2(200)
);

At this time, the design is unreasonable, because the contact information is composed of a variety of data: telephone, address, email, mobile phone, zip code, so this design is not in line with, now you can modify the design:

 
CREATE TABLE member (
  mid NUMBER PRIMARY KEY,
  name VARCHAR2(200) NOT NULL,
  address VARCHAR2(200),
  zipcode VARCHAR2(6),
  mobile VARCHAR2(20),
  tel VARCHAR2(20)
);
 

But there are two points to note here:

  • The first point, regarding the name, in foreign watch design, the name is also divided into two categories: surname and first name, but in China it is the name preservation;
  • The second point, regarding birthday, birthday has a special data type (DATE), so it cannot be set as birthday year, birthday month, birthday day;

The so-called indivisible means that all data types use the various data types provided by the database.

Second, the second normal form: many-to-many

Second Normal Form: The non-key fields in the data table have partial functional dependencies on any candidate key fields;

The second normal form can be understood in two ways:

  • Understanding 1: There should be no functional relationship between columns. Now there is the following design:
 
CREATE TABLE orders (
  oid NUMBER PRIMARY KEY,
  amount NUMBER,
  price NUMBER,
  allprice NUMBER
);
 

The current total price of the product (allprice) = the unit price of the product (price) * the quantity of the product (amount), so there is a function dependency;

  • Understanding 2: Reflect it through the design of a data table and complete a student course selection system. If it is said that it is in accordance with the first paradigm, it is as follows:
 
CREATE TABLE studentcourse (
  stuid NUMBER PRIMARY KEY,
  stuname VARCHAR2(20) NOT NULL,
  cname VARCHAR2(50) NOT NULL,
  credit NUMBER NOT NULL,
  score NUMBER
);
INSERT INTO studentcourse (stuid,stuname,cname,credit,score) VALUES (1,'张三','Java',3,89);
INSERT INTO studentcourse (stuid,stuname,cname,credit,score) VALUES (2,'李四','Java',3,99);
INSERT INTO studentcourse (stuid,stuname,cname,credit,score) VALUES (3,'王五','Java',3,78);
INSERT INTO studentcourse (stuid,stuname,cname,credit,score) VALUES (1,'张三','Oracle',1,79);
INSERT INTO studentcourse (stuid,stuname,cname,credit,score) VALUES (2,'李四','Oracle',1,89);
 

这种设计符合于第一设计范式,但是不符合于第二范式,因为程序会存在如下的错误:

  • 数据重复:学生和课程的数据都处于重复的状态,而且最为严重的是主键的设置问题;
  • 数据更新过多:如果说现在一门课程已经有了3000人参加的话,则更改一门课程学分的时候需要修改3000条记录,肯定性能上会有影响;
  • 如果一门课程没有一个学生参加,这门课程就从学校彻底消失了;

如果要想解决此问题,则可以将数据表的设计修改如下:

 
CREATE TABLE student (
  stuid NUMBER PRIMARY KEY,
  stuname VARCHAR2(20) NOT NULL
);
CREATE TABLE course (
  cid NUMBER PRIMARY KEY,
  cname VARCHAR2(50) NOT NULL,
  credit NUMBER NOT NULL
);
CREATE TABLE studentcourse (
  stuid NUMBER REFERENCES student(stuid),
  cid NUMBER REFERENCES course(cid),
  score NUMBER
);
INSERT INTO student (stuid,stuname) VALUES (1,'张三');
INSERT INTO student (stuid,stuname) VALUES (2,'李四');
INSERT INTO student (stuid,stuname) VALUES (3,'王五');
INSERT INTO course (cid,cname,credit) VALUES (10,'Java',3);
INSERT INTO course (cid,cname,credit) VALUES (11,'Oracle',1);
INSERT INTO course (cid,cname,credit) VALUES (12,'Linux',2);
INSERT INTO studentcourse (stuid,cid,score) VALUES (1,10,89);
INSERT INTO studentcourse (stuid,cid,score) VALUES (2,10,99);
INSERT INTO studentcourse (stuid,cid,score) VALUES (3,10,78);
INSERT INTO studentcourse (stuid,cid,score) VALUES (1,11,79);
INSERT INTO studentcourse (stuid,cid,score) VALUES (2,11,89);
 

这种设计与之前讲解运动会-项目-成绩的设计是一样的。

三、第三范式:一对多

例如,现在一个学校有多个学生,如果用第一范式无法实现,而如果用第二范式则表示多对多的关系,即:一个学校有多个学生,一个学生在多个学校,不符合于要求,所以此时可以使用第三范式,参考之前的部门和雇员操作实现,一个部门有多个雇员,所以按照设计编写如下:

 
CREATE TABLE school (
  sid NUMBER PRIMARY KEY,
  sname VARCHAR2(20) NOT NULL
);
CREATE TABLE student (
  stuid NUMBER PRIMARY KEY,
  stuname VARCHAR2(20) NOT NULL,
  sid NUMBER REFERENCES school(sid)
);
 

而在实际的工作之中,第三范式的使用是最多的。

以上的三个范式只是作为参考使用。

 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326127209&siteId=291194637