relational-database

What is the best approach to retrieve records from one table that stores it's history?

[亡魂溺海] 提交于 2019-12-06 09:39:16
I have one dummy table 'Users', let's call it U, and another table 'DataFromUsers', DU. From time to time i'll save a new record into DU, related to one U, and now we have 2 records pointing to the same user U. It'll come the time when i'll have lots of entries in DU resembling to this condition. The best approach i can see to solve this problem is always storing it's insertion date, once all i care is the most recent DU from U, and am only keeping the old entries for analytical purposes. Another way would be having a FK in U pointing straight to the most recent record from DU, but that would

Rails 5: Displaying form from Joined table

最后都变了- 提交于 2019-12-06 08:57:52
This is my first foray into joined tables and Many-to-Many relationships and I am relatively new to Ruby/Rails as well. This is a direct follow up to a previous question where I built out the appropriate related tables/Models. But for sake of clarify, I'll redefine the layout here... My Models: order.rb class Order < ApplicationRecord belongs_to :user has_many :quantities has_many :meals, through: :quantities end meal.rb class Meal < ApplicationRecord has_many :quantities has_many :orders, through: :quantities end quantity.rb class Quantity < ApplicationRecord belongs_to :order belongs_to

Why is multi-value field a bad idea in relational databases

亡梦爱人 提交于 2019-12-06 06:18:51
问题 Having been working with Mongodb and Solr/Lucene, I am starting to wonder why multi-value field for relational databases are (generally) considered an bad idea? I am aware of the theoretical foundation of relational database and normalization. In practice, however, I ran into many use cases where I end up using an meta table of key-value pairs to supplement the main table, such as in the cases of tagging, where I wish I don't have to make multiple joins to look up the data. Or where

What is the purpose of a database table that contains only primary and foreign keys?

荒凉一梦 提交于 2019-12-06 06:11:34
I'm trying to understand a simple music database design. There are some tables that contain only foreign keys and a primary key. I'm not sure how and when to use these tables or what to insert into them. The design looks like this: Track: id primary key title duration live-performance (true or false) year Artist: id primary key name ArtistTrack: id primary key artistID trackID Album: id primary key title AlbumTrack: id primary key albumID trackID track-number Genre: id primary key name GenreTrack: id primary key genreID trackID For example, if I insert a track into the Track table and an

Is it possible to use JDBC as an abstraction layer for RDBMS?

跟風遠走 提交于 2019-12-06 06:02:17
JDBC provides an API, which may be used to connect to different RDBMS or similar datastores. But the datastores differ in implementation (e.g. SQL dialects). Is it possible to use JDBC in such a way, that my queries and statements work on most common RDBMS (e.g.: Oracle, PostgreSQL, SQL Server, MySQL)? That question is interesting for me at two aspects: * Common SQL (INSERT, UPDATE, SELECT etc.) * Accessing Meta data (getting information about tables and columns) I am currently experimenting with an self written persistence framework and want to plug a JDBC datastore under it. So if I write a

Core Data: Abstract entities and inheritance relationships

自闭症网瘾萝莉.ら 提交于 2019-12-06 05:48:56
My exact model is complicated to explain, so say that I'm modeling fruits and their seeds in Xcode's Core Data modeler. Here's some "pseudo Core Data code": abstractEntity Fruit attribute sweetness relationship Seed abstractEntity Seed attribute shape concreteEntity Apple inherits Fruit concreteEntity Orange inherits Fruit concreteEntity AppleSeed inherits Seed concreteEntity OrangeSeed inherits Seed The reason I modeled in this way is that I want to be able to fetch a mix of fruits and sort them by their seed shapes. Given this model, here's my question: Is there any direct way in the Xcode

How to store taxes in database?

倾然丶 夕夏残阳落幕 提交于 2019-12-06 05:16:55
问题 I need to add per-province/state taxes to my project. I'm debating whether I should add a many-to-many relationship between Provinces and Taxes, or just add a tax1_name, tax1_rate, tax2_name, tax2_rate to each province. I don't think any place has more than 2 taxes? I will also need to store the tax rate at the time of purchase with each invoice. So my options are add 2 many-to-many tables, or add 8 fields. Which would you go with, and why? Or I could just have 1 combined tax. I don't think

Implementing table level check constraint

萝らか妹 提交于 2019-12-06 03:46:58
We have a table that contains prices that are depending on a base amount. As an example let say that if the base amount is less or equal to 100 then the price is 10 but if the base amount is greater that 100 but less or equal to 1000 then the price is 20 and finally if the base amount is greater than 1000 then the price is 30. A simplified version of our table for this should be something like this: PRICE_CODE START_RANGE END_RANGE PRICE_AMOUNT 100 0,00 100,00 10,00 100 100,01 1000,00 20,00 100 1000,01 99999999,99 30,00 110 0,00 99999999,99 15,00 With columns level check constraints you can

Why would I want to use a non-relational database?

你。 提交于 2019-12-06 03:09:56
问题 The latest craze in databases seems to be centered around non-relational databases. Why? It seems kind of counterproductive. For example, it makes much more sense to me to express my data in a relational way (example code in Django + SQL for tables): class Post(models.Model): name = models.CharField() created = models.DateTimeField(auto_now_create = True) class Comment(models.Model): text = models.TextField() post = models.ForeignKey('Post') created = models.DateTimeField(auto_now_create =

Count visitor hits per day, last week and last month

老子叫甜甜 提交于 2019-12-06 02:23:24
I make a site with articles and I want to count the views of each article for displaying most popular articles: today, this week and this month. How would you do the database schema for that? If it is enough to know the number of times an article was displayed you could use something like this: daily_article_views( ,article_id ,view_date ,view_count ,primary key(article_id, view_date) ,foreign key(article_id) references articles ); For each article view, you would lookup a row in the table using the Current Date and the ID of the article currently being viewed. If a row exists, you increment