I am trying to create a FK using a composite PK and here are the details that I want to achieve.
Table -A
Column A1
Column A2
Primary key (A1, A2).
Table -B
Column B1 Primary key.
Column B2
FK (B2) References A(A1).
When I try to do this I am getting some errors.
Questions:
First of all is this possible? If yes, then how do I can create it?
A foreign key is declared when some columns' subrow values always appear as some unique (PRIMARY KEY or UNIQUE NON NULL) columns' subrow values. A (A1) is not unique so you cannot be wanting a FK to it. What is the constraint on your tables that you do want? If A (A1) is unique, declare it so. If then B (B2) is always in that column declare FK B (B2) referencing A (A1). If A (A1) is not unique and you want B (B2) to be in it then you must constrain by other means such as triggers. Or maybe you should have a different schema.
Related
I have 3 tables:
Table A
Table B
Table C
Table A has a foreign key to Table B. Table C has a foreign key to Table B. So B acts as an intermediary through table, but it does not have any foreign keys to A or C itself, so I cannot declare a ManyToManyField from A to C in the conventional way, but I would like to do so in order to use prefetch_related when grabbing C records from A. Is there a way to do this?
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)
could someone explain me how to use foreign keys in hsql?
I would like it in an create table, but working alter table is also ok.
I am working without tools, just in eclipse
whats wrong with the hsql code?
CREATE TABLE user(
t_id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY,
name VARCHAR(30),
lastname VARCHAR(30),
email VARCHAR(30),
--FOREIGN KEY (b_id) REFERENCES bookingnumber(b_id)
);
CREATE TABLE bookingnumber (
b_id INTEGER PRIMARY KEY
);
ALTER TABLE user
ADD FOREIGN KEY (fk_b_id) REFERENCES bookingnumber(b_id);
Perhaps you are trying to link each booking number to a user. In this case, multiple booking numbers may exist for each user. If you want to do this, add a column T_ID to the BOOKINGNUMBER table and created the foreign key on this table.
But your statement is linking each user to a booking number and doesn't have the correct syntax. It needs a column named B_ID in the USER table to work. And the syntax would be like this:
ALTER TABLE user ADD FOREIGN KEY (b_id) REFERENCES bookingnumber(b_id);
I was facing a similar situation and this helped me
CREATE TABLE child(c1 INTEGER, c2 VARCHAR, FOREIGN KEY (c1, c2) REFERENCES parent(p1, p2));
A more detailed explanation can be found at http://www.d.umn.edu/~tcolburn/cs4531/hsqldb_docs/guide/guide.html#N102F8
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
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.