My Database Design Flaw...Explain a better way PLEASE.... ((Correct Way)) - mysql

First lets start off with I am writing a program in VB.net and have a database set up and everything works fine as of now. The program is user based and requires the user to log into the program and it will show them all of there user information, orders, settings, fees etc, etc... and everything that is needed. The issue I have is right now I have each user set up with a different database. I have the user login and connect to a database that just holds the Users and initial program start up settings. Along with this it loads the users database address and password into the program so I can then access there database and pull from all the tables relating to them.
I feel that having a database for each user is a bit overboard but each database pertains to the specific person and the information can be sensitive including address, amount of money made, and many other things so I don't know how to set it up to be most productive because if i have 50 users I don't want to create 50 databases.
Please help with this. Even if you just point me to several places to read and learn I would love that.
Thank you in advance.

Creating seprate db for each user is not a way to go. You have create a new user table in 1 db and add userid in your ref tables. Below is an example how to achieve this
User - Userid, UserName, Pwd etc.
Address - Userid, Addressid, HouseNum, StreetName, City etc.
Shipping details - Userid, Shippingid, Shippingdate etc.
In all your queries based on Userid pull data specific to the logged in user.
Also you should always connect to your db using some service account from your application. Validate if user is authenticated or not using some stored proc or query.

Related

Update a NULL column that was added

I'm not a coder, but from time to time I have to interact with out client database. The company I work for does online education. The problem is that we have two SQL databases
Client_personal
client_educational
Client personal keeps all of the name, email, address, phone type of information. Client educational keeps track of what classes they have purchased. I need to take the information from client educational and tag the client personal information in our CRM. The problem is that the the two databases only have one common field "client id" and my CRM only allows me to search for duplicates by client name. So basically I need to add the client name column to my client educational database. I've added it, but it just says null. Anybody have any advise?
Your question is a little vague but assuming:
They're on the same machine
The table is called clients in both databases
The "client name" field is called clientName in both databases
The "client id" field is called clientID in both databases
You want to copy the data from one table into the other as a one off
You can use a user account with access to both databases
As always please don't run this on your real data. Try a mock first.
something like:
UPDATE client_educational.clients
SET client_educational.clients.clientName = Client_personal.clients.clientName
FROM client_educational.clients
INNER JOIN Client_personal.clients
ON client_educational.clients.clientID = Client_personal.clients.clientID
If you don't actually want to copy data as a one off, you should create a view or use join statements instead.

Approach to store user login and password on database when there are some roles totally different

I am creating site and there are 3 roles on it. Administrator, Teacher, Parent. User with any role has access to site, but 1 role has full access, teachers can only put marks for students, parents can only watch these marks.
I understand how to separate roles into scripts. Just save some parameter depending on what database would return when I check login and password.
So! Administrator has only login name and username. Also it is possible to store information about how to contact the user to make system more user-friendly. That's all.
Teacher has firstname, lastname, midname (father's name), address, phone etc. And it must have login and password fields...
Parent has Firstname, Lastname, Midname only. And sure somewhere I should to store data about login and password.
So should I just check this tables 1 by 1 and depending on table I will find $login and $password I inputted in I will return role? I don't think it is a nice idea. Suggest something else please.
What I think to do. I think that it would be not very bad idea if I will create 1 more table like users where I will store: id, username, login, role, user_id (need to think about it). The problem is user_id. How exactly this table will be connected to others? For example user_id` would be 1. 101% there would be 1 row with such id.
I want to ask about exprience of your self-build systems. May you suggest something please!
You're right. It doesn't make sense to have different tables for users. I would recommend you to store authorization and personal data in separate tables. And of course you'll need additional table for roles:
The idea behind this scheme is to keep thin users table for fast authorization. If authorization was successful, than you should check permissions from roles or get personal data from user_data tables.
Look at this sql-fiddle for DDL.

How does MySQL deal with multiple inserts at the same time with unique values?

Suppose I have a website which allows users to create an account. I have user A and user B trying to register for my website. User A and User B both has chosen the same user name and the user name field in the Database table is unique. Both hit Register at the same exact time and the network conditions are the same let's assume some how the query reaches the database at the same time. How does MySQL deal with this? What happens?
If my question is too big to answer here please refer me to some source with the info so that I may study it.

DB design - store selection in database

I'm working on a web application where I need to do some research before I implement the database. I hope you can help me make some good decisions before I start to code.
Today i have a database that among other things contains about two million contacts in a table
Contact:
cid, name, phone, address, etc...
Users of the application can search the contact table based on different criteria, and get a list of contacts.
Users are stored i a separate database table
User: uid, name, email, etc...
Now I want to make the users able to store a search result as a selection. The selection has to be a list of cid's representing every contact in the search result the user got. When the selection is stored, a user can open the selection and add notes, statuses etc to the different contacts in the selection.
My first thought is to make a selection table and a selection-contact mapping table like this:
Selection: sid, name, description, uid, etc
SelectionContactMap: sid, cid, status, note, etc...
With an average selection size between 1 000 and 100 000 contacts, and several thousand users storing many selections, I see that the SelectionContactMap table is going to grow very big very fast.
The database is MySql and the application is written in PHP. I'm on a limited budget so I can not throw unlimited hardware on the task.
I'm I on the wrong way here?
Do you have any suggestions to solve this the best possible way?
Other database?
MySql specific suggestions, table type etc?
Other database design?
Any comments and suggestions are appreciated.
Thanks in advance :)
-- Tor Inge
Question: What happens if the results of the query change - eg: a selected contact no longer has the chosen attribute or a new contact gets added?
If the answer is "The result set should be updated" - then you want to store the criteria in the database, not the results themselves.
If you need to cache the results for a period of time, this may be better handled by the application, not the database.

Database Schema help needed for a social networking site

I'm in middle of creating a social networking site something like facebook, and got struck up with database schema design for it.. from previous questions i posted here, i confirmed that i cant create new database or tables for a new user who registers onto my site. I need to insert new row [thats my only option(correct me if i'm wrong!)]. but, this works out for registration for the first time. what if the user posts something new on his profile.. where should i insert this update.. coz, i cant insert them into rows as dey correspond to each user.. and too many columns cannot be created.. what is the solution for this.?
schema =>
TableName : User_content
userId Name College City Status_Updates Messages
1 a sfd fds fsds sdds
2 f dfg dfd fdf dfd
what if user 1 updates something.. what i need to do nw. Think its a status update, how to go about it.?
Before you start thinking about this social network site you dream if
Developing you need to understand database modelling fundamentals. This link will help you with some simple concepts. http://www.databaseanswers.org/tutorial4_data_modelling/index.htm
Add tables based on what you want to allow your users to do and the related data you need to store as a result. Some example tables:
'user_account': User account information (e-mail address, password) - referenced by user ID
'user_profile': Basic user profile information - referenced by user ID
'user_status': User status message - referenced by user ID
I have the sense that this is your first major database application, and if so, you need to learn more about database design in general before you take on creating an application like this. There are many things to consider when designing a database schema and designing it well from the start is key. The core of your application will rely on how well your data is organized and accessible, so ensure you spend enough time developing a good design.
Good luck!