SQL parent table Childtable in one to many relationship - sql

I have the following two schemas for two tables
create table My_contacts
(
Contact_ID, last name, first name, email, phone, profession_ID
)
Create table Profession
(
Profession_ID, Profession Name
)
The relationship between tables profession and My_contact and is one-to-many.
I need a fast way to decide which one is parent table and which one is child table, so my assumption is that because in the table My-Contacts has the foreign key profession_ID, it is the child table. (Based on primary key belongs to parent table, foreign key is in child table.)
However, I read the book Headfirst SQL, page P311. it says
we moved the profession column into a new child table, and change the profession column in the parent table into foreign key profession_ID"
according to the above, the book means the profession table is the child table, and the my_contact table is the parent table.
I have tried to figure out this conflict, but could not.

Related

Handling many-to-many relationship in a sql query

So as you know in data modeling, the many-to-many relationships are handled by creating a bridge table. This will enable us to have foreign key constraints.
My question relating to query data from tables that have many-to-many relationships.
I will give an example. Let's say we have the following tables
Table 1
Column 1
Column 2
Column 3
Table 2
Column 1
Column 4
Column 5
Table 3 (bridge table)
Column 1
Ok, so when I tried to query data from table 1 and left joining with table 2 I got the same results as query data from table left joining with table 3 and left joining with table 2. And that makes me wonder if the bridge table is a necessary step to include in a SQL query!
Thank you in advance :)
Your bridge table has just one column, which suggests that you are confusing one-to-many relationships (or many one-to-one), and many-to-many relationships.
In a one-to-many relationship, the child table refers directly to the parent table, and there is no need for a bridge table:
create table orders (
order_id int primary key,
order_date date,
...
);
create table order_items (
order_item_id int primary key,
order_id int references orders(order_id),
...
)
In a many-to-many relationship, the bridge table has (at least) two columns, one for each of the referential table that come into play in the relation:
create table authors (
author_id int primary key,
name varchar(50),
...
);
create table books (
book_id int primary key,
title varchar(50),
...
);
create table book_authors (
author_id int references authors(author_id),
book_id int references books(book_id),
primary key (author_id, book_id)
)
Let's make your example less abstract. There are posts with dislikes and likes. Table 1 = likes, Table 2 = dislikes.
Table likes (post_id, who, why)
Table dislikes (post_id, who, why)
The bridge table is then
Table posts (post_id, content)
This is an example where two child tables are related by a parent table. You don't create the bridge table in order to get an m:n relation between the two child tables, but because the child tables make no sense without their parent table. The child tables are m:n related, but we don't usually call this an m:n relation, because the tables are only losely related (a like belongs to a post and a dislike belongs to a post, but the like doesn't really belong to the dislike).
When talking about m:n relations, we are usally not talking about parent/child relationship.
An example: An order can contain many products and a product can be in many orders.
Table orders (order_id, date)
Table products (product_id, name, price)
The bridge table is the position in the order:
Table order_detail (order_id, product_id, amount)
There is no parent/child relation between orders and products, but the two are related, because products get ordered. This is what we typically call an m:n relation. The bridge table establishes the relation between order and product.

sql primary key "mapping table ?"

i have a database for university so the students take many courses but each student have id as primary key also each course have an id as primary key its not allowed to have duplicate id at same table what should i use to make a table that same id but different courses id
In a normalized relational database, when you have a situation where items from one table can be associated with many items in a second table, and likewise those related items can be associated with many items in the first table, you should use a third table to store those relationships. In your case, that table might be called Enrollments.
You can use JOIN operations on that third table to relate, group, and aggregate across that relationship. You can also store any metadata associated with that relationship in the JOIN- table, for example you might store the enrollment date there.
Like vicatcu said, create a third table
Course_id | Student_id
The ID's of the courses and the students are uniq in their own tables. The relation between course < - > student is stored in that third table.

Table with multiple composite attributes

I have a flight table that only has composite attributes.
I have the PK, which is flight_no obviously, then I have From(city,country) and some other attributes that I similar to "From".
How can I get my table to 1NF? I know that in order to be in 1NF, there cannot be repeating groups.
Another concern is, if I create other tables from those composite attributes, I'd be left with flight_no only. Would that be OK?
I'd extract a locations tables that has a primary key (location_no, for argument's sake) and the city and country attributes.
Then, your flights table could be composed of its own key, flight_no and to and from foreign keys to the locations table.

