Foreign key constraints seem ignored by mysql - mysql

I am just making a foreign key reference of parent table in a child table. When I try to delete the row from the parent table whose reference is there in child table, surprisingly it allows me to delete it. I have tried to create child table explicitly by writing on delete restrict and also without it, but to no help. Any ideas why this is happening? Below is the code I am using while creating the tables.
CREATE TABLE region
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(50) NOT NULL
);
CREATE TABLE aggregator
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(50) NOT NULL
);
CREATE TABLE gateway
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(50) NOT NULL,
region_id int ,
aggregator_id int ,
is_public boolean DEFAULT 0 NOT NULL,
FOREIGN KEY (region_id) REFERENCES region(id),
FOREIGN KEY (aggregator_id) REFERENCES aggregator(id)
);

Both parent and child table need to be INNODB tables.
Try:
CREATE TABLE region
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(50) NOT NULL
) ENGINE=INNODB;
CREATE TABLE aggregator
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(50) NOT NULL
) ENGINE=INNODB;
CREATE TABLE gateway
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(50) NOT NULL,
region_id int ,
aggregator_id int ,
is_public boolean DEFAULT 0 NOT NULL,
FOREIGN KEY (region_id) REFERENCES region(id),
FOREIGN KEY (aggregator_id) REFERENCES aggregator(id)
) ENGINE=INNODB;

Do mean to say that if you delete parent table and child table deletes automatic? If yes then once go through cascading rules.

Related

Create Two Subtype (or child) tables with same primary key which is also a foreign key to the parent table sql

I have the following tables:
CREATE TABLE employee(
id INT(11) UNSIGNED PRIMARY KEY,
namee VARCHAR(60) NOT NULL)
ENGINE = InnoDB;
CREATE TABLE consierge(
id INT(11) UNSIGNED PRIMARY KEY,
namee VARCHAR(60) NOT NULL,
zonee VARCHAR(20),
functionn VARCHAR(20),
FOREIGN KEY (id) REFERENCES employee(id))
ENGINE = InnoDB;
CREATE TABLE guard(
id INT(11) UNSIGNED PRIMARY KEY,
namee VARCHAR(60) NOT NULL,
rol VARCHAR(20) NOT NULL,
wtime ENUM ('Day','Night'),
FOREIGN KEY (id) REFERENCES employee(id))
ENGINE = InnoDB;
When I try to create an consierge or guard, it firts verify the existence of that id in employee, which is fine. The problem is that, ie, when an id is already in guard (hence in employee) and I create an consierge with that same id, the consierge gets created, so, an employee can be guard and consierge; and I want that an employee can be guard OR consierge EXCLUSIVElY.
You can do this -- without triggers -- using some additional logic with generated columns:
CREATE TABLE employee(
id INT(11) UNSIGNED PRIMARY KEY,
namee VARCHAR(60) NOT NULL,
type varchar(255),
c_id int generated always as (case when type = 'c' then id end) stored,
g_id int generated always as (case when type = 'g' then id end) stored,
check (type in ('c', 'g'))
);
CREATE TABLE consierge (
id INT(11) UNSIGNED PRIMARY KEY,
namee VARCHAR(60) NOT NULL,
zonee VARCHAR(20),
functionn VARCHAR(20),
type varchar(255) generated always as ('c') stored,
unique (type, id),
FOREIGN KEY (id) REFERENCES employee(id)
);
CREATE TABLE guard(
id INT(11) UNSIGNED PRIMARY KEY,
namee VARCHAR(60) NOT NULL,
rol VARCHAR(20) NOT NULL,
wtime ENUM ('Day','Night'),
type varchar(255) generated always as ('g') stored,
unique (stored, id),
FOREIGN KEY (id) REFERENCES employee(id)
);
ALTER TABLE employee ADD CONSTRAINT fk_employee_c_id
FOREIGN KEY (c_id) REFERENCES concierge(id);
ALTER TABLE employee ADD CONSTRAINT fk_employee_g_id
FOREIGN KEY (g_id) REFERENCES guard(id);
This adds computed columns to allow the desired foreign key relationships.

Should Keys be INDICES or compound PRIMARY KEY

