I'm trying link two tables together using a foreign key. One table is users, the other is userInfo. When I delete a user I also want to delete their info as well. When I delete a user from the users table their entry in usersInfo is still there. I can't seem to figure out what I'm doing wrong.
CREATE TABLE users (
userid INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(32) NOT NULL,
userlevel INT UNSIGNED NOT NULL,
rating int,
organization int(1),
timestamp varchar(20) NOT NULL,
);
Here is my table for userinfo. Yes, I know it could be in the same table. I'm just doing this for an easy example.
CREATE TABLE usersInfo(
userid int auto_increment NOT NULL,
userlocation varchar(50),
about varchar(300),
userkeywords varchar(150),
FOREIGN KEY(userid) REFERENCES users(userid) ON DELETE CASCADE ON UPDATE CASCADE
);
Edit - problem solved. Thanks to everyone who helped.
Added TYPE = InnoDB at the end of the CREATE TABLE statements.
Add ENGINE=INNODB to these CREATE TABLE statements to ensure they're InnoDB tables. MySQL versions below 5.5.5 will default to MyISAM, which does not support foreign-key relationships, but which will not throw an error if you define a foreign-key relationship either.
Related
Lets say I have the following table:
CREATE TABLE IF NOT EXISTS tbl_mg_accounts (
account_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,L,
holder_id INT NOT NULL,
FOREIGN KEY (holder_id) REFERENCES tbl_mg_holders(holder_id) ON UPDATE CASCADE ON DELETE CASCADE
);
I want to add another column that auto-fills but it's based on a column within the foreign table reference.
E.g.
CREATE TABLE IF NOT EXISTS tbl_mg_accounts (
account_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,L,
holder_id INT NOT NULL,
username VARCHAR(50) NOT NULL,
test_col VARCHAR(100) GENERATED ALWAYS AS CONCAT(tbl_mg_holders.holder_name, username) VIRTUAL,
FOREIGN KEY (holder_id) REFERENCES tbl_mg_holders(holder_id) ON UPDATE CASCADE ON DELETE CASCADE
);
This is giving me an error...
Perhaps the addition of below is a syntax issue or is this actually a limitation with MySQL/MariaDB?
test_col VARCHAR(100) GENERATED ALWAYS AS CONCAT(tbl_mg_holders.holder_name, username) VIRTUAL
Ok Yes this seems like a limitation.
I've instead created a VIEW for my requirements and let the backend process this way.
When I use this query:
CREATE TABLE users(
id int not null auto_increment primary key,
username varchar(30) not null unique,
email varchar(255) not null unique,
password varchar(255) not null
);
CREATE TABLE items(
id int not null auto_increment primary key,
name varchar(30) not null,
user_id int not null,
FOREIGN KEY user_key(user_id)
REFERENCES users(id)
);
DROP TABLE users;
It shows this error:
1217 - Cannot delete or update a parent row: a foreign key constraint fails
Which is alright because that is how mySQL database naturally reacts when we want to drop table that is referenced by other table that depends on it.
However, this same query shows no errors and actually drops users table on my pal's PC.
What could be the case? Is there a way to disable it?
You may be using different database engines. MyISAM and InnoDB have different FK support/enforcement, I believe. It could also be that the data in each of your tables is different.
If you want to drop a table that is a dependency of another table, though, the "right" way is to remove the FK from the dependent table and then drop the table that you want to.
So I don't understand why I cannot insert data in my table that have foreign constraint keys or even modify anything in it.
Here is an example of the tables that are created. I am trying to insert data in the addresses table:
///////////////////////ADDRESSES TABLE ////////////////////////
CREATE TABLE IF NOT EXISTS addresses (
id INT NOT NULL AUTO_INCREMENT,
addressline1 VARCHAR(255) NOT NULL,
addressline2 VARCHAR(255) NOT NULL,
postcode VARCHAR(255) NOT NULL,
phonenumber INT(13) NOT NULL,
country_id INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (country_id) REFERENCES countries(id)
ON UPDATE CASCADE
ON DELETE RESTRICT
) ENGINE=InnoDB ";
///////////////////////COUNTRIES TABLE ////////////////////////
CREATE TABLE IF NOT EXISTS countries (
id INT NOT NULL AUTO_INCREMENT,
countryname VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
)
The issue here is that you are trying to insert into a referencing table (addresses) when the referenced entry (the country you reference) does not exist. That's what's triggering the FOREIGN KEY CONSTRAINT exception.
Try first inserting some countries into the countries table, then inserting some addresses where you reference those countries you entered in the first step.
As for your second question, that's a choice for you to make. I would probably choose to have the User have an Address (address field in the User table), but some of that depends on how the data is being used/updated.
Have a quick look through this resource if you're new to relational database design. It covers (in brief) topics like relationship types, key constraints, and normal forms.
There are many similar question but this is bit different.
I have one table which has one foreign key that will reference to two tables.
I used below query for testing.
CREATE TABLE users
(
id int NOT NULL,
username varchar(255) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE admins
(
id int NOT NULL,
username varchar(255) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE info
(
id int NOT NULL,
fullname int NOT NULL,
user_id int,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (user_id) REFERENCES admins(id)
);
Above queries works fine.
but when I try to draw model in mysql workbench it create on new field in info table that I don't want. I want user_id should work and show relation as foreign key for users and admins table.
One more thing, am I trying to do that is not well standard? Also suggests a correct way to do it.
Table names used only for example purpose. There is no logic here. I am trying to find solution for one key as foreign key for multiple table and faced issue with mysql work bench.
Try this:
Save your DDL in a file.
Create new model in MySQL Workbench
File > Import > Reverse Engineer MySQL Create Script
Browse to file created in step 1. Ensure that 'Place imported objects on diagram' is selected.
Click 'Execute'
From a data modelling point of view you might be better off specifying a user as an admin by including an extra column on the users table. Hence:
CREATE TABLE users
(
id int NOT NULL,
username varchar(255) NOT NULL,
isAdmin boolean not null default false,
PRIMARY KEY (id)
);
CREATE TABLE info
(
id int NOT NULL,
fullname int NOT NULL,
user_id int,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES users(id)
);
I have created two tables within a MySql database:
create table scope (
name_scope varchar(50) not null primary key,
description varchar(100)
);
create table value_scope (
id_value int not null primary key AUTO_INCREMENT,
name_scope varchar(50) not null,
value varchar(100) not null,
foreign key (name_scope) references scope(name_scope) ON DELETE CASCADE
);
A scope can have multiple values. When I delete a scope; I expect its respective values to be deleted as well, but nothing happens. I try to do it the opposite way, but the scope still exists.
What am I doing wrong?
MyISAM does not support referential integrity.Use InnoDB instead.