Parent Child Concept in SQL Server Relational Database - sql

I have a Teacher and a Student table in SQL Server. I want to use the inheritance concept to make a table with the name Person and put the common attributes of Student and Teacher there.
Practically how can I do it?
What I want to know is:
how a Person record will show in Student table?
If I update the name from Student how will it get updated in Person table and etc.
Please help me I don't know anything about to try and I need it badly please...

"Teacher" and "student" would be your tables that inherit from "Person".
person stores all the details that are common for both teachers and students (eg, name, address, contact info etc)
teacher stores specific info for teachers, and a foreign key pointing to the person table to identify their personal information. similarly, student stores information specific to students
your person table could look like
+-----------+---------+----------+------------+
| person_id | fname | lname | dob |
+-----------+---------+----------+------------+
| 1 | tracey | wright | 10/10/1990 |
| 2 | max | smith | 11/11/1998 |
| 3 | chris | brown | 12/06/1978 |
+-----------+---------+----------+------------+
lets assume that tracey and max are students, and max is a teacher.
Your teacher table could look like
+------------+-----------+------------+
| teacher_id | person_id | speciality |
+------------+-----------+------------+
| 1 | 3 | maths |
+------------+-----------+------------+
teacher_id is the table identifier, and person_id is the the one that chris has in the person table. This now identifies chris as a teacher
Similarly, your student table could look like
+------------+-----------+------------+--+
| student_id | person_id | detentions | |
+------------+-----------+------------+--+
| 1 | 1 | 4 | |
| 2 | 2 | 5 | |
+------------+-----------+------------+--+
This now identifies tracey and max as students (and for example, specific information regarding the students might be number of detentions)
In each case, with teacher and student, person_id is used as a foreign key. Foreign keys are primary keys within another table, thus allows other tables to reference each other.
In order to access the information for each record, you would need to use a join statement. Depending in the DBMS you are using, this varies in syntax (see http://www.w3schools.com/sql/sql_join.asp to get a proper understanding)

Here you can learn about creating relationship between tables..
https://www.simple-talk.com/sql/t-sql-programming/questions-about-primary-and-foreign-keys-you-were-too-shy-to-ask/

Related

How to check if two columns are consistent in a table. sql

I'm struggling to ask the question so I will just put an example table. Basically, if I have two columns with headings person and insured car, how can I check if the same person consistently insures the same brand of car.
------|------
|person|brand |
------|------
| 0 |Toyota|
| 0 |Mazda |
| 1 |Toyota|
| 1 |Toyota|
| 2 |Honda |
| 2 |Honda |
| 3 |Ford |
------|------
So basically in this table I want to filter out person 0 because he insures both Toyota's and Mazda's, however the other people exclusively insure one brand of a car.
Thanks.
If you just want the people and car, you can use aggregation:
select person, min(brand) as the_brand
from t
group by person
having min(brand) = max(brand);

Is it better to use a separate table to store a list of values, or include the value directly in the current table?

I have a jobs table that stores information such as title, department, and salary. I'm wanting the user to be able to create a job using a form that has fields for the aforementioned information, as well as a field for the job category. category would be something like retail, or IT, for example.
I don't have any issues with the actual coding itself, but rather what the best way to design the database store the information in it. So my question is this: should I create a separate table categories that stores each job category, along with an ID, so that the tables would look something like this
categories jobs
+----+---------------+ +----+---------------+-------------+--------+-------------+
| id | category | | id | title | department | salary | category_id |
+----+---------------+ +----+---------------+-------------+--------+-------------+
| 1 | Retail | | 1 | Retail | department1 | 10000 | 2 |
+----+---------------+ +----+---------------+-------------+--------+-------------+
| 2 | IT | | 2 | IT | department2 | 12000 | 1 |
+----+---------------+ +----+---------------+-------------+--------+-------------+
where category_id is a foreign key linking to the categories table,
or should I do something like this, where all the information is stored in a single table:
jobs
+----+---------------+-------------+--------+-------------+
| id | title | department | salary | category |
+----+---------------+-------------+--------+-------------+
| 1 | Retail | department1 | 10000 | IT |
+----+---------------+-------------+--------+-------------+
| 2 | IT | department2 | 12000 | Retail |
+----+---------------+-------------+--------+-------------+
Which is the better option? They both seem to achieve the same result, but what are the pros and cons of doing it either way, and which way would be the more preferred way of doing it?
In general, you want to store "entities" in separate tables. In this case, category is a separate entity from jobs.
Why do you want to do this?
There is only one row per category, so you don't have to worry about duplication -- and errors.
There may be additional information that you want to store, such as the creation date, abbreviation, who created it, and so on.
Properly declared foreign key constraints ensure that only valid categories are stored.
Categories may be shared across different tables, and a separate reference table ensures that the values are consistent.

How can I separate out multiple values from a column via another table?

I have a table named Team. Each record can have more than one member. Rather than jamming members comma separated value, I am hoping to use separate table to meet the requirement. Id is primary key.
----------------------------
| Id | Name | Member
----------------------------
| 1 | TeamA | Jim, Jack
----------------------------
| 2 | TeamB | Micah
I thought of creating a table named User. UserId is the PK and TeamId is FK.
--------------------------
| UserId | Name | TeamId
--------------------------
| 123 | Jim | 1
--------------------------
| 456 | Jack | 1
--------------------------
| 789 | Micah | 2
Then I want parent table Team be able to refer Jim and Jack from its child table but I am not sure how to represent it in one to many relationship... I know below expression is not correct....
------------------------
| id | Name | Member
------------------------
| 1 | TeamA | 123, 456
(Table) Team:
TeamId(PK), Name
(Table) Member:
MemberId(Pk), Name
Case 1 (one to many):
Every Member can be in one team:
Simply add TeamId(FK) to Member Table
Case 2 (many to many):
Every Member can be in as many teams as you want:
Add a linking table:
(Table) TeamMember:
TeamId(FK), MemberId(FK)
That indeed means to normalize data further meaning to split tables your data isnt atomic at each column level. Hence should be normalized to avoid difficulties in data retrievel.
Users Table
User Id(pk) TeamId(fk)
Members Table
TeamId(pk) , MemberId, NAME
Query
Select
u.userid, memberid, m.namefrom
users u join members m
On u.userid=m.memberid and
u.teamid=m.teamid
It looks like you need to create a mapping table to represent the Team, Member relationship, which would make this a many-to-many relationship.
TeamMemberMap - TeamId, MemberId

Foreign key for many to many relationship in sql

I have an SQL table called Listing which is representing houses that have been rented. The table has a primary key id and another field called amenities with the things each house had to offer. The amenities of each house are separated from each other with a comma. For example TV, Internet, Bathroom.
I used the following commands to create a table called Amenity with all the unique different amenities offered and a SERIAL number for each amenity.
CREATE TABLE Amenity AS(
SELECT DISTINCT regexp_split_to_table(amenities,',') FROM Listing
);
ALTER TABLE Amenity
RENAME regexp_split_to_table to amenity_name;
ALTER TABLE Amenity ADD COLUMN amenity_id SERIAL;
ALTER TABLE Amenity ADD PRIMARY KEY(amenity_id);
My problem is that I need to connect these two tables with a foreign key and I don't know how since the relationship between them is a many to many relationship. I have checked other questions regarding foreign keys in many to many relations but could not find anything similar. If there exists something similar please explain the way it is similar to my question.
You must create another table which will hold the one-to-many relationships between a house and its amenities.
So your 3 tables looks look like this:
Table HOUSE
+----------+------------+
| house_id | house_name |
+----------+------------+
| 1 | Uncle Bob |
+----------+------------+
| 2 | Mom Sara |
+----------+------------+
Table AMENITIES
+------------+--------------+
| amenity_id | amenity_name |
+------------+--------------+
| 1 | TV |
+------------+--------------+
| 2 | Internet |
+------------+--------------+
| 3 | Kitchen |
+------------+--------------+
Table HOUSE_AMENITIES
+----------+------------+
| house_id | amenity_id |
+----------+------------+
| 1 | 1 |
+----------+------------+
| 2 | 1 |
+----------+------------+
| 2 | 2 |
+----------+------------+
| 2 | 3 |
+----------+------------+
So the house Uncle Bob has only TV while the house Mom Sara has TV, Internet and a fully equipped kitchen.
Remember - you should never use the same column to store multiple values (separated with comma). In all such cases you have to use another table, converting the multiple comma-separated values into distinct rows inside this detail table and referencing the primary key of the master table.

Field involving multiple rows from another table

I have created/am creating 3 tables: Slaves, Citizens and Incidents.
How should I go about Incident involving multiple Citizens and Slaves?
Now I'm thinking about making two fields in Incidents containing list of CitizenID's and SlaveID's (SlaveID1, SlaveID2...,SlaveIDn), but it seems plain dumb.
Actually your idea doesn't sound dumb at all. You can design your Incidents table like this:
+------------+-----------+---------+
| IncidentID | CitizenID | SlaveID |
+------------+-----------+---------+
| 1 | A | A | <-- incident #1 involved 2 citizens and 1 slave
| 1 | B | A |
| 2 | A | A | <-- incident #2 involved 2 citizens and 2 slaves
| 2 | B | A |
| 2 | A | B |
| 2 | A | B |
+------------+-----------+---------+
Now when you query for a certain incident ID you can obtain a list of all citizens and slaves involved in the incident. This is a many-to-many relationship in your database schema.
You can make it by making 2 bridge table along with one master table
Master table
_______________
Incidents
Bridge tables
___________
incident_slave(pk of incidents table , slave information field(s) or pk of slave table)
incident_citizen(pk of incidents table , citizen information field(s) or pk of citizen table)
You are looking for a many-to-many relationship.
Basicly you could get away with a table of kind:
CREATE TABLE [dbo].[incidents](
citizen_id*
,slave_id*
)
The * mark means column is a part of primary key. This ensures there is only one relationship between citizen John and slave Patrick.