I have three master tables for location information
Country {ID, Name}
State {ID, Name, CountryID}
City {ID, Name, StateID}
Now I have one transcation table called Person which hold the person name and his location information.
My Question is shall I have only CityID in the Person table like this:
Person {ID, Name, CityID}'
And have view of join query which give me detail like "Person{ID,Name,City,State,Country}"
or Shall I replicate the mapping
Person {ID, Name, CityID, StateID, CountryID}
Please suggest which do you feel is to be selected and why? if there is any other option available, please suggest.
Thanks in advance.
I would just use a reference table, which will allow you to have more expandability in the future:
Person {ID, Name}
PersonLocation {PersonID, CityID}
Just pay attention to the primary key you use on the PERSON table, so you can distinguish one record from the other
Related
Actually I'm confused for the case, which relation fits best for my case, and in my opinion the best one is to have a table with 3 primary keys.
To be more specific.
I have a Person model in one of my db's, which has structure like
Person:
Id,
FirstName,
LastName,
...
And the other model Department, which has structure mentioned below
Department:
Id,
Name,
Description,
...
And goal is to set up Editors of schedule for each department and add also admins, whioch will approve requested schedules from editors. Editors and Admins are from same Person table, and if to assume, we need to map some Persons and department with some type.
I'm thinking about to have a mapping table with structure
PersonID,
DepartmentID,
Type (editor or admin)
And not sure, which relation fits best for this. If to have belongsToMany relation here with primary keys PersonID and DepartmentID, we will face an issue, because same Person possibly can be as editor and as admin for one single department. I have MS SQL server as a db.
Any suggestions will be appreciated
you can define many to many relations and use wherePivot method to select by pivot table Type column:
// Department model
public function admins()
{
return $this->belongsToMany(Person::class)->wherePivot('type', 'admin');
}
public function editors()
{
return $this->belongsToMany(Person::class)->wherePivot('type', 'editor');
}
// Person model
public function departmentsWhereIsAdmin()
{
return $this->belongsToMany(Department::class)->wherePivot('type', 'admin');
}
public function departmentsWhereIsEditor()
{
return $this->belongsToMany(Department::class)->wherePivot('type', 'editor');
}
// Note: we use methods names without parentheses
// usage for department
$department = Department::first(); // for example
dump($department->admins);
dump($department->editors);
// usage for person
$person = Person::first(); // for example
dump($person->departmentsWhereIsAdmin);
dump($person->departmentsWhereIsEditor);
I have a junction object called "JIRA_Data" and its linked/ related to Accounts. How do I query to fetch data from this relationship. I am trying to extract id,type from Account object and Name from JIRA_Data junction object.
Could anyone assist. Thanks
You could do it multiple ways depending on how you want the data. If you query it from the parent account it would be something to the effect:
SELECT Id, Name, (Select ID, Name, Other_Parent__r.Name FROM Jira_Datas__r) FROM Account
or from the junction
SELECT Id, Name, Account.Name, Other_Parent__r.Name FROM Jira_Data__c
Good day, i need help on how to plan tables in phpmyadmin. This site has
table for users: username, password, email, phone, country, state, city, address, etc
table for comments:comment1, comment2, comment3, .... (these comments are on differnt pages)
the idea is, when a user logs in, he or she will comment. i want a way to plan the tables so that when a user comments, it will insert record into the comments according to the userid.
One possibility is to add a PrimaryKey userId to your users table and Foreign Key userId to your comments table. Then you can grab the userId from the user who is logged in and write it with the according comment to your table.
For Example:
table for users: userId, username, password, email, phone, country, state, city, address, ...
table for comments: userId, comment1, comment2, comment3, ...
I hope this may help you.
As you say each comment is tied to a particular page and user, I would recommend that you structure your comments table as:
user_id, page_id, comment_id
Each comment from each user on a page will then have a corresponding row in the table. This structure will make it much easier to manage adding new pages or re-ordering your existing pages if you need to do so.
I am creating a small playlist program on VB, which contains adduser, deleteuser and also user can modify its playlist.
My stupid question is, how do I manage user playlist? Consider I am using database, where should I add user?
As a new table in Database?
As a new Entry in some kind of Table which contains userID, Name and its undefined number of choices?
If I select option 2, what kind of datatype handles a integer set of undefined size?
Thank you.
You would create 3 tables:
Users table
-----------
userID
email
password
name
Playlist table
--------------
playlistID
userID
trackID
Tracks table
------------
trackID
trackName
You would then create relationship between the tables:
Users.userID 1-* Playlist.userID (1 to many)
Tracks.trackID 1-* Playlist.trackID (1 to many)
Then you would store the users choices in the playlist table.
To see a users tracks you could do:
SELECT Playlist.trackID, Tracks.trackName
FROM Playlist
JOIN Tracks ON Playlist.trackID=Tracks.trackID
WHERE Playlist.userID = 12
ORDER BY Tracks.trackName
This is the basics of relational database system and normalisation of data.
For more information see:
http://www.dreamincode.net/forums/topic/179103-relational-database-design-normalization/
I have two tables:
customer (
client_id(PK),
lastname,
firstname,
flightID (Fk)
)
flight (
flight_id(PK),
flightarrival,
flightdepart
)
My questions are:
Are there any problems with the schema?
How can I add data to the flight table?
I really want to separate the two because bulk tables are confusing.
Here is an example of a "bulk table":
customer(
client_id(PK),
lastname,
firstname,
flightarrival,
flightdepart
)
but I want to normalize it and separate it from the customer table and just link them.
The schema you proposed would mean that each customer could be related to one (not zero, not more than one) flight, which feels wrong.
In essence, I think you have a many-to-many relationship, which you can do with three tables:
customer (id(PK), lastname, firstname)
flight (id(PK), flightarrival, flightdepart)
customer_flight (
customer_id REFERENCES customer(id),
flight_id REFERENCES flight(id)
)
You should create a separate cross-reference many-tomany - table which will be like
customer_flights(id int (pk),customer_id int, flight_id int,timecreated(optional))
one customer may have several flights booked like in past and in future...
and later join the two tables basing on the records in this table