简体   繁体   English

数据库和表管理

[英]Database and Table Management

I have been creating a web app and am looking to expand. 我一直在创建一个Web应用程序,并且正在寻求扩展。 In my web app I have a table for users which includes privileges in order to track whether a user is an administrator, a very small table for a dynamic content section of a page, and a table for tracking "events" on the website. 在我的Web应用程序中,我有一个供用户使用的表,该表包含用于跟踪用户是否为管理员的特权,用于页面的动态内容部分的很小的表以及用于跟踪网站上“事件”的表。

Being not very experienced with web application creation, I'm not really sure about how professionals would create systems of databases and tables for a web application. 由于不具备Web应用程序创建的丰富经验,因此我不确定专业人员将如何为Web应用程序创建数据库和表系统。 In my web app, I plan to add further user settings for each member of the website and even a messaging system. 我计划在Web应用程序中为网站的每个成员甚至是消息系统添加更多的用户设置。 I currently use PHP with a MySQL database that I query for all of my commands, but I would be willing to change any of this if necessary. 我目前将PHP与查询所有命令的MySQL数据库一起使用,但是如果需要,我愿意更改其中的任何一个。 What would be the best wat to track content such as messages that are interpersonal and also specific user settings for each user. 跟踪内容(例如人际关系消息以及每个用户的特定用户设置)的最佳途径是什么? Would I want to have multiple databases at any point? 我想随时拥有多个数据库吗? Would I want to have multiple tables for each user, perhaps? 我是否想为每个用户有多个表? Any information on how this is done or should be done would be quite helpful. 关于如何完成或应该完成的任何信息将非常有帮助。

I'm sorry about the broadness of the question, but I've been wanting to reform this web app since I feel that my ideas for table usage are not on par with those that experienced programmers have. 我对这个问题的广泛性感到抱歉,但是我一直想对这个Web应用程序进行改革,因为我觉得我对表使用的想法与有经验的程序员所提出的想法并不一致。

Here's my seemingly long, hopefully not too convoluted answer to your question. 这是我看似冗长的答案,希望对您的问题不太复杂。 I think I've covered most, if not all of your queries. 我想我涵盖了大部分(如果不是全部)您的查询。

For your web app, you could have a table of users called "Users", settings table called "UserSettings" or something equally as descriptive, and messages in "PrivateMessages" table. 对于您的Web应用程序,您可以有一个名为“ Users”的用户表,一个名为“ UserSettings”或类似描述的设置表以及“ PrivateMessages”表中的消息。 Then there could be child tables that store extra data that is required. 然后可能会有子表存储所需的额外数据。

User security can be a tricky thing to design and implement. 用户安全性是设计和实现的棘手事情。 Do you want to do it by groups (if you plan on having many users, making it easier to manage their permissions), or just assign individually due to a small user base? 您是要按组进行操作(如果您打算拥有多个用户,以便更轻松地管理其权限),还是由于用户群较小而仅进行单独分配? For security alone, you'd end up with 4 tables: 仅出于安全性考虑,您最终将获得4个表:

Users
UserSettings
UserGroups
UserAssignedGroups

That way you can have user info, settings, groups they can be assigned to and what they ARE assigned to separated properly. 这样,您就可以正确分离用户信息,设置,可以分配给他们的组以及分配给他们的内容。 This gives you a decent amount of flexibility and conforms to normalization standards (as mentioned above by DrSAR). 这为您提供了相当大的灵活性,并符合归一化标准(如DrSAR所述)。

With your messages, don't store them with the username, but rather the User ID. 对于您的消息,不要将其与用户名一起存储,而应与用户ID一起存储。 For instance, in your PrivateMessages table, you would have a MessageID, SenderUserID, RecipientUserID, Subject, Body and DateSent to store the most basic info. 例如,在PrivateMessages表中,您将具有MessageID,SenderUserID,RecipientUserID,Subject,Body和DateSent来存储最基本的信息。 That way, when a user wants to check their received messages, you can query the table saying: 这样,当用户想要检查其收到的消息时,可以查询该表,其中说:

   SELECT * FROM PrivateMessages WHERE RecipientUserID = 123556

A list of tables for your messages could be as such: 您的邮件的表格列表可能是这样的:

PrivateMessages
MessageReplies

