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 );
Related
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;
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
I have to table with relation.
State
id
name
City
id
name
state
Which is better in performance?
city.state.id or city.state_id
city.state_id is better anyway. city.state will do another fetch from database.You can avoid this using select_related.If you need only id of foriegn key, no need of select_related here.Just do city.state_id(since foriegn key id will fetch in the query which gives city object).
city.state_id is better than city.state.id. Because It makes only a query instead of two.
BTW, You can use Django Debug Toolbar for debugging queries.
the <field>_id field you see is the database column name
docs
Behind the scenes, Django appends "_id" to the field name to create its database column name. In the above example, the database table for the Car model will have a manufacturer_id column
So this means it doesn't need to make a separate query to retrieve the foreign key instance (See Select a single field from a foreign key for more details).
But this assumes you haven't used select_related or prefetch_related
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.