MySQL Schema DBAdmins can you please look this over? - mysql

Database Schemas are not my strong point however I'm making a Recipe Schema and would like some input from experts on this before I move forward with my code.
InnoDB with relations.
Tabels Recipe_Steps and Recipe_Step_Ingredients have relations setup for ON UPDATE RESTRICT and ON DELETE CASCADE.
Ok I rearranged things a bit, I think I made it more simple without loosing any functionality. Usually recipes list the ingredients and the instructions separate so I don't think I need the steps to be linked to the ingredients.
What do you guys think?

One suggestion is that Recepie steps table is not connected with recepie ingredients.... you might want to do that since different ingredients are used in different steps .

Related

MS access multiple relationships between two tables

We had an MS Access guru at our company who left for another position. Before she left she gave me a quick introduction on how to create queries from a sql server. I am really struggling with this and as I have no one to turn to at our company I was hoping you guys could help.
Hope you can help!
Thanks!
Well, keep in mind that when you build a query, it DOES NOT necessary mean that a enforced relationship exists here. (it might).
Further more, if you imported the tables, then again its doubtful that relations are defined in Access unless you use the relationships window to "enforce" such relationships.
However, when building a query? We will often join on two fields. When you build a query in the query builder, you are free to "make up" any kind of join you want.
Say I was given two different spreadsheets. One had some people, and another had a list of hotels.
Ok, so say we want to generate a list of all people in the same city as the hotels.
You might join between table "People" and say Hotels with city.
however, WHAT happens if there is more then one state with the same City name?
Well, then just join on City AND State!!!
So you get this:
So I not have some related tables here. I just feel like and want to, and need to join the two tables of data.
As such, we never cared or setup or "had" some relationship defined, but all we care about is creating and building a working query.
So, don't confuse the simple act of building some query with that of having setup a corrrect relatonships between tables.
For a working application? Yes, you most certainly will setup relatonships.
So, if you setup relatonships correctly, then you not be able to say add a customer "invoice" reocrd without FIRST having a customer record. You don't have to do this, but it is a very good idea for a working applicaton.
However, when dealing with imported data? You often may not have an pre-defined relationships.
Now, of course in "most" cases, a query that involves multiple tables will in near all cases "follow" what you defined as relationships in the relationships window but it not necessary a requirement at all.
As noted, when building a working application? Then yes, of course you want to setup the relatonships BEFORE you start adding data.
But for general data processing, and creating queries against say different tables of data you are slicing and dicing and working with?
You are free to cook up and draw lines between the tables in the query builder, and as such, often such quires will have zero to do with the relationships you defined, or in fact even when you don't have any relationships defined at all.
That above People and the list of hotels is a great example. I mean, it rather cool that I simple joined on both City and State, and did not have to write one line of data processing code for my desired results
(a list of people in cities that live in the same city as my hotel list).
So don't confuse what we call "referential integrity" and defined relationships. We define these relationships so it becomes impossible for you the developer to add a customer invoice without first having added the customer. And it also means that you, your code, or even a editing the tables directly will not allow this to occur.
However, when dealing with just reporting, or importing data to work on? Well, then often we will not have any relationships defined, but that sure does not stop us from firing up the query builder and drawing join lines between tables.
Between two given Tables you can have one relationship involving two (of more) fields or two (or more) relationships each involving one field. Both cases are possible and have different implications.
The first case, as the first commenter pointed out, is typically used when you have a compound key in the master Table of the relationship.
The second case is typically used when you have two candidate keys in the master table, each of which is used as a master field in each of the two independent relationships.
In Ms-access the case of two independent relationships may be identified because it implies two table-boxes for the same table in the relationships pane.

Display/list database tables and relationships in SQL

Is there a way to visually represent the tables in a sql database; showing relationships, if there are any, between the different tables? Thank you.
In SQL server, under the database name is a folder named "Database Diagrams". Right click and "add new database diagram".
Click on the tables you wish to include, then hit the "Add" button.
Provided you have already setup your relationships the connections will be shown, 1 to many, many to many, etc. You can name and save, then if you add relationships after the fact, they will automatically appear (but if you add tables after the fact, you will have to manually add them to your saved diagram(s)).
You can move the tables and relationship chains around to make it more clear, and add labels for headers, etc. If you have a lot of tables I find zooming out while working on arranging things in a logical order easiest.
Use database diagrams. Here is a link that may help: http://www.mssqltips.com/sqlservertip/1816/getting-started-with-sql-server-database-diagrams/

How do I properly structure my relational mySQL database

