SQL: good database design for cards? - sql

This is a question about model design for SQL or SQLite.
Suppose you have some card game which has many reusable cards and the players make decks out of those card (e.g. MtG)
Naturally you do not want to make new instances of the cards for every deck that a user might create. That would have a lot of redundancy.
Thus there should be a "master" card (with all of its associated info).
My question is then, how to best integrate the cards (and quantity there of) in a given deck?
(I am not actually making this database, it just highlights the design of having a user having any number collections of multiple items in various quantities from some general stock).

Have three tables: One table has the data on the cards (name, cost, etc.). Another table has decks (deck name, creator, etc.). And another table has the cards in the deck (deck_cards) (linking card_id to deck_id with quantity).
EDIT: addendum: This is the same set up (and rationale) as the classic go-to example used in all database design primers: products, orders, and order-line-items. The only piece missing is "customers" (which could be added if you wanted to link decks to players or deck-builders).

I would imagine something like:
Cards - Table containing card details
Card_ID Card_Name Card_Dmg Card_Attribute Card_Health
123 Fire Mage 20 Fire 100
345 Water Man 6 Water 200
037 Earth Dwarf 10 Earth 150
Players - Table containing Player details
Player_ID Player_ForeName Player_Surname
1 Fred Smith
Player_Cards - Table detailing the players deck, with a new row for each card they own, e.g:
Player_ID Card_ID
1 123
1 345
1 037

Related

How to check string similarity from two tables with multiple words input in BigQuery

I have two tables containing a list of company names. The first one would be the index table therefore the value inside would be clean and the format is consistent. The second table contains user-input company information and therefore there might be typos and format inconsistencies.
The first one (the index table) looks similar to this:
company_name | industry
Apple Inc Technology
Amazon Inc Retail
Kraft Heinz Food Processing
New York Life Insurance Company Insurance
Tesla Inc Tesla
Walmart Inc Retail
The second table (user input table) looks similar to this
company_name
Apple
Apple Inc.
Amazon, Inc
Kraft
New York Life
Tsla
Walmart
Notice that the second table does not have the industry column since the main goal would be to add the industry column to the second table, but since there is no key we can't simply join the two tables.
I think the first step would be to compare the similarity between the company_name and if it is similar enough we can just assume it is the same.
I have done some research and I think we would need to use Levenshtein Distance. Honestly, I am not very familiar with that method, but from my limited understanding, I wonder if it is applicable for inputs containing multiple words (e.g New York Life Insurance Company) or it is just effective for one-word input (e.g Apple).
Any suggestions or guidance would be greatly appreciated.

If row contains value, return value from column A value, for all rows

I've seen some similar posts, but none with a really helpful answer for my particular issue. I'm a programmatic advertising data analyst, so i'm trying to associate a many-to-many relationship.
We run "personas", which is a group of apps. A persona has many apps, and apps have many personas. I have the data organized by persona: each row is a persona, and each column in that row contains one of the apps comprising that persona. Each persona has a different number of apps. i.e.
Row Persona App 1 App 2 App 3
1 Casino Persona "Slot Kings" "Wild Casino" "Real Gambling App"
2 RPG Persona "Dragon Valor" "KOTOR"
3 Sports Persona "MLB: The App" "Real Soccer" "Hockey Fans 2016"
4 Gen-X Females "Scrapbook App" "Baby Monitor" "PostMates"
So i know which apps belong to each persona. I'm now trying to determine which personas belong to each app. I'd like to create another worksheet that switches "apps" and "personas." e.g.
Row App Persona 1 Persona 2 Persona 3
1 Slot Kings "Casino Persona" "Slot Persona"
2 KOTOR "RPG Persona" "Star Wars Fans" "SciFi Persona"
3 MLB: The App "Sports Persona" "Baseball Fans"
I can't figure out any way to do this an insane nested statement, VBA, or a similarly crazy array formula.
I understand what you're asking for. However, trying to rearrange your data with either formulas or VBA would take a lot of time and effort; and is also quite unnecessary.
The layout of your data is good for viewing, but is not suitable for any kind of data analysis. If you want to perform analysis, data should always be stored in a database format. See this article for more info:
https://support.office.com/en-us/article/Guidelines-for-organizing-and-formatting-data-on-a-worksheet-90895CAD-6C85-4E02-90D3-8798660166E3
For your data, you should just have 2 columns, one for Persona and one for App. Then on every row, you list just one possible combination of Persona and App. It doesn't matter in what order you place the data.
With your data set up correctly, you can now create PivotTables that automatically arrange your data either way. It's also possible to easily count the number of apps in each persona and vice versa and then also create charts. Look up PivotTables and PivotCharts for more info.
To create the top left table, put Persona into the Rows field and then put App into Rows field as well. (Turn off subtotals and totals to make the table neater, and change the layout to Tabular to see the field names.)
To create the top right table, put App into the Data field instead.
To create the bottom tables, add the fields in the opposite order.
Note that if you have more details about either personas or apps that you want to record, they should be stored in separate tables. This is now getting into the realm of designing a relational database. Look for references on database design for more information.