Two foreign keys reference the primary key of another table

So I have two tables
Person(personID, first_name, last_name);
Relation(relationID, child_personID, parent_personID);
personID and relationID are both primary keys. child_personID and parent_personID are both foreign keys.
I want to make a query so I have the first names and last names of both the child and parent.
child.first_name child.last_name and parent.first_name, parent.last_name
One way to go about this is using joins and table aliases. Something like this:
select
child.first_name,
child.last_name,
parent.first_name,
parent.last_name
from relation r
join person child on r.child_personID = child.id
join person parent on r.parent_personID = parent.id

Designing 1:1 and 1:m relationships in SQL Server

In SQL Server 2008, how does one design a 1:1 and 1:m relationship?
Any relationship requires that the "parent" table (the one side) have a Primary (or unique) Key (PK), that uniquely identifies each row, and the "child" table (the other side) have a Foreign Key column or columns, that must be populated with values that are the same as some existing value[s] of the Primary Key in the parent table. If you want a one to many (1-M) relationship then the Foreign Key should be an ordinary attribute (column or columns) in the child table that can repeat (there can be many rows with the same value)
If you want a one to one (1-1) relationship then the Foreign key should itself be a Primary Key or unique index in the child table that guarantees that there may be at most one row in the child table with that value.
A 1-1 relationship effectively partitions the attributes (columns) in a table into two tables. This is called vertical segmentation. This is often done for sub-classing the table entities, or, for another reason, if the usage patterns on the columns in the table indicate that a few of the columns need to be accessed significantly more often than the rest of the columns. (Say one or two columns will be accessed 1000s of times per second and the other 40 columns will be accessed only once a month). Partitioning the table in this way in effect will optimize the storage pattern for those two different queries.
Sub-Classing. The above actually creates a 1 to zero or one relationship, which is used for what is called a sub-class or subtype relationship. This occurs when you have two different entities that share a great number of attributes, but one of the entities has additional attributes that the other does not need. A good example might be Employees, and SalariedEmployees. The Employee table would have all the attributes that all employees share, and the SalariedEmployee table would exist in a (1-0/1) relationship with Employees, with the additional attributes (Salary, AnnualVacation, etc.) that only Salaried employees need.
If you really want a 1-1 relationship, then you have to add another mechanism to guarantee that the child table will always have one record for each record/row in the parent table. Generally the only way to do this is by enforcing this in the code used to insert data (either in a trigger, stored procedure or code outside the database). This is because if you added referential integrity constraints on two tables that require that rows always be in both, it would not be possible to add a row to either one without violating one of the constraints, and you can't add a row to both tables at the same time.
One-to-One Relationship
Create Table ParentTable
(
PrimaryKeyCol ... not null Primary Key
, ...
)
Create Table ChildTable
(
, ForeignKeyCol ... [not] null [Primary Key, Unique]
, ...
, Constraint FK_ChildTable_ParentTable
Foreign Key ( ForeignKeyCol )
References ParentTable( PrimaryKeyCol )
)
In this case, I can never have more than one row in the ChildTable for a given ParentTable primary key value. Note that even in a One-to-One relationship, one of the tables is the "parent" table. What differentiates a One-to-One relationship from a One-to-Many relationship purely in terms of implementation is whether the ChildTable's foreign key value has a Unique or Primary Key constraint.
One-to-Many Relationship
Create Table ParentTable
(
PrimaryKeyCol ... not null Primary Key
, ...
)
Create Table ChildTable
(
, ForeignKeyCol ... [not] null
, ...
, Constraint FK_ChildTable_ParentTable
Foreign Key ( PrimaryKeyCol )
References ParentTable( PrimaryKeyCol )
)
In this scenario, I can have multiple rows in the ChildTable for a given ParentTable primary key value.
A 1:1 relationship exists where table A and table B only exist once in regards to each other.
Example: A student has 1 master student record. The student would be table A and the record in table B. Table B would contain a foreign key to the student record in table A (and possibly vice-versa)
A 1:m relationship exists where table A can be referenced or linked to by many entries in table B.
Example: A student can take several books out from the library. The student again would be table A and the book could be the entry in table B. The entry in table B would contain a foreign key to who checked the book out, and many books could reference the same student.