I am making a database that is for employee scheduling. I am, for the first time ever, making a relational mySQL database so that I can efficiently manage all of the data. I have been using the mySQL Workbench program to help me visualize how this is going to go. Here is what I have so far:
What I have pictured in my head is that, based on the drawing, I would set the schedule in the schedule table which uses references from the other tables as shown. Then when I need to display this schedule, I would pull everything from the schedule table. Whenever I've worked with a database in the past, it hasn't been of the normalized type, so I would just enter the data into one table and then pull the data out from that one table. Now that I'm tackling a much larger project I am sure that having all of the tables split (normalized) like this is the way to go, but I'm having trouble seeing how everything comes together in the end. I have a feeling it doesn't work the way I have it pictured, #grossvogel pointed out what I believe to be something critical to making this all work and that is to use the join function to pull the data.
The reason I started with a relational database was so that if I made a change to (for example) the shift table and instead of record 1 being "AM" I wanted it to be "Morning", it would then automatically change the relevant sections through the cascade option.
The reason I'm posting this here is because I am hoping someone can help fill in the blanks and to point me in the right direction so I don't spend a lot of hours only to find out I made a wrong turn at the beginning.
Maybe the piece you're missing is the idea of using a query with joins to pull in data from multiple tables. For instance (just incorporating a couple of your tables):
SELECT Dept_Name, Emp_Name, Stat_Name ...
FROM schedule
INNER JOIN departments on schedule.Dept_ID = departments.Dept_ID
INNER JOIN employees on schedule.Emp_ID = employees.Emp_ID
INNER JOIN status on schedule.Stat_ID = status.Stat_ID
...
where ....
Note also that a schedule table that contains all of the information needed to be displayed on the final page is not in the spirit of relational data modeling. You want each table to model some entity in your application, so it might be more appropriate to rename schedule to something like shifts if each row represents a shift. (I usually use singular names for tables, but there are multiple perspectives there.)
This is, frankly, a very difficult question to answer because you could get a million different answers, each with their own merits. I'd suggest you take a look at these (there are probably better links out there too, these just seemed like good points to note) :
http://www.devshed.com/c/a/MySQL/Designing-a-MySQL-Database-Tips-and-Techniques/
http://en.wikipedia.org/wiki/Boyce%E2%80%93Codd_normal_form
http://www.sitepoint.com/forums/showthread.php?66342-SQL-and-RDBMS-Database-Design-DO-s-and-DON-Ts
I'd also suggest you try explaining what it is you want to achieve in more detail rather than just post the table structure and let us try to figure out what you meant by what you've done.
Often by trying to explain something verbally you may come to the realisations you need without anyone else's input at all!
One thing I will mention is that you don't have to denormalise a table to report certain values together, you should be considering views for that kind of thing...

How do I to add multiple directories to an SQL database while sharing the same schema?

A buddy of mine needed help adding multiple directories to an SQL database while sharing the same schema yet have different listings. I've scoured my "MySQL" books and Google, I can't find any definitive information. I was just hoping someone here knew how to do this.
To clarify, there are a total of 12 directories. 11 are to be searched independently of one another and the 12th (which already exists) will be a directory comprised of all 11 directories, all of which will have the same schema.
The goals are to create these unique directories using the existing schema model, and to upload multiple directory entries at a time to the directories.
Does this mean the names of the directory entries have to be unique (ie. Name_1, Name_2, Name_3, etc...?) Or, will it be a matter of duplicating the schema under a different name? Any advise will help.
A directory, in this context, is like a roledex. I mean to have 12 roledexes with each rolodex having unique entries frem eachother. How do you approach this database-wise?
After logging into your PHPMyadmin select or go in to the table you want to duplicate-and-rename, then click on "Operations." There you see a series of boxes with one called "Copy table to (database.table)" You can choose to duplicate structure and data, or structure only. Type the name you want and click go. And your new table with listing will be ready to edit and search!
If I understand what you're asking... I think you want to create a new database for each "directory," as you put it, with the same table definitions in it.
The word "database" in mySQL parlance means the same thing as "schema" in the parlance of some other RDBMS systems. You can't have two sets of data with the same table names in the same database (a/k/a schema). You need a different schema for this.
It's very common to have lots of databases in a single server with precisely the same tables / columns / keys etc. And yes, phpmyadmin can handle this.
The word "directory" is confusing, though. It ordinarily refers to the on-disk data storage used by the mySQL (RDBMS) server program. But I don't think that's what you mean. If you DO mean it, please make sure you are highly skilled at system administration before you try to muck around with the file systems on the mySQL server. It's easy to break stuff. (Ask me how I know how easy it is to break stuff :-) :-)

Create ERD for a mySQL DB by selecting only some of the main tables

I need to create an ERD for our DB system. As it happens, there are lots and lots of tables, and no one really understands much of it. What's more, the same DB has tables for different applications, so not all tables are relevant to my application. I need an ERD for only my application's tables.
I tried creating ERD from MySQL workbench. It allows me to select a subset of the tables to put on an ERD, but that's the problem: I don't know which tables to select ( One of the reasons to build the diagram in the first place :-) ).
What I do know are some of the 'main' tables involved. What I'm looking for is this: I tell the tool some 5-10 of these main tables I'm interested in, and the tool automatically picks up all the tables that are linked to these tables, and creates the ERD for them.
Any pointers?
Otherwise, I'll have to live with building my list of tables manaully, one by one...
http://www.fabforce.net/ has a nice tool called DBDesigner, I have used this in the past for some reverse engineering on a datamodel.