简体   繁体   English

数据库规范化和数据输入(管理员后端)

[英]Database Normalisation and Data Entry (admin backend)

Take a look at the items table below, as you can see this table is not normalized. items下面的items表,因为您可以看到该表未标准化。 Name should in a separate table to normalize it. 名称应在单独的表中进行规范化。

mysql> select * from items;
+---------+--------+-----------+------+
| item_id | cat_id | name      | cost |
+---------+--------+-----------+------+
|       1 |    102 | Mushroom  | 5.00 |
|       2 |      2 | Mushroom  | 5.40 |
|       3 |    173 | Pepperoni | 4.00 |
|       4 |    109 | Chips     | 1.00 |
|       5 |     35 | Chips     | 1.00 |
+---------+--------+-----------+------+

This table is not normalize because on the backend Admin site, staff simply select a category and type in the item name to add data quickly. 该表未规范化,因为在后端管理站点上,工作人员只需选择一个类别并键入项目名称即可快速添加数据。 It is very quick. 非常快。 There are hundreds of same item name but the cost is not always the same. 有数百个相同的项目名称,但成本并不总是相同的。

If I do normalize this table to something like this: 如果我将此表标准化为如下形式:

mysql> select * from items;
+---------+--------+--------------+------+
| item_id | cat_id | item_name_id | cost |
+---------+--------+--------------+------+
|       1 |    102 |            1 | 5.00 |
|       2 |      2 |            1 | 5.40 |
|       3 |    173 |            2 | 4.00 |
|       4 |    109 |            3 | 1.00 |
|       5 |     35 |            3 | 1.00 |
+---------+--------+--------------+------+
mysql> select * from item_name;
+--------------+-----------+
| item_name_id | name      |
+--------------+-----------+
|            1 | Mushroom  |
|            2 | Pepperoni |
|            3 | Chips     |
+--------------+-----------+

Now how can I add item (data) on the admin backend (data entry point of view) because this table has been normalized? 现在,由于该表已被规范化,如何在管理后端(数据输入的观点)上添加项目(数据)? I don't want like a dropdown to select item name - there will be thousands of different item name - it will take a lot of of time to find the item name and then type in the cost. 我不想像下拉菜单一样选择商品名称-会有成千上万种不同的商品名称-查找商品名称并输入费用需要花费大量时间。

There need to be a way to add item/data quick as possible. 需要有一种尽快添加项目/数据的方法。 What is the solution to this? 有什么解决方案? I have developed backend in PHP. 我已经用PHP开发了后端。

Also what is the solution for editing the item name? 另外,编辑商品名称的解决方案是什么? Staff might rename the item name completely for example: Fish Kebab to Chicken Kebab and that will effect all the categories without realising it. 工作人员可能会完全重命名商品名称,例如:Fish Kebab改为Chicken Kebab,这将影响所有类别,而没有意识到。 There will be some spelling mistake that may need correcting like F1sh Kebab which should be Fish Kebab (This is useful when the tables are normalized and I will see item name updated every categories). 会有一些拼写错误,可能需要更正,例如F1sh Kebab,应该是Fish Kebab(当表格标准化后,这很有用,我会看到每个类别的商品名称都会更新)。

I don't want like a dropdown to select item name - there will be thousands of different item name - it will take a lot of of time to find the item name and then type in the cost. 我不想像下拉菜单一样选择商品名称-会有成千上万种不同的商品名称-查找商品名称并输入费用需要花费大量时间。

There are options for selecting existing items other than drop down boxes. 除下拉框外,还有用于选择现有项目的选项。 You could use autocompletion, and only accept known values. 您可以使用自动完成功能,并且仅接受已知值。 I just want to be clear there are UI friendly ways to achieve your goals. 我只想清楚有一些UI友好的方法可以实现您的目标。

As for whether to do so or not, that is up to you. 至于是否这样做,则取决于您。 If the product names are varied slightly, is that a problem? 如果产品名称略有不同,这是一个问题吗? Can small data integrity issues like this be corrected with batch jobs or similar if they are a problem? 像这样的小数据完整性问题是否可以通过批处理作业解决?

Decide what your data should look like first, based on the design of your system. 根据系统设计,首先确定数据的外观。 Worry about the best way to structure a UI after you've made that decision. 做出决定后,担心构建UI的最佳方法。 Like I said, there are usable ways to design UI regardless of your data structuring. 就像我说的那样,不管您的数据结构如何,都有可用的方法来设计UI。

I think you are good to go with your current design, for you name is the product name and not the category name, you probably want to avoid cases where renaming a single product would rename too many of them at once. 我认为您最好使用当前的设计,因为您的名字是产品名称,而不是类别名称,您可能希望避免重命名单个产品会立即重命名它们的情况。

Normalization is a good thing but you have to measure it against your specific needs and in this case I really would not add an extra table item_name as you shown above. 规范化是一件好事,但是您必须根据自己的特定需求对其进行衡量,在这种情况下,我确实不会像上面显示的那样添加额外的表item_name。

just my two cents :) 只是我的两分钱:)

What are the dependencies supposed to be represented by your table? 表应该代表什么依赖关系? What are the keys? 有什么钥匙? Based on what you've said I don't see how your second design is any more normalized that your first. 根据您所说的,我看不出您的第二个设计比您的第一个更标准化。

