MySQL Database Design Suggestion - php

I want suggestions for DB Design in MySQL.
I have a table Movie_Celebrity_Role which contains movie id, celeb id, role id.
This Table is the link between movies, celebs (CelebName & CelebID) and roles (eg: Producer, Director etc and resp ids) tables.
I have another table Movie_Company_Role which is similar as above But here instead of Celebs, Companies IDs are stored linked to Companies Table and Roles are different such as Production House etc.
My problem is there are some fields like Media Relations which may have both Company and Celebs.
Hence, If i create another table as MoviedID_CelebID_CompID_RoleID When joining the tables i will get duplicate error as celebs table and Companies table have to be joined on matching this table and also Movie_celeb_role and Movie_comp_role.
Please suggest on how to go about this.
I want the Result on my site to be like this:
Producer
Celeb1
Celeb2
Director
Celeb1
Celeb3
...
...
...
Media Relations
Celeb2
Company1
Company12
PS: Also when i click a celeb or company link on this page it must go to celeb page or company page where the movies the celebrity or company associated must be listed with the role.

very confusing...
what 'duplicate error' will you get? so long as the role_id is valued properly from the role table, you should be ok.
Where you are not ok is that you have an ambiguity in your role definition. In your example, you want to have 2 role_id values in your table, one for the company, and one for the person.
I'm not sure you would event want to do this anyhow... i would expect that a company may perform more than one role in a movie, and a person may as well. (e.g. someone may be the director, and an actor at the same time)
here you want this to be a separate table, not all jammed into one.

Related

implementing user login MySQL

i have three tables to store details of different types of uses: customers, suppliers, staff
here are the structures of them,
customer(id,f_name,....)
supplier(id, name, address....)
employee(id, name, job_title....)
now i need those to log-in to the system , the log-in details stored in separate table,
user(user_id, password, role, ref_id)
how i planed to work this is, when registering some one, firstly insert the record to customer, supplier or employee according to the person, then insert a record in to the USER table in which the "ref_id" is the id of the previous table. the user is provided the "user_id" which can not be changed and they can change their password themselves.
when log-in, check the user_id, password combination, if ok then takes the ref_id and type, the appropriate table can be determined by the type which may be customer, supplier or employee....
the reason i done this in above way is,
customer, supplier and employee table has many different attributes except few like id, name...so can not maintain all the data in one table. in this situation if we use ids of customer, supplier and employee..would provide duplicate ids because they are separate tables!
so i need to know,
Is it correct the way i have implemented the authentication ?
if it isn't what is the correct way? (please mentioned that the details of the three parties should be handled separately)
i need to define relationship between supplier, customer, employee --> with user table. so is it ok to define three relationship as follows or another solution, how if the user table keep alone without relationship? is it violate the relational database concept?
customer (id-pk) ---->user (ref_id-fk)
supplier (id-pk) ---->user (ref_id-fk)
employee (id-pk) ---->user (ref_id-fk)

Querying multiple tables MySQL

I use MySQL and trying to write a PHP script for my school project.
There is one table named lessons contains this columns:
-id
-lessonid.
-studentid
I also have two different tables for notes and announcements
announcements and notes tables contains these columns:
-id
-lessonid
-content
-createdtime
I need to order both announcements and notes from latest to oldest by createdtime but also need to show all lessons a student takes.
For example: A students takes maths and physics lessons. I need to display him/her both notes and announcements for both of physics and maths and all items should be ordered by date. (like a timeline.) And of course I will not show him/her the notes and announcements for chemistry lesson. Also it will be good if I can say it is note or announcement on the list.
Can you help me to write SQL and PHP code for that?
Thanks.
EDIT: This is where I have stuck:
I have combined two tables and ordered them by date. But can't combine them with the lessons a student take.
SELECT title, created, lessonid FROM (SELECT title, created, lessonid FROM notes UNION SELECT title, created, lessonid FROM announcements) as a ORDER BY created DESC
First of all, thanks for letting us know that this is for a school project - therefore I won't give you the answer. If it is in the project then your teacher should have given you the concepts to come up with a solution.
Your question is well put together and I can see how to solve it but ... It's your project so you need to have a crack at it and post what you come up with.
I will give you some hints to get you started.
You need a query to combine the announcements and notes table. Then you need to group the data by the lesson and join that to the students. This is all basic SQL.
Good luck. Post what you come up with.
I'll also, follow fellow posters advice, and not do the legwork for you. but won't let you go empty handed, so will give you the concept.
there is a thing called third normal form, we decide how many tables according to that concept, so if its a big database then separate table for first name and separate for last name, as many people share those among themselves, so saves space and redundancy etc. so one table for person has personid as primary, and has lastname foreign key to refer to last name table , we generally name it lastNameRef, similarly firstNameRef. so now, each person has lot of classes, and each class has lot of persons(students) in it. so this is a many-many relation - we create a allreference table to solve this many to many problem. so there is one table for classes which has class id as primary key, so now u create a all reference table which a recordId as primarykey, (just for namesake) and personRef(refers to personId in person table) and classref(refers to classId in class table) if one person has two classes, another entry with same personId but different class Id, at the end, you can query the name of person from person table, and name of class from class table and create join on their foreign keys but use all three tables, result is (JOHN MATH, JOHN SCIENCE) etc, same way you display all notes for john searching name in person table, and subject in class table,and notes in notes table

How to design a database to keep track of books my family owns?

I am not necessarily looking for MySQL or PHP code. Rather I'm trying to get a concept of how to set everything up.
I want to create a database using MySQL (and using PHP to update it) of all the books my family owns. I want to set up different 'bookshelves' for each person in my family so we can see who has a certain book.
My first thought was to have a table for all the titles, authors, etc and have a field for user id to show who had the book. However, I might have a copy of Hunger Games and my grandmother might have a copy of Hunger Games. I want to be able to show it on both bookshelves. The only way my idea would work is if we had no duplicate books.
My next idea was to use a different table for each user and have a field that contains the book id for each book the user owns. I think this would work on a small scale but it does not seem like an efficient design. I am planning on making the database public for everyone in my town to use (thousands of people) once I get a stable website going so I want to start off with the right kind of design.
How should this be designed?
BOOK
--------
book_id
title
other_book_related_info
PERSON
-------
person_id
name
other_person_info
BOOK_PERSON
-------------
book_id
person_id
possibly-dates-when-this-person-owned-this-book
Here is one simple solution i can think of:
Book Table : List of all unique books
User Books : contains the user id and the book id. multiple users can own the same title.
Users : List of users;
This is pretty basic. Owner, book and author should be self explanatory. Add any additional fields to those tables you want. The bookshelf and book_authors are both cross reference tables so each book can have multiple owners and each book can have multiple authors.
**owner:**
owner_id
owner_name
...
**book:**
book_id
book_name
...
**author:**
author_id
author_name
...
**bookshelf:**
owner_id
book_id
**book_authors:**
book_id
author_id
You might like to differentiate between ownership of the book and current possession, since people will doubtless be borrowing. So the tables of BOOK (best call it ITEM if you're going to expand to DVD's etc) and PERSON, and the ownership table BOOK/MEDIA_OWNER, might be usefully accompanied by an ITEM_LOAN table.
You might like to also allow grouping of sets of items so that multiple volumes of a book, or discs of a show season, can be identified individually. Books (and films etc) also come in series, so think about how to represent that as well.
By the way, it's a generally accepted rule that if an edition of a work changes by more than 20% between print runs then it is a new impression, but it is not always granted a new ISBN. Depends on the publisher. Also, the hierarchy for books is based on Work -< Edition -< Impression, and these folks would be a good source of information of data structures relating to books.
Here's another solution:
**** BOOK ***
book_id
book_title
book_desc
book_bought
*** USERS ***
user_id,
name,
dateOfBirth
** Copies **
copy_id (PK)
user_id (FK)
book_id (FK)
NoOfCopies

Schema advice for minibus booking system

I'm developing with CakePHP 2.0 and MySQL.
I'm trying to create a minibus booking solution but I'm unsure if I'm following the right approach.
We have one minibus which we can book out. I'm not bothered about booking times overlapping at this stage. I've made a table for the minibus properties but I need to define the relationship between the minibus and the passengers.
Each minibus can have many (16) passengers.
A passenger can travel on more than one minibus (one today, one tomorrow etc).
Also I need to be able to set the type of passenger to either passenger or driver.
Will this need three tables? I was thinking:
Buses table (id, description)
Users table (id, firstName)
Passengers table (buses.id, users.id, passenger_type)
Any advice would be appreciated.
I think I would introduce a Trip table and assign the bus to an instance of a Trip. Then introduce a junction table to resolve the many-to-many relationship between Trips and Passengers.
Given that one minibus can service many passengers, and one passenger can ride on many minibus routes, then what you have is a many-to-many (m-to-n) relationship.
Therefore, you'll need three tables: one for Passenger, one for Minibus, and one that relates the first two via key associations:
Passenger_Minibus
------------------
PassengerID INT NOT NULL
MinibusID INT NOT NULL
It will look something like this:
One table to store capacity of the minibus. (CapacityId)
One table to store passenger information. (PassengerId, Name, address etc)
One table to store Minibus information. (MiniBusId, CapacityId, Make, Model, Year, color etc..)
One table for BookingInformation(BookingId, date, time, FromDestination, ToDestination etc)
One table for TripSchedule(TripId, BookingId, MinibusId)(Only if you have multiple buses per booking else you can add MiniBusId to BookingInformation table and get rid of this table)
One Link table to store TripId and PassengerId. (If you have only one minibus per booking, add BookingId instead of TripId)

which database type are good for arrays?

i want my database to support one company haveing many users how can i do that? exampleusers table(UID,username,password)company table(CID,companyname,usersthatistheownerofthecompany) how can i do that? what should i do ? should i make an array in php like 1241,423,4123 *uid's that will be inserted on usersthatistheownerofthecompany row ? or is there any better idea ?
If you want each user to have one (and never more) company, you should have :
user table
uid
username
...
company_id
company table
company_id
company_name
...
Then, user.company_id would be a foreign key, that references company.company_id.
And, then, you store :
One line in user for each user
Referencing the id of the right company for that user
which is the company_id of the right line in the company table.
And one line for each company in company
There is no user's related information stored in the company table -- and as each user "points" to a company, a company can have several users.
Storing data as an array like you suggested is definitely not quite a good idea -- just not the way a relational database works.
If each user can have several companies, and each comparny can have several users, you'll have to add a third table (a join table), that will indicate, for each user(s), to which company(ies) they are attached to :
user table
uid
username
...
company table
company_id
company_name
...
user_company table
uid
company_id
In this situation, there is no user-related stuff in the company table, and there is no company-related stuff in the user table : the link between those is in the user_company table.
Of course, user_company.uid will be a foreign-key to user.uid ; and user_company.company_id will be a foreign-key to company.company_id.
There is a better idea - it's called a cross-table join. What you do is you create a third table, which contains two columns. In those two columns you store the primary key of the tables you're connecting to eachother.
The idea is that you're creating a relation between a company and a user. In a relational database, relations are indicated between tables by using foreign keys.
Of course, this only applies when you want to connect multiple users to multiple companies (an "M-N" relationship). If you want to connect multiple users to a single company, simply add a column for the company id to the user.
Any relational database is a good way to go. Have a look at MS SQL or MySQL.

Categories