One to many mapping in Cassandra(Ecomerce data schema) - database

i am new to cassandra db and i am making simple ecommerce schema. In relational world we have following schema like
Category table (id,name),
Product table (id,categoryId,name,price,discounted_price)
so in cassandra how can i model my data if i have following queries
select all categories;
select all products;
select specific product with category name
select specific categories with their products
And if i update category name does it impact to all tables?

Here's a possible data model:
CREATE TABLE categories (
category_id text,
category_name text,
...
PRIMARY KEY (category_id)
)
CREATE TABLE products (
product_id text,
category_id text,
product_name text,
category_name text,
price decimal,
discounted_price decimal,
PRIMARY KEY (product_id)
)
CREATE TABLE products_by_category (
category_id text,
product_id text,
product_name text,
category_name text,
PRIMARY KEY ((category_id),product_id)
)
To get a list of categories:
SELECT category_name FROM categories
It's a full table scan which should be fine unless you have hundreds of categories.
To get a list of products:
SELECT product_name FROM products
As above, this is also a full table scan and will be problematic if you have thousands of products and/or a large cluster.
To select a specific product with the category name:
SELECT product_name, category_name FROM products WHERE product_id = ?
To get a list of products for a category:
SELECT category_name, product_name FROM products_by_category WHERE category_id = ?
Cheers!

Related

How do i update a column with specific values to reference the primary key ID of another table?

My goal is to normalize my table. My table before normalization is called Product Specifications, to normalize it I have two other tables called buyers_table and product_table
Table "Product Specifications"
Columns: Product_id , model_number, buyer
Buyers_table
Column: Buyer_id, buyer
Product_table
product_id, model_number, buyer_id
How do I insert the buyers from my product_specification table into my product_table but have the values show up as the buyer_id value instead of buyer?
Sorry I am new to this. This is a simplified version of the database that I have set up for my company.
use this design for database
Product_buyer: Columns:Id, Product_id , Buyer_id
Buyers_table: Column: Id, Buyername
Product_table: Id, Model_number
Product_Specifications: Id,Product_id,Productname,Price
then For insert data to database
First enter the information in the Product_table table
Retrieve the latest Product_table table record and enter Id as Product_id and the other of the information in the Product_Specifications table.
Enter the buyer information in the Buyers_table table when purchasing
Product_buyer
Create tables
create table Product_table
(
Id int identity,
Model_number int,
primary key(Id));
create table Product_Specifications
(Id int identity,
Product_id int,
Productname nvarchar(150),
Price int,
primary key(ID),
foreign key(Product_id) references Product_table);
//**********************
//and define other table
//......................
Retrieve product information
select Product_table.Id,Product_table.Model_number,Product_Specifications.Productname,Productname.Price
from Product_table join Product_Specifications
on Product_table.Id = Product_Specifications.Product_id
Retrieve Product_buyer information
select Product_table.Id,Product_table.Model_number,Product_Specifications.Productname,Productname.Price,Buyers_table.Id,Buyers_table.Buyername
from Buyers_table join Product_buyer on Buyers_table.Id = Product_buyer.Buyer_id
join Product_table on Product_buyer.Product_id = Product_table.Id
join Product_Specifications on Product_table.Id = Product_Specifications.Product_id

How to Design a Table for scoring system in Cassandra

I have a table for my products in Cassandra. In this table I store the product name and seller name (there may be thousands of sellers) and score (Product score of the seller) so as following
CREATE TABLE products
(
product_name varchar,
seller_name varchar,
score int,
primary key (product_name, seller_name)
);
I need to update score in my code
UPDATE products SET score = 2 WHERE product_name = "iphone-7" AND seller_name = "jack" ;
Everything is fine, except for Select Query:
SELECT * FROM products WHERE product_name = "iphone-7" ORDER BY SCORE DESC;
I need to get products by score order But as you know, it is not possible to sort without score being the primary key But if I put the score in the primary key, it will be not possible to update it what's the solution?
The solution is to have two tables that cover your requirements. For example the second table could be
CREATE TABLE products_score
(
product_name varchar,
score int,
primary key (product_name, score)
);
so you populate both when writing and use the second one for ordering by score

