If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > Help About Subtype/Supertypes!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-26-11, 09:32
itiwcsingkaww itiwcsingkaww is offline
Registered User
 
Join Date: Oct 2011
Posts: 1
Help About Subtype/Supertypes!

i'm having trouble inserting record to a supertype/subtype relationship.
lets say i have my supertype EMPLOYEE and subtypes HOURLY and CONSULTANT. now i how can i insert a record to this kind of situation..

the truth here is that im using MS Access database . i mapped the supertype and subtypes and it results to one-to-one relationship and i enforced referential constraint.. when i tried to insert in the hourly employee table and added details to the Employee Table .. it says that "cannot insert record because related record is required in CONSULTANT" ..

What i really want to happen is that the primary key of my SUBTYPES corresponds to the Supertype EMPLOYEE ..

and i can't do this please anyone help me..

im a student and doesn't know much about DBMS ... please help me.
Reply With Quote
  #2 (permalink)  
Old 10-26-11, 10:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by itiwcsingkaww View Post
the truth here is that im using MS Access database .
just curious ... why didn't you post in the MS Access forum?

Quote:
Originally Posted by itiwcsingkaww View Post
and i can't do this please anyone help me..
you will have to show us your tables
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-26-11, 13:14
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
In SQL Server, I would write something in the line of this:
Code:
CREATE TABLE EMPLOYEE(
	id	BIGINT	identity (1, 1)	NOT NULL,
	Type	CHAR(1)	NOT NULL
		CONSTRAINT cc_EMPLOYEE_Type CHECK(Type in ('H', 'C')),
	...
)

CREATE TABLE HOURLY(
	EmployeeId	BIGINT	NOT NULL,
	...
)

CREATE TABLE CONSULTANT(
	EmployeeId	BIGINT	NOT NULL,
	...
)

ALTER TABLE HOURLY
	ADD CONSTRAINT FK_HOURLY_is_an_EMPLOYEE FOREIGN KEY (EmployeeId)
		REFERENCES EMPLOYEE (Id)

ALTER TABLE CONSULTANT
	ADD CONSTRAINT FK_CONSULTANT_is_an_EMPLOYEE FOREIGN KEY (EmployeeId)
		REFERENCES EMPLOYEE (Id)

DECLARE @Id	BIGINT

INSERT INTO EMPLOYEE (Type, ....) 
VALUES('H', ...)

SELECT @Id = SCOPE_IDENTITY()

INSERT INTO HOURLY (EmployeeId, ...) 
VALUES (@Id, ...)
If one would define two FK's between Employee and Hourly, things won't work. They call it the "crossing FK" thingy. If you notice that thingy, you know your data model is flawed. You could check that.

But I think you swapped the two tables in the FK definitions.

Wrong:
ALTER TABLE EMPLOYEE
ADD CONSTRAINT FK_CONSULTANT_is_an_EMPLOYEE FOREIGN KEY (Id)
REFERENCES CONSULTANT(EmployeeId)
instead of

Right:
ALTER TABLE CONSULTANT
ADD CONSTRAINT FK_CONSULTANT_is_an_EMPLOYEE FOREIGN KEY (EmployeeId)
REFERENCES EMPLOYEE (Id)

You probably created the relation using the Access GUI. I don't know if that will make it easier to spot the problem or harder.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #4 (permalink)  
Old 10-26-11, 13:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by Wim View Post
I don't know if that will make it easier to spot the problem or harder.
chuckle

nice explanation, by the way

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-27-11, 07:14
paultech paultech is offline
Registered User
 
Join Date: Sep 2011
Posts: 71
This article Implementing Table Inheritance in SQL Server - SQLTeam.comseems to explain one of the simplest ways to use a constraint to check whether a parents type is the correct type for the child table it also has data in. But I am stuck thinking of a way to implement the constraint of having to have a child existing. In the example in the article, a person could exist in the people table and be of type teacher, but not have an entry in the teachers table.
Quote:
I thought that I could use triggers for this constraint, but I really don't know how to write something like that. Perhaps if there was a method of a trigger running after an entire batch rather than a single interaction on a table, I could write a solution. But I don't think such a method exists. Here is some code to show my requirements a bit more clearly
Reply With Quote
Reply

Tags
dbms, ms access, sql, subtype, supertype

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On