I'm trying to create a small website in ASP.NET MVC that uses twitter. I want to be able to pull some information about twitter users and store it in a database, which I will update periodically.
I am using the following tables:
Users
user_id - uses the twitter id (int)
twitter_name - nvarchar(255)
last_updated - datetime
TwitterData
user_id
date
num_tweets
num_favorites
num_lists
Unfortunately I'm not really good with databases, so is this a good design? Can I use one table instead?
Thanks in advance,
Sasha
If there is a 1-1 relationship between a record in Users and a record in TwitterData, then you could use a single table. If you were going to have other kinds of data (FacebookData, for example), then you'd keep the two tables, but probably move twitter_name to TwitterData.
Read this for an introduction to Normalization, which will help you get started in designing tables.
http://en.wikipedia.org/wiki/Database_normalization
That looks like a good start to me. You can use the Twitter API as a model for your data schema.
If you are wanting to store normalized data I would go with a separate table for Users and their Tweets.
Related
I am getting started in microservices architectures and I have a couple of questions about the data persistence and databases.
So my understanding is each microservice has it's own database (not necessarily, but usually). But given that case, consider a usual social media platform with users, posts and comments. There will be two microservices, a user's microservice and a posts' microservice. The user's database have a users table and the posts' database has posts and comments tables.
My question is on the posts microservice, because each post and comment has an author, so usually we would create the foreign key pointing to the user's table, however this is in a different database. What to do then? From my perspective there are 2 options:
Add the authorId entry to the table but not the foreign key constrain. If so, what would happen in the application whenever we retrieve that user's data from the user's microservice using the authorId and the user's data is gone?
Create an author's table in the posts' database. If so, what data should that table contain other than the user's id?
It just doesn't feel right to duplicate the data that is already in the user's database but it also doesn't feel right to use the user's id without the FK constraint.
One thing to note, data growth is quite different
Users -> relatively static data.
Posts & Comments -> Dynamic and could be exponentially high compared to users data.
Two microservices design looks good. I would prefer option-1 from your design.
Duplication is not bad, In normal database design this is normal to have "Denormalization" for better read performance. This is also helping in decoupling from users table , may help you to choose different database if require. some of your question what if users data is missing and posts is available, this can be handle with business logic and API design.
I have a scenario that I'm struggling to find a clean solution to. In my CakePHP app, I have a User and Survey model. The idea is that the admin can create surveys, and assign them to users. Users can then respond to the surveys (multiple times) and those responses get stored.
Now, I'm planning to make a Responses model, and associate the Responses with Users via hasMany/belongsTo. Easy. The problem comes in assigning Surveys to Users. Since users will be able to respond to multiple surveys, it makes no sense to try and add "survey_id" columns to my Users table for associations.
What makes the most sense is a new survey_assignments table, that has id, survey_id, and user_id columns.
The problem is I'm unclear how to handle that kind of intermediary association table cleanly, sticking to CakePHP best practices. What I need to do is fairly standard in straight php/mysql, but I want to avoid straight SQL queries, and keep things in line with CakePHP convention.
What's the cleanest way to go about creating and working with a (potentially model-less) association table, while avoiding manual queries?
Of course I found the answer after posting:
a HABTM association with a join table!
I need to design DB for an application using cassandra. How to select column families and super column families? I mean, in RDBMS we select tables right? How to select them in case of cassandra?
Lets take an example of a normal user login app? Could someone give the design for DB?
Thanks for your time :)
In general, you store your information how you intend to retrieve. Duplication and denormalization are common and expected.
The following should provide a helpful starting point for more information:
http://www.datastax.com/docs/0.8/data_model/index
I'm looking into Doctrine as my ORM for an upcoming CodeIgniter project. I've never used an ORM before, but based on what I have read so far, it's a great thing. So, I'd like to get my hands in it.
Questions:
In your experience, what are the benefits of Doctrine?
I noticed that I can identify certain tables to include created_at and updated_at columns. How beneficial is it to know when a record was created and last updated? Should I do this for all my tables?
Thanks.
I haven't personally used Doctrine, but have been told it works well. My understanding is that it requires a significant amount of setup, and works magically after that.
The ORM I typically use with CodeIgniter is called DataMapper ORM, which is a native CodeIgniter solution. Installation is simple (copying a couple files), setting up models is stupidly easy, the tables are simple, and it uses the existing application's database settings. For all the magic without the setup, I'd recommend DataMapper.
Regarding the use of created_at and updated_at columns, only add those columns when you need to track that anyway, such as a blog post or a system user. Specifying those columns lets the ORM handle those fields, so you don't need to, so whenever you create or update objects created from the database, those fields are updated automatically.
What are the Tables that would be present in a social networking site (ex: Twitter).
I have a users table as of now. How to keep track of followers and people I do follow?
Should I maintain a separate table for followers and people I follow?
What are the columns which would be there in those tables?
Please don't think this as Subjective/Off topic. As I am a beginner, I thought experts can guide me to get a good DB design?
Try having a look at Database Answers in particular the data models. They have several different designs for various systems. This one is for a social networking site which may give you an idea of what's required.
You may want to search on SO for other social network database questions. I found this one that had a link to flickr showing a schema which appears to be from Facebook.
Your database design will be based around your system requirements. Without knowing exactly what you are trying to achieve, it is difficult to give you the best design.
you can use this Messenger Database Design Concept: Messenger DB
You can create a separate table for follower/ followed relationships. So, when x follow y, create an entry with follower_id = x.id followed_id = y.id.
You can query the relationship table to look for all the users x has relations with by select * from relationships where follower_id = x.id or vice versa.
When/if x un-follow y, you just have to delete the entry you originally created.