I have a schema with a parent and child table.
A parent record, may have many children (1 to many)
To link these, a third table has the PRIMARY KEY of the parent, and child records.
Should this linking table, have a compound PRIMARY KEY?
Or, should this simply be two FOREIGN KEYS, each with an INDEX()?
CREATE TABLE parent(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
PRIMARY KEY(id)
) ENGINE=INNODB;
CREATE TABLE child(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
PRIMARY KEY(id)
) ENGINE=INNODB;
So, should the linking table be like..
CREATE TABLE parent_child_link(
parent_id INT NOT NULL,
child_id INT NOT NULL,
INDEX(parent_id),
INDEX(child_id),
FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE CASCADE,
FOREIGN KEY (child_id) REFERENCES child(id) ON DELETE CASCADE
) ENGINE=INNODB;
Or should this be
CREATE TABLE parent_child_link(
parent_id INT NOT NULL,
child_id INT NOT NULL,
PRIMARY KEY(parent_id, child_id)
) ENGINE=INNODB;
Since its one to many and not many to many, adding a parent_id to the child table is sufficient.

Adding multi columns and foreign key

I`m a beginner in structured query language . I want to add multi columns with different foreign key.Like the example:
drop schema humman;
create schema humman;
CREATE TABLE humman.father (
id int not null auto_increment,
firstname varchar(200) not null,
primary key(id)
);
create table humman.mather(
id int not null auto_increment,
FirstName varchar(200),
primary key(id)
);
CREATE TABLE humman.child (
id int not null auto_increment,
firstname varchar(200) not null,
primary key(id)
);
use `humman`;
alter table humman.child
ADD `parentId` int ,
ADD `motherId` int,
ADD foreign key (`parentId`) references father(`id`),
ADD foreign key (`motherId`) references mother(`id`);
Error code: 1215 Cannot add foreign key CONSTRAINT
Your code is good except for a typo, you spelt "mother" as "mather" in your second table definition;
create table humman.mather(
id int not null auto_increment,
FirstName varchar(200),
primary key(id)
);
correct that and it should work.

Enforcing cross table uniqueness of records in mysql

I have three tables:
CREATE TABLE A (
id int(11) NOT NULL AUTO_INCREMENT,
adata varchar(255),
PRIMARY KEY (id)
);
CREATE TABLE B (
id int(11) NOT NULL AUTO_INCREMENT,
bdata varchar(255),
a_id int(11) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (a_id) REFERENCES A(id) ON DELETE CASCADE
);
CREATE TABLE C (
id int(11) NOT NULL AUTO_INCREMENT,
cdata varchar(255),
b_id int(11) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (b_id) REFERENCES B(id) ON DELETE CASCADE
);
Elements of C (associated with B's) must be unique only for a given A. I.e., the combination of (cdata and b_id.a_id) must be unique.
I realize that I could add another column (pointing to A) to the C table and create a unique key. Is there any way to enforce uniqueness without adding another column to C?

How can I add a foreign key when creating a new table?

I have these two CREATE TABLE statements:
CREATE TABLE GUEST (
id int(15) not null auto_increment PRIMARY KEY,
GuestName char(25) not null
);
CREATE TABLE PAYMENT (
id int(15) not null auto_increment
Foreign Key(id) references GUEST(id),
BillNr int(15) not null
);
What is the problem in the second statement? It did not create a new table.
The answer to your question is almost the same as the answer to this one .
You need to specify in the table containing the foreign key the name of the table containing the primary key, and the name of the primary key field (using "references").
This has some code showing how to create foreign keys by themselves, and in CREATE TABLE.
Here's one of the simpler examples from that:
CREATE TABLE parent (id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (id INT, parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE
) ENGINE=INNODB;
I will suggest having a unique key for the payment table. On it's side, the foreign key should not be auto_increment as it refer to an already existing key.
CREATE TABLE GUEST(
id int(15) not null auto_increment PRIMARY KEY,
GuestName char(25) not null
) ENGINE=INNODB;
CREATE TABLE PAYMENT(
id int(15)not null auto_increment,
Guest_id int(15) not null,
INDEX G_id (Guest_id),
Foreign Key(Guest_id) references GUEST(id),
BillNr int(15) not null
) ENGINE=INNODB;
Make sure you're using the InnoDB engine for either the database, or for both tables. From the MySQL Reference:
For storage engines other than InnoDB,
MySQL Server parses the FOREIGN KEY
syntax in CREATE TABLE statements, but
does not use or store it.
create table course(ccode int(2) primary key,course varchar(10));
create table student1(rollno int(5) primary key,name varchar(10),coursecode int(2) not
null,mark1 int(3),mark2 int(3),foreign key(coursecode) references course(ccode));
There should be space between int(15) and not null