473,804 Members | 3,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

self referential database table

Hello every body,

I need some clarification of concept regarding self referential
tables.

consider we have a "Person" table that stores data about employees of
an organisation.
in case a person change his name or any details we dont want to update
our database by loosing old information and adding new one. what we
need is to hold previous details as well as the new ones.
theoratically for me its easy i will just add another row and link
that row with one of the existing row. to do so i created a relation
between the "Emp_id" column of "Person" table to itself. so one
"Emp_id" could be related to another "Emp_id" in the same table.
after doing so i dont know how can i indicate while inserting a record
that this new record in linked with one of the previous records. means
in insert statement how the relation ship would be added.
here i am not sure if i am thinking in the right direction or not as
we may need to add another table or another column to indicate the
relation between old an new row. but if we have to add that new column
say "old_Emp_id " in "Person" table then what does the relationship
between "Emp_id" with itself serves.

thanks

Ali

May 21 '07 #1
4 6726
aa****@gmail.co m wrote:
I need some clarification of concept regarding self referential
tables.

consider we have a "Person" table that stores data about employees of
an organisation.
in case a person change his name or any details we dont want to update
our database by loosing old information and adding new one. what we
need is to hold previous details as well as the new ones.
Make sure you restrict who has delete permission, and/or add a delete
trigger that rolls back delete attempts with an appropriate complaint.
theoratically for me its easy i will just add another row and link
that row with one of the existing row. to do so i created a relation
between the "Emp_id" column of "Person" table to itself. so one
"Emp_id" could be related to another "Emp_id" in the same table.
after doing so i dont know how can i indicate while inserting a record
that this new record in linked with one of the previous records. means
in insert statement how the relation ship would be added.
here i am not sure if i am thinking in the right direction or not as
we may need to add another table or another column to indicate the
relation between old an new row. but if we have to add that new column
say "old_Emp_id " in "Person" table then what does the relationship
between "Emp_id" with itself serves.
I would go with a composite key of (emp_id + date_inserted). Preferably
in a separate history table, but if I had to do it in the main table,
then I would create a view of current data:

create view vPersons as
select *
from Persons p
where date_inserted = (
select max(p2.date_ins erted)
from Persons p2
where p2.emp_id = p.emp_id
)
May 21 '07 #2
thanks Ed,
is there a way without adding a column or what i am thinking to just
reply on relationship between emp_id with itself is not sufficient.

May 21 '07 #3
(aa****@gmail.c om) writes:
consider we have a "Person" table that stores data about employees of
an organisation.
in case a person change his name or any details we dont want to update
our database by loosing old information and adding new one. what we
need is to hold previous details as well as the new ones.
theoratically for me its easy i will just add another row and link
that row with one of the existing row. to do so i created a relation
between the "Emp_id" column of "Person" table to itself. so one
"Emp_id" could be related to another "Emp_id" in the same table.
after doing so i dont know how can i indicate while inserting a record
that this new record in linked with one of the previous records. means
in insert statement how the relation ship would be added.
here i am not sure if i am thinking in the right direction or not as
we may need to add another table or another column to indicate the
relation between old an new row. but if we have to add that new column
say "old_Emp_id " in "Person" table then what does the relationship
between "Emp_id" with itself serves.
There are probably several solutions. Which is the best may depend on what
the business requirements at hand. Since I don't know what they are, I
can only voice my preference. And that is that you have your Persons
table to hold current values only. Most of the time users will work with
current value, and if you mix current values with historic, you can cause
mess and confusion.

Instead, I would recommend that you have a table PersonHistory that holds
pervious version of the rows. If Emp_id is the key in Person, the
key in PersonHistory would be (Emp_id, Change_no) where Change_no is
a running number. The PersonHistory would hold columns you are required
to track, and and also columns who report and when this version was
created. You would add a row to this table whenever a row is inserted
or updated. That is, the would be a row for the current version of the
record as well.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 21 '07 #4
aa****@gmail.co m wrote:
is there a way without adding a column or what i am thinking to just
reply on relationship between emp_id with itself is not sufficient.
You need some column that determines which row contains the current
data for a given emp_id - e.g. 'date_inserted' , or 'is_active', or
an IDENTITY column. Rows are not inherently sorted in order of
insertion (Celko: "rows are not records") - you have to impose a
sort order based on one or more columns, which can match order of
insertion if you configure things to that end.

I agree (as I think I mentioned previously) with Erland's suggestion
of keeping current data in one table and history in another; failing
that, creating a current_data view and using it consistently.
May 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3099
by: Darius | last post by:
I was having an issue inserting data into a table in an I-Series DB2 database. The Insert statement itself is very simple: Insert into Table1 select * from Table2 These two tables have identical structures. What happens is that the process gets locked and one cannot kill it regardless of what one does. The only solution seems to be to IPL the
1
3690
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into "access_log", the referential integrity triggers generate these queries: SELECT 1 FROM ONLY "public"."application_type" x
7
2473
by: Jimmie H. Apsey | last post by:
Referential Integrity on one of our production tables seems to have been lost. I am running Postgres 7.1.3 embedded within Red Hat kernel-2.4.9-e.49. Within that I have a table with referential integrity constraints which no longer work. I do not know how to disable referential integrity on a column in a table. I do not know how to view what Postgres thinks my referential integrity
5
4029
by: Geisler, Jim | last post by:
So, as far as I know, PostgreSQL does not have any way of verifying the loss of referential integrity. Are there any recommended methods or utilities for checking referential integrity in a PostgreSQL database?
1
1958
by: HGT | last post by:
Hello all, I am currently on a project which the source data come into the databases is always dirty (not surprisingly); however, due to the design of the database, it is very difficult to impose referential integrity (esp. Foreign Keys) on these tables. Am I just not thinking straight? This solution is implemented on multi RDBMS. Example, we have 2 tables, one with a number of columns and a TIMEID column to specify the load date (in...
80
7904
by: Andrew R | last post by:
Hi I'm creating a series of forms, each with with around 15-20 text boxes. The text boxes will show data from tables, but are unbound to make them more flexible. I want the form to be used for both adding new data and modifying existing data. I have created a save button on the form. When the user clicks the save button, the code checks to see if there
3
2662
by: Wayne | last post by:
I've inadvertently placed this post in another similar newgroup, and I apologise if you get it twice. I'm building a database that consists of frontend and backend. Some of the lookup tables need to reside in the frontend database. The data in the lookup fields that relate to the backend tables will never change, but a descriptor for items in these fields will. I may need to update the descriptors periodically, hence the need to keep...
1
2229
by: Paul H | last post by:
I have an Employees table with the following fields: EmployeeID SupervisorID Fred Bob Bob John Bob Mary Bill Bill I have created a self join in the relationships window, with
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.