i have one product table
which have
product_id name price
and i want to store multiple images of single product here product_id is primary key and auto increment so how would store i store multiple images of single product.
one solution is define anothe table which has product_id as a foreign key
that is
product_id images
suppose i add
product_name moblie it id is auto incerment.....so how would i store the multiple images of this as i does not know what product_id is genertaed as it is auto increment?????
please help anyone????
Create a second table with a generated id, product_id (foreign key) and image_location or something.
Then you're there.
Related
I have MySQL database containing just one table, having 100 rows and 12 columns: ID (primary key), NAME, LATITUDE, LONGITUDE, TYPE, COUNTRY, CONTINENT, DESCRIPTION_PATH, STORY_PATH, PICTURE_PATH, ICON_PATH and VIDEO_PATH. Do you think it is OK to keep all the data in one table or should data be distributed within more tables?
This is a perfectly fine table design as long as your Name rows only need at most one description, one story, one picture, one icon, and one video.
As soon as you want someone to have two pictures, you'll need to create another table for pictures and store one picture per row, each one referencing your first table.
For example:
CREATE TABLE Pictures (
picture_id INT,
name_id INT,
picture_path VARCHAR(100),
PRIMARY KEY (picture_id, name_id)
FOREIGN KEY (name_id) REFERENCES Names (id)
);
And you'd remove the picture_path column from first table of course.
I am using Access to create a database. I have two tables with the following data.
Car
CarID - PK
CarName
CarPrice
CustomerID
Customers
CustomerID -PK
Username
Password
CarID
I wish to have the relationship as many cars to one customer. Would I need a 3rd 'link' table or is there a way to do this without another table? Sorry for such a simple question
Remove CarID from your Customer table. Make CustomerID in the Car table a foreign key to Customer, and remove any existing unique constraints on that column.
Remove CarID from your customers table and you would be set. Just be sure to have the CustomerID field in the Car table be a Foreign Key.
I am creating a relationship between 2 tables:
The relationship I like to form is between the Inventory an InventoryExtended tables.
The primary key for the Inventory table is InvID (Inventory ID).
The reason why I created the InventoryExtended is becauses only 1% of the inventory items in the Inventory table will need additional or extended fields, the rest will not.
Instead of adding these additional fields in the Inventory table where 99% will be blank for 50 additional fields that I need I decided to create an InventoryExtended table and store the 50 fields there.
The relationship between the Inventory an InventoryExtended table will be 1 to 1.
Meaning, for the 1% of the records in the Inventory table , the InvId will be the same as the InvId in the InventoryExtended table.
My question is that should the InvID in the InventoryExtended table be a FK (Foreign Key) or should it be a PK and a FK?
I am thinking it should be a PK and a FK as there the InvID will be unique in the InventoryExtended table.
Thanks in advance.
You are correct.
The InvID should be a PRIMARY KEY and a FOREIGN KEY as it will be unique in the InventoryExtended table.
This type of relationship is indeed 1:1 or (more accurately) 1::0..1, as only some of the rows in the Inventory table will have a related row in InventoryExtended.
Also note that the InventoryExtended (InvID) should not have the IDENTITY property, even if the Inventory (InvID) has it.
The InvID will be both a PK and FK for the extended table.
If you know in advance (meaning when you attempt to fetch data) whether or not a particular inventory type will have the extended data, for such records you can even skip the original table altogether and simply use two disjoint tables smallInventory and bigInventory such that no records of one are present in the other.
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
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)