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 9 years ago.
Improve this question
I have started working on a todo list application in php. I haven't worked on relational database earlier so i am confused about the structure of my database. I want to keep it simple.
This is my plan:
Users
U_id,
username,
password,
email
Tasks
t_id,
task_name,
description,
u_id reference users table
Lists
l_id,
list_name,
?
My main cause of confusion is
How to connect lists and tasks? //each user will be able to create multiple lists e.g.- Home, personal, office, Home work etc.
What is the best way to connect users and list? //each user can have multiple lists with multiple tasks, so how to manage this.
To know all the tasks in a list you need a relation table if a task can belong to one or more list.
Users U_id, username, password, email //a user can have zero or one or many lists
Tasks t_id, task_name, description // a task can belong to zero or one or many lists
Lists L_id, list_name, U_id // a list can belong to one and only one User
Lists_Tasks t_id, L_id // a task can belong to one or more list
to get all the tasks form a list
SELECT T.* FROM Tasks AS T
JOIN Lists_Tasks AS L_T ON T.t_id = L_T.t_id
WHERE L_T.l_id = id_from_the_list
to get all the Lists from a User
SELECT * FROM Lists WHERE U_id = id_of_user
n:m relation table: which list is connected with a task
each list can have a u_Id field. When more than one user can have the same list then do n:m relation, too
example for n:m relation:
list_task_relation: list_id, task_id
Keep a U_id is your Lists table.
Also, keep a l_id in your Tasks table
Read MySQL Tutorial before you go forward.
Related
I'm building a small survey application in Laravel and I'm not sure how to structure the database. There are three main entities for it:
Users
Surveys
Questions
A user can fill in many surveys and a survey can have many questions. For this I'm creating two pivot tables:
user_survey (id, user_id, survey_id)
survey_question (id, survey_id, question_id)
But I'm not sure how to store answers as a user may also fill out the same survey multiple times so there needs to a way differentiating between different versions of a users answers.
My thought at them moment would be to create an additional table called user_survey_answers with the following columns:
id
survey_instance_id (will reference the id column from the user_client table)
question_id
question_answer
Would this be the best structure to use for this? If so, how would I create the eloquent relationships within Laravel to reference the user_survey_answers table?
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 have a small app in php using the cakePHP framework connected to mySql database.
I have two types of users (business and employees) with relationships to other tables also. I am now adding a login facility which means I will be adding a users table to hold the password details. So both employees and businesses will be able to login.
I am not sure what way to add the relationships between the users table and the businesses and employees table. I will be adding an admin role also.
My current tables are:
- course_files
- course_modules
- courses
- courses_employees
- employees
- businesses
My Choices:
Merge the businesses and employees table with the users and have a
users (id, role employee_name, business_name, business_address, etc)
roles (admin, employee, business)
My issue with this approach is that the fields for businesses and employees are very different so the users table will have a lot of fields. But it will make it easier for the login functionality.
Add business_id and employee_id to the users table. This option will be a little more involved and one field will always be blank like employee_id.
users (id, employee_id, business_id, role)
roles (admin, employee, business)
employees (business_id)
businesses
So before I go down one route and find I went in the wrong direction I was wondering what would be best practice?
I disagree with both of your approaches. I would have a users table for login, and tables for employees and business, just like you suggested in your second approach. The difference is that I wouldn't have employee_id and business_id in users because, as you've already stated, one will always be blank. Why don't you instead have a user_id in both tables, as they will always have a user profile (intended for login)? I see you use CakePHP, so these will be relationships as follows:
User hasOne Employee
User hasOne Business
Employee belongsTo User
Business belongsTo User
Let me know if any other business rules in your application make my approach hard to implement.
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 7 years ago.
Improve this question
I am looking to implement a new database and store students exam data and marks in the database. There are an arbitrary number of students and each student has an arbitrary number of modules with corresponding marks. I need the database to be created so that I am able to query the database and return the students name followed by each of their modules and corresponding marks and then to move onto the next student. This is because I want to display it in PHP with a list of students names in bold and then a table of their modules and their marks.
I initially considered to organise the table like this:
| StudentName | Module | Result |
With a new entry for each module that the student takes and just store multiple students but then I do not know how I would then query the database and retrieve each StudentName individually and then be able to loop through their corresponding modules and results to store it in an HTML table.
Any ideas would be appreciated
Give each table an id field that auto increments
Where you need to link results to modules, for example, you'd add a
moduleID field to the result table to tie the result to the module.
You'd then do queries with joins to bring in both sets of data.
To tie students to modules, you'd need another table that has a
studentID field and a ModuleID field. You'd then query this table
and join on StudentName and Module to get the respective data for
each.
First, you need several tables. One table cannot do it. Create a table for each type of objects. In this case, you need a table for students and one for modules.
Both tables need an id column to identify the rows. Create a primary key on them.
Students table: Id, StudentName
Module table: Id, ModuleName
There is a many to many relationship between students and modules (each student can have more than one module and each module can be chosen by many students). A many to many relationship requires a separate table.
You were not clear if there is one grade per student and module. If there is only one grade, the best solution is to include the grade in this relationship table.
Grade table: Id, StudentId, ModuleId, Grade
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 8 years ago.
Improve this question
I'm trying to decide the best possible way to structure my user table. Users can have access to multiple "brands" and each brand will have multiple tables. That is, if user X wants to see data for brand Y, the database contains the information to say which tables I need to make calls to.
For example, user X can access Brand1 and Brand2. Brand1 has its data in table1, table2, and table3. Brand2 has its data in table4, table5, table6. User selects Brand2 and the application makes a call to find out that table4, table5, table6 should be used until user selects a different brand.
What's the best way to structure this knowing that a single brand might have multiple users that can access the data?
Do I need more than just a user table and, if so, what else and how would that connect to the user table?
Thanks.
Like Mark Baker pointed, you can have one user table, one brand table and one user_brand table.
user table - stores user_id (and other user data)
brand table - stores brand_id (and other brand data)
You've already defined relationships between users and brands. It's M:N (many to many), which means that:
one user can have access to multiple brands.
one brand can be accessed by many users.
Table user_brand solves the access problem.
user_brand table - stores user_id and brand_id (and optional data which better describes this relationship).
Here is an example about sql syntax (enforcing foreign key constraints).
You can use GRANT query so the user can access just 2 tables in a database, then in the application, you can code it just to select 1 table, until the user changes the brand. The brand itself is the table, isn't it?
In PHP code, the code and query should be like this:
<?php
$db = new mysqli('hostname', 'db_username', 'db_password', 'db_name');
$brand = $_SESSION['brandName']; // use this if you use sessions to cache the data
$db->query("SELECT * from `$brand`");
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 8 years ago.
Improve this question
I am a beginner to databases.I am working on site where i have to save education qualification in database for different users.for e.g
There is a user Alex.He is graduate in Computer science from Singapore
University in 2006. He is also masters in Computer Science from Canada
University in 2009.
It will be different for different users.
Should i try a new table for all the education qualification and give id to user table.
Select * from users,education;
How can i save this in database keeping best practices in mind ?
Thanks In advance. :)
You'll want to normalize your data, which means remove redundancies and inconsistencies. While you can get away with doing a very crude version of the layout with 2 tables where you allow a user to have multiple educations, you'll want to make sure that you're not creating more work for yourself going down the road.
The problem with the 2 table layout is that you may have records for the same schools, degrees, or majors, but they may be entered differently.
So if you did a search like `SELECT * FROM users INNER JOIN education
ON users.user_id=education.user_id WHERE education.major LIKE '%Computer Science%' you might not return all of the results for related majors if some of the majors are entered as something like "CompSci."
So you might actually have multiple tables. An example of these multiple tables might be:
users
user_degrees
schools
degree_types
majors
states
countries
Your table structure for user_degrees might look something like this:
user_degree_id (INT)
user_id (INT)
school_id (INT)
degree_type_id (INT)
major_id (INT)
graduation_year (DATETIME)
Schools would look something like this:
school_id (INT)
school_name (TINYTEXT)
school_city (VARCHAR(45))
state_id (INT)
country_id (INT)
Degree types might be:
Associate of Arts
Associate of Science
Bachelor of Arts
Masters
Doctorate
etc
There are several other types of degrees.
You get the idea. Basically, the premise is that you enter the information for each type of record once, then you can cross relate that information with the various tables without needing to enter it again. Also it allows a global change to any records using that same information.
Here you can read more about Database normalization.
You must have created 2 tables like users and education
Users table structure is like
id username etc etc
education
id user_id course/degree
then you can fetch is like
select * from users as u ,education as e where u.id = e.user_id