Presumably the determinants of "name" in the first design are the same as the determinants of "item_name_id" in the second? 大概第一个设计中“名称”的决定因素与第二个设计中“ item_name_id”的决定因素相同吗? If so then moving name to another table won't make any difference to the normal forms satisified by your items table. 如果是这样,那么将名称移动到另一个表将与您的项表满足的普通格式没有任何区别。

User interface design has nothing to do with database design. 用户界面设计与数据库设计无关。 You cannot let the UI drive the database design and expect sensible results. 您不能让UI驱动数据库设计并期望获得合理的结果。

You need to validate the data and check for existence prior to adding it to see if it's a new value. 您需要先验证数据并检查是否存在,然后再添加它以查看它是否为新值。

$value = $_POST['userSubmittedValue']

//make sure you sanitize the variable (never trust user input)

$query = SELECT item_name_id 
         FROM item_name 
         WHERE name='$value';

$result = mysql_query($query);
$row = mysql_fetch_row($result);

if(!empty($row))
{
//add the record with the id from $row['item_name_id'] to items table
}
else
{
//this will be a new value so run queries to add the new value to both items and item_name tables
}

There need to be a way to add item/data quick as possible. 需要有一种尽快添加项目/数据的方法。 What is the solution to this? 有什么解决方案? I have developed backend in PHP. 我已经用PHP开发了后端。

User interface issues and database structure are separate issues. 用户界面问题和数据库结构是独立的问题。 For a given database structure, there are usually several user-friendly ways to present and change the data. 对于给定的数据库结构,通常存在几种用户友好的方式来呈现和更改数据。 Data integrity comes from the database. 数据完整性来自数据库。 The user interface just needs to know where to find unique values. 用户界面只需要知道在哪里可以找到唯一的值。 The programmer decides how to use those unique values. 程序员决定如何使用这些唯一值。 You might use a drop-down list, pop up a search form, use autocomplete, compare what the user types to the elements in an array, or query the database to see whether the value already exists. 您可以使用下拉列表,弹出搜索表单,使用自动完成功能,将用户键入的内容与数组中的元素进行比较,或者查询数据库以查看该值是否已存在。

From your description, it sounds like you had a very quick way to add data in the first place: "staff simply select a category and type in the item name to add data quickly". 从您的描述开始,听起来好像您有一个非常快速的添加数据的方法:“工作人员只需选择一个类别,然后键入项目名称即可快速添加数据”。 (Replacing "mushroom" with '1' doesn't have anything to do with normalization.) (用“ 1”代替“蘑菇”与规范化无关。)

Also what is the solution for editing the item name? 另外,编辑商品名称的解决方案是什么? Staff might rename the item name completely for example: Fish Kebab to Chicken Kebab and that will effect all the categories without realising it. 工作人员可能会完全重命名商品名称,例如:Fish Kebab改为Chicken Kebab,这将影响所有类别,而没有意识到。

You've allowed the wrong person to edit item names. 您允许错误的人编辑商品名称。 Seriously. 说真的

This kind of issue arises in every database application. 在每个数据库应用程序中都会出现这种问题。 Allow only someone trained and trustworthy to make these kinds of changes. 只允许受过训练且值得信赖的人进行此类更改。 (See your dbms docs for GRANT and REVOKE. Also take a look at ON UPDATE RESTRICT.) (有关GRANT和REVOKE的信息,请参见您的dbms文档。另请参阅ON UPDATE RESTRICT。)

In our production database at work, I can insert new states (for the United States), and I can change existing state names to whatever I want. 在工作的生产数据库中,我可以插入新的州(美国),也可以将现有的州名称更改为所需的名称。 But if I changed "Alabama" to "Kyrgyzstan", I'd get fired. 但是,如果我将“阿拉巴马州”更改为“吉尔吉斯斯坦”,我将被解雇。 Because I'm supposed to know better than to do stuff like that. 因为我应该比做那样的事情更了解。

But even though I'm the administrator, I can't edit a San Francisco address and change its ZIP code to '71601'. 但是,即使我是管理员,也无法编辑旧金山地址并将其邮政编码更改为“ 71601”。 The database "knows" that '71601' isn't a valid ZIP code for San Francisco. 数据库“知道”“ 71601”不是旧金山的有效邮政编码。 Maybe you can add a table or two to your database, too. 也许您也可以在数据库中添加一个或两个表。 I can't tell from your description whether something like that would help you. 我无法从您的描述中得知类似的方法是否会对您有所帮助。

On systems where I'm not the administrator, I'd expect to have no permissions to insert rows into the table of states. 在不是管理员的系统上,我希望没有权限将行插入状态表。 In other tables, I might have permission to insert rows, but not to update or delete them. 在其他表中,我可能有权插入行,但没有更新或删除行的权限。

There will be some spelling mistake that may need correcting like F1sh Kebab which should be Fish Kebab 会有一些拼写错误,可能需要更正,例如F1sh Kebab,应该是Fish Kebab

The lesson is the same. 这课是一样的。 Some people should be allowed to update items.name, and some people should not. 应该允许某些人更新items.name,而有些人则不应。 Revoke permissions, restrict cascading updates, increase data integrity using more tables, or increase training. 撤消权限,限制级联更新,使用更多表提高数据完整性或增加培训。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM