Multiple Parents For One Foreign Key
Jan 30, 2004
Hi,
I am trying to figure out if this is possible in Oracle or Mysql
Lets say I have 3 tables such that C could have either A or B as its parent.
A
{ id, name}
B
{id, name}
C
{other_id, comment}
Now other_id could be either A.id or B.id. What I want to be able to do is to define a foreign key constraint of the type:
CONSTRAINT FK_C FOREIGN KEY (other_id) REFERENCES A(id) ON DELETE CASCADE,
CONSTRAINT FK_C FOREIGN KEY (other_id) REFERENCES B(id) ON DELETE CASCADE
such that deleting A.id automatically deletes C.other_id where A.id = C.otherid and same for B.id
Ofcourse i am not able to do this. Is there any way that this can be done in Oracle and Mysql?
Thanks a lot,
Priyanka
View 2 Replies
ADVERTISEMENT
Jul 20, 2005
I have a user assigned multiple roles and a role can be inherited frommultiple parents (see below). How do I answer such questions as "Howmany roles does the user belongs to?"I answered the above questions by using .NET but I think it can bemore efficient by using just SQL. I would appreciate if you can giveme an answer.Thank you.CREATE TABLE [dbo].[tb_User] ([Id] [int] IDENTITY (1, 1) NOT NULL PRIMARY KEY,[Name] nvarchar(99) NOT NULL UNIQUE,[Password] nvarchar(99) NOT NULL,) ON [PRIMARY]GOCREATE TABLE [dbo].[tb_Role] ([Id] [int] IDENTITY (1, 1) NOT NULL PRIMARY KEY,[Name] nvarchar(99) NOT NULL UNIQUE,) ON [PRIMARY]GOCREATE TABLE [dbo].[tb_User_Role] ([UserId] [int] NOT NULL ,[RoleId] [int] NOT NULL,) ON [PRIMARY]GOALTER TABLE [dbo].[tb_User_Role] WITH NOCHECK ADDCONSTRAINT [PK_tb_User_Role] PRIMARY KEY CLUSTERED([UserId],[RoleId]) ON [PRIMARY]GOCREATE TABLE [dbo].[tb_Parent_Role] ([RoleId] [int] NOT NULL ,[ParentRoleId] [int] NOT NULL ,) ON [PRIMARY]GOALTER TABLE [dbo].[tb_Parent_Role] WITH NOCHECK ADDCONSTRAINT [PK_tb_Parent_Role] PRIMARY KEY CLUSTERED([RoleId],[ParentRoleId]) ON [PRIMARY]GO
View 1 Replies
View Related
May 9, 2008
Hi all!
I am trying to organize a hierarchical data structure into a table. I need to have the possibility to set 2 parents for some nodes. Curently I see following two options:
Example 1
id parent_id name-----------------------------------1 0 Level 1 Parent A2 0 Level 1 Parent B3 1,2 Level 2 Child
Example 2
id parent_id name-----------------------------------1 0 Level 1 Parent A2 0 Level 1 Parent B3 1 Level 2 Child3 2 Level 2 Child
Is any of the two examples valid database logic wise? In fact, is it possible to achieve the requirement by using only one table?
Thanks in advance,
View 4 Replies
View Related
Aug 18, 2015
I have made an SSRS report using the recursive parent functionality to show a hierarchical tree of values. The problem I have is that some children have more than one parent, but because (in order to use the recursive parent nicely) I need to group the results by Id, I only see distinct entries. This means that I only see each child once, even if it "should" appear in multiple locations in the report (under each of its parents).
View 6 Replies
View Related
Jun 25, 2004
What are the possible issues I could run in to having multiple foreign keys in a table. Here is why I ask. I have a db (sql server) that has a participant table, a forum table, and a forum reply table. Every record in the forum reply table is associated with the forum table via a PK-FK relationship w/cascading updates/deletes. The participants who post in these tables are not tied back to the participant table via a PK-FK relationship w/cascading updates/deletes. Should they be?
The problem I ran in to is that one particpant was deleted from the participant table but a post with their partid still existed in the forum or forum reply tables.
My feeling is that anytime a participant is deleted, everything that pertains to them should go too, right? If I am right, what do I have to be careful of if I do that?
Let me know! Thanks!!
View 1 Replies
View Related
Nov 20, 2004
I am constructing a db in sql server 2000 that will score cross-country running meets. I have an individual results table that needs to only contain participants that are entered as participants but are specific to a certain race as well. Can I have this table be linked back to TWO other tables via the PK-FK relationship and what issues might I have doing that?
Thanks!
View 5 Replies
View Related
Dec 30, 2004
I am having trouble creating multiple foreign keys on a table so that I can set up cascading update and cascading delete from two different primary tables. I am using the diagram to do this but when I try to save the diagram I get the following error.
ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]ALTER TABLE statement conflicted with COLUMN FOREIGN KEY constraint 'FK_IndResults_RaceData'. The conflict occurred in database 'VIRA', table 'RaceData', column 'RaceID'.
What would cause this to happen? Is it possible that I have records in the foreign table that do not transfer back to the primary table?
Thanks!
View 2 Replies
View Related
May 9, 2008
Starting with an example will make explaining this much easier.
I have two (or more) tables defined as follows.
Code SnippetTable1 Table2
Id [uniqueidentifier] Id [uniqueidentifier]
[...] [...]
Now, I would like to create a table Table3 that has its own primary key and references Table1 OR Table2.
Code Snippet
Table2
Id [uniqueidentifier]
ForeignId [uniqueidentifier, references Table1 or Table2]
This would enable me to insert any value into ForeignId that is present as the Id field in Table1 or Table2. Is this possible?
Best regards,
Till
View 4 Replies
View Related
Feb 21, 2015
CREATE TABLE IssueLog (
ItemNo int NOT NULL IDENTITY PRIMARY KEY
REFERENCES IssueProgress (ItemNo)
ON UPDATE CASCADE
ON DELETE CASCADE,
REFERENCES Approvals(ItemNo)
ON UPDATE CASCADE
ON DELETE SET NULL,
Msg 142, Level 15, State 2, Line 0
Incorrect syntax for definition of the 'TABLE' constraint.
I'd like to update or delete the IssueProgress plus update and setting null to the Approvals tables at the same time whenever the ItemNo of the parent table namely IssueLog is updated or deleted.
How can I achieve this?
View 1 Replies
View Related
Jun 6, 2008
I'm a total newb to SQL so I apologize if I butcher the description of what I'm looking to do :) Hopefully the example makes more sense. Is it possible for a single record in 1 table to contain relationships to several records in a different table?
What I've got so far is 2 tables - Hardware and Software, with ID Specifying primary keys HardwareID and SoftwareID respectively. the hardware records are for computer details and the software obviously are for programs. What I need to be able to do is pull up a hardware record and be able to see what software is installed on it, and vice versa pull up a software record to see which computers it's installed on. The problem I'm running into is that a computer can have multiple programs installed on it and a program can be installed on multiple computers, so I don't know how to create that relationship to account for that.
What I'd like to see is in the Hardware table a column for SoftwareID that has a foreign key relationship to the SoftwareID field in the Software Table, and vice versa....My question though is is that possible to do and have potentially multiple separate records it links to from that same column field? I might have a computer with say Windows XP Pro, Office 2003 Standard, Adobe Acrobat 8 and a proprietary rate calculator program that i need each to be displayed with their details when I open that computer's record. Or on the software side if I need to see which computers a license is already installed on then I want to make sure I can pull up the full list of computers.
And finally if what I'd like to do isn't possible as I described it...any recommendations for a better way?
Thanks all for the help!
View 3 Replies
View Related
Feb 28, 2008
Hi,
I've been working as web dev for quite sometime now but there are still few things that i would like to clarify. Hope you guys can shed lights.
I currently have several tables and all this tables are having some 1 to many relationships. I know that how i insert value into my tables are very inefficient. I do it in this manner..
-> Insert a value in TableA
-> Query the latest ID that i have inserted in Table A then
-> Insert this value in TableB. And so on and so forth.
I believe there is an efficient way to to do this but i'm not sure how? Could anyone shed me a light on this matter?
Your reply would be highly appreciated. TIA.
View 6 Replies
View Related
Jul 23, 2005
Hi All,I'm trying to solve this for a few days now and I just can't figure itout...I have three tables set up, I'll simplify them for this question:Table 1: HOTELSColumns: HOTEL_ID, HOTEL_NAMEExmple row: 123 || 'Hotel X'Table 2: SERVICESColumns: SERVICE_ID, SERVICE_NAMEExample rows:1 || 'Breakfast in bed'2 || 'King size bed'Table 3: LINK_HOTELS_SERVICESColumns: FK_HOTEL_ID, FK_SERVICE_ID, SERVICE_VALUEExample rows:123 || 1 || 1123 || 2 || 1In table 3 I link different services to different hotels. In the same tableI set the "value" for the service.The first example row of table 3 means something like: Hotel X offersBreakfast in bed. In this case 1 stands for TRUEThe second example row of table 3 means: Hotel X offers King size beds(again: 1 stands for TRUE).What I'm struggling with is selecting the hotel ID's which offer multipleservices. To stay in the example: how can I select all hotels whereSERVICE_ID = 1 AND SERVICE_ID = 1. I can't seem to figure out how to doit...I hope anyone can help... Thanks a lot in advance!!!Robert
View 2 Replies
View Related
Oct 26, 2007
Hi guys,
I have created 3 tables namely Static, Dynamic and Demo...
Static has colums FormID(uniqueidentifier, PrimaryKey) and FormName(Nvarchar(50)).
Dynamic has colums formID(uniqueidentifier,PrimaryKey) and FormName(Nvarchar(50)).
Demo has 4 colums namely ValueID(uniqueidentifier, Primary Key), formID(uniqueidentifier, foreign key), Name(nvarchar(50)), Value(nvarchar(50).
Now the formID coloum in Demo table i have set as foreign key to both the dynamic table as well as static table on the formID colum in both table.
Now first i insert a row in the dynamic table then take the uniqueidentifier which is generated automatically and try
to insert in the Demo table in formID colum as that colum is FK to dynamic and static but when i try to insert a row it show that its violating the FOREIGN KEY CONSTRAINT
the exact error is
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Demo_Dynamic1". The conflict occurred in database "huzefaJTest", table "dbo.Dynamic", column 'formid'.
The statement has been terminated.
please if anyone know do reply.....
View 3 Replies
View Related
Feb 12, 2014
Is there any possibility to create a foreign key references more than one tables.
say for example,
Create table sample1(id int primary key)
Create table sample2(id int primary key)
Create table sample3(
id int primary key,
CONSTRAINT fk1 FOREIGN KEY REFERENCES sample1 (ID),CONSTRAINT fk1 FOREIGN KEY REFERENCES sample2 (ID))
this shows no error while creating, but in the data insertion it shows error..
View 8 Replies
View Related
Apr 9, 2007
I want to create a table withmember id(primary key for Students,faculty and staff [Tables])and now i want to create issues[Tables] with foreign key as member idbut in references i could not able to pass on reference as orcondition for students, faculty and staff.Thank You,Chirag
View 3 Replies
View Related
Jul 23, 2005
Hi,I have a table with filled out below data:+------+-----+|parent|child|+------+-----+|A |B ||B |C ||B |E ||C |D ||E |F ||E |G |+------+-----+So I have to make a query which get all 'parent' values values forgiven child value.For example :-----------------If I have to get all parent values for 'D' child., query must get thisvalues : C, B, A.If I have to get all parent values for 'F' child., query must get thisvalues : E, B, A.If I have to get all parent values for 'C' child., query must get thisvalues : B, A.If I have to get all parent values for 'B' child., query must get thisvalues : A only.-----------------Is it possible to create a query which will covers all above conditionsor not using only sql statement without UDF or stored procedures.Any solutiuons?Sincerely,Rustam Bogubaev
View 3 Replies
View Related
Oct 8, 2015
We have stored some special characters in our database and when we extract as XML Auto, Elements it through error illegal characters during import. I am looking and saw CDATA with Explicit option but don't know how to use it with multi tags like:
<1>
<12>
<23>
<34>
</34>
</23>
</12>
</1>
View 12 Replies
View Related
Sep 29, 2006
Hi!
How can I make a relation that would let me relate an employee to two departments?
There is a table with emloyees and each one works in a department and there is a foreign key relation in the child (employees) table to the parent (departments) table. So can an employee work in two departments? (it's not impossible to imagine, is it?) And how would I retrieve all the employees of a department?
Or in other words, if I were working on some kind of software for my local supermarket and they need to keep track of all bills they ever gave out. I would make this products table and this bills table. Then I would create a foreign key column in the products table and fill it with bill_ids from the bills table and make retrieving bill items a simple getchildrow[](bill_row). Note that this is only an analogy, I need it done this way. How? (using C# express 2005, and SQL express and DataSets)
View 12 Replies
View Related
Jul 20, 2005
Hi,I have a tree structure which is maintained through the use of a pathenumerated column:CREATE TABLE items (item_id NUMERIC NOT NULL,path VARCHAR2(64) NOT NULL);The path is is a colon separated list of ids of the nodes of the tree.So, for example, in this structure:0 -> 1 -> 2 -> 3 -> 4item id 4 would have a path of '0:1:2:3' (0 is the root of allitems, and does not actually exist). Notice that the path does notinclude the item's own id.I would like to select all of the items in a given item's path:SELECT id, path FROM items WHERE id IN (PATH_TO_IDS(path));or maybe:SELECT id, path FROM items WHERE PATH_EQUALS(id, path));or maybe something else altogether. This should return:ITEM_ID PATH------- -------1 02 0:13 0:1:24 0:1:2:3
View 1 Replies
View Related
May 10, 2007
I have created a "parent" table with various columns and added an extra detail row to drop a nested subreport into the table passing parameters into the sub in order to obtain the child subreport. The problem is that the the while I can line up the columns in the sub report with the parent report,
I observe something like this:
parent columns:
a b c d e f
child columns
a b c d e f
The problem is in the last column of the parent "f" it appears stretched out for some reason-about an inch even though can grow is set to false.
Its like the child is forcing the parent's last column to expand yet when i observe the border around the table in the child the "f" is clearly more narrow than the parent's f despite my matching the column widths in the layout exactly the same . In preview mode, the parent's last column is extended width-about an inch more was added. Note I tested it with No data in the child's last column and it still expands the parent column by that inch.
Also note when I dropped the subreport into the parent report I'm merging the cells on the detail row that the subreport fits across columns a, b, c, d, e, f.
So even though the subreports data lines up properly under a, b, c, d, and e; the parent's "f" still expands outward.
Any clues or suggests on what I should consider changing. I'm stumped.
View 1 Replies
View Related
Apr 11, 2007
I need to extract data to send to an external agency in their supplied format. The data is normalised in our system in a one to many relationship. The external agency needs it denormalised.
In our system, the parent p has p_id, p_attribute_1, p_attribute_2, p_attribute_3 and the child has c_id, c_attribute_a, c_attribute_b, c_parent_id_fk
The external agency can only use a delimited file looking like
p_id, p_attribute_1, p_attribute_2, p_attribute_3, c1_attribute_a, c1_attribute_b, c2_attribute_a, c2_attribute_b, ...., cn_attribute_a, cn_attribute_b
where n is the number of children a parent may have. Each parent can have 0 or more children - typically between 1 and 20.
How can I achieve this using SSIS? In the past I have used custom built VB apps with the ADO SHAPE command but this is not ideal as I have to rebuild each time to alter the selection criteria and and VB is not a good SQL tool.
View 4 Replies
View Related
Feb 12, 2008
Using SQL Server Express 2005, I have two databases. AppDB - The main application database.GeoDB - A somewhat static ZIP code / states / other geographic stuff databaseI need to have some foreign key columns in tables in AppDB reference columns in the GeoDB database tables. Eventually other application database besides AppDB will be doing the same thing in our infrastructure. After googling and reading for days, here is what I
think I know:You cannot create foreign keys that reference tables in another database in SQL Server.You
cannot create foreign keys that reference columns in a view, and you definitely cannot make an index on a view that has base tables in another database.You can create a trigger that references tables in another database, but this can be flaky? (nested/recursive trigger problem).SQLServer
2005 supports multiple schemas within the same database. Maybe I should logically separate my databases this way? Seems like it would be a tough solution to manage since I already have some databases live in production that will eventually use this 'static' GeoDB. Also, seems like it
wouldn't be as portable as keeping the GeoDB info in its own database,
but maybe I'm being too software engineer-ish here - afraid of low
cohesion, high coupling.I will greatly appreciate any advice I can get, or any more options I am missing. Thanks,Adam Nofsingerucnmedia.com
View 2 Replies
View Related
Jul 23, 2005
I have a table called BidItem which has another table calledBidAddendum related to it by foreign key. I have another table calledBidFolder which is related to both BidItem and BidAddendum, based on acolumn called RefId and one called Type, i.e. type 1 is a relationshipto BidItem and type 2 is a relationship to BidAddendum.Is there any way to specify a foreign key that will allow for thedifferent types indicating which table the relationship should existon? Or do I have to have two separate tables with identical columns(and remove the type column) ?? I would prefer not to have multipleidentical tables.
View 26 Replies
View Related
Dec 27, 2003
Hello,
I want to make a poll and have to use a foreign key in my database. This is not possible in Web Matrix so I have to use SQL Enterprise Manager. But don't know how. I read the help but can't figure it out. Can someone help me??? Thanks in advance.
Regards,
Roel Alblas
View 5 Replies
View Related
Sep 14, 1998
Hi!
Is it possible in SQL 6.5 to create a Foreign Key on a field that
has a datatype of SMALLINT? I have created FK`s with
other datatypes, but it doesn`t seem to work with this particular
datatype. Is there some trick to this?
Thank you for your assistance!
Toni
View 2 Replies
View Related
Mar 30, 2006
When you set a foreign key to reference a primary key on another table, does the foreign key automatically update? Can anyone explain just how to make a relational table from just the nuts and bolts SQL statements.
View 1 Replies
View Related
Dec 20, 2004
Hi there,
I'm trying to make a few tables in SQL (First time user). The tables have been derived from a logical model based around my assignment...
I'm having problem getting two foreign keys in one table in the SQL. I keep getting the duplicate foreign key error...
I'm not sure where i'm going wrong.....
http://img.photobucket.com/albums/v294/Jertsy/Agh.jpg
there’s a pic of my tables.... There are 4 tables, blog, blogger, blog_entry, and comment.. Blog is only table without a FK.
have a look & thanks for any and all help....
Spencer.
View 1 Replies
View Related
Jun 23, 2007
Hi Friends,Is there any way to get the table name which is referenced by theforeign keyfor example: consider two table "Staff" and "Department"Staff with following columnsPK_IDFK_DepartmentIDNameAddressDepartment with following columnsPK_DepartmentIDDeptNameActually what i need is: Initially i would be having the table name as"Staff"from Staff table i need to identify that the column FK_DepartmentID isa foreign keyand the primary key is in the Department tablei need to traverse from Staff table and identify that FK_DepartmentIDis a primary key in Department tablethis has to be accomplished by sql query.... probably this could befetched fromData Dictionary but i couldnt find the relationship between the systemtables.ThanksArunDhaJ
View 1 Replies
View Related
Jun 5, 2007
I would like to create a foreign key but the Primary table has 2 fields as it Primary Key. Is there a way to create a Foreign Key that links only on one field of the primary key.
Ex: table 1: id int , language char(2), description varchar(100) PK = ID + language
table 2 : id int, idlanguage int PK = id FK (idLanguage refers to id from table 1)
This cause an error because the foreign key does not include all part of the primary key.
Rufen
View 3 Replies
View Related
Jan 4, 2008
table1
-------
a1 number(10) PK
b1 varchar2(3)
c1 number(10)
Table2
----------
d1 number(10) PK
b1 varchar2(3)
c1 number(10)
Can I make a Foreign key from Table2(b1,c1) to table1(b1,c1) if b1 in table1 might be null.
When I tried that I got this.
ORA-02270: no matching unique or primary key for this column-list
View 6 Replies
View Related
Apr 13, 2008
Hi everyone,
My main table has a column named "customer" which is a foreign key pointed to my secondary table consisted of a column "customer_id".
I wanted to change my secondary table's customer_id's name into another name whereby deleting it and create a new column instead (i dont know another way of changing names by code) but i'm not allowed to do that due to "customer_is"'s being attached to my main table as FK.
Any suggestion how to do it ?
Thanks a lot.
View 9 Replies
View Related
Aug 10, 2007
i want to make primary key and foreign key relationship of table A1 and table B1 but A1 exist in database A and B1 exist in database B
column name u can pretain as C1, C2
View 5 Replies
View Related
Aug 8, 2006
I am deleting a column from a table in code. Before I drop the colum I need to find if the column belongs to a foreign key. I have the names of the foreign keys for the table from sysobjects.How can I determine what columns are part of the foreign key?
View 9 Replies
View Related