Foreign Key Input error in Link table (Error 1005) - foreign-keys

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.

Related

Can't add foreign key users table in mysql Workbench

I'm having a problem and don't have much time so solve it.
I have thoses tables in my database, one called wall and the other is users.
For the wall table, I can add a foreign like you can see :
But in the users table, I can't and I don't know why. Both tables are empty.
Here you can see the users table :
And this is the error code I get :
I think there is not possible to create foreign key to the same table.
In this SQL you change table 'user' and add to this table foreign key from 'user' table.
If I'm wrong please correct me.
Here is example:
alter table `wall` add constraint `id_user_constraint_name` foreign key (`id_user`) references `users`(id);
If you don't have a 'id_user' column use this sql:
alter table `wall` add column id_user bigint;

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

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)

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

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 );