SQL many to many relation, multiple tables [closed] - mysql

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 4 years ago.
Improve this question
Hey so i have been doing alot of reading and i am finding many conflicting ways to link my tables. hoping someone can help me put the foreign keys in the correct places.
tables -
customers: customer_id(primary), customer_name ;
employee: employee_id(primary), employee_name ;
appointments: appointment_id(primary), appointment_date ;
inventory: inventory_id(primary), item ;
so i have primary key for each table but i need to link 1 customer to the appointments table and multiple employees to the appointments tables.
the inventory table must link to the appointments tables.
Here is an example, customer sets an appointment then employee(s) fills appointment. during that appointment the customer buys an item(s).
it is sufficent enough to just show the item with the appointment. the item does not need to link to the customer.
thank you for help. i was thinking i needed to make a 5th table to fill with all the keys but im really unsure and the tables have alot more information in them than i posted above. would be alot of trial an error to then see if not work one of the ways i need.

You should add customer_id foreign key to the appointments table
You should create table appointment _employee:id (primary key),appointment_id(foreign key), employee_id(foreign key)
You should create table appointment_inventory:appointment_id, inventory_id

Related

selecting a table in mysql [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a mysql database and some tables.
One table is Products: Its columns are prod_id, prod_type.
Prod_types are mobile or laptop or book.
Each of them have different attributes (eg: publisher, author for books, battery_condition, model for laptop etc).
Should I store all the data in the Products table by adding columns like 'author', 'model' etc and keep null for those columns which don't apply to my product.
For example for an entry with prod_type='book' , 'model', 'batter_condition' etc will be null. Or should I create different tables for 'book', 'mobile' and 'laptop' ?
Yes. Create new tables for Book, Mobile and Laptop. Have the product_id attribute in each of those tables. This product_id attribute is a foreign key to the primary key of table products, which is product_id as well.
Storing all data in products will result in a lot of NULL fields and will make the data storage inefficient.

Does it make sense to create a uniqueID for this MySQL table in a database? [closed]

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 5 years ago.
Improve this question
I’m currently trying to design my table structures for a made up database. The database will have three separate tables and track the populations of cities for 10 years. After every year, population figures will be added for each city in the database. Here is how I’ve laid out my three tables so far:
Cities (city_id, city_name, city_abbrv)
Year (year_id, year)
Stats (year_id, city_id, population)
I’m worried about not having a unique identifier in my Stats table. With 10 cities, the year data will be the same for 10 entries. Once I enter multiple years of data, the city_id will be reused. In my research on this site I’ve read that having a unique ID for every table is not required but the book I’m using to learn database design (while brief) never mentions that this is okay. I would like to know the best way to design a database that receivers data entries for the same group of things on a daily/weekly/monthly/yearly schedule. Should I add in a unique_id column to my Stats table? Or would this be a wasted column? Thanks for the help!
First of all you need each of those tables to have the column id as primary key.
A primary key is used to uniquely identify a table row. A primary key cannot be NULL since NULL is not a value. So basically those columns will be unique yes.
Question: Why you need primary keys in your tables?
Answer:
If you are looking to select data from different tables you are opting for join so you need keys to use for that join.
If you want your
table to be clustered, you need some kind of a primary key.
Side Note: You may need to get familiar with indexing columns, see advantages of indexing.
Cities (id, city_name, city_abbrv) //with id as primary key
Year (id, year) //with id as primary key
Stats (id, year_id, city_id, population) //with id as primary key
//And year_id and city_id as foregin key connected by their respective ids
If you are still beginner with MYSQL see the W3school tutorial for SQL primary keys.

Creating a contacts table for users in a MySQL database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm a newbie.. not sure if this is the right place to post this, so please forgive me if it isn't.. i wanted to find out how or what would be the best approach when it comes to making a table for contacts in a database.. Here is what i have :
I have a table for users, this table contains user info like usernames and passwords,etc.. i want to make a program that will allow these users to add contacts,now i don't want to create a contacts table for each user,i would like to have a contacts table which contains all contacts and be able to reference them using an id .. Help please
You have a table of users and a table of contacts:
User - UserID, Username, Pw... etc.
Contacts - ContactID, name, phone, info...etc.
For a user to have multiple contacts and for multiple users to have the same contact, you need to create a bridge table to handle the many to many relationship. In this table you'll reference the primary keys (the UserID, and ContactID) from the tables above.
UserContacts - UserID, ContactID. (this can be a composite primary key)
You need to have a table with name of your choice..let's say Contacts
You can have this structure
ContactId (Primary Key)
ContactName
UserId
Etc..
You can start with above structure and improvise..
I recommend this article..
http://www.datanamic.com/support/lt-dez005-introduction-db-modeling.html
Table creation sample code:
CREATE TABLE contact(
contact_id VARCHAR(20),
contact_name VARCHAR(20),
user_id VARCHAR(20),
password VARCHAR(20)
);
data insertion sample code:
INSERT INTO contact(contact_id,contact_name,user_id,password)
VALUES("A123","Daniel","dan2015","password");

Saving payment date and amount in database [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am developing an application for a small financing company, where they can input payment amount. The date will be generated automatically. My question is, what is the best way to save those payments in MySQL, considering that some loans will be as short as 6 months, while others will be as long as 84 months?
I'm thinking about a table with 169 columns:
REC_ID as primary key,
Date1, Amt1, Date2, Amt2, etc...
But, how efficient would it be? On a 6-month loan, out of those 169 columns, only 13 would be used.
Thanks in advance for your output.
Create a payment table that has a the foreign key of the loan id in it.
the payment table will look something like this
id|loan_id|payment_date|payment_amount
Have a separate table for loans that will look like
id|loan_amount|loan_length
When you need to find out how many payments have been made run a query on the payment table with the loan_id as a search parameter.
This would be a gross violation of relational design paradigms, each payment and Date should probably be a its own tuple in a table with a many to one relation to the accounts table.
Accounts (REC_ID, other_stuff, other_stuff) Primary key(REC_ID);
Payments (REC_ID, timestampe, amt) Primary key (REC_ID, timestampe);
Also consider using foreign keys to enforce existence of a valid loan for each payment. And I would guess the policy would be cascade so that deletion of load and renames of loans would carry through to the Payments table.

How to store "wishlist" in database? [closed]

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 2 years ago.
Improve this question
I am thinking of creating a "wishlist". Each user will be able to purchase wishes in a shop. How do I store the wishlist? I am thinking of creating an user table and a wishlist table.
The structure of user table:
Columns: id, username, password etc
Columns: id, wish, price, quantity etc
user id is the primary key for user table and its a foreign key in wishlist table. However, when I come to think of it, my wishlist table will have duplicate items for each user, won't it?
Is there a better way to store the wishlist things?
I am using mysql. Thanks
You should also have another table to store the purchases:
Purchases
Id
UserId
WishId
PurchaseDate
Users
Id
Name
Password
Wishes
Id
Wish
Price
Quantity
Every time a user purchases a wish, you create a record in the Purchases table, and decrement the Quantity count for that wish.
It is a 1 to n relationship.
Asuming you have a User table with a user_id as primary key then
create a table Wish which has user_id as foreign key.
Add the attributes you want to this second table and violá.
Luis
You should have another table for wishes (wish id, price, description etc), and you should have a joining table (user id, wish id, quantity, quoted price etc). This table will allow you to have a many-to-many relationship between users and wishes.
(Further to my comment... see? You have two different suggestions already. Ultimately you have to decide what is best, and ideally your business/project requirements should guide you.)
EDIT: make that three different suggestions
You essentially need to know just a few things to add information to a wishlist. Here's how I have it in one of my client projects:
Wishlist
wishlist_id
user_id
product_id
created_date_time
It would make sense to have wishlists only for authenticated users, so ensure that you are taking care of that.