Most appropriate way to store/retrieve User Input in a eCommerce iOS application?

I'm a bit confused with Sqlite, Core Data, NSUserDefaultsand PropertyList. I know what is what, but not a very clear idea of about where to appropriately use them.
I know that there are lots of tutorials, but I'm good at learning through situation based understanding. So kindly do help me to understand this in the situation that I'm facing right now and to make use of the available options wisely.
I'm working on an ECommerce iOS (native) application, where I'm highly dependent on API's for data display. Now I'm in need of recording user's review for a product and send it over through an API.
ie. I have three components, rating title, rating value(for that title) and another rating title ID. I'm defining with an example, I need to store multiple rows with details,
Components Data to be stored
**Title** - Quality | Value | Price
| |
**Rating** - 2 | 3 | 1
| |
**TitleID** - 10 | 11 | 12
Like this, there will be so many entries, i.e, the number of components differs for various users, for some users, there might be more than three components, which must be saved & send through an API. So how should I save these data? which is the RIGHT way to save these data temporarily?
If I understand you correctly, as vaibhav implied your question seems pretty general and probably relates more to structuring your data to fit your requirements than to technical aspects of the iOS / CoreData environment. In that vein, I’ll offer a few thoughts I’d have in structuring a data structure for quality ratings per your description.
If your ratings will always be for the three categories you show, i.e. Quality, Value and Price, I wouldn’t over-complicate things; I’d just use three properties in a rating record to hold the values that a user assigns in his/her rating of a product (just showing selected attributes and relationships in all following lists):
Product
name
Rating
ratedProduct (many to one)
qualityRating Int
valueRating Int
priceRating Int
Done this way you’d need to associate the values with their types in code for the APIs, such as (where item is a retrieved rating record):
display(product: item.ratedProduct.name, quality: item.qualityRating, value: item.valueRating, price: item.priceRating).
On the other hand, you may be describing a more generic approach that would allow for ratings categories that vary more frequently, or perhaps vary among products. This could apply where, for example, ratings include how well things fit for clothing but not for other products like books. In that case, you’d need a more complicated structure where a product could have a variable number of ratings of different types, so you’d need another layer of entities that let you create an arbitrary number of rating records that applied to a product.
Here you'd create a separate rating record for each rating that a user assigned to a product.
The simplest form of that structure would be like the following:
Product
name String
UserEvaluation
ratedProduct (many to one)
productRating (one to many)
ProductRating
ratingType (many to one)
value Int
RatingType
ratingTitle String
ratingID String or Int
Then you’d have to have a bit more structure where you'd list the product and then access the ratings with a loop that cycled through the set of all of the ratings linked to the product record somewhat like this (where item is a retrieved UserEvaluation):
displayTitle(product: item.ratedProduct.name)
for rating in item.productRating {
displayRating(ratingTitle: item.productRating.ratingType.title, ratingValue: item.productRating.value)
}
You'd probably want to combine these into a method that takes the name and an array of ratings.
To keep track of things, you’d also probably want to create another entity that defined product classes and specified what specialized ratings applied to each class (like fit for clothing and mileage for cars). By default, you also may want to allow for a few generic rating types that apply to all products (like the quality and price ratings you show). For this approach, the full structure would look like this:
Product Category
title
ratingType (many to many)
Product
productType (many to one)
UserEvaluation
ratedProduct (many to one)
productRating (one to many)
ProductRating
ratingType (many to one)
value Int
RatingType
ratingTitle String
ratingID String or Int
With this structure, once a product is assigned a productType, the application would know what ratings to ask for in the UI.
You could try building more complicated rating records with all of the types that apply to a product category, but that would get very messy if the applicable categories vary over time. You could also create a "custom" rating type that let a user specify a title and input a rating, in which case you'd need a text field in the rating record that only applies if the ratingType is "custom".
I hope this helps…

How Can I generate a crystal reports using 3 tables?

I have 3 tables:
Store (Store_Id, Store_Name, Store_Location)
Bike(Store_Id, Bike_Id,Bike_Model, Bike_Price)
Parts(Store_Id, Bike_Id, Part_Id, Part_Description, Part_Price)
The Store is related to the Bike using Store_Id, the Bike parts is related to the Bike using bike_id. Each bike is assembled with different parts.
I want to create report that displays all the stores, bikes, and the all the bike parts used on a bike that has "Pink streamers in the description.
I honestly don't know where to start.
Please take in consideration that this database can contains thousands of bikes.
As others have said, this is a very basic question and reading some of the basics can give you the help you need.
To answer the question: in the Database Expert you will see your 3 tables. Link them together by dragging and dropping the related fields on each other. ie. link Store.Store_ID --> Bike.Store_ID --> and Bike.Bike_ID --> Parts.Bike_ID.
In the record selection formula set the description value: {parts.part_description} = "Pink Streamers".
This will give you all stores and the bikes in those stores that have "Pink Streamers"

Search products with parent and child categories

I'm building a shopping cart website and using SQL tables
CATEGORY
Id int,
Parent_Id,
Description varchar(100)
Data:
1 0 Electronics
2 0 Furniture
3 1 TVs
4 3 LCD
5 4 40 inches
6 4 42 inches
PRODUCTS
Id int,
Category_Id int
Description...
Data:
1 5 New Samsung 40in LCD TV
2 6 Sony 42in LCD TV
As you can see I only have one column for the last Child Category
Now what I need to do is search by Main Category at homepage, for example if the user clicks to Electronics, show both TVs as they have a Parent-Parent-Parent Id at Electronics, keeping in mind that Products table do have only one column for Category.
Shall I update the Products Table and include 6 columns for category childs in order to solve this? Or how can I build an effective SQL Stored Procedure for this?
Thank you
Jerry
in Oracle, you would use CONNECT BY
If you're using SQL 2008 then you might want to look at the HIERARCHYID data type. Otherwise, you might want to consider redesigning the Category table. How you have it modeled now, you have to use recursion to get from children notes to parents or from parents down through children.
Instead of using the linked list model (which is what you have) you could use the nested set model for hierarchies. Do a search on Joe Celko and Nested Set Model and you should be able to find some good descriptions of it. He also wrote an entire book on modeling trees and hierarchies in SQL. The nested set model requires a bit of set up to maintain the data, but it's much easier to work with when selecting out data. Since your categories will probably remain relatively stable it seems like a good solution.
EDIT: To actually answer your question... you could write a stored procedure that sits in a WHILE loop, selecting children and collecting any products found in a table variable. Check ##ROWCOUNT in each loop and if it's 0 then you've gotten to the end. Then you just select out from your table variable. It's a recursive (and slow) method, which is why this type of a model doesn't work very well in many cases in SQL.
Under almost no circumstances should you just add 6 (or 7 or 8) category IDs to your products table. Bad. Bad. Bad. It will be a maintenance nightmare among other things (what happens when your categories go 7 levels deep... then 8... then 9.
Use recursive CTEs to do this ! works like a dream ! http://msdn.microsoft.com/en-us/library/ms186243.aspx