I have a table familiars , a table mages and a table mages_familiars where I keep which familiar belongs to which Mage.
How can I model this cross reference table?
Thanks in advance
You don't need the intermediary table unless it's possible that a familiar can belong to multiple mages and mages can have multiple familiars (a HABTM relationship). In that case the table should have fields id (int), created (datetime), modified (datetime), mage_id (int) and familiar_id (int). Also, the table should be named familiars_mages.
If a familiar can belong to only one mage, a familiar belongsTo a mage and a mage hasMany familiars. The familiars table should have a mage_id field.
If a familiar can belong to only one mage, and a mage can have only one familiar, you can use a hasOne relationship. In that case either the mages table has a familiar_id field or the familiars table has a mage_id field.
Related
I have two tables many to many, so i used mapping table for it
1st table: course that contains course_id, course_name, and credit_hrs attributes
2nd table: y_term that contains yt_id, year, and term attributes
mapping table is y_t_course that contains yt_id and course_id attributes
then i create a table, its name is topic, it is contains cpt_id, topic_text.
NOW i used y_t_course as a reference to topic table, it means has foreign key from y_t_course.
my question is how can i represent this relation in ER-Diagram
I would use a tool like Visual Paradigm to get the ER diagrams required. Eclipse also has a plugin that does this.
How i do a crud with a relationship between a user and services order? With the code, i can't add users in a team for service order. When cakebake, the controller TeamHasUsersController is created, but the form add don't have any field.
The crud has generated with cake bake.
In service_orders: change requester_users_id into user_id, the icon in front of the field will change to a red key (foreign key). Cake conventions need a foreignkey to be named [model]_id.
The name of table with associations should be "team_users".
Three database tables are required for a BelongsToMany association. In the example above we would need tables for articles, tags and articles_tags. The articles_tags table contains the data that links tags and articles together. The joining table is named after the two tables involved, separated with an underscore by convention. In its simplest form, this table consists of article_id and tag_id.
habtm relationship
Can someone tell me the easy way to explain the differentiation between an entity and a table in database?
Entity is a logical concept of relational database model. And table is used to express it, but there is a slight difference. Table expresses not only entities, but also relations.
For example, assume that you want to make a database of projects and employees of a company. Entity is a unit of information that has meanings by itself. In this case, there will be two entities - "Project" and "Employee". Each entity has its own attributes.
In relational DB model, there is another idea, 'relation'. If employees participate in several projects, then we can say that there is a relation with a name 'works_on'.
Sometimes, relation can have its own attribute. In this case, 'works_on' relation can have attribute 'start_date' and so on. And if this relation is M:N relation(Like this case: In project 1, there are 5 employees. Employee A works on two projects.), then you have to make an extra table to express this relation. (If you don't make an extra table when the relation is M:N, then you have to insert too many duplicated rows into both 'Project' and 'Employee' table.)
CREATE TABLE works_on(
employee,
project_id,
start_date
)
An entity resides in a table, it is a single set of information, i.e: if you have a database of employees, then an employee is an entity. A table is a group of fields with certain parameters.
Basically everything is stored in a table, entities goes into tables.
In a relational database the concept is the same. An entity is a table.
In OOP (Oriented Object Programming) there is a nice article in Oracle docs:
In general terms, entity objects encapsulate the business policy and
data for
the logical structure of the business, such as product lines,
departments, sales, and regions
business documents, such as invoices, change orders, and service
requests
physical items, such as warehouses, employees, and equipment
Another way of looking at it is that an entity object stores the
business logic and column information for a database table (or view,
synonym, or snapshot). An entity object caches data from a database
and provides an object-oriented representation of it.
Depending on how you want to work, you can create entity objects from
existing database tables (reverse generation) or define entity objects
and use them to create database tables (forward generation).
There is little difference between an entity and a table, but first we should define the concepts of “tuple” and “attribute”. I want to express those concepts with a table.
Here is an example of a table. As you can see it has some columns. Each column represents an attribute and each line represents a tuple(row). This is the employee table. In this example, the table is being used for representing an entity which is employee. However, if we create a new table named superior to specify the superiority relationship between those employees, it would be a table that represents a relation. In summary, we can use tables for representing both the entities and relations so entities and tables are not the same.
What is the right plural/singular table naming convention in CakePHP.
This:
posts
posts_pictures
posts_picture_captions <-- Not sure about this
Or this:
posts
posts_pictures
posts_pictures_captions <-- Not sure about this
I know CakePHP want the tables in alphabetical order but this should in this question ignored. Please only consider plural/singular table naming.
Cake Inflector Should do exactly what you are looking to do.
From the cakephp book:
Model class names are singular and CamelCased. Person, BigPerson, and
ReallyBigPerson are all examples of conventional model names.
Table names corresponding to CakePHP models are plural and
underscored. The underlying tables for the above mentioned models
would be people, big_people, and really_big_people, respectively.
You can use the utility library Inflector to check the singular/plural
of words. See the Inflector for more information.
Field names with two or more words are underscored: first_name.
Foreign keys in hasMany, belongsTo or hasOne relationships are
recognized by default as the (singular) name of the related table
followed by _id. So if a Baker hasMany Cake, the cakes table will
refer to the bakers table via a baker_id foreign key. For a table like
category_types whose name contains multiple words, the foreign key
would be category_type_id.
See http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions
So at first, your "posts_pictures" table should be named "post_pictures".
As a result, neither of the two is correct. The correct name according to the cake naming conventions is "post_picture_captions".
I have a problem with a many-to-many relation in my tables, which is between an employee and instructor who work in a training centre. I cannot find the link between them, and I don't know how to get it. The employee fields are:
employee no.
employee name
company name
department job title
business area
mobile number
ext
ranking
The Instructors fields are
instructor name
institute
mobile number
email address
fees
in a many-to-many relationship the relationships will be in a 3rd table, something like
table EmployeeInstructor
EmployeeID
InstructorID
to find all the employees for a specific instructor, you'd use a join against all three tables.
Or more likely there will be classes involved --
Employee takes Class
Instructor teaches Class
so you'll have and EmployeeClass table,
an InstructorClass table,
and join through them. And Class needs to be unique, or else you'll need
Class is taught in Quarter on ClassSchedule
and end up joining EmplyeeClassSchedule to InstructorClassSchedule.
This ends up being one of your more interesting relational designs pretty quickly. If you google for "Terry Halpin" and "Object Role Modeling", this is used as an illustrative situation in the tutorial.
First of all, you will need a unique key in both tables. The employee number may work for the employee table, but you will need another for the instructor table. Personally, I tend to use auto incrementing identity fields called ID in my tables. This is the primary key.
Second, create a new table, InstructorEmployee. This table has two columns, InstructorID and EmployeeID. Both fields should be indexed. Now you can create an association between any Employee and any Instructor by creating a record which contains the two IDs.