Mysql creating 1 table to be used for multiple tables - mysql

I currently have made tables Person, branch, and clients. They all need to have data for where they are located.
My question is can I have 1 locations table for all three tables and locationid will auto_increment or should I create Location table for each one; person_location, branch_location, and client_location? Maybe I could add all the columns within each table Person, Branch, and Clients instead in a separate table.

no u dont need to create location table for each other tables.just make one table location..
but in other table add an column locationid which will hold the location id from location table.
thus u can get the location in each table.

Related

Storing multiple ids in a single (SQL) row?

So I want to make a webapp that needs to store the following.
"Users"
some "Events"
and some "Customers"
each "Event" has a "User" which is represented by a userId column.
But each "Event" can have multiple customers, to be able to store all this info,
I thought it's better to have a new table "Customers_In_Events" that has 3 columns:
id, -- Not sure if that's needed
e_id -- Event ID
c_id -- Customer ID
I have 2 questions...
Is there some better way to do this?
If I decide to go with the way I mentioned (an extra table), Would the "id" column be needed?
I am using MySQL by the way.
You can use a bridge model to represent your data, which is also normalized.
every event can have multiple customers
every customer can have multiple events
so the tabkes would look like
Events events_coustmers cutonuers
e_id(PK) e_id(fk) c_id(Pk)
e_name c_id(fk) c_name
PK(e_id,c_id)

How to use a single table for multiple users

I want to create a database which has a table named warehouse which contains the details of each product. I have another table named users. At present there are 2 users in the users table - ABC and DEF.
My main question is user ABC has it's own warehouse and so for user DEF. How can I use the warehouse table for 2 or more different users ?
One to one
If one user own only one warehouse then you should add user_id column in warehouse table. then reference the user_id from user table via foreign key.
One to many
if multiple user owns an warehouse then you should add warehouse column in user table. then reference the warehouse from warehouse table via foreign key.
Many to Many
if an user owns multiple warehouse and and warehouse has multiple owner then you should add a table like warehouse_users and add keep warehouse and user_id.

Add data from one table into another based on another value in MySQL

I need to take data from one table and add it to another. This is to link a supplier to a product, however it's not a simple query, so I'm not sure if this can be done.
I have a table which is xxx_product and then another table which is xxx_product_to_vendor.
I need to take each record in xxx_product and add it to xxx_product_to_vendor.
xxx_product_to_vendor has three columns.
product_to_vendor_id
product_id
vendor_id
product_to_vendor_id must auto increment in numbers as it's the record number.
product_id must be taken from the product_id in table xxx_product.
vendor_id must be assigned as following. If supplier_id in xxx_vendor is 4 then vendor_id in xxx_product_to_vendor must be 1.
Here are images of my two tables.
I managed to solve my issue without the need of a query. I simply exported the table as CSV for MS through phpmyadmin and deleted the columns I didn't need and updated the data in excel. Imported the edited sheet into the other table without any issues.

Traverse list of related tables

I have a peculiar situation. In my MySQL database, I have around 90 odd tables and most of the tables have been indexed (We are using INNODB). Some of the tables are having a link like this:
A -> B -> C->D->E
Is there a way where-in I can find the list of all the sub-child tables when i have only table A and E to work with? I have do a dynamic query builder mechanism, and for that purpose I list the users with the list of tables, and in a given situation like above, need to get the required information from just table "A" and "E" alone, without the tables "B","C" and "D" being selected by the search user.
The tables are linked in normal manner..each table is linked to another via a proper foreign key constraint.
Eg.
Table A (Transaction)
Id, Trxn-Date, Amount
Table B (Transaction Header)
Id, Agent_Id (FK to Agent_Profile), Upd_Time, Trnx_Hdr_ID (FK to Table A)
Table C (Agent_Profile)
Id, Prof_ID (FK to Profile)
Table D (Profile)
ID, Pers_Info_Id (FK to Personal_Info)
Table E (Personal_Info)
Id, Firstname, Lastname
User selects Trxn_Date, FirstName, LastName.
How can I retrieve the sub-linked table information, when the selected tables(in this case), happens to be only Transaction and Personal_Info.
Seems like you could consolidate a few of those tables into 2-3 tables without losing anything useful. Tables C D and E just contain columns to point to other tables.
If changed, a query with trxn_date and first/last name would be a ton easier.

Can i create nested tables on MySql?

for example,access example db, every student record have a nested table about guardians.
http://img101.imageshack.us/img101/9881/53882937.jpg
No. Something like that is almost always done as a single mapping table for all students, with a foreign key column pointing to the student table to specify which student a particular row relates to.
You then just filter the table to match a given student, and present that list without a student column, in the UI. It looks like a separate table to the user, but that's not actually how it's stored.
(If you did create a separate guardians table for each student, you'd make it impossible to do queries like ‘find students for a particular guardian’.)