I have created a sample blog site to work with that stores user information in separate tables based on the type of information.
For instance CustomerGeneral table would contain all user's username, password, and ID(primary key) values.
CustomerContact table would contain email, address, contactId(primary key) and ID(foreign key) values.
and lastly the CustomerPicInfo table which contains picId(primary key), ID(foreign key), picInfo and picPath values.
Ive seen approaches where each user has their own table instead but have never seen a proper demonstration of it to learn more. I am curious of what method professional php developers go about with the creation of their user and perhaps in a little bit of detail the method that is took to do so. Also if there is something wrong with my approach, i would like just a summary of my fault so that I can further perfect it.
Related
I've just started exploring SQL databases, but I've run into an issue with how I store 'compound' structures in an existing table (if that's even the right way to go about it). For example, let's say that I have a database table with rows of users, where each user has a Unique ID, a hashed password, an email address, a phone number, etc.
Simple enough. But, then I want to allow each user to create and store an array of posts. Each post would have a post id, content, date, and various other metadata. If this was C++, I would probably have an array/vector of Posts as a member of the User class, and than I'd store an array/vector of User objects somewhere. Is it possible to store a table within a table in SQL, so that each user has access to their own individual table of posts?
Or, would it be better to create two separate tables (a users table, and a posts table), using some common element (like user ID or user name) to retrieve user-specific data from the posts table, and vice-versa?
I'm trying to understand how to implement a complex database that might be able to manage a large number of users, with user-specific sets of data like posts, messages, etc. So what might be a good approach to take going forward?
As you already mentioned, in relational data model, you can define two tables like below:
table 1 : Users
user_id user_name
----------- ------------------
1 'Tom'
2 'John'
table 2 : Posts
post_id user_id content post_date
-------- ---------- ------------------- ---------------------
1 1 'Hello, I am Tom.' 2014-04-02 14:14
2 1 'good bye' 2014-04-02 20:10
3 2 'I am John' 2014-04-02 22:22
You can read an introductory article here:
Relational_model:
http://en.wikipedia.org/wiki/Relational_model
Hope this helps.
You don't store table within table. You can store data in multiple tables and assign primary key for one table and foreign key for another table.
Read about Primary key, Foreign key and Relational Model.
Once your these concepts are cleared read about Database Normalization
You don't store tables within tables. As your third paragraph suggests, the strategy is to use some common key to "relate" table rows to each other.
The "unique ID" you describe is usually called a "primary key". You might have a table of users with a primary key that auto-increments each time you add a record. A function would be available to you so that after inserting, you could determine what the primary key is of the record you just added, so that you can add records to other tables that refer to the primary key of the users table.
You should probably read about Database normalization ant the relational model, specifically about the differences between Normal Forms.
With regard to selection of a field to relate posts to users, I suggest you don't use the username, and instead use some internal reference that isn't visible to the users. While your application might not allow it now, if you wanted to offer users the opportunity to change their username, tying internal database structure to something based on user input would only cause problems in the future.
I am not sure if what I am trying to do is even possible but, if it is, I am obviously not Googling properly and would appreciate any assistance I can get here, even if it is just a link to an "Idiot Guide".
Okay, at the moment, I have a database table of 150-odd records. Each record contains basic details (name, location, contact information, etc.) and login credentials (UserID, password, et al). These details are captured by the website admins (i.e. no general public registration) after the prospective user has undergone a successful interview process. When a record is created, a 6-char "username prefix" is assigned to the user (e.g. 'UNPREF') and this, along with the auto-incremental UserID (e.g. 125), is used as the username (e.g. UNPREF125) to log into the website. However, the username is not actually stored in the the database. Instead, when a user logs in, the login script splits the provided username and the two chunks are checked against their relevant fields.
In addition to this primary user table, there are a number of other tables which contain additional information (for instance, educational qualifications, work history, etc), which are linked to the user by means of the UserID, as per the primary table. Now, both users and admins can update a user's data and, therefore, I have created a field for each row that logs who last modified the record (modby) and when (modon) so that, if there are any shenanigans, I can ascertain who fiddled last and, in theory, deal with that particular individual without any "he said/she said" nonsense.
Now here is the tricky bit. My users and admins are stored in separate databases on separate servers (the latter being beyond my control) but I have recently discovered the joys of Federated Tables, which work brilliantly. One small quirk, tho; because my users and admins are stored in separate databases and because I want to maximise the number of records I can store in a single database (there is a size limit of 100mb per database), with the company's current rate of expansion and each branch requiring two admin accounts, it is not an improbable scenario that a user and an admin will end up with the same UserID. Therefore, the modby fields store the full username (i.e. UNPREF125 - admins get their own, unique Username Prefix so as to differentiate between admins and users)
Now, perhaps it is because I am such a newbie at Federated Tables but I can't seem to find a way to compare a field in a table on Server A (i.e. modby) with 2 separate fields (i.e. unprefix and userid) in the Federated Table, called from Server B, but I have come up with a workaround by creating an additional field in Server B's table, namely username, which stores the merged values (namely 'DBPREF125') and modby is checked against this instead, which works fine (I'm sure there is an easier way but I will save that lesson for another time).
Now, here is my question. The admin table is currently small (only 26 records) and so I captured the usernames manually, using phpMyAdmin, but I would prefer to avoid having to manually create usernames for the 150+ records in my users table. Is there any way I can get MySQL to pull the values of the userid and unprefix fields, join them together and store the result into the username field of the same record or would I need to turn to PHP for this and, if so, how would I go about this?
I apologise for the length of my question but I hope this will help explain why Google was not my friend today.
Many thanks in advance.
To store the combination in the table:
UPDATE TableB
SET username = CONCAT(unprefix, userid);
Or you can just use it when comparing:
SELECT *
FROM TableA a
JOIN TableB b
ON a.modby = CONCAT(b.unprefix, b.userid);
I am working on a user based social network. I am building the site in PHP and I want to use a MySQL database to store user data. I can create databases/tables no problem (I use phpMyAdmin)
I am not sure how many tables are necessary and what would be more practical for my web application. Would it be smart to have many tables? For example, a USERS table. With column names USER_ID, EMAIL, PASSWORD, LAST_LOGIN and then a table named USER_SETTINGS that would hold the account settings for each user, and another table named POSTS with the names and values attributed to a "status update". Or is smart to have everything in one table? What is the best practice?
Definately do NOT keep "everything in one table". You'll likely end up with "many tables", but that sounds bad - basically, you should segment your data based on logical usage.
For instance, if you DID keep posts in the users table - how would that work? What happens when they make a new post - would you add another field? (bad) - or add another item TO a field and separate by a character (bad)...etc. The only real way to do it is to have another table. You should definitely NOT keep posts in the same table as users.
As far as 'profile data' (or whatever you want to call it), I like to keep it separate - some people like to keep it in the users table - matter of preference there.
In your case, I'd suggest something like this:
//users table
id,
email,
password,
last_login,
//...
//profiles table
id,
user_id,
profile,
age,
gender,
//...
//posts table
id,
user_id,
data,
created (datetime),
modified (datetime)
I'm presently building a social networking site as well. DO NOT keep everything in one table. In fact I'd go as far as to say, you CANNOT keep everything in a single table without encountering massive issues fairly immediately.
Where users are concerned, I like to keep passwords in a separate table with a hidden user id junction. Profile data itself depending upon how you wish to enforce data integrity for validation and output may involve tables junctioned to your users table.
I would also keep all posts in a separate table. This is purely from the prospective that you can then query according to user id, then limit to the number of posts, or posts appropriate to whatever you're viewing. Simply put, to have them in the users table is like saying that you are what you write and it is you, rather than saying you're separate yet related objects.
I need a table where to keep imported contacts (emails) by users, something like an address book.
Now the table look like this: imported_contacts: id, user_id, email, etc....
I'm thinking to create a table imported_contacts: id, email, etc and another table user_contacts: id, user_id, imported_contact_id, date, etc to avoid keeping duplicate emails in table imported_contacts. So with the new idea I keep in imported_contacts all imported email in the other table user_contacts I keep relationship between users and imported_contacts.
Is this a useful idea?
If I understand this correctly, if two users add the same contact, it is stored on just one row in the table, and both the users see the same row.
I see two problems with this:
if one user updates the email (or any other information of the contact), the information will also appear updated to the second user, and the initial information of the second user would be lost.
I don't know if you store other fields besides email, but if you do, you must consider that they might have different values, depending on the user; for example, if you store the name of the contact, the first user might write the contact's full name, and the second user, the high-school nickname.
Now it really depends on what this application is used for. If it's just for the internal use of one company, their client database for example, then it might be useful to have any changes propagate to all the users, so problem no. 1 would not really be a problem. But otherwise it wouldn't be ok.
I am building a service which provides a newsletter system for the users.
My question is, how to organize it on the database? user opens account -> there is a news row on the data base -> how the email will be stored? I thought about something like:
user#mail.com,HASHCODE|user2#anothermail.com,HASHCODE|someone#mail.com,HASHCODE ..
(that will be stored on one field of the user's row, HASHCODE for remove the email)
Then using explode() to order it in an array. but I don't know if it's the best way to order the mails.. what do you think?
Why don't you store emails in separate table UserEmails and make a relationship with user table. For starting point you may look at this link
Useremail table will have three fields UseremailID email UserID
UseremailID email UserID
1 sss#ss.com 1
2 asasf#ssf.com 1
I would recommend you to read some relational database so that you get some idea about tables and relationships
You should consider using a table structure like this:
Table 'subscription'
id int(20) PK auto_increment
email varchar(100) UNIQUE index
This will cause you having to insert a new row into the table with a ID and a e-mailaddress (which will both be unique so you dont get double records)
I would create a table to store the newsletters and another one to create the relation between users and newsletters so you'll have a better control over your information.
Three tables: User, User_Newsletter, Newsletter
The User_Newsletter will only store the user_id and newsletter_id
Database services don't seem to be so flexible (even though they were introduced to be). Normal UNIX filesystem hierarchy and plaintext files are the best way to store information. You don't know the internal structure of a database. But you know everything about your filesystem, including the file permissions and encryption
For example, take Croud Mail, a free e-mail newsletter service from me. I don't use databases, but it the coding is very flexible and safe.