开发者

Should one create a new table for each user on a web app?

开发者 https://www.devze.com 2023-01-30 16:26 出处:网络
I\'m writing my first web app, and I know enough about databases to know that the schema is important but not enough to know how to actually write a good one.

I'm writing my first web app, and I know enough about databases to know that the schema is important but not enough to know how to actually write a good one.

Is there a standard protocol for handling information stored in each user account? My instinct is to have one table that stores the 开发者_如何学编程user's key and log-in info, and a handle to their table (probably the key?), and then have one table for each user.

But I wonder if there are performance issues around having table for each user or if that seems an incredibly stupid way to do it. This seems like it should be a "solved problem" since basically all web apps have user accounts, but I haven't been able to find anything via search. Are there any resources with "solved" schemas for storing various sorts of web data?


Usually you would not create a separate table for each user - this solution does not scale well.

Instead you usually put all users' data into a single table (or one table per type of data) and use conditions in the WHERE clause to ensure that a user can only read/write their own data.


Unless your app is something extremely custom, you will want to choose a web-app framework to work with. For the simple things most common frameworks work just about equally well (though there is some rabid disagreement on that). I will assume that you have a programming language where you feel you are the most strong so I would suggest you let that be your primary guide for a web framework. Some common frameworks by language are (this is not even a close to an exhaustive list):

  • Ruby: Ruby on Rails
  • Python: Django
  • Java: Spring

As to your question about the schema, you might want to look into database normalization. Most frameworks will already have tools to deal with user creation and authentication, but in general you will never want to create a table per user. More common approach is to have a table called users, that had some id as a primary key and that is then used as a foreign key that references other data.


Yeah definitely not a good idea. What you should do, is to get familiar with some web application framework because most/all of them provide this for you already. Good choice is for example Django.

0

精彩评论

暂无评论...
验证码 换一张
取 消