Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

SQLite creating Tables

How can I create a table for each user of my message app, i have one table for all users named = USERS, i want to create a table every time a new user is added to the USERS table? Please Help me!

3 Answers

What you should do is create a relational map. It's essentially a drawing of what the schema or structure will look like. What you will need is to utilize is Primary keys and Foreign Keys. This will allow you to associate columns from one table, with data in a column from another table.

Now, the tricky part comes in when trying to save the date of each message. You can't group messages all into a single expanding field in a row. Because there would be no way to associate the date. You also have to think about what you might not have thought about. Things like, will I need to add a block user feature? How am I going to Delete messages without deleting the users? Say I like usersNames with Contacts? If I remove a message I don't want to remove the user for it could effect my Contacts list. That sort of thing.

As I am writing as fast as I can think. I can tell this will be a long one so stay with me here lol.

I would do this!

Table 1
ID, UserName

Table 2
UserName, Message, Date

I would make a primary key with ID in Table 1 because it should never be null. I would link UserName from Table 1 with UserName in Table 2 with a foreign key.

If I text you for the first time. It creates a new Row in table 1 with an ID that is auto-incremented. So if I were the first person to text you, I would have an ID of 1. Then my username Ryan.

Table 1
ID,  Username
1    | Ryan

My text message date and anything else I wanted would actually go in table 2 but be linked together by UserName

Table 2
UserName,  Message,   Date
Ryan       Hello       9/9/2009

Now, if I text you again. I would ask, does this userName exist in the UserName Column of table 1. YES! Ok cool, let's grab that UserName and create a new Row in Table 2 with the UserName Message and Date etc. So each new message is stored in table 2 as a new row. This will allow you to save the date of each individual message.

Table 1
ID,  Username
1    | Ryan


Table 2
UserName,  Message,  ---   Date
Ryan       Hello           9/9/2009
Ryan       Whats up       9/9/2009

This does mean that Table 2 will have several rows with the same UserName. You might want to save the date by day and seconds. That way if two messages come in close together, you can still query them seperately. You may get a lot of messages in a single day from the same user. You may or may not want them associated with such a broad date. Not sure if it matters or not.

If you do do date, you may want a third table called date. Removing Date from Table 2 and replacing it with say an ID_2. That way you can match up ID_2 from Table 2 with a new Table 3 called Date. You can use the ID_2 and some column in the date table. So that if multiple messages come in from the same user in the same day. You only save 1 date for that user. And all their messages are associated with a single date. This would use less memory.

Than your tables will look like this after a bunch of people text you.

Table 1
ID,  Username
1    | Ryan
2   | Sally
3   | Hottie
4   | Crazy 



Table 2
UserName, Message,  ---   Date
Ryan         Hello         9/9/2009
Ryan         Whats up      9/9/2009
Sally        Yo            9/10/2009
Crazy        WOAH!        9/11/2009
Hottie       Hey Babe     9/11/2009
Crazy        WTF!         9/12/2009
Sally        Dinner?     9/13/2009

Does this make sense? This way you can query the number of unique users you have. With Table 1. Than you can query all the messages where userName in Table 2 equals (either Table 1 UserName or you can give a userName).

ALSO when you delete messages. You can delete them from Table 2. No need to touch Table 1. Table two will find the Message and delete it.

Say you deleted all the messages but mine.

Table 1
ID,  Username
1   | Ryan
2   | Sally
3   | Hottie
4   | Crazy 



Table 2
UserName, Message,  ---   Date
Ryan       Hello          9/9/2009
Ryan       Whats up       9/9/2009

Now let's say Crazy is getting out of hand. What if I want to block them, but I don't want to delete their contact data that I associated with it at some point. I can add a column to Table 1 called BLOCK. Which wont save the texts, userName or anything in Table 2.

Table 1
ID,  Username    BLOCK
1    | Ryan           N
2   | Sally           N
3   | Hottie          N
4   | Crazy           Y

Does this make sense at all? I kinda just started typing and 15 minutes later this is what I have lol.. Let me know. Thanks!

:O that was a super answer!!!! THANKS A LOT!!!... you were right it was better to make multiple tables but not one for each user, and the block thing was great!!! I am going to give it a try and see how it goes!!! ♥♥♥

Thanks! It's hard to think of everything but that is the whole reason OO programming exists. To make modifications and changes quickly with minimal damage. One should look at their DB and break it up too so that it makes sense. Ya never know what you might need to do in the future.

Hello,

You don't actually want a new table every time there is a new user. How about a new ROW every inside the Users Table. So it looks like this for example.

                                               Users Table
-----------------------------------------------------------------------
ID |FirstName |  LastName |  Age | PhoneNumber | Address |
__| _________|__________ |____|_____________|________|  
__| _________|__________ |____|_____________|________|
__| ________  |__________ |____|_____________|________|
---------------------------------------------------------------------

This way each time a new user is added. They are added into a new ROW. This way, you can run a query against a single table to get all your user data.

Creating an individual table for all the user data is not really an except able way to back end develop. You could create a foreign key to link to some other table for additional user info or tag a user based on something etc etc..

To create a table with say for example the table above would be something like this.

CREATE TABLE Users
(
ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
FirstName varchar(255) NOT NULL,
LastName varchar(255),
Age int NOT NULL,
PhoneNumber varchar(255),
Address varchar(255),
PRIMARY KEY (P_Id)
)

This will create a table with those fields. Then I can insert code into the table from say a form. I fill out a form with all the data and it will autoincrement the ID to the next number, and add that user data into a new row.

Does this make more sense to you? Let me know.

ahhh ok ♥♥♥ I understand, my original idea was to create a new table for each user that sends me a message, so for example, if you send me a message it will create a new table with your name, your actual message, the time the message was send, and also add the messages I send to you. So every time i want to access all messages between you and me will be stored in one table. How can I accomplish that idea on SQLite, should I use one big table for all the messages that all the users send me? Thanks ♥♥♥!

Ahh so keeping track of the time and date of a message sent along with your responses and and the actual message associated with it etc. Now you are talking lol. Thats a great idea. You still dont want a new table per user, but you need multiple tables linked together by some column in the table. Give me a moment to think about it and I will tell you exactly how you should do. Its been a long day and I am far to many beers deep now to explain it but I'll do it first thing in the am lol. Get ready lol