My intention is to create a table with 2 primary keys with one of those autoincrementing and the other specified when inserting and when I create a new field for this table it must start the recount if the not incremented primary key changes. This is what I had:
I have been able to get this changing the table engine to MyISAM. But there is something missing, the auto_increment does not start at 100 as it happened before.
CREATE TABLE CONFIGURABLES(
CODIEI2 INTEGER AUTO_INCREMENT,
CODIEI1 INTEGER,
SKU VARCHAR(30),
COLOR INTEGER,
COLOR2 INTEGER,
TALLA INTEGER,
CONSTRAINT PK_CODIEI PRIMARY KEY(CODIEI1,CODIEI2),
CONSTRAINT FK_CODIEI1 FOREIGN KEY(CODIEI1) REFERENCES PRODUCTOS(ENTITY_ID) ON DELETE CASCADE,
CONSTRAINT FK_CCOLOR FOREIGN KEY(COLOR) REFERENCES COLORES(CODICOL) ON DELETE CASCADE,
CONSTRAINT FK_CCOLOR2 FOREIGN KEY(COLOR2) REFERENCES COLORES(CODICOL) ON DELETE CASCADE,
CONSTRAINT FK_CTALLA FOREIGN KEY(TALLA) REFERENCES TALLAS(CODITLL) ON DELETE CASCADE) ENGINE=MyISAM;
ALTER TABLE CONFIGURABLES AUTO_INCREMENT = 100;
Is this happening because when the auto_increment value is different from the default number the engine must be set to InnoDB?
Is there a way to get it as I want?
SOLUTION:
The table can be back to InnoDB which is much better and there is no need for auto_increment on CONFIGURABLES table as this will be controlled when doing the insert.
CREATE TABLE CONFIGURABLES(
CODIEI2 INTEGER,
CODIEI1 INTEGER,
SKU VARCHAR(30),
COLOR INTEGER,
COLOR2 INTEGER,
TALLA INTEGER,
CONSTRAINT PK_CODIEI PRIMARY KEY(CODIEI1,CODIEI2),
CONSTRAINT FK_CODIEI1 FOREIGN KEY(CODIEI1) REFERENCES PRODUCTOS(ENTITY_ID) ON DELETE CASCADE,
CONSTRAINT FK_CCOLOR FOREIGN KEY(COLOR) REFERENCES COLORES(CODICOL) ON DELETE CASCADE,
CONSTRAINT FK_CCOLOR2 FOREIGN KEY(COLOR2) REFERENCES COLORES(CODICOL) ON DELETE CASCADE,
CONSTRAINT FK_CTALLA FOREIGN KEY(TALLA) REFERENCES TALLAS(CODITLL) ON DELETE CASCADE);
And when doing the insert do this:
BEGIN;
SELECT #id := IFNULL(MAX(CODIEI2)+1,100) FROM CONFIGURABLES WHERE CODIEI1 = 10001 FOR UPDATE;
INSERT INTO CONFIGURABLES
(CODIEI1,CODIEI2,SKU,COLOR,COLOR2,TALLA)
VALUES
(10001,#id,'',4,2,2);
COMMIT;
BEGIN;
SELECT #id := MAX(id)+1 FROM foo WHERE other = 123 FOR UPDATE;
INSERT INTO foo
(other, id, ...)
VALUES
(123, #id, ...);
COMMIT;
How do you insert your data? If you give CODIEI2 as a column and a value as well, this will overwrite the auto_increment.
Test in SQLFiddle:
Build schema:
CREATE TABLE CONFIGURABLES(
CODIEI2 INTEGER AUTO_INCREMENT,
CODIEI1 INTEGER,
CONSTRAINT PK_CODIEI PRIMARY KEY(CODIEI2));
ALTER TABLE CONFIGURABLES AUTO_INCREMENT = 100;
INSERT INTO CONFIGURABLES ( CODIEI1 )
VALUES ( 1 );
Now overwriting the auto_increment:
INSERT INTO CONFIGURABLES ( CODIEI2, CODIEI1 )
VALUES ( 1, 2 );
Run SQL:
SELECT *
FROM CONFIGURABLES;
Output:
CODIEI2 CODIEI1
1 2
100 1
Related
tested this on postgreSQL and it doesn't let me delete any row(primary key) associated with another table. Is MySQL different or is there is a way to do that here in Mysql?.
CREATE TABLE account (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(30)
);
CREATE TABLE transaction (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
account_id BIGINT REFERENCES account(id)
);
INSERT INTO account (name) VALUES ('john'); //account_id = 1
INSERT INTO transaction (account_id) VALUES (1);
DELETE FROM account where id = 1;
);
when I delete an account row in account table, mySQL doesn't prevent the deletion of it.
Here is the example for MYSQL. You need add the foreign key with update and delete constrains. You can also read through the documents for more details. Foreign Key
CREATE TABLE account (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(30)
);
CREATE TABLE transaction (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
account_id BIGINT,
FOREIGN KEY (account_id) REFERENCES account(id) ON UPDATE CASCADE ON DELETE RESTRICT
);
INSERT INTO account (name) VALUES ('john');
INSERT INTO transaction (account_id) VALUES (1);
DELETE FROM account where id = 1;
-- 18:32:14 DELETE FROM account where id = 1 Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`test`.`transaction`, CONSTRAINT `transaction_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `account` (`id`) ON UPDATE CASCADE) 0.039 sec
Below is the simplified picture of the relationships I have in my DB:
create table attribute (id int auto_increment, primary key (id));
create table state_sample (id int auto_increment, primary key(id));
create table state_sample_attribute (
state_sample_id int,
attribute_id int,
primary key(state_sample_id, attribute_id),
foreign key (state_sample_id) references state_sample(id) on update cascade,
foreign key (attribute_id) references attribute(id) on update cascade
);
create table note (
id int auto_increment,
state_sample_id int,
attribute_id int,
primary key(id),
foreign key (state_sample_id) references state_sample(id) on update cascade,
foreign key (state_sample_id, attribute_id)
references state_sample_attribute(state_sample_id, attribute_id) on update cascade
);
insert into attribute values (1);
insert into state_sample values (1);
insert into state_sample_attribute values (1, 1);
insert into note values (1, 1, 1);
Whenever I try to update the ss table, it fails:
update state_sample set id = 2;
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`demotemplate`.`note`, CONSTRAINT `note_ibfk_1` FOREIGN KEY (`ss_id`) REFERENCES `ss` (`id`) ON UPDATE CASCADE)
As far as I understand, this is what happens:
It tries to set state_sample.id = 2.
It sees the cascade to note and tries to update note.state_sample_id.
However, note.state_sample_id is also involved in the foreign key to to state_sample_attribute(state_sample_id, attribute_id), so it goes to check whether that's still valid.
As state_sample_attribute.state_sample_id has not yet been updated, the constraint fails.
Is my assumption correct? And if so, is there a way to work this around?
Give the ssa table its own id primary key, and use that in the foreign key in notes, rather than referencing the ss_id and a_id columns.
create table ssa (
id int auto_increment,
ss_id int,
a_id int,
primary key (id),
unique key (ss_id, a_id),
foreign key (ss_id) references ss(id) on update cascade,
foreign key (a_id) references a(id) on update cascade);
create table note (
id int auto_increment,
ss_id int,
ssa_id int,
primary key(id),
foreign key (ss_id) references ss(id) on update cascade,
foreign key (ssa_id) references ssa(id) on update cascade);
Now you don't have the redundant dependency.
It's also not clear that note needs ss_id at all, since it's redundant with the related ssa row.
Try
DISABLE KEYS
or
SET FOREIGN_KEY_CHECKS=0;
make sure to turn it on
SET FOREIGN_KEY_CHECKS=1;
after.
What solved it in the end is dropping the extra FK:
alter table note drop foreign key (state_sample_id) references state_sample(id);
I have an error code which is Error Code: 1215. Cannot add foreign key constraint in MySQL. My code is
DROP TABLE IF EXISTS Formed;
DROP TABLE IF EXISTS Album;
DROP TABLE IF EXISTS Band;
DROP TABLE IF EXISTS Customers;
DROP TABLE IF EXISTS Track;
CREATE TABLE Formed(
FormedID int NOT NULL,
YearFormed int,
CountryFormed varchar(50),
CityFormed varchar(50),
BandMembers varchar(400),
PRIMARY KEY(FormedID))
ENGINE=InnoDB;
CREATE TABLE Track (
TrackID int NOT NULL,
AlbumID int NOT NULL,
Songs varchar (100),
TrackNumber varchar (20),
Title varchar (20),
TrackDuration varchar (4),
PRIMARY KEY (TrackID),
FOREIGN KEY (AlbumID) REFERENCES Album(AlbumID)ON DELETE SET NULL ON UPDATE CASCADE)
ENGINE=InnoDB;
CREATE TABLE Album(
AlbumID int NOT NULL,
TrackID int NOT NULL,
BandID int NOT NULL,
Price varchar(5),
PublicationDate varchar(11),
Title varchar(30),
Genre varchar (36),
PRIMARY KEY(AlbumID),
FOREIGN KEY (TrackID) REFERENCES Track(TrackID)ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY (BandID) REFERENCES Band(BandID)ON DELETE SET NULL ON UPDATE CASCADE)
ENGINE=InnoDB;
CREATE TABLE Band(
BandID int NOT NULL,
AlbumID int NOT NULL,
RecordLabel varchar(50),
PRIMARY KEY(BandID),
FOREIGN KEY (AlbumID) REFERENCES Album(AlbumID)ON DELETE SET NULL ON UPDATE CASCADE)
ENGINE=InnoDB;
CREATE TABLE Customers (
CustomerID int NOT NULL,
CName varchar (20),
CPhone int (11),
CEmail varchar (50),
CPPaid varchar (50),
CPDate date,
PRIMARY KEY (CustomerID))
ENGINE=InnoDB;
It creates the first table named Formed, but it's giving me the error when it tries and creates the second table and I don't know why. I know a bit about MySQL and I'm teaching myself about Foreign Keys. I have had a look online on why I could be getting this error but couldn't find anything useful.
From the error message, it seems like you are trying to create Track table before Album table and hence, it's failing as it is not able to find parent table (and column) for foreign key constraint.
I would suggest creating the tables with Primary keys only and then, apply foreign key constraints once all 3 tables are created, e.g.:
ALTER TABLE `Track`
ADD FOREIGN KEY (AlbumID) REFERENCES Album(AlbumID)ON DELETE SET NULL ON UPDATE CASCADE);
As a rule All reference fields in child tables MUST HAVE index defined on them as like in parent table. They must also follow some other constraints.
As per documentation on foreign key constraints:
REFERENCES parent_tbl_name (index_col_name,...)
Define an INDEX on relevant child columns. And make sure that child column definitions must match with those of their parent column definitions.
Code change suggested:
CREATE TABLE Formed( ... ) -- no change suggested
CREATE TABLE Customers ( ... ) -- no change suggested
CREATE TABLE Band(
...
AlbumID int NOT NULL,
...
KEY (AlbumID), -- <---- add this line
...
);
CREATE TABLE Album(
...
TrackID int NOT NULL,
BandID int NOT NULL,
...
KEY (TrakID), -- <---- add this line
KEY (BandID), -- <---- add this line
...
-- FOREIGN KEY (TrackID) -- <-- add this using ALTER
-- REFERENCES Track(TrackID) -- after creating
-- ON DELETE SET NULL ON UPDATE CASCADE, -- Track table
);
ALTER TABLE Band ADD
FOREIGN KEY ( AlbumID )
REFERENCES Album( AlbumID )
ON DELETE SET NULL ON UPDATE CASCADE;
CREATE TABLE Track ( ... ) -- no changes suggested
ALTER TABLE Album ADD
FOREIGN KEY ( TrackID )
REFERENCES Track( TrackID );
ON DELETE SET NULL ON UPDATE CASCADE;
Refer to:
MySQL Using FOREIGN KEY Constraints
[CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name, ...)
REFERENCES tbl_name (index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]
reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION
I'm planning to make a notification system that sends out different types of messages to users (private messages among users, message about their posts being published etc) What I'm looking for is some sort of conditional foreign key on the column this_id in notification table so that it can find a specific row from either table comments or pm, and I have found this thread that's exactly what I want. So I create a table "supertable" with just one column (SERIAL PRIMARY KEY) to be referenced by comments and PM. And to make sure a new row is insert into the supertable first to generate a key before any new row inserts into either comments and PM, I set up two BEFORE INSERT INTO triggers
CREATE TRIGGER `before_insert_comments`
BEFORE INSERT ON `comments`
FOR EACH ROW BEGIN
INSERT INTO supertable (this_id) VALUES ('')
END;
CREATE TRIGGER `before_insert_pm`
BEFORE INSERT ON `PM`
FOR EACH ROW BEGIN
INSERT INTO supertable (this_id) VALUES ('')
END;
But when inserting a record into table comments or PM, I'm still getting the error
Cannot add or update a child row: a foreign key constraint fails ( CONSTRAINT comments FOREIGN KEY (this_id) REFERENCES supertable (this_id) ON DELETE CASCADE ON UPDATE CASCADE). Anyone know what's the problem with the triggers?
Table schema
CREATE TABLE notification (
id INT NOT NULL AUTO_INCREMENT,
this_id SERIAL PRIMARY KEY,
user_id INT,
is_read TINYINT,
FOREIGN KEY (this_id) REFERENCES supertable(this_id)
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE TABLE supertable (
this_id SERIAL PRIMARY KEY
);
CREATE TABLE comments (
this_id SERIAL PRIMARY KEY,
user_id INT ,
post TEXT,
is_approved TINYINT,
...
FOREIGN KEY (this_id) REFERENCES supertable(this_id)
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE TABLE PM (
this_id SERIAL PRIMARY KEY,
sender_id INT,
recipient_id INT ,
msg TEXT,
...
FOREIGN KEY (this_id) REFERENCES supertable(this_id)
ON UPDATE CASCADE
ON DELETE CASCADE
);
Check again your column mapping. Few key to point here.
your notification table primary key should set to id instead of this_foreign_id(I rename it to avoid confuse).
datatype for this_foreign_id should be BIGINT UNSIGNED to map with SERIAL, since 1 table only can have 1 auto increment column and it should be primary key.
on your trigger, insert null instead of '' cause SERIAL column is meant for BIGINT UNSIGNED
Let me know if it work.
CREATE TABLE supertable (
this_id SERIAL PRIMARY KEY
);
CREATE TABLE notification (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
this_foreign_id BIGINT UNSIGNED,
user_id INT,
is_read TINYINT,
FOREIGN KEY (this_foreign_id) REFERENCES supertable(this_id)
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE TABLE comments (
this_id SERIAL PRIMARY KEY,
user_id INT ,
post TEXT,
is_approved TINYINT,
FOREIGN KEY (this_id) REFERENCES supertable(this_id)
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE TABLE PM (
this_id SERIAL PRIMARY KEY,
sender_id INT,
recipient_id INT ,
msg TEXT,
FOREIGN KEY (this_id) REFERENCES supertable(this_id)
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE TRIGGER before_insert_comments
BEGIN
BEFORE INSERT ON comments
FOR EACH ROW BEGIN
INSERT INTO supertable (this_id) VALUES (NULL);
END;
CREATE TRIGGER before_insert_pm
BEGIN
BEFORE INSERT ON PM
FOR EACH ROW BEGIN
INSERT INTO supertable (this_id) VALUES (NULL);
END;
Here's the basic gist of what I'm trying to do:
create table main(id char(1) primary key);
create table other (
id int primary key auto_increment,
main_id char(1),
key m (main_id),
constraint fk foreign key (main_id) references main(id)
);
insert into main(id) values('a');
insert into other(main_id) values('a');
update main inner join other on other.main_id=main.id
set main.id='b', other.main_id='b'
where main.id='a';
This results in a foreign key constraint failure. Is there any way to accomplish this without dropping the foreign keys (not really an option on my large production database)?
You can do this simply by temporarily setting foreign_key_checks=0 in your session:
set foreign_key_checks=0;
update main inner join other on other.main_id=main.id
set main.id='b', other.main_id='b'
where main.id='a';
Another option is to configure the foreign key with the ON UPDATE CASCADE option so that if the primary key is updated on the parent table it will cascade to the child table:
create table main(id char(1) primary key);
create table other (
id int primary key auto_increment,
main_id char(1),
key m (main_id),
constraint fk foreign key (main_id) references main(id) on update cascade
);