Mysql Relation DB for mailer - mysql

I want to create a DB for a PHP Mailer App, The program will allow user to Login, Once user logs into the system they will be able to write Send to: email, Subject, from and select HTML template form drop down menu. I have an gd understanding of how to accomplish these but because I am not so good with designing a Database I decided to ask for help here.
So I have identyfied 3 tables :
Users[id, firstname, lastname, email, password]
Message[id, send_to, subject, template_used, from, date, ip_address]
Template[id, temp_name, temp_html].
I am struggling with the relational database concepts but this is how I see it:
User 1..* -----------1..* Message, Message 1..1 ----------- 1..1 Template,
Template 1..1--------1..* Users
Not to sure about these relation and if the relation between User and Message is correct is it this a problem where two tables are 1..*.....?

A good database in relational model requires the tables at least to conform up to the third normalization rule (3N).Please read something about 1N 2N 3N.For now, that is, the existing relationships should be reduced to (one to many). There cannot be many to many if you expect a wonderful RD.
Lets analyse the relationship of user to messages tables:
Situation 1:
One user can have many messages,i.e (user ----messages) ----(one to many)
One message can be for many users, I.e (message -----users)-----one to many
This implies messages to users has many to many relationships. By normalization rules, this is not correct (For the sake of referential integrity).
This can be avoided by introducing another table that would act as intermediary , call it (messageuser). This table will contain the primary key of messages foreign key and user foreign key as it primary key thus composite key.
What therefore do we notice:
(Figure: simple illustration-not standard)-sorry i could not upload the picture to show you that as guided by the site's rules and policies.
We now have three tables with two relationships with each one in the form of (one to many).
i.e user to usermessage (one user has one message with his id) and message to user(one message is send to one user with particular id).
Is that not great.

Related

Intermediate SQL Table : What for?

I'm managing to create my first complicated J2E Solution and in every tutorial I find some sort of intermediary tables usage, like here :
Tables : User, User_Roles, Roles
While logic would simply add a key to user Table referring to it's role on Roles table, why the usage of that intermediary table ?
I thought it's one or two developpers choice, but everywhere I look for a tutorial, I find this sort of sql schema.
Is it better ? Does it help in something particular ? Speed, security ? Cause from a logic point of view, using one table User and a foreign key to Roles is better.
Thank you
This is a common database relationship modeling called M-N (Many To Many). A User can have many Roles, and a Role can be assigned to many Users, so you need the intermediary table. Here's another example: a Teacher can teach many Classes, and each Class can be taught by many teachers (during different semesters, for example). In this case you need a Teacher-Class intermediary table.
A different kind of relationship is 1-N (one to N). A User can have many Telephones, but each Telephone is owned by a single User. In this case, a User's primary key (PK) is exported as a foreign key (FK) into the Telephones table. No need for an intermediary table.

Database model with 3 kind of users

Im thinking about what is the better way to design a database with the following statements:
There are 3 kinds of users (3 differents roles).
They have some common fields, however they have differents fields too.
Solution A:
All user in the same table (some users will have empty fieds depending of their role).
Solution B:
A main table where i will add users and their role. In addition i will create 3 extra tables where i will record with extra fields (depending of the table) and each record will have an id related with the user in the main table.
Solution C: A main table with basic user info, and a second table with the metadata user info where each record means a field of a user. That it's similar than WordPress
Yours is a case of Specialization/Generalization. Since you said User (1, 2, 3) share common properties, it is best to have a General entity - User with all the common attributes.
Then you can Specialize to 3 different types and have the specific attributes to each type.
So, option B is what I think is best solution for your problem.
Option A will cause too many null values and option C is overly complicated.
That depends :-)
Will you ever treat the users the same? E.g. at login time: Will someone just login with a username and this can be any of the three user types? Then you need a user table with a username and a unique index on it. This is solution A or B. I'd prefer B over A, so you can decide which of the fields are nullable for the specific role.
Or will you never deal with a user with an unknown role (such as: a person logs in with a role plus a username, so it suffices to have three different tables each with their own usernames)? This would be three special user tables and no common user table.
Option C would be something easy to implement in order to give users additional attributes, but can become a hassle. Especially when there are obligatory fields and fields that link to other tables (such as a job number that is meant to be the key in the jobs table, but you cannot use a foreign key then). I usually don't take this route, if it is avoidable.

MySQL: One Auto Increment ID for every entities in the database

