Can a field from primary key be used as a foreign key in the same table? - foreign-keys

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)

Related

how to use foreign-keys with hsql create table?

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

ORA-01735 Primary key and Foreign Key

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

Foreign Key Input error in Link table (Error 1005)

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.

Change a Primary Key to Foreign Key in Toad Data Modeler

I reverse engineered a MySQL database in Toad Data Modeler 4.3, but there are no foreign keys imported.
What is the easiest way to change the primary keys to Foreign keys?
The only way I know is to create new foreign keys and delete the old ones, but I wonder if there's a better way, especially as the field names are the same (e.g. UserID is the primary key in the Users table, UserID is a foreign key from Users in the Templates table)
The easiest way is doing with SQL. Something like this should do it for you:
ALTER TABLE Templates
ADD CONSTRAINT <name of foreign key> FOREIGN KEY (UserID )
REFERENCES Users (UserID );

DB2 v8 Nullable Foreign Keys

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.