The PrivateMessages table can store the parent message, and then the MessageReplies table can store the subsequent replies. PrivateMessages表可以存储父消息,然后MessageReplies表可以存储后续的答复。 You could store it all in one table, but depending on traffic and possibly writing recursive functions to retrieve all messages and replies from one table, a two table approach would be simplest I feel. 您可以将它们全部存储在一个表中,但根据流量以及编写递归函数以从一个表中检索所有消息和答复的情况,我觉得最简单的是两表方法。

If I were you, I'd sit down with a pencil and paper, and write down/draw what I want to track in my database. 如果我是你,我会拿着铅笔和纸坐下来,写下/画出我想在数据库中跟踪的内容。 That way you can then draw links between what you want to store, and see how it will come together. 这样,您便可以在要存储的内容之间绘制链接,并查看它们如何结合在一起。 It helps me when I'm trying to visualise things. 当我尝试形象化事物时,它对我有帮助。

For the scope of your web app you don't need multiple databases. 对于您的Web应用程序,您不需要多个数据库。 You do need, however, multiple tables to store your data efficiently. 但是,您确实需要多个表才能有效地存储数据。

For user settings, always use a separate table. 对于用户设置,请始终使用单独的表。 You want your "main" users table as lean as possible, since it will be accessed (= searched) every time a user will try to log in. Store IDs, username, password (hashed, of course) and any other field that you need to access when authenticating. 您希望您的“主要”用户表尽可能精简,因为每次用户尝试登录时都将访问(=搜索)该表。存储ID,用户名,密码(当然是哈希值)以及您需要的任何其他字段验证时需要访问。 Put all the extra information in a separate table. 将所有其他信息放在单独的表中。 That way your login will only query a smaller table and once the user is authenticated you can use its ID to get all other information from the secondary table(s). 这样,您的登录名将仅查询较小的表,并且在对用户进行身份验证之后,您可以使用其ID从辅助表中获取所有其他信息。

Messages can be trickier because they're a bigger order of magnitude - you might have tens or hundreds for each user. 消息可能比较棘手,因为它们的数量级更大-每个用户可能有数十或数百个消息。 You need to design you table structure based on your application's logic. 您需要根据应用程序的逻辑设计表结构。 A table for each user is clearly not a feasible solution, so go for a general messages table but implement procedures to keep it to a manageable size. 为每个用户创建一个表显然不是可行的解决方案,因此请使用通用消息表,但要执行一些程序以使其保持在可管理的大小。 An example would be "archiving" messages older than X days, which would move them to another table (which works well if your users aren't likely to access their old messages too often). 一个示例是“存档”早于X天的邮件,这会将它们移动到另一个表中(如果您的用户不太可能经常访问其旧邮件,则该表将很有效)。 But like I said, it depends on your application. 但是就像我说的,这取决于您的应用程序。

Good luck! 祝好运!

Along the lines of Cristian Radu's comments: you need to split your data into different tables. 遵循Cristian Radu的评论:您需要将数据拆分到不同的表中。 The lean user table will (in fact, should) have one unique ID per user. 精益用户表将(实际上应该)为每个用户提供一个唯一的ID。 This (unique) key should be repeated in the secondary tables. 此(唯一)键应在辅助表中重复。 It will then be called a foreign key. 然后将其称为外键。 Obviously, you want a key that's unique. 显然,您想要一个唯一的密钥。 If your username can be guaranteed to be unique (ie you require user be identified by their email address), then you can use that. 如果可以确保您的用户名是唯一的(即,您要求用户通过其电子邮件地址标识),则可以使用该用户名。 If user names are real names (eg Firstname Sirname), then you don't have that guarantee and you need to keep a userid which becomes your key. 如果用户名是真实姓名(例如,名字“ Sirname”),那么您就没有这个保证,您需要保留一个用户ID,它成为您的密钥。 Similarly, the table containing your posts could (but doesn't have to) have a field with unique userids indicating who wrote it etc. 同样,包含您的帖子的表可以(但不必)具有一个字段,该字段包含唯一的用户ID,以指示是谁撰写的,等等。

You might want to read a bit about database design and the concept of normalization: (http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html) No need to get bogged down with the n-th form of normalization but it will help you at this stage where you need to figure out the database design. 您可能需要阅读一些有关数据库设计和规范化概念的信息:(http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html) -标准化的形式,但是它将在需要确定数据库设计的现阶段提供帮助。

Good luck and report back ;-) 祝你好运,并回报;-)

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

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