i'm working on a social network. With activity (like, comment, follow, post, etc...), user, page-to-follow, album, photo and some other thing.
Now, one of the most important things in a social network is the activity feed. I've a structure with activity_feed table, user_table, follow_table, like_table, etc... i was thinking: what if i use a unique AUTO INCREMENT to identify every single entities in my app?
Example:
A user join my site by registration:
Add the activity to activity_feed (and obtain an ID)
Add the user to user_table (with foreign key to activity_feed)
Example 2:
A user upload a photo:
Add the activity to activity_feed
Add the photo to photo table (with foreign key)
This will simplify api calls (ex: http://api.domain.com/ID), DELETING entities, future database sharing and many other things.
I notice that facebook have a structure like this. Obviously indexing can be made on other tables (ex: activity_index_type with activity_type -> user and activity_id).
might be a good solution?
Sorry for my english! :$
This is the concept of Globally Unique ID's (GUID). MySQL has a function to generate them : UUID(). And here's a suggestion on SO for storing GUID's

Users table - one table or two?

i wanna have a Users details stored in the database.. with columns like firstname, last name, username, password, email, cellphone number, activation codes, gender, birthday, occupation, and a few other more. is it good to store all of these on the same table or should i split it between two users and profile ?
If those are attributes of a User (and they are 1-1) then they belong in the user table.
You would only normally split if there were many columns; then you might create another table in a 1-1 mapping.
Another table is obviously required if there are many profile rows per user.
One table should be good enough.
Two tables or more generally vertical portioning comes in when you want to scale out. So you split your tables in multiple tables where usually the partiotioning criteria is the usage i.e., the most common attributes which are used together are housed in one table and others in another table.
One table should be okay. I'd be storing a hash in the password column.
I suggest you read this article on Wikipedia. about database normalization.
It describes the different possibilities and the pros and cons of each. It really depends on what else you want to store and the relationship between the user and its properties.
Ideally one table should be used. If the number of columns becomes harder to manage only then you should move them to another table. In that case, ideally, the two tables should have a one-one relationship which you can easily establish by setting the foreign key in the related table as the primary key:
User
-------------------------------
UserID INT NOT NULL PRIMARY KEY
UserProfile
-------------------------------------------------------
UserID INT NOT NULL PRIMARY KEY REFERENCES User(UserID)
Depend on what kind of application it is, it might be different.
for an enterprise application that my users are the employees as well, I would suggest two tables.
tbl_UserPersonallInformation
(contains the personal information
like name, address, email,...)
tbl_UserSystemInformation (contains
other information like ( Title,
JoinedTheCompanyOn,
LeftTheCompanyOn)
In systems such as "Document Managements" , "Project Information Managements",... this might be necessary.
for example in a company the employees might leave and rejoin after few years and even they will have different job title. The employee had have some activities and records with his old title and he will have some more with the new one. So it should be recorded in the system that with which title (authority) he had done some stuff.

Can i create each table for each user in my social networking site?

I'm creating a social networking site with features similar to Facebook.
I want to start with schema design for my database.
What i thought was to create each table for each user who registers to our site.. am i doing right?
If a million users register to my site, a million tables will be created. how to go on about optimizing this? Please do suggest me techniques to overcome this and some references or books to learn about such concepts will be vry useful..
Thanks in Advance.
This is not the way you want to do it.
What you want to do is have a table (perhaps called 'users') that contains one row for each user that registers. Creating a new table for each user is completely pointless and would cause terrible performance.
Maybe something like this:
TABLE users
- username AS VARCHAR(255)
- password AS VARCHAR(255) (use a hashed password, of course)
- ...
Then when a user registers, simply insert the information they provide into the users table as a new row.
That would be massive overkill. You should probably read up on database design (start with normalisation, but don't overdo it). Then write down what you want to save for each user, and think about how to save it without saving data double.
But I'm pretty sure a table-per-user is not an option for this.
You must be confusing the meaning of the words database, table, field (or column), record (or row).
A database contains all your data for a specific project. There is always one database per project (or almost always)
A table contains all data of a specific entity and by saying entity, I mean an object type that is imaginable as real or seperatelly existing by itself. A person is an entity, a book is an entity, a phone is an entity, a movie is an entity, etc. Each of these would be seperate tables in a database.
A field (or column) is a data type that represents a specific characteristic (feature) of a table's entity. For example a table of users can have the fields: NAME, SURNAME, AGE, etc. These are all features that a user has.
A record (or row) is an actual item of one table. It is a single 'piece' of the table's entity. For example in a table of users, one record is one single user, namely {NAME:"John", SURNAME:"Smith", AGE:"32"}.
In your example, I can tell you for sure that you only need one database. You want to store information for many users, so you need one table called USER. You will need to store features to your users, like: name, surname, age, address, etc., then you will need to create the respective fields in this table: NAME, SURNAME, AGE, ADDRESS, etc. Then you will need to insert your data in the database as records. It will be one record per user you want to store.