I have a script in MySQL that creates two tables, the second table references a field in the first table, now when I try to run this script in one batch it returns an error. My guess is that it checks the referenced table in the second table definition before creating the tables.
Any idea how I can create both tables at once?
Thanks
Edit:
Example:
CREATE TABLE table1
(
ID INT NOT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB;
CREATE TABLE table2
(
ID INT NOT NULL,
FID INT NOT NULL ,
PRIMARY KEY (ID),
FOREIGN KEY (FID) REFERENCES table1 (ID)
) ENGINE=InnoDB;
If I create the first table, then create the second table everything works fine, but when I run this in one batch it returns an error
Update:
It seems that this problem has been solved with MySQL 5.5. It now works correctly in one batch even if you have foreign key constraints in some table definitions.
the simple thing , to create first the 2 tables,
after that do alter table and add the reference.
Related
I have two tables, and I've made a relation using the Designer between the id column of my first table to the user_id column of my second table. Where and how do I add code or do something so that when, for example, the parent (id) is deleted, the user_id values which correspond to the deleted id will also be deleted? I tried deleting one of the registered ids, but the corresponding rows in the child table didn't get deleted.
I've done some searching, but I'm still very confused.
Thank you.
Note: I'm experimenting with MySQL and PHP, and this is for a little blog I'm making.
Please add an ON DELETE referential action to the foreign key constraint.
More details could be found here:
https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
For your case, ON DELETE CASCADE should be fine.
set id from first table as primary key
CREATE TABLE tbl_first(id INT PRIMARY KEY AUTO_INCREMENT, name varchar(20))
create your second table as tbl_second
CREATE TABLE tbl_second(id INT PRIMARY KEY AUTO_INCREMENT, fk_id int)
add constraint like this:
alter table tbl_second
add constraint fk_first
foreign key tbl_second(fk_id)
references tbl_first(id)
on delete cascade
it seem SQL Server and mySql are a little different, but it should work, i test it in mySQL
I have a table created in MySQL (see the code below). As you see in the code I have a foreign key manager which references the idNo. However I only want to reference to employees with the cat='B'. So I need something like
FOREIGN KEY (manager ) REFERENCES Employee(idNo WHERE cat='B').
Any ideas how I can accomplish this.
idNo SMALLINT AUTO_INCREMENT UNIQUE,
name VARCHAR(25) NOT NULL,
telNo INT(11) NOT NULL,
cat CHAR(1) NOT NULL,
manager SMALLINT,
PRIMARY KEY (idNo ),
FOREIGN KEY (manager ) REFERENCES Employee(idNo) on DELETE CASCADE)ENGINE=INNODB
AUTO_INCREMENT=1000;
Foreign keys do not allow conditions.
If you want to force the condition, you must do it via coding.
It can be done inside your non DB code (Java, PHP, VB,...), or you could create a procedure in MySQL that would be called to perform the insert and that will return an error code if condition is not matched.
If you insert from various codes/application, the procedure is the way to go since it would be centralized.
Create a new UNIQUE KEY in Employee combining idNo and cat.
ALTER TABLE Employee ADD UNIQUE KEY (idNo,cat);
Make a foreign key in the child table that references that new unique key.
ALTER TABLE SomeTable ADD FOREIGN KEY (idNo,cat) REFERENCES Employee(idNo,cat);
Then you just need to make sure cat is constrained to the single value 'B' in the child table. One solution is to create a lookup table containing just the single value 'B'.
CREATE TABLE JustB (cat char(1) PRIMARY KEY);
ALTER TABLE SomeTable ADD FOREIGN KEY(cat) REFERENCES JustB(cat);
Now the only value you can use in the child table is 'B', so naturally it can only reference rows in Employee that have a cat of 'B'.
Another solution would be to use a trigger, but I favor the lookup table.
I'm trying to add a foreign key to my user table.
All of my tables are InnoDB, and are using the same charset. I've got not idea as to why it isn't working :(.
Here is a screenshot of the user table:
As you can see, my userid, is an integer with the max length of 10.
This is the second table (called Content Enabled):
userid, in Content enabler, is identical to the userid in the users table, except it's not a primary index.
When I want to link them via a foreign key, using this query:
ALTER TABLE `contentenabler` ADD FOREIGN KEY ( `userid` ) REFERENCES `tietgen`.`users` (
`userid`
) ON DELETE CASCADE ON UPDATE CASCADE ;
This returns the error Error creating foreign key on userid (check data types)
As far as I can see, the data type are the same, where am I going wrong?
userid is UNSIGNED in your users table, but not your other table.
I am in the process of designing the databases for my system. There are a lot of foreign key constraints.
I was wondering whether I could get some advice, whether I should do which of the following:
1) Specify the constraints during table creation itself ie,
CREATE TABLE IF NOT EXISTS abc
(
keyword VARCHAR(20) NOT NULL,
id INT UNSIGNED NOT NULL,
FOREIGN KEY (id) REFERENCES xyz(id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB;
2)create the table without FK constraints and 'alter' the table later on ie,
CREATE TABLE IF NOT EXISTS abc
(
keyword VARCHAR(20) NOT NULL,
id INT UNSIGNED NOT NULL,
)ENGINE=InnoDB;
ALTER TABLE abc ADD CONSTRAINT fk_constraint FOREIGN KEY (id) REFERENCES xyz(id)
ON DELETE CASCADE ON UPDATE CASCADE;
Table xyz is simply another table with 'id' as a primary key.
You may create the FK at once. But this is not always possible because they can refer to each other in a circular fashion. Also, you may want to add columns later, with a FK.
It may be slightly faster to add it at once, because MySQL has to validate and rebuild the table structure for some changes (although I'm not sure adding FKs is one of those). But this process will be reasonably fast on empty tables, so it doesn't matter much when you add the FK.
The result will be the same. So, there is no differences.
If I create new database, I'd create table and its foreign key in one statement. The script will look better. But in this case parent tables must be created before the child tables.
If you don't want to take into account dependencies when creating tables, you can create tables in random order in the beginning of the script and then add foreign keys using ALTER TABLE.
I'm creating a table that has a basisId field as the primary key. There's also another field parentBasis which would be a reference to another tuple with that.basisId equal to this.parentBasis. What I want to do to is express this constraint while creating the table.
Something like: ADD CONSTRAINT CHECK EXISTS this.parentBasis AS somewhere.basisId (Obviously not real MySQL).
A quick browse through the MySQL dev pages didn't do much good. Any help would be appreciated.
Thanks.
If you're using InnoDB then you can create a foreign key from the table to itself. For example:
create table t (
id int not null primary key,
parent int null
);
alter table t add constraint foreign key (parent) references t(id);
then t.parent would either have to be NULL or a t.id value.