I have question related to mysql relationships. Let's say I have 4 table companya companyb companyc and companyd, each containing 3 columns: name, phone, and age. Also I have another table called address which I want to link to each companyX with this address table , let’s say if I did select * from companyX; the address column should appearnext to each name.
Can this be done with mysql?
First of all, if the company tables have the exact same structure why don't you use only a single table to store the information? You may want to read about database normalization.
Secondly you could create a relation table named something like 'companiesAddresses' and store fields such as "CompanyID" and "AddressID" in it (you obviously would have to create a primary key column in your companyX's table and do the same in the 'addresses' table so then you could use the INNER JOIN clause to get the information you want such as SELECT companyX.companyName, addresses.Address FROM companyX INNER JOIN companiesAddresses ON companyX.CompanyID = companiesAddresses.CompanyID INNER JOIN addresses ON companiesAddresses.AddressID = addresses.AddressID.
I hope I understood what you want correctly.
Related
I'm creating a database based on Pokemon but I'm currently stumped on inserting Pokemon with different moves.
Each Pokemon has a move set, so not just one move, but many. However, as I attempt to insert the Pokemon with its variable-length amount of moves into the table, MySQL ignores the previous ones and only inserts the last move.
In short: how do I insert multiple records of the same Pokemon but with its different move?
[I guess a good similar real-world example would be a Person having multiple email addresses. How would I go about inserting that into a table?]
The problem is that you're implementing it as a one-to-one relationship, but what you have is a many-to-many relationship (each Pokemon has many moves, each move can be learned by many pokemon).
What you'd probably want to do is have 2 tables.
Table 1: Pokemon
ID, Name, Move1ID, Move2ID, Move3ID, Move4ID, Types etc.
Table 2: Moves
ID, Name, PP, Power, type etc.
Then you could use another table which contains all the join information between those 2 tables. You'd have multiple rows containing the same Pokemon ID and multiple rows containing the same Move ID, but the [Pokemon ID, Move ID] combination would be unique.
Table 3: PokemonMoves
PkID, MoveID
Then you could just do a join from the Pokemon table to the Moves table via this relationship table
SELECT *
FROM Pokemon AS p
LEFT JOIN PokemonMoves AS pm on p.ID = pm.PkID
LEFT JOIN Moves AS m ON m.ID = pm.MoveID
There are lots of posts on SO about many-to-many relationships, this looks like a good place to start: Many to many relationship?
Well, what do the tables look like? (and is their structure under your control?)
If you are constrained to a single "Email" field, the only way I see you can associate multiple email addresses with a single record(=person) is to treat the Email field as a comma (or whatever) delimited list.
If you control the structure however, you can switch to a one-to-many relationship between "Person"s and "Email"s - something like:
tblPerson
[id]
tblEmailAddresses
[person_id]
[email]
You'd query that like this:
SELECT id, email
FROM tblPerson INNER JOIN tblEmailAddresses ON
id = person_id
WHERE id = <person you're interested in>
Which would return as many records as that person has email addresses.
Hard to say exactly how the insert would look without seeing your code/data, but you could do something like:
sID = <whatever>
For each sEmail in EmailCollection
INSERT INTO tblEmailAddresses
(person_id, email)
VALUES (sID, sEmail)
Next
I'm creating a table for a college major. The table is called major. The columns will be majorID, majorName, and requiredCourses.
In Access how can I make requiredCourses a multivalue field? Required courses will be around 20 courses.
Thank you for your help.
You need to create a one-to-many relationship. The way is is usually done is like this:
You need to create a new table for the courses. Call it course. The table will contain CourseID, CourseName, etc. CourceID will be a primary key of this table
You will need to create another table that will act as a link between your major and your course tables. The table can be called something like majorCourses. The table will contain at least these two fields: majorID and courseID (you can of course add more fields, like dateAdded, isInactive, etc.).
To link your tables you will need to JOIN these tables, like this:
SELECT m.majorID, m.majorName, c.courseID, c.CourseName
FROM major m
INNER JOIN (majorCourses mc INNER JOIN course c ON mc.courseID = c.courseID)
ON m.majorID = mc.majorID
I was thinking that I would have two tables for mysql. One for storing login information and the other for shipping address. Is that the conventional way or is everything store in one table?
For two tables... is there a way where it automatically copies a column from table A to table B, so that I can reference the same id to grab their shipping address...
If its a single address and if it is going to be updated everytime , then you can have it in a single table something like
**Customer**
customer_id [pkey]
customer_name
login_id
password
shipping_address
whereas if you want to store all the shipping addresses for a single customer(across multiple visits) then it would be a good design to have another table customer_shipping_address
**Customer**
customer_id [pkey]
customer_name
login_id
password
**Customer_Shipping_Address**
customer_id [fkey to customer]
shipping_address
This is my answer to your question regarding using 1 table or 2 tables. This decision depends on may factors. But i would suggest that you should use 2 separate tables. Because the log-in information is something that you will be retrieving very often compare to shipping information. Now if you have all the info in one table then table size will be huge and you will have to query this huge table everytime you need login information of user.
I think using two tables is better way to go. then just join them when you want to do the shipping.
The SQL for that would be like this.
SELECT
table1.id, table2.id, table2.somethingelse, table1.somethingels
FROM
table1 INNER JOIN table2
ON table1.foreignkey = table2.primarykey
WHERE
(some conditions is true)
The code above would need to be run on the shiping page itself.
I'm using MySQL as a database and I have some design questions:
I have a table "locations" containing locID, lat & long & name values of a certain location.
I also have a table categories with ID and category-name & locID.
Now I want to make sure that categories can be assigned to multiple locations. How do I do that correctly? It doesn't make too much sense to have the same category stored multiple times, once for every locID, does it? What's the way to go here?
Thank you!
Ron
You can use a join table to model the many-to-many relationship.
location_category
location_id category_id
1 1
1 2
2 1
2 3
3 4
Add foreign key constraints to prevent invalid values from being entered into the table:
From location_category.location_id to locations.locID
From location_category.category_id to categories.ID
Also make (location_id, category_id) the primary key for the table to prevent adding a category to the same location multiple times.
When reading the data from the table, use JOINs to get the related data from the main tables:
SELECT ...
FROM location_category
JOIN locations ON location_category.location_id = locations.locID
JOIN categories ON location_category.category_id = categories.ID
WHERE ....
im doing project for a school...and new to php and mysql..just learning..
i have to create one loginpage with 3 different users like corres,principal and the staff
in the table field what type i have to give like ENUM or SET..
i want to assign 0 for corr,1 for princi and 3 for staff..
how to do this and shud i give any field in the login page in php??
but im going to create only 2 tables one for corr and other one is for staff
corr table fields r corrs_id,username,password,staff_id,staff_role
staff table fields r staff_id,staff_role,name,qualification,address,state etc...
both the tables have 2 common fields staff_id and staff_role
now how to connect 2 tables and when username and password is given it shud first check the staff_role like 'princ' or a 'staff'
please help me....
Don't keep two same fields in 2 tables if you can get rid of one. Read about database normalization first.
Remove the staff_role field from the corr table and leave only the staff_id to join both tables as I said above.
The two tables are joinable using this exact staff_id
select u.name,r.role from users u
inner join roles r
on u.role_id=r.id
where r.role='princ' and u.username='bleh' and u.password='bleh'
Cheers