by constructing a brigde table I would link many salesman and group together
I've made three table like below
salesman
========
uId
salesGroupLinked
================
uId
groupId
group
======
groupId
so it should be able to allow 1 salesman can have many groups and also many groups can own by 1 salesman. But now I thought of expand the relationship.
Says I want each salesman can have their performances score for each group. Where should I put the column? If it's at group table then it's obsoleted, because groupId is unique, so it may give a score to many salesman.
Just by thinking about the problem: What is the key of the performance score? It is settings of one salesman's group. So, the settings should be on the table where they are linked. It will be unique for each pair of (salesman, group).
So the table would look like
salesGroupLinked
================
uId
groupId
PerformanceScore
put the score in the link table.
The pk on that table should still be uId and groupId, but it should also have a column for the score.
EDITED to shopw DDL statements to Create table with Primary Key
create table salesGroupLinked
( uId integer not null,
groupId integer not null,
performanceScore integer null,
Constraint PK_salesGroupLinked Primary Key clustered (uId, groupId)
)
The composite PK for this table itself guarantees that there can never be more than one row in this table with the same values for uId and groupId.
Related
I have a user table and a virtual item table, each with ID values as primary keys.
I also have a favorites table where users can store their favorite items. The favorites table maps user_id to item_id. Many users can favorite many items.
table users
user_id bigint primary key auto_increment
...
table items
item_id bigint primary key auto_increment
...
table favorites
user_id bigint
item_id bigint
Since I cannot create indexes on the favorites table, because there are no unique or primary keys, how do i optimize search queries, such as SELECT? Is there a better way to store the data?
Since I cannot create indexes on the favorites table, because there are no unique or primary keys
Huh? Of course you can create indexes. In fact, two come to mind:
favorites(user_id, item_id)
favorites(item_id, user_id)
These would be used to answer different questions. Respective examples are: What are the favorites of user X? What users have a favorite of Y?
I've made a simple shopping cart with PHP and AJAX. You can add multiple products to it and you can set the quantity of each product.
It might be a silly question, but honestly, I don't really know how should I store these orders in the mysql database, with all the multiple products per order and the quantity of each product per order.
Can someone help me please?
EDIT: I learned a lot about databases, I know about relations, I just don't know how should I do this correctly.
You should use aggregation table
That means, you have to create a table where primary key is combination of 2 foreign keys, one is a primary key of order, 2nd is primary key of product
and then, you have extra fields in table like count
That means you have a unique key for each row in table, because it is combination of order and product
But I have to say, this is literally basic knowledge of SQL databases
1 Create a PRODUCTS table with a PRODUCT_ID as primary key.
2 Create a ORDERS table with a ORDER_ID as primary key.
3 Create a ORDER_LINES table with foreign keys ORDER_ID and PRODUCT_ID.
The latter will have a column QUANTITY.
Let's say you have Products Table that contains the following columns
(ProductID, ProductName, Qty). and you want to sell many products to many customers. a many-to-many relationship in the database, you just need to add two more tables to your database.
The first table and let's assume it's called [Cart] in which you're going to store the Cart ID, Customer ID.
CREATE TABLE [dbo].[Cart](
CartID int NOT NULL,
CreatedDate datetime NULL, --to get the current date and time
CustomerID int NULL,
PRIMARY KEY (CartID)
)
The second table and let's assume it's called Cart_Detail in which we're going to store the ordered products and the quantity of each product.
CREATE TABLE [dbo].[Cart_Detail](
LineID int IDENTITY(1,1) NOT NULL,
CartID int NULL,
ProductID int NULL,
Price decimal(18, 2) NULL,
Qty int NULL,
Amount decimal(18, 2) NULL
) ON [PRIMARY]
LineID column is to facilitate the creation of views and operations such as selecting, updating, or deleting a specific product
In SQL i have a table for users. On my website i want people be able to make groups consisting of 2 or more users, but i have no idea how to store that. People can be a part of multiple groups at the same time. If i'd make a table groups, how could i store it's members in that table?
You would have a table groups and one called userGroups. These would look like:
create table groups (
groupId int auto_increment primary key,
groupName varchar(255)
);
create table userGroups (
userGroupId int auto_increment primary key,
userId int not null,
groupId int not null,
foreign key (userId) references users(userId),
foreign key (groupId) references groups(groupId)
);
What you're describing is called a many-to-many relationship, and it requires a third table:
UserTable: UserID (unique), UserName, etc.
GroupTable: GroupID (unique), GroupName, etc.
UserGroups: UserGroupID (unique), UserID, GroupID
With a third table holding the primary key(pk)) of a group and a pk of a person. These columns are the pk of the table. You can have other to
Columns too, e.g. date joined or whatever. You have one row in this table per person/group.
It's a common concept/pattern so make sure you learn and understand it.
I have browsed a similar topics at Stackoverflow but haven't found the solution I'm looking for. Please give me an idea how to organise the tables relation. I have 2 tables: the one contains contractor's names and number of contracts for each contractor, the second have invoice's data (like number, amount, date) which relates to each contractor. How should I have to connect these two table correctly? I have an idea to store in the first table contartor's ID,contractor's name and number; in the second - contractor's ID and all invoces' details. But should I store all the invoices' data in one table or create separate table (with invoice's data) for each contractor? I'm newcomer in db-issues and have no enough experience in it. I would appreciate any suggestions.
You're doing it correctly. It'd be best to have two tables as the data will be less redundant giving your more efficient space usage.
As an example:
CONTRACTOR TABLE
ID NAME PHONE
----------------------------------
1 TEST1 619-123-4567
2 TEST2 619-234-5678
INVOICE TABLE
ID CONTRACTOR_ID AMOUNT DATE
----------------------------------------------------------------
1 1 150.00 2014-04-17 00:00:00
2 1 150.00 2014-04-18 00:00:00
So in the example table, I have two contractors. The first one, named TEST1, has two invoices linked to them in the INVOICE table.
If you're worried about performance, all I would suggest is to add indexes on both tables and foreign keys linking the contractors information with the invoices by the contractors ID.
The two tables should be correct
Contractor
NumberOfContact NameOfContact
Invoice Data
Number Amount Date
The Number of the invoice table would be a FK to contractor Number and if that person were to have multiple entries into the invoice table it would be identified by the date.
Your line of thinking is correct. The script would be like this:
CREATE TABLE `contractor` (
`id` INT NOT NULL,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`));
CREATE TABLE .`invoice` (
`number` INT NOT NULL,
`amout` INT NOT NULL,
`date` DATE NOT NULL,
`id_contractor` INT NOT NULL,
PRIMARY KEY (`number`),
INDEX `fk_invoice_1_idx` (`id_contractor` ASC),
CONSTRAINT `fk_invoice_1`
FOREIGN KEY (`id_contractor`)
REFERENCES `contractor` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
I have a table named group Info. It has three fields namely id,user_id, and group_id.
My question is how to assign unique users to a group i-e same user may not be repeated in a same group.
Table structure for group Info is as follows;
id int(11) Auto increment,Primary key NOT NULL,
user_id int(11) Not Null,
group_id int(11) Not Null
I have made the user_id unique.But there are two groups(2 group_id(1 and 2)).Selecting users for groupB gives error duplicate entry.
user_id = 1,2,3,4,5,6,7,8;
group_id= 1,2;
Kindly help me how to solve this.Iam not good in english so apologies for my language.
ALTER TABLE `tbl_name` ADD UNIQUE (
`col1` ,
`col2`
);
Change col1 with user_id and col2 with group_id and tbl_name with your table name.
If user_id is unique, then you probably shouldn't be creating a join table: just add a group pointer to the user table.
If, on the other hand, a user can belong to more than one group, then your current design is probably appropriate, but you shouldn't make user_id unique (although you might want to make a unique composite key on the combination of user_id and group_id).