I have an ER diagram, shown below.
I need help to create a relational database. To explain the ER, I have Services and service has 1-6 objectives and each objective can match up to 1-8 goals. So the relationship between services and objectives is 1 to many (1-6) and the relationship between objectives is also 1 to many (1-8).
This is what I have so far (* = primary key)
Goals: (GID*, Goal_statement)
Objectives (OID*, Objective_statement)
Services (SID*, Service_Name)
I have to create an PHP script and display the services and then when I click a service, I will be redirected to show its objectives and the related Goals to the objectives. How can I show this? Do I have to create a table for MATCH and HAVE?
No, you don't need joining tables in this case. One service can have many objectives, and one objective can have many goals. That's two 1:many relationships, so all you need is a foreign key in each of the "has" tables.
Thus, you'll also need these columns:
service.objective_id (so a service can specify which objective it is related to)
objective.goal.id (so an objective can specify which goal it is related to)
To create your web application, you'll need to write some SQL, which you can decide before you write the PHP. It sounds like you will need:
A list of services (simple SELECT)
A list of objectives and their goals, given a service ID (a SELECT with an INNER JOIN)
Related
i want to create a platform by laravel 6 included classes students an masters
masters can put the student's scores and the students can see them in their profile...
there is a many to many relation between classes and masters and between student and classes too.
the masters an students are not seperated and all of them store in user table and determine by his role_id
my big issue is uploading of scores by masters... i am extremely confused
has any one any idea ?
What you might want to simplify things is an associative table of users, classes, and scores (I've drawn a diagram for you https://dbdiagram.io/d/5e9787f039d18f5553fdabb1). With this table, you can query pretty much all you could ever want.
Now all you need to do is configure privileges based on user role. A master can read from and write to all class_user_score entities where if there is a record of him being in a class, he can read and CRUD all class_user_score entities with the same classId. A student can only read class_user_score entities with his userId in them.
Unfortunately, I can't help you with the Laravel implementation (they also call an associative table a pivot table for some weird reason) since I'm more of a React, Nodejs type of guy, but I hope this at least helps you to reason about the problem.
I am trying to model a database for my current project and I came across a new problem. I have a Project which is supervised by Supervisor, Coordinator and Company. So Project table has Supervisor.id as foreign key and so on. There is also Student table which contains Project.is as a foreign key (because many users can do a project). This is how it is right now. What I would like to do is to have a User table containing a column named type which allows me to see what the role of that particular user is (also student). Even though the table will contain many NULL entries, I will have far less redundant code.
However, the main reason I want to have one User table is that I am using CakePHP and it is not easy to have different models log in. Is this possible in a nice way?
Thanks
EDIT: Maybe I should say that every one of these roles will have different permissions.
I see three tables: USER, GROUP, and ROLE.
Users would be assigned to groups, and groups given roles. I think you need three, not one.
And cardinality matters: I can see where a USER could be assigned to many GROUPS; a GROUP could have many USERS; a ROLE could be assigned to several GROUPS; and a GROUP could have many ROLES. There are many to many JOIN tables as well:
USER <-> USER_GROUP <-> GROUP <-> GROUP_ROLE <-> ROLE
This is normalized - nothing is repeated this way. USER, GROUP, and ROLE have primary keys. The JOIN table primary key is a composite of the two IDs in each row.
It depend on how you will use your associations.
Why not
For example: if you use relation to output data later and you sure, that you database scheme will not changed, than ... why not? your main targets: quality of code and speed of development, so, not matter how much columns with null you will have.
But
if you not sure in your requirements or plan to extend database scheme you can use two columns
supervisor_model
supervisor_id
which will store apropriate values: Company, 77 (I mean that 77 it's id of come Company )
Another approach
You can UUID for you supervisor_id so, it will be unique among several tables and you have not many NULL and extra columns.
I am developing a social network for an intranet, and I came across a problem [?]. I have the entities User and Business as main entities of the network.
Note: A Business does not have, necessarily, a relationship with a User.
Following this idea, I have a group table, and a group can be created either by a user or by a business, there comes a question, how can I make the author field in this table?
I did the following, I created a table type, with the following data:
id | name
1 user
2 business
And my table groups like this:
id
name
description
tid (FK -> type.id)
author (INT)
Thus, if a group has a tid equals to 1, means that the author is referring to a User, if it have tid equals to 2, it is referring to a Business.
What do you think about this implementation? It is correct?
What can I do to improve it?
I'm using PHP 5.3.6 (Zend Framework and Doctrine2) + MySQL 5.1.
Per request:
The terminology for this is "polymorphic". Bill Karwin had a good answer that I can't find at the moment about it. From a database design perspective, I've been told the logical model term for this is an "arc" relationship. Either way, I see no issues with what you posted, other than supporting multiple authors.
The bad thing about this is that you cannot use the foreign key constraints to check and verify your data if you do this.
I would recommend using 2 tables instead. one to store the relationship between user and group, and one to store the relationship between business and group.
This will allow you to use all the fk benefits like cascading deleted when the group or business or user is deleted.
I have two tables that look as Follows:
Person (Table Name)
Name1/Phone1/Email1/Address1/Organization1/Notes1 (Fields)
Organization (Table Name)
Organization1/Phone2/Email2/Address2/Web2/Notes2 (Fields)
Organization1 is the only field in common between the two tables.
When I display data on a person, I want to also check and see if there is data on their organization and display it as well if it exists. I'm using PHP to interface with mySQL.
You need to JOIN the tables.
SELECT * FROM Person LEFT JOIN Organization ON Person.Organization = Organization.Name;
This assumes the relationship is the Organization Name. I've done a LEFT JOIN since you said if exists. Check out this tutorial for more detail on joining tables.
Note: I agree and would recommend making your database more relational by adding Primary Keys and using them as Foreign Keys in your other tables.
This post is an explanation of relations, not code for you to use. If you want that, look elsewhere
Well, connections between tables are called relations. There are 3 types of relations.
1) One -> One - This type of relation means 1 row is related to 1 other row in a different table
2) One -> Many - This type of relation means 1 row is related to a variable number of rows in a different table.
An example may be A folder can have multiple files, but a file can't have multiple folders. So in this case the 1 would be the folder, and the many would be the files.
3) Many -> Many - This type of relation means many rows can relate to many other rows.
An example may be labels. You can label many things the same name (desk appliance for example), and each thing can have multiple labels (a lamp can have both desk appliance & light labels).
.
So now that you know the different relations, we will go into your question. The relation you are looking at is a one to many, one corporation can have many people, but a person can only have one corporation. I suppose a person could work for multiple people, but that is much more complex (so we'll skip it).
One to many relations are by far the most common, and are pretty easy to do. This is where joins come in (left, right, and inner joins). Tizag has an excellent tutorial on joins here: http://www.tizag.com/sqlTutorial/sqljoin.php.
Hope that helps.
You should use a foreign key, but you need to use the InnoDB storage engine (MyISAM does not support foreign keys yet).
Make your tables look something like this:
Person_ID, Name, Phone, Email, Address, Organisation_ID, Notes (or if you have multiple notes, create a seperate table that maps person_id to a note).
Organisation_ID, Name, Phone, Email, Address, Web, Notes.
Select your person, then if Organisation_ID exists, select the Organisation where Organisation_ID equals the ID you obtained from the person row.
I'm modelling an ontology in SQL using a tree to indicate relations between concepts.
My problem is how to disambiguate concepts in a graph/tree without replicating concepts.
Eg. I have the concept of courses: "karate", "sailing" (lessons one can take from instructors) and places: "gym", "boat club" where these courses can be taken. In the graph it will be:
gym boat club
has has
courses
of of
karate sailing
How can I model this graph to avoid boat club having karate courses and without duplicating the concept of courses?
Thanks!
I would turn the whole data architecture around and think about this as a graph. Have a table for all the concepts (nodes) - gym, boat club, courses, karate, sailing - and another table to create the links (vertices) between them. That table looks something like:
Links
from (concept foreign key)
to (concept foreign key)
link_type (has/of/etc)
It may take a lot more SQL to build up all your information in memory, but this schema will handle these relationships simply.
The table that contains the data Karate and Sailing would contain the primary keys for the other two tables as foreign keys. This would allow for you to have as many of each data type as you like while still being able to traverse the tables to get your information.