Does DB2 version 8 z/OS support nullable foreign keys?
For example, a foreign key on a person table to the supervisor table (where some people do not have supervisors).
According to the documentation, it should be possible to have a foreign key that is nullable in DB2 for z/OS v8. See the insert, update, and delete rules for these details:
insert rule
A nonnull insert value of the foreign key must match some value of the parent key of the parent table. The value of a composite foreign key is null if any component of the value is null.
update rule
A nonnull update value of the foreign key must match some value of the parent key of the parent table. The value of a composite foreign key is treated as null if any component of the value is null.
delete rule
Controls what happens when a row of the parent table is deleted. The choices of action, made when the referential constraint is defined, are RESTRICT, NO ACTION, CASCADE, or SET NULL. SET NULL can be specified only if some column of the foreign key allows null values.
Site reference: http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2.doc.sqlref%2Frcncrfi.htm
DB2 v8 Foreign Key Clause reference (also notes that nulls are allowed): http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/admin/c0004981.htm
It would be quicker to try it than study the documentation, which will be removed from the IBM web site after January 2013.
Here's part of the explanation of the CREATE TABLE statement.
The foreign key of the referential constraint is composed of the identified columns. |Each column-name must |be an unqualified name that identifies a column of the table except |a LOB, ROWID, or security label column, and the same column must not |be identified more than once. The number of identified columns must not exceed 64, and the sum of their length attributes must not exceed 255 minus the number of columns that allow null values. The referential constraint is a duplicate if the FOREIGN KEY and parent table are the same as the FOREIGN KEY and parent table of a previously defined referential constraint. The specification of a duplicate referential constraint is ignored with a warning.
So yes, a foreign key can contain one or more nullable columns.
Related
I'm trying to add unique columns on a pivot table created via a ManyToMany association.
I found this page of the documentation explaining how to generate a database unique constraint on some columns with this example:
/**
* #Entity
* #Table(name="ecommerce_products",uniqueConstraints={#UniqueConstraint(name="search_idx", columns={"name", "email"})})
*/
class ECommerceProduct
{
}
But this only works if I create the pivot table via a third entity and, in my case, I created the pivot table using a ManyToMany relation (in the same fashion as this code).
Is there a way to add unique columns on pivot table while still using ManyToMany or do I need to rely on a third entity?
While #Table annotation proposes a uniqueConstraints option, #JoinTable does not. Thus, if you want to add a unique constraint on your association table, you will have to actually create another entity explicitly.
That being said, the default join table should not need anything more than the default configuration set up by Doctrine. Currently, when adding a ManyToMany association, the join table is composed of two fields and a composite primary key relying on both fields is created.
If your association table only contains the two basic fields referring to both sides of your association (which is necessarily the case if you use #ManyToMany), the composite primary key should be all you need.
Here is the generated SQL for the basic example where a User has a ManyToMany association with Group (from this section of the documentation):
CREATE TABLE users_groups (
user_id INT NOT NULL,
group_id INT NOT NULL,
PRIMARY KEY(user_id, group_id)
) ENGINE = InnoDB;
ALTER TABLE users_groups ADD FOREIGN KEY (user_id) REFERENCES User(id);
ALTER TABLE users_groups ADD FOREIGN KEY (group_id) REFERENCES Group(id);
As you can see, everything is properly set up with a composite primary key which will ensure that there can't be duplicate entries for the couple (user_id, group_id).
Of course there is another alternative, Alan!
If you need a Zero to Zero relationship, the only alternative is defining the unique constraint per each pk in the agregated table, to make doctrine figuring out about zero to zero relationship.
The problem is that Doctrine's people hadn't considered zero to zero relationships, so the only alternative for this is manytomany relationship with one unique constraint per pk.
If you have doubts about final-state of your doctrine implementation of your E-R model, I strongly recommend mysql-workbench-schema-exporter. With this php tool, you can easily export your mysql workbench E-R schema to a Doctrine's working classes schema, so you would be able to easily explore all your alternatives ;-)
Hope this helps
I have a table T1 with 4 fields (F1, F2, F3 and F4) together acting as unique identifier of the table rows. Can one of these fields (F4 say) be used as a foreign key for the same table? We already have master data table (T2) for the same.
Yes, it can.
You can easily set one of the primary key fields as a foreign key and assign it a check table. By default SAP proposes you domain value table of this field as a check table for it.
Moreover, while creating you can specify the role of a foreign key field in your T1 table: either it is key or non-key.
The only limitation: you cannot create foreign key for a field if it's already assigned the same value table in its domain as current. For example, one cannot make field MATNR in MARA foreign key, because it has value table MARA in its domain. You'll get an error E2165:
Check table of the domain is current table (FK maintenance not possible)
Alter table residential add constraint pk_restype primary key (customerID) REFERENCES customer(customerID);
I'd like to set primary key constraints on 'residential' table but ORA-01735 error appears indicating "invalid ALTER TABLE option". I've also tried the following to make foreign key relation but it also comes up as the same error code.
Alter table residential add constraint fk_restype foreign key (customerID, customertype) REFERENCES customer(customerID, customertype);
Your problem is you are creating a primary key as if it is a foreign key.
The proper PK syntax is:
alter table residential add constraint pk_restype primary key (customerID);
No references clause is allowed in a Primary Key, on a Foreign Key.
The PK says this column customerID is unique and identifies a unique row in the residential table. It has nothing to do with referencing another table.
A FK would be:
alter table tab_child add constraint fk_child FOREIGN key (child_id)
REFERENCES tab_parent(id);
The FK says a column child_id in table tab_child refers to and is constrained by the column id in table tab_parent
I am trying to work with QSqlRelationalTableModel of QT. I am new to MySQL table relationship but still I tried and can't make it work properly in QT.
I can get the result from MySQL:
create table stu(idd int auto_increment primary key,stu_name varchar(60),stu_age int);
create table stuInfo(idd int auto_increment primary key,stu_city varchar(60),stu_sub varchar(100), foreign key(id) references stu(id));
select stu.stu_name,stuInfo.stu_city from stu inner join stuInfo on stu.id=stuInfo.id;
To retrieve data from MySQL :
select stu.stu_name,stuInfo.stu_city from stu inner join stuInfo on stu.id=stuInfo.id;
In QT I can't make it work. I am getting confused with setRelation() and QSqlRelation() . I am not exactly understanding that how I can execute the same query in QT, I tried it in various way but sometime I get blank data, ugly header, errors etc.
Here is my learning code:
model = new QSqlRelationalTableModel();
model->setTable("stu");
model->setRelation(0,QSqlRelation("stu","id","stu_name","stu_age"));
model->setRelation(0,QSqlRelation("stuInfo","id","stu_city","stu_sub"));
model->select();
ui->tableView->setModel(model);
A QSqlRelation replaces the value of a field by the value of the other field in the relation, the replaced field won't appear in the query anymore, so you can't have 2 relations assigned to the same column, and you can't assign a relation to the primary key (as stated in the documentation of setRelation).
Basically the structure for which QSqlRelationalTableModel should be used would be a main table which would have 1 or more foreign index fields, and each of these fields could be replaced by the value of a chosen field in the tables from which the foreign indexes comes from (e.g.: to replace a "city_id" numerical field in the main table by the name of the city coming from another table for which that "city_id" is the primary key).
For what you want to do, you should use QSqlQueryModel with a manually constructed query instead of QSqlRelationalTableModel.
The problem is that your code does not really express the model you described.
You have a primary table called stuInfo, which references another table called stu.
To do this in Qt, you should create a table based on "stuInfo" (and not "stu"!):
model=new QSqlRelationalTableModel();
model->setTable("stuInfo");
Then you can implement your foreign key, as a relation:
model->setRelation(3,QSqlRelation("stu","id","stu_name"));
You need to point to index "3", which is the position of the reference field "id", on stuInfo table (0 will point to the primary key, which is not what you want!). The parameters of the QsqlRelation are the reference table name ("stu") the primary field name ("id") and the reference table field to which you want to point: in this case I am pointing to "stu_name"; if I wanted to point to the age, I could do something like this instead:
model->setRelation(3,QSqlRelation("stu","id","stu_age"));
After this code:
model->select();
ui->tableView->setModel(model);
you should have a view that shows you all the fields on stuInfo, and whose last field ("id") is mapped to the name (or age) on the "stu" table;
I was trying to input my foreign keys into my Link table all at once.
I created all my other tables first then my link table, i added columns to my link table for the foreign keys (that worked). Then i went to insert my foreign keys and it doesn't work (by the way none of my other tables have foreign keys)
it says
#1005 - Can't create table 'waget.#sql-798_842' (errno: 150) (Details...)
i clicked on details and it comes up with INNODB
[ Variables | Buffer Pool | InnoDB Status ]
i click on variables it has a question mark next to 3 things Autoextend increment,Buffer pool size,Data home directory
im so lost i just want to be able to create foreign keys please Help
My foreign key insert code
use Dbase;
alter table Link
add foreign key (C_id) References C (C_id),
add foreign key (D_id) References D (D_id),
add foreign Key (T_id) References T (T_id),
add foreign Key (B_id) References B (B_id),
add foreign Key (H_id) References H (H_id);
Make sure both collumns are exactly the same: both int or varchar etc., both the same length, both null or not.