I'm currently designing a database for a project I have in college and I would really appreciate some feedback. Basically, the idea of the project is to create a food plants, food inventory database. I have created a schema for the database but I'm not sure if the products table abides by 3NF.
So my products table contains p_id( the primary key), the name of the product, what type is the product (eg pizza or dessert), the country it comes from, the region in that country and who the product is suitable for (eg vegan).
So basically when I break it down, I feel that Region is dependant on Country and type is dependant on the name? Would I be right in assuming this? I could then split the table into 3 separate tables. 1 which would contain p_id, name, Country and another one which would contain Country, Region and a third one which would contain name and type. Would this then be a fully normalised database up to 4NF?
Here is a my schema:
Related
I want to build a ER-model for companies.
Some fields for companies are the same, like the name, legal_address and others, but some fields are defined based on the country, where the company is registered.
For example if we take Russia, the company should have fields like inn, ogrn, etc.. (don't matter the names).
If we will take another country - the company will have another set of fields.
I need an advice of how to plan (architect) this.
Any ideas would be great.
At the moment I have this done:
[locations]
id
country
city
lat
lng
zip_code
[companies]
id
location_id
name
director
legal_address
actual_address
[company_russia]
id
company_id
inn
kpp
ogrn
stat_codes
reg_number_fss
reg_number_pfr
But I think there will be problems joining the tables and that each other country requires a separate table - is not a good approach.
I'm creating one project for restaurants where user can browse and choose food. I didn't made before such a think and have some troubles with design and relations in database tables. This is the case
1. Restaurants
2. Customer click on restaurant-1
3. Customer get menu list for foods and drinks
4. Customer browse the food via sub-categories ( Salads, Drinks, Desserts and so on )
5. Customer choose some food and drinks...
As I can see here I would need 4 main tables restaurants, meals, meal_types and menu. Table Restaurants will hold restaurants
id
name
menu
image
text
address
Table Meal_types will hold main meal category - Drinks, Salads, Desserts and so on
id
name
Table meals will hold all foods/drinks
id
name
image
text
weigh
price
Table menu must keep which food/drinks to which restaurant to show when is selected.
id
name
So here is the tricky/hard part for me. How to make relations between them. One of the relations that I see and must have is between meals and meal_types. But others? How to connect restaurants with them and when user click on some restaurant to see the food that is served only from this restaurant. First thought for me was with this table menu but don't know how exactly.
I found it easier to Name the ids according to the items they represent. So I would suggest to Name the column id in your table restaurant Restaurant_id. This might help you Keep the general view in your relations.
Start from your smallest part: Your table meals. You have to combine this table with your Meal_types table since you have a 1:1 relationship between both (each meal has exactly one type, so just add another colum meal_type to meals).
Second you have your restaurants.
Now you just need an Information about the meals a Restaurant offers. This Translation table is your table menue and holds columns like this:
Restaurant_id,
Meal_id
To get the menue of a certain Restaurant you would query
SELECT
meals.meal_type, meals.name, meals.price
FROM
restaurant, menue, meals
WHERE
Restaurant.restaurant_id = menue.restaurant_id
AND menue.meal_id = meals.meal_id
AND Restaurant.name = 'Mc Donald's'
I will create 3 tables in mysql:
Movies: id-name-country
Tv-Series: id-name-country
Artists: id-name-country
Instead of entering country information into these tables seperately, i am planning to create another table:
Countries: id-country
And i will make my first three tables take country data from Countries table. (So that, if the name of one country is misspelled, it will be easy just to correct in one place. Data in other tables will be updated automatically.
Can i do this with "foreign keys"?
Is this the correct approach?
Your approach so far is correct, ONLY IF by "country" in Tv-Series and Artist you mean country ID and NOT a value. And yes you can use foreign keys (country id in tv-series and artist is a foreign key linking to Countries);
Edit:
Side note: looking at your edit I feel obliged to point out that If you are planning to link Movie/TV-Show with artist you need a 4th table to maintain normalization you've got so far.
Edit2:
The usual way to decide whether you need tables is to check what kind of connection 2 tables or values have.
If it's 1 to many (like artist to country of origin), you are fine.
If you have Many to many, like Movie with Artist where 1 artist can be in multiple movies and 1 movie can have multiple artists you need a linking table.
If you have 1 to 1 relation (like customer_ID and passport details in a banking system, where they could be stored separately in customer and Passport tables, but joining them makes more sense because a banks only hold details of 1 valid passport for each customer and 1 passport can only be used by 1 person) you can merge the tables (at the risk of not meeting Normalization 3 criteria)
I am new in SQL, and I have been trying to learn this on my own so that I can do some works later on. I am working on mySQL. I have been working on a product vs producer database for practice and I am stuck at some point of it. My tables are:
producer(producerid , produceraddress)
product(productid , productname)
cost(producerid , productid, price)
Now I want to find: 1. products produced by at least two different producers and 2. pairs of producers such that the first producer charges more for some product than the second producer.
I am totally lost. Can anyone help?
With these schemas you can't do it.
You need to do again your schemas and read about relations between tables.
For example if you have the Producer table with producer_id and producer_name and in your products table product_id and product_name you can't say I want to see this product that is made by this producer.
In your product table you must add a new field that could be producer_id and it will be a reference to the producer_id from producer table. With this you could retrieve from the database a product that it's made for the producer you are interested.
I am new to database structure and design. Currently, I am in the process of creating a course catalog that will match course description according to course name and date. I have sketched one table describing the courses, which include course_code, name and every other relevant information. Then I sketched another table linking those courses to when they will be taught.
I am missing classes that are classified as all_year. Also I am missing a way how to label the courses under a major. Since hypothetically a course can belong to several majors, putting the data from one into the other would force you to duplicate data. Any ideas how I would implement this two things to my tables design? Or suggestion in how to restructure my design. If possible please show me a query to execute in my phpmyadmin DB.
Example of table courses
id serial
course_code text
description text
Example of table course_dates
id serial
course_id serial
year date
semester
Example of table majors
major_id int
course_id int
So a populated database could contain the following:
Table courses
id course_code description
1 INF1000 "Basic programming"
2 INF1001 "More basic programming"
Table course_dates (0 for spring 1 for fall)
id course_id year semester
1 1 2012 0
2 1 2013 1
3 2 2013 1
To link courses to majors - this is a one to many relationship (one course to many majors) - you want to use a linking table that has this type of structure:
table courses_majors
major_id int
course_id int
Remember to index this table as well - its very important. Then you can populate it and have one course even go to many majors and many course to one major (many to many relationship).
Then you can run a join on the tables across this table:
select * from courses left join courses_majors on courses.id = courses_majors.course_id left join majors on courses_majors.majors_id = majors.id
Of course you can add a where clause, etc.
The other way is to create a table of majors:
majors
id int
name varchar
Then add a major_id to your courses table - this will just give you a one to one relationship from courses to majors, but many courses can join a major.
As for Yearly, I would just add a field in the database to account for this, probably a tiny int and just make it 0 or 1.