简体   繁体   中英

How to design a database where I can add unsettled number of items

I have a form in which I fill following things:

Nick: SbsNick;
Item: SomeItem1; Price: PriceOfItem1
Item: SomeItem2; Price: PriceOfItem2  
Item: SomeItem3; Price: PriceOfItem3
................................

And I can add items with varying number of inputs. It means that each Nick can have different number of items.

I want to create the database where I can query

SELECT ? FROM items WHERE Item="SomeItem"

and receive all nicks and prices of SomeItem, ie I want to end up with the table like that:

Nick   |Price
-------------
Jack   |20
BlackB |18
Karl   |25
.............

I have a problem with designing such database, cause I feel that I lack of one additional dimension. If I want to fix ammount of tables I see that I need unlimited amount of columns in some table. If I want to bound ammount of tables and columns I see that cells must behave like array.

Is there a clever way to design such database?

I think that you need to study about Relational Data Bases. I can not understand what is your exact problem.

You need three tables: USERS, ITEMS, PURCHASES . One for representing the users, another for the items and the last for representing the relation between both of them.

 USERS(ID_USER, NAME, NICK, REGISTER_DATE)

 ITEMS(ID_ITEM, DESCRIPTION, PRICE)

 PURCHASES(ID_PURCHASE, ID_USER, ID_ITEM, PRICE, PURCHASE_DATE)

I have used the name PURCHASES because I personally don't know the exact nature of the relation between users and items in your problem, be free to name that third table whatever you want.

There is no need for extra new columns for each purchase that the user does in table USERS nor in table ITEMS, you only have to insert a new row in table PURCHASES.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM