Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I want to manage sales, there was many to many relationship between sale and product, so i broke it by adding an entity sales description among them now the relationship among these three entities is as follows.
"Sales descrition" entity have composite primary key consisting upon customerId, customerName,date,time attribute which should go as foreign key in "Product" table. Now i am confused that whenever i will add product i will have to add customerId,customerName,date,time which does not seems logically true.
Any idea please that how should i handle relationship between sale and product?
I would organize this by storing customer data in a customers table. Then the sale table uses the customerid from the customers table as a foreign key. The sale table should have its own primary key. That saleid key becomes a foreign key in the sales description table. The product is linked by including the productid in the sales description table as a foreign key. So your tables would look something like this:
Customer table
- customerid
- customer name
- customer address
Sale table
- saleid
- customerid (foreign key)
- datetime
Product table
- productid
- product name
- product price
Sale description table
- saleid (foreign key)
- productid (foreign key)
- quantity
It should be the other way around. The product id should be a foreign key in the Sales Description table. The Sales Description is the relationship between Sale and a Product. The customer id belong in the Sale table and should probably be a foreign key to a customer table. This way a single customer may have many sales and each sale may have many products. I Assume here that Sale == Order.
Related
I have table which contain say 3 columns( companyname, years, roc). I want to find out companyname which satisfy below condition.
Between years 2010 and 2020 and roc> 10 each years. Mean if in any years between 2010 and 2020 roc<10 it should not include that company. It should only show companyname if each year roc >10.
Filter the table for the years that you want and aggregate with a condition in the HAVING clause for the column roc:
SELECT companyname
FROM tablename
WHERE years BETWEEN 2010 AND 2020
GROUP BY companyname
HAVING MIN(roc) > 10;
Okay, I think just now I got your problem. What is the main issue in here is, the companies get duplicated within a single table which violates the DB principles what apparently makes you harder when querying back.
So the best thing you could do is, break this single table into two - COMPANY_TABLE and COMPANY_ROC
So the COMPANY_TABLE will only have the companyID(PRIMARY KEY) and the companyName.
Next have another table called COMPANY_ROC - which will have roc , years , companyID(FOREIGN KEY - PRIMARY KEY of COMPANY_TABLE).
CREATE TABLE COMPANY(
companyID int PRIMARY KEY NOT NULL,
companyName varchar,
)
CREATE TABLE COMPANY_ROC(
companyID int,
roc int,
years int,
FOREIGN KEY(companyID) REFERENCES COMPANY(companyID)
)
SO when you querying, you can query as follows using an INNER JOIN
SELECT COMPANY.companyName from COMPANY INNER JOIN COMPANY_ROC WHERE COMPANY_ROC.years>=2010 AND COMPANY_ROC.years<=2020 AND COMPANY_ROC.roc>10 AND COMPANY.companyID = COMPANY_ROC.companyID
Maybe my Query might have some issues as I didn't test it. Just understand what I explained and give it a try. Breaking the table into two and having primary , foreign keys is the key for easy querying :)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a table in my app
CREATE TABLE VisitDetails (VisitId INTEGER, UserId TEXT, VisitNumber
TEXT, JobId INTEGER, JobNumber TEXT, StatusId INTEGER, Status TEXT,
SignatureRequired BOOL, StatusDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP
,StartDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP, EndDate TIMESTAMP
DEFAULT CURRENT_TIMESTAMP, IsPPMJob BOOL)
and it was working fine but now I need to alter this table to make already existing column 'VisitId' to become the primary key. Can somebody help me please? It might work by adding Unique constraint to VisitId column of the table but I am trying to put some efficient solution !
You can't change SQLite tables field's primary key once the table is created,
Possible solution is,
Create new table with desired primary key
Copy all data
Drop old table
Here is the documentation link : https://www.sqlite.org/omitted.html
What tables and relationships should I design for an app supporting multiple customers?
I am developing a web application which has multiple customers and a single product. I want daily sales data to be stored for each customer - there are about 10 customers.
My application has a screen where the following data is entered and saved:
Customer Name
Price per quantity
Quantity
Total amount
Amount
Balance
Old Balance
Total Balance
I need to decide how to create tables, the number of tables and the relationships between them. For example, should I have separate tables for each customer? I need the total sales to be maintained in database.
You can have two tables. One customer main table with columns Id,Name
Id can be of identity type which increments for every insert of customer name.
CREATE TABLE Customer(
Id INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(30)
)
Second table can be sales table as there is a single product , you can just have that as a column in this table. Columns for this table would be
id(Again identity primary key for sales table)
Product name
Quantity
Total amount
Amount
Balance
Old Balance
Total Balance
CustomerId (foreign key referencing the customer table)
Also sales date can be added to the sales table, which will be helpful to retrieve sales based on date.
While retrieving, you can use the relationship between the tables to get the customer name.
Hope this information helps.
This question was asked to me recently, any suggestions are welcome.
There is a form containing company details of say 5 companies.>> C_ID, C_Name, c_Address. (Stored in Table Company)
Below there is a grid view which displays employee records and has insert functionality. the fields of grid view are say>> E_ID, E_Name, E_Address. (Stored in Table Employee)
Now this employee has to be mapped with one of the 5 companies. But there is no reference available. How to find the Foreign key? How to maintain relationship between the two Tables?
Add a new field in Employees Table named "C_ID" and make it a FK(foreign key) to be mapped in Company table..
after adding the field, right click Employees table click design. On the design right click and click relationship. Add new relationship, click Tables and Columns Specifical, on the dropdown choose Customer table and choose C_ID.
You'll need to add the column to map the relationship, and add the foreign key to enforce referential integrity for this relationship.
If the employee can be linked to only one company at a time:
(e.g. in Sql Server):
ALTER TABLE Employee ADD CompanyID INT;
ALTER TABLE Employee ADD CONSTRAINT FK_Employee_Company
FOREIGN KEY(Company_ID) REFERENCES Company(C_ID);
If the employee can be many to many to a company, e.g. part time, contractor, or tracking a history across multiple companies, then you will need a new junction table between employee and company to model this relationship.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
In my database I have 3 tables, one for customers, one for orders and one for products. A customer can have any number of orders and an order can have any number of products. How can I implement this relationship between the 3 tables?
Information stored in the database:
Customer: social security number, name, address, phone number
Order: order number, date
Product: product id, category, price
Ok Here's where to start.
First you need an ID for each table (an ID is a unique identifier, so that if i say want customer X, there won't be 2 customer X in the table)
For customer u can use social security number (or you create a column called CUSTOMER_ID)
A customer can have any number of orders, So you should put a column in order to know this order belongs to which customer. So in order you add a column called CUSTOMER_ID that references CUSTOMER_ID in the table CUSTOMER
so these 2 orders belong to CUSTOMER with CUSTOMER_ID = 1:
ORDER_NUMBER DATE CUSTOMER_ID
1 1/1/2013 1
2 2/1/2013 1
CUSTOMER_ID in ORDERS is called a FOREIGN KEY
The same goes for the rest.
(PS : change the name of the table ORDER, ORDER is a keyword used in SQL, to order the items SELECT .... FROM TABLE1 ORDER BY ....)