Databases design and primary key composed - sql

I have a table named minibar_bill and i use it for keeping evidence of client's expenditure. I'm trying to build a hotel/pension system management.
I thought that i could make a table
Minibar_bill with (id_bill, id_minibar_product, id_client)
And i would like to add those info on an invoice based on bill_id...
How should i do it ?
I mean i want to have something like that:
Id_bill(1)
id_minibar_product(1,2,3)
id_client(123)
So first 3 records will be :
1, 1, 123
1, 2, 123
1, 3, 123
And i want the id_bill to be on invoice ... maybe i could switch id_product with id_bill
Where id_bill(1) - would be the first bill record in database
id_minibar_product(1,2,3) - would be product 1,2,3 which has been consumed by client
id_client(123) - client id which we use on invoice to collect data from Client table in order to print them on invoice( i will use C# for UI ).
What I have tried:
I've tried to make a db with field id_bill and id_product but i think it's a wrong approach since i made them a composed primary key and i cannot add them to foreign key in Invoice table.

Here are some suggestions for your design:
It's a good idea to name things descriptively, but if you create a table called Minibar_bill, that's going to be inconsistent and short sighted if you want to start charging in-room movies and in-room dining, services etc. to the room. I suggest you call it something more generic - remove Minibar from all of your table names.
You must never put comma separated values into a single field.
There are a million sales data models online, including, as already suggested, templates in MS Access. There's no point reinventing the wheel
I suggest you have something like this
Client A list of clients
Products A list of products you can be billed for (not just minibar)
Bill A client has zero or more bills (usually one)
BillLine A bill has zero ore more lines. Each line represents
One product being charged for on a bll
So Bill is the header. It's up to you whether you add a column indicating when / if it is invoiced, paid etc., or whether you want to create a seperate invoicing module.
With regards to this comment:
What i wish for is to link Invoice to minibar_bill in order to have the status on a single Invoice of all products from minibar which have been bought by a customer.
If you have a seperate invoice table you can write the BillID to it to link it.
I'm not sure if you understand that all this info exists across different tables, and when, for example, you print an invoice, you go and collect all the info from across the tables at that time.

Related

having an order with multiple requests

i want to do a autoshop software... where they keep up the cars they have and what they need(engine and other parts for example) but i dont know how to do the database to accept multiple items at once
example:
a car needs on one visit to the auto shop:
left frontal door
tires
oil change
filters
how to i add this in one go to the database(with prices included) so that i can see it all after and print a bill wheer it shows all... but my main priority is being able to insert all in one go and in one table
Hard to tell without any idea about your db strucutre. Lets assume db isn't constructed yet, you don't want to decrease parts stock or keep any track of wich exact part (i mean with serial etc.) was used. You want it quite simple, just a table with a car bought some parts.
In this case i woulde use a table looking like this : id|date|car_id|parts_used
where parts_used is a string containing parts and prices with separators. For example : "left frontal door=500+tires=100+oil=10" and then split the string when reading db.
I'm not sure it's what you want but your question isn't quite precise :)

MS Access manual Auto incrementing field

Im building a system for my company to keep track of internal orders, inbetween our warehouses, we have material that goes out warehouse 1 to warehouse 2 and we kind of lose track of how much of "x" is in warehouse 1 and how much in warehouse 2, so i want to implement this access db where a user fills a form and says: order 1: 500 of "x" order 2: 300 of "y". then another user fills an exit form where he says 1 of "x" going out, so i would need the program to keep track of total order and how much as gone out to fill order 1 and so on...
My idea here is to have both an order number and an id number for each of "x" everytime someoneone assembles 1 "x" they fill the form and print a label directly from the access (i have this part working already) while keeping a record of when it was assembled, who verified and what was verified (it will work as a quality control also).
What i dont know is how to program the db so when it reaches 500 of "x", the id number for "x" starts again from 1
This is the one major issue with my program right now, i'm not experienced in access db's or vba, but im getting there with a tip and a trick from here and there, so, no need to be careful with the technical language, i will google it if i have to :p
EDIT:
The table structure goes as follows:
1 table as the main table where I record the check that is made for every product, where I include the model of the product, the said ID that I want to reset after a number of products checked, and a concatenated field that includes most of this information to generate a qr code.
Then there is a table for the Order Number, which is connected to a form to record each new order with a date/time field, the order number itself and the number of products. This number of products must then be called from the code that will count how many products have been checked to date and keep the order number field updated so we can keep track of the order.
Then there is another minor table just to get values for the form, the product models
Thank you for your answers ;)
See this MSDN Documentation
Unfortunately in Access, you cannot 'reset' an ID field, unless you move the records to a newly created table and use that table for every 500 records.
As for the user control and login form, I'm afraid those are separate questions that must be asked in a different thread.
To get you started:
You can set the RecordSource of a form to a table, and when users make entries, the data will be saved to the table. You can also use a form with controls (text boxes, comboboxes, etc.) and create a button that runs a query to insert these records into a table.
The login piece - you can encrypt the database with a password. That may/may not be sufficient.
I would suggest you change your schema, if possible. Something like the following:
Orders
OrderID (Autonumber)
ProductID (link to your Products table)
QuantityRequested
Deliverables
DeliverableID (Autonumber)
OrderID (link to your Orders table)
SequenceNumber: in the BeforeInsert event set this value equal to:
DCount("*", "Deliverables", "OrderID=" & Me.OrderID) + 1
I'm assuming that your form has a control named OrderID that is bound to the OrderID field of the Deliverables table.
The code uses the DCount() function to get the count of all the other deliverables that have already been created for this order. If this is the first deliverable, DCount() will return 0. It then adds 1 to this count to get the sequence number of the next deliverable.
If the new SequenceNumber is greater than the quantity requested, you could display a message saying that the order has been filled and cancel the creation of the Deliverable record.
This is just one approach and it is not a complete solution. I'm assuming that once assigned a sequence number a deliverable cannot be deleted. You might need to make allowances for deliverables that get lost or damaged. You could incorporate a status field to the Deliverable table to deal with this, but you would still need to make a decision about what to do with the SequenceNumber.

Database Design: Line Items & Additional Items

I am looking for a solution or to be told it simply is not possible/good practice.
I currently have a database whereby I can create new orders and select from a lookup table of products that I offer. This works great for the most part but i would also like to be able to add random miscellaneous items to the order. For instance one invoice may read "End of Tenancy Clean" and the listed product but then have also an entry for "2x Lightbulb" or something to that effect.
I have tried creating another lookup table for these items but the problem is i don't want to have to pre-define every conceivable item before I can make orders. I would much prefer to be able to simply type in the Item and price when it is needed.
Is there any database design or workaround that can achieve this? Any help is greatly appreciated. FYI I am using Lightswitch 2012 if that helps.
One option I've seen in the past is a record in your normal items table labeled something like "Additional Service", and the application code will recognize this item and also require you to enter or edit a description to print with the invoice.
In the ERP system which we have at work, there is a flag in the parts table which allows one to change the description of the part in orders; in other words, one lists the part number in the order and then changes the description. This one off description is stored in a special table (called NONSTANDARD) which basically has two fields - an id field and the description. There is a field in the 'orderlines' table which stores the id of the record in the special table. Normally the value of this field will be 0, which means that the normal description of the part be displayed, but if it's greater than 0, then the description is taken from the appropriate row in the nonstandard table.
You mean something like this?
(only key attributes included, for brevity)

Consolidate duplicate clients and sales

I have a question and I can't come up with a good way to solve it, this is using SQLServer 2008R2.
I have 3 tables
Client, Invoice, Car
The client is duplicate n times, but each client has a ticket and one product.
So I am trying to consolidate the clients (with a unique identifier), now my question would be how to update the reference field on the product and ticket.
Example
**Client**
[Nombre]
,[Apellido_Paterno]
,[Apellido_Materno]
,[Sexo]
,[Estado_Civil]
,[Fecha_Nacimiento]
,[RFC]
,[Saludo]
,[Persona]
,[Razon_Social]
,[Direccion]
,[Colonia]
,[Municipio_Delegacion]
,[Estado]
,[Codigo_Postal]
,[Lada_Casa]
,[Telefono_Casa]
,[Ext_Telefono_Casa]
,[Lada_Oficina]
,[Telefono_Oficina]
,[Ext_Telefono_Oficina]
,[Telefono_Celular]
,[Email_Personal]
,[Vehiculo_Actual_Anterior]
,[Marca_Actual_Anterior]
,[AnioModelo_Actual_Anterior]
,[Color_Actual_Anterior]
,[Escolaridad]
,[Venta_Id]
,[Nombre1]
,[Nombre2]
**Invoice**
[Factura_Cliente]
,[Factura_Distribuidor]
,[Precio_Base_Vehiculo]
,[Precio_Accesorios]
,[Precio_Vehiculo_DeContado]
,[Descuento]
,[Incentivo_Calculado]
,[Fecha_Entrega_DelVehiculo]
,[Fecha_Factura_Cliente]
,[Clave_Distribuidor]
,[Seguro_Gratis]
,[Clave_Promocion]
,[Tipo_Venta]
,[Unidad_de_Intercambio]
,[Venta_Id]
**Car**
[Modelo]
,[Marca]
,[AnioModelo]
,[Basico]
,[Cabecera]
,[Version]
,[Color_Exterior]
,[Color_Interior]
,[VIN]
,[Motor]
,[Transmision]
,[Origen]
,[Basico_Linea_Modelo]
,[Venta_Id]
I can consolidate the clients, (even if they have discrepancies on their fields), but I can't find a effective solution for this.
Edit: The first column on client shows the duplicate, while the second is the id that match the ticket and product.
i would make two more tables.
one for new consolidated clients (with all the same fields as the normal client)
and a mapping table between the old client and the new client.
this mapping table should store the id of the old one, and which i it was mapped to as a new one.
from here its pretty easy. update the other tables with the new id based on its current old id in the map table..

how to update all clients after each db transcation

I am developing a Client-Server based app using WPF, SQL-Server and WCF. This is basically a kind of POS application (not using any POS library but features are almost same). Server side coding is almost done. Now I am moving to client side coding. I am new in WCF (first app on WCF) and Client-Server architecture, so I surrounded by some doubts before I begin my coding. According to my Client (Person), he should be able to print item sales bill (Memo) from Client computers as well as Server. Every memo should have a Memo No for that transaction which should increment automatically and salesman should be able to see which memo no he is going to use. So for this purpose I have added a Memo No field on Application Sales Windows which would show Memo No (Last Memo No + 1) from database.
Here comes the problem, suppose there are 3 sales counters and when salesman opened their respective Sales Windows for billing then all of them would see (last Memo No + 1). And if 3 of them bill simultaneously then same memo no will be stored for 3 different transactions.
I must tell you that I have 2 tables in database to store sales. Table 1 stores discount, grand total, salesman id and Memo no and Table 2 stores memo no, item code, quantity, bill date etc. So its Memo no which binds two tables and if this get manipulated then you know what mess it can create in Monthly Reports.
This situation in overflowing my brain and came here to get solution. What I can do to overcome this. Any suggestion, link, code will be very helpful.
You could consider amending your design so that a memo number is assigned when a client begins a sale.
One way to implement this would be to use another table to store the sequence of memo numbers:
CREATE TABLE MemoSequence
(MemoId int identity(1,1),
DateAssigned datetime
)
Each time the sales window is opened on the client, the current date/time should be inserted into MemoSequence.DateAssigned, and the corresponding MemoId value returned (using an OUTPUT clause or SCOPE_IDENTITY()) to the client as the MemoId.
That way, each client would get a unique memo number assigned from the start of the transaction.
NB - this may not be a suitable solution if it is a requirement that there are no gaps in the sequence of Memo numbers.