如何避免表适当的表设计中的冗余条目 [英] How to avoid redundant entries in the table-proper table design

查看:73
本文介绍了如何避免表适当的表设计中的冗余条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有一张包含以下条目的表: -

Hi All
I have a table with the following entries:-

CustName |	CustId |CustomerType|ProdID |ProdName|Units|PurchaseDate
Mr.James Butt	100JSBTM	R	1001	SVS1	5	6/15/2016

Mrs.JosephineDarakjy 101JEDYF	F	1011	SVS1	11	6/15/2016

Mr.ArtVenere	102ATVEM	F	1021	SVV2	4	6/15/2016





现在ProductName有更多的子类型常见的group_ProdID和(子产品ID的次要扩展)。问题是如果一个人购买特定产品的几个子类型,所有与客户相关的条目似乎都需要重新编写。任何解决这个问题的方法,以避免重复的条目)



我尝试过:





Now ProductName has further subtypes with the common group_ProdID and (minor extension to sub product ids).The thing is if a person purchases few subtypes of a specific product,all the customer related entries seems needs to rentered. Any workaround this problem to avoid the repetitive entries)

What I have tried:

 CustName  |	CustId |CustomerType| ProdID    | ProdName|Units |PurchaseDate
Mr.James Butt	    100JSBTM	R	     1001SVST1	 SVS1T1	   5	  6/15/2016
Mr.James Butt	    100JSBTM	R	     1001SVST2	 SVS1T2	   5	  6/15/2016
Mr.James Butt	    100JSBTM	R	     1001SVST3	 SVS1T3	   5	  6/15/2016

推荐答案

您需要了解数据库规范化

数据库规范化的基础知识 [ ^ ]

1NF,2NF,3NF和BCNF DBMS教程| Studytonight [ ^ ]

数据库规范化技术 [ ^ ]



在这种情况下你可以(例如,有客户,产品和订单的表格。

You need to learn about Database Normalisation
The Basics of Database Normalization[^]
1NF, 2NF, 3NF and BCNF in Database Normalization | DBMS Tutorial | Studytonight[^]
Database Normalization Techniques[^]

In this case you could (for example) have tables for Customer, Product and Orders.
create table Customer
(
	Id int identity(1,1) Primary Key,
	CustId nvarchar(50),
	CustName nvarchar(125),
	CustType char(1)
)
create table Product
(
	Id int identity(1,1) Primary Key,
	ProdID nvarchar(50),
	ProdName nvarchar(125)
)
create table Orders
(
	OrderId int identity(1,1),
	C_Id int, -- Foreign key to Customer Id
	P_Id int, -- Foreign key to Product Id
	Units int,
	PurchaseDate date
)



对于您在示例中包含的数据,您将拥有这些值


For the data you included in your example you would have these values

insert into Customer values ('100JSBTM', 'Mr.James Butt','R')
insert into Product values
('1001SVST1', 'SVS1T1'),
('1001SVST2', 'SVS1T2'),
('1001SVST3', 'SVS1T3')
insert into Orders values
(1,1,5,'2016-6-15'),
(1,2,5,'2016-6-15'),
(1,3,5,'2016-6-15')



然后你会使用这个查询得到你的结果表


And then you would get your results table back using this query

SELECT CustName, CustId, CustType, ProdID, ProdName, Units, PurchaseDate
FROM Orders O
INNER JOIN Customer C ON O.C_Id = C.Id
INNER JOIN Product P ON O.P_Id = P.Id



注意我的示例未经过优化 - 请参阅链接以获取更多想法


Note my example is not optimised - see the links for further ideas


这篇关于如何避免表适当的表设计中的冗余条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