Normalizing Products > Category relation in SQL Server

I am creating a SQL Server database and I have a question about one relation. I have the two entities Product and Category. The relation is one to many, so one product can have one category, but one category can have many products.
Which way is normalized and why (which normalization form)?:
Product { productId, productName, productPrice }
Category { categoryId, Name }
Product { productId, productName, productPrice, categoryName }
If Category has more attributes and not only name I would make another table without doubt. But in this case I am not sure whether I have to make new table since it contains only name or leave it as varchar column. Isn't that going to create a lot more columns and take more space in database?
I think you should create the following two tables:
1. Category { categoryId, Name }
2. Product { productId, categoryId, productName, productPrice }
Considering that the categoryId in category table is a Primary Key, you should creata a foreign key constraint on categoryId in the Products table referencing to categoryId in the category table.

multiple prices 1 item

I'm wondering if this would be the best way to build a table for an item that can have more than 1 price. Each product is identified by its model_number. Should I use a prefix before the model_number for each individual seller? I can't use model_number for the primary key. For example:
seller_product_id model_number seller price
SELLER_1_MODEL_NUMBER MODEL_NUMBER seller_1 9.99
SELLER_2_MODEL_NUMBER MODEL_NUMBER seller_2 19.99
For the sake of loose coupling, I suggest you build seperate Items and Price tables, and have another table (called Junction table) Item_Price which maps many items to many prices as you like.
This is called Many-to-Many relationship
Basically, it links an Item with its itemId to a Price with priceId, and stores this link in an Item_Price itemPriceId (or whatever you call the 3rd primary key)
Here's a sample diagram EDIT: sorry about the previous diagram.
Here's a sample SQL DDL of 3 tables, plus 2 junction tables to associate Item to Price, and Seller to Item.
CREATE TABLE Item (
item_id int PRIMARY KEY,
..
..
)
CREATE TABLE Price (
price_id int PRIMARY KEY,
..
..
)
CREATE TABLE Seller (
seller_id int PRIMARY KEY,
..
..
)
-- This is the junction table for Item to Price mapping.
CREATE TABLE Item_Price (
item_id int REFERENCES Item (item_id),
price_id int REFERENCES Price (price_id),
PRIMARY KEY (item_id, price_id)
)
-- This is the junction table for Seller to Item mapping.
CREATE TABLE Seller_Item (
seller_id int REFERENCES Seller (seller_id),
item_id int REFERENCES Item (item_id),
PRIMARY KEY (seller_id, item_id)
)
One attribute that have many properties? Sounds like one-to-many relations... go for a new table have foreign key help you out.
Products
prod_id (PK) | model_no
Sellers
seller_id(PK)|seller_name
Pricing
price_id (PK)|price|prod_id (FK) | seller_id(FK)
yes You can a prefix before the model_number for each individual seller.
If you still have stuff then let me know more clear your problem

Table with multiple languages

I want to store multiple translations of an text in a MSSQL database.
for example one table product with the columns
ProductId
ProductPrice
ProductNameId
primairy key = ProductId
and a table with the productnames
Id
Language
Name
primairy key = Id and Language
How can i create an foreign key to link the Product.ProductNameId to ProductName.Id
I think that the most appropriate pk-fk relationship is between ProductId on the Product table, and a ProductId (without Language) in the ProductName table. The field on the Product table is the pk, and the field in the ProductName table is the fk. That will ensure that there are no records in the PeoductName table that don't match with a record in the ProductName table.
If you want to treat Language similarly, then you can create a Language table with a LanguageId field. then, make a LanguageId field in the ProductNames table, and make that a fk.
when you retrieve product information, you JOIN Product and ProductName on their ProductId fields, and then specify the LanguageId in the WHERE clause.
I'd change the ProductNames table to:
Id
ProductId
Language
Name
With these constraints:
Primary key on Id
Foreign key on ProductId
Unique constraint on (ProductId,Language)

Resources