See also:
- SQL server merge statement with two tables as Target
Does the SQL MERGE statement support or what is the workaround to deal with Intersection Tables, when the source table has no intersection table design?
An intersection table is a table that sits between 2 tables so that you can have many to many relationships. The intersection table just holds the keys of both (e.g see here: Many to Many Relation Design - Intersection Table Design)
In my case the source table looks like
a primary key A
a comment field 1
a comment field 2
a comment field 3
The target table(s) are a table that holds:
a primary key B (unique, specific for this table)
a comment
and an intersection table that holds
the primary key A (the same as the source table)
the primary key B (the one from the target table)
I want to merge from and to so both sides with 2 merge statements.
Related
This ERD is part of my school work and something doesn't seem right. The ERD table 'Course' looks like its referencing 2 tables. Is the column titled 'Qual_Code' in table 'Course' from the 'Prerequisite' table, the 'Qualification' table or both? I don't think its both because you cannot have a single column with a foreign key that references two different tables.
Help because I have to write the SQL codes for this!
At the first glance, it appears that the column qual_code of the COURSE table is redundant. When writing the DDL code for this, you can include or exclude this column - see the following examples:
course table with qual_code column
course table without qual_code column
The PREREQUISITE table maps courses to qualifications. The PK constraint in this table will not allow NULLs in either of the columns ie if a row gets INSERTed, it must contain values for both course_num and qual_code.
I don't think its both because you cannot have a single column with a
foreign key that references two different tables.
It seems that you are misinterpreting this. The PK column of the QUALIFICATION table is referenced twice (FK columns reference a PK or UNIQUE column, not the other way round -> see the DDL code on DBfiddle).
I have 7 tables. Each table represents transmission type where each row in table represents the Trasmission.
These are the tables:
Some of the tables have task_reference_id field and some of them doesn't have.
This means that one task can referenced by many tables.
No I have to change this structure: Each transmission table with task_reference_id field should reference to many tasks references.
This means that I need to add 5 tables with fields: task_id, transmission_id that will hold the tasks references.
But this makes me think, instead of adding 5 new tables maybe I can create parent "Transmission" table that all other tables will "inherit" from (primary key will be foreing key in the parent table).
And such it will take only one table to make references to tasks. This table will have base_transmission_id, task_id field.
What do you think?
What will be better: add 5 tables or change the structure with parent table so I will have to add only 1 table in order to reference the tasks?
I would go for supertype/subtype, here are few examples.
I have two tables and I want to use the database to enforce a constraint against them.
Table A has a one to many relationship with table B, that is many table B rows belong to exactly one table A row. Table B has a column of the Table A primary key that it belongs to. Anyway, I want to add a column to both table A and table B where the value of it in a table A record must equal the value of it in the corresponding table B records.
I want to tell SQL Server 2000 to disallow updates or additions of table B records unless the values in the sister columns are the same. How can I do this? I tried using a check constraint but it doesn't like subqueries. Will I have to venture into the world of triggers? Is there another way?
Any help would be appreciated.
Thanks
Isaac
What you're trying to do sounds like a foreign key relationship - but you already seem to have that (Table B has a column of Table A primary key).
I don't think you can define such a constraint - CHECK constraints cannot span tables, as far as I know. So in the end, you will probably need to use triggers instead on both tables to achieve this.
I have two tables A,B like
A(A_pk_id,A_name), B(A_pk_id,B_pk_id,B_Name);
Here A_pk_id is primary key of table A, And table B has composite primary key comprising of two fields A_pk_id(Table's primary key),B_pk_id,
Now whenever i tried to define relationship between these two tables [on A_pk_id] MS Access 2007 set their relationship type 1 to 1, but i want it to be 1 to many, cardinality of table must be set to 1.
Can any body guide how i could accomplish my goal.
Regards
Ahsan
If you're getting a 1:1 relationship, it means you're joining two fields that both have a unique index on them (regardless of whether both are PK or not). The many side needs to have a non-unique index.
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.