Can't create constraint key - mysql

I have these 3 MySQL tables:
Apps:
CREATE TABLE IF NOT EXISTS `apps` (
`identifier` INT NOT NULL AUTO_INCREMENT,
`id` VARCHAR(255) NOT NULL,
`name` VARCHAR(255) NULL,
`description` VARCHAR(255) NULL,
`cloud` VARCHAR(255) NULL,
`onpremise` VARCHAR(255) NULL,
`type` VARCHAR(255) NULL,
`editor` VARCHAR(255) NULL,
`provider` VARCHAR(255) NULL,
`administrators` INT NULL,
PRIMARY KEY(`identifier`),
CONSTRAINT admin_ref_team
FOREIGN KEY(`administrators`) REFERENCES `teams`(`identifier`)
ON DELETE CASCADE
ON UPDATE CASCADE
);
Quality:
CREATE TABLE IF NOT EXISTS `quality` (
`identifier` INT NOT NULL AUTO_INCREMENT,
`quality` VARCHAR(255) NOT NULL,
`label` VARCHAR(255) NOT NULL,
PRIMARY KEY(`identifier`)
);
Association of apps and quality:
CREATE TABLE IF NOT EXISTS `assoc_apps_quality` (
`identifier` INT NOT NULL AUTO_INCREMENT,
`apps_id` VARCHAR(255) NOT NULL,
`quality_id` INT NOT NULL,
PRIMARY KEY(`identifier`),
CONSTRAINT apps_ref_quality
FOREIGN KEY(`apps_id`) REFERENCES `apps`(`identifier`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT quality_ref_apps
FOREIGN KEY(`quality_id`) REFERENCES `quality`(`identifier`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
Apps and Quality are created, but the third table is giving me this error and I can't create it:
Error in query (1215): Cannot add foreign key constraint
I don't see where is the problem. The constraint names are unique among all the database and there is no typos. Any ideas ?

Your apps_id defined in "quality_ref_apps" is wrong please try below sql.
CREATE TABLE IF NOT EXISTS `assoc_apps_quality` (
`identifier` INT NOT NULL AUTO_INCREMENT,
`apps_id` INT NOT NULL,
`quality_id` INT NOT NULL,
PRIMARY KEY(`identifier`),
CONSTRAINT apps_ref_quality
FOREIGN KEY(`apps_id`) REFERENCES `apps`(`identifier`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT quality_ref_apps
FOREIGN KEY(`quality_id`) REFERENCES `quality`(`identifier`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
Change from apps_id VARCHAR(255) NOT NULL, TO apps_id INT NOT NULL,

Check data type.
FOREIGN KEY(`apps_id`) REFERENCES `apps`(`identifier`)
apps_id is VARCHAR,
identifier is INT

Related

MySqlException: Table 'membership.aspnetusertokens' doesn't exist ASP.NET Core 5 Identity

I am setting up Microsoft ASP.NET Core Identity on my website as an exercise to move on to other web applications.
I have gotten to the part where I am trying to implement two factor authentication. I get an error as in the title because I do not have AspNetUserTokens as a table in my database. I cannot seem to find the schema for this detailed anywhere. Does anyone know the schema for this table?
This is the script I currently have for my database:
DROP DATABASE Membership;
CREATE DATABASE Membership;
USE Membership;
CREATE TABLE `AspNetRoles` (
`Id` varchar(128) NOT NULL,
`Name` varchar(256) NOT NULL,
PRIMARY KEY (`Id`)
);
CREATE TABLE `AspNetUsers` (
`Id` varchar(128) NOT NULL,
`Email` varchar(256) DEFAULT NULL,
`NormalizedEmail` varchar(256) DEFAULT NULL,
`EmailConfirmed` tinyint NOT NULL,
`PasswordHash` longtext,
`SecurityStamp` longtext,
`PhoneNumber` longtext,
`PhoneNumberConfirmed` tinyint NOT NULL,
`TwoFactorEnabled` tinyint NOT NULL,
`LockoutEndDateUtc` datetime DEFAULT NULL,
`LockoutEnabled` tinyint NOT NULL,
`AccessFailedCount` int NOT NULL,
`UserName` varchar(256) NOT NULL,
`NormalizedUserName` varchar(256) NOT NULL,
`ConcurrencyStamp` longtext,
`CustomTag` longtext,
`LockoutEnd` datetime,
PRIMARY KEY (`Id`)
);
CREATE TABLE `AspNetUserClaims` (
`Id` int NOT NULL AUTO_INCREMENT,
`UserId` varchar(128) NOT NULL,
`ClaimType` longtext,
`ClaimValue` longtext,
PRIMARY KEY (`Id`),
UNIQUE KEY `Id` (`Id`),
KEY `UserId` (`UserId`),
CONSTRAINT `ApplicationUser_Claims` FOREIGN KEY (`UserId`) REFERENCES `AspNetUsers` (`Id`) ON DELETE CASCADE ON UPDATE NO ACTION
);
CREATE TABLE `AspNetUserLogins` (
`LoginProvider` varchar(128) NOT NULL,
`ProviderKey` varchar(128) NOT NULL,
`UserId` varchar(128) NOT NULL,
PRIMARY KEY (`LoginProvider`,`ProviderKey`,`UserId`),
KEY `ApplicationUser_Logins` (`UserId`),
CONSTRAINT `ApplicationUser_Logins` FOREIGN KEY (`UserId`) REFERENCES `AspNetUsers` (`Id`) ON DELETE CASCADE ON UPDATE NO ACTION
);
CREATE TABLE `AspNetUserRoles` (
`UserId` varchar(128) NOT NULL,
`RoleId` varchar(128) NOT NULL,
PRIMARY KEY (`UserId`,`RoleId`),
KEY `IdentityRole_Users` (`RoleId`),
CONSTRAINT `ApplicationUser_Roles` FOREIGN KEY (`UserId`) REFERENCES `AspNetUsers` (`Id`) ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT `IdentityRole_Users` FOREIGN KEY (`RoleId`) REFERENCES `AspNetRoles` (`Id`) ON DELETE CASCADE ON UPDATE NO ACTION
);
CREATE TABLE `AspNetUserTokens` (
?????
);
Much thanks!
CREATE TABLE [dbo].[AspNetUserTokens] (
[UserId] NVARCHAR (450) NOT NULL,
[LoginProvider] NVARCHAR (450) NOT NULL,
[Name] NVARCHAR (450) NOT NULL,
[Value] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_AspNetUserTokens] PRIMARY KEY CLUSTERED ([UserId] ASC, [LoginProvider] ASC, [Name] ASC)
);

New to SQL, CREATE TABLE error

CREATE TABLE `ROUTE` (
`route_ID` INT NOT NULL,
`route_name` VARCHAR(45) NOT NULL,
`DELIVERY_VEHICLE_veh_ID` INT NOT NULL,
`DELIVERY_DRIVER_dr_ID` INT NOT NULL,
PRIMARY KEY (`route_ID`),
CONSTRAINT `fk_ROUTE_DELIVERY1`
FOREIGN KEY (`DELIVERY_VEHICLE_veh_ID` , `DELIVERY_DRIVER_dr_ID`)
(`VEHICLE_veh_ID` , `DRIVER_dr_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
I'm new to SQL and trying to learn by practicing. There's an error in my syntax that I don't understand. Any help/suggestions?
(VEHICLE_veh_ID , DRIVER_dr_ID) at line 9
CREATE TABLE `DRIVER` (
`dr_ID` INT NOT NULL,
`dr_title` VARCHAR(15) NOT NULL,
`dr_fname` VARCHAR(45) NOT NULL,
`dr_lname` VARCHAR(45) NOT NULL,
`dr_DOB` DATETIME NOT NULL,
`dr_licenceNo` VARCHAR(45) NOT NULL,
`dr_phone` VARCHAR(15) NOT NULL,
`dr_email` VARCHAR(45) NOT NULL,
PRIMARY KEY (`dr_ID`));
CREATE TABLE `VEHICLE` (
`veh_ID` INT NOT NULL,
`veh_reg` VARCHAR(15) NOT NULL,
`veh_make` VARCHAR(45) NOT NULL,
`veh_model` VARCHAR(45) NOT NULL,
`veh_mileage` INT NOT NULL,
`veh_MOTdate` DATETIME NOT NULL,
`veh_servicedate` DATETIME NOT NULL,
PRIMARY KEY (`veh_ID`));
These are the other tables ROUTE is related to. I had no issues inserting DRIVER and VEHICLE tables into the DB, what's wrong with ROUTE?
It looks like there are two foreign keys in the ROUTE table.
One of them references the DRIVER table.
The other references the VEHICLE table.
Your foreign key constraint definitions should look more like this:
, CONSTRAINT `fk_ROUTE_DELIVERY_DRIVER`
FOREIGN KEY (`DELIVERY_DRIVER_dr_ID`)
REFERENCES `DRIVER` (`dr_ID`)
ON DELETE RESTRICT ON UPDATE RESTRICT
, CONSTRAINT `fk_ROUTE_DELIVERY_VEHICLE`
FOREIGN KEY (`DELIVERY_VEHICLE_veh_ID`)
REFERENCES `VEHICLE` (`veh_ID`)
ON DELETE RESTRICT ON UPDATE RESTRICT
CREATE TABLE `ROUTE` (
`route_ID` INT NOT NULL,
`route_name` VARCHAR(45) NOT NULL,
`DELIVERY_VEHICLE_veh_ID` INT NOT NULL,
`DELIVERY_DRIVER_dr_ID` INT NOT NULL,
PRIMARY KEY (`route_ID`),
CONSTRAINT `fk_ROUTE_VEHICLE`
FOREIGN KEY (`DELIVERY_VEHICLE_veh_ID`)
REFERENCES `VEHICLE`(`veh_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_ROUTE_DRIVER`
FOREIGN KEY (`DELIVERY_DRIVER_dr_ID`)
REFERENCES `DRIVER`(`dr_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);

Syntax Error while creating a table in SQL

I am trying this query and despite several attempts, I am still getting a syntax error when I create Table Call. The other tables get created just fine.. I don't understand why
Code for Student
CREATE TABLE Student (
`student_id` int NOT NULL AUTO_INCREMENT,
`phone_number` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
`school` varchar(50) NOT NULL,
`class` varchar(50) NOT NULL,
PRIMARY KEY (`student_id`)
)
Table Stories
CREATE TABLE Stories (
`story_id` int NOT NULL AUTO_INCREMENT,
`story_name` varchar(50) NOT NULL,
`number_questions` int NOT NULL,
`file_name` varchar(100) NOT NULL,
PRIMARY KEY (`story_id`)
)
Table Questions
CREATE TABLE Questions (
`question_id` int NOT NULL,
`story_id` int NOT NULL,
`file_name` varchar(100) NOT NULL,
`concept_tested` varchar(100) NOT NULL,
`difficuly` varchar(50) NOT NULL,
`number_options` int NOT NULL,
`correct_answer` int NOT NULL,
`call_number` int NOT NULL,
PRIMARY KEY (`question_id`,`story_id`),
FOREIGN KEY (`story_id`)
REFERENCES `Stories`(`story_id`)
ON DELETE CASCADE
)
Table Call
CREATE TABLE Call (
`call_id` int NOT NULL AUTO_INCREMENT,
`student_id` int NOT NULL,
`story_id` int NOT NULL,
`question_id` int NOT NULL,
`call_number` int NOT NULL,
`total_number` int NOT NULL,
PRIMARY KEY (`call_id`),
FOREIGN KEY (`student_id`)
REFERENCES `Student`(`story_id`)
ON DELETE CASCADE,
FOREIGN KEY (`story_id`)
REFERENCES `Stories`(`student_id`)
ON DELETE CASCADE,
FOREIGN KEY (`question_id`)
REFERENCES `Stories`(`question_id`)
ON DELETE CASCADE
)
Here is my error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Call ( `call_id` int NOT NULL AUTO_INCREMENT, `student_id` int NOT NULL, ' at line 1
I have tried editing my code and checking it several times but the problem remains unsolved
I solved the problem,
It is:
CREATE TABLE `Call` (
`call_id` int NOT NULL AUTO_INCREMENT,
`student_id` int NOT NULL,
`story_id` int NOT NULL,
`question_id` int NOT NULL,
`call_number` int NOT NULL,
`total_number` int NOT NULL,
PRIMARY KEY (`call_id`),
FOREIGN KEY (`student_id`)
REFERENCES `Student`(`student_id`)
ON DELETE CASCADE,
FOREIGN KEY (`story_id`)
REFERENCES `Stories`(`story_id`)
ON DELETE CASCADE,
FOREIGN KEY (`question_id`)
REFERENCES `Questions`(`question_id`)
ON DELETE CASCADE
)
I found that these conditions must be satisfied to not get error 150:
The two tables must be ENGINE=InnoDB. (can be others: ENGINE=MyISAM works too)
The two tables must have the same charset.
The PK column(s) in the parent table and the FK column(s) must be the same data type.
The PK column(s) in the parent table and the FK column(s), if they have a define collation type, must have the same collation type;
If there is data already in the foreign key table, the FK column value(s) must match values in the parent table PK columns.
And the child table cannot be a temporary table.
Hope this helps.
Try like this
CREATE TABLE Questions (
`call_id` int NOT NULL AUTO_INCREMENT,
`student_id` int NOT NULL,
`story_id` int NOT NULL,
`question_id` int NOT NULL,
`call_number` int NOT NULL,
`total_number` int NOT NULL,
PRIMARY KEY (`call_id`),
FOREIGN KEY (`student_id`)
REFERENCES `Student`(`story_id`)
ON DELETE CASCADE,
FOREIGN KEY (`story_id`)
REFERENCES `Stories`(`student_id`)
ON DELETE CASCADE,
FOREIGN KEY (`question_id`)
REFERENCES `Stories`(`question_id`)
ON DELETE CASCADE
)ENGINE=MYISAM CHARACTER SET UTF8;
FIDDLE DEMO
Take a look at here

ERROR 1215: Cannot add foreign key constraint when using ON DELETE SET NULL

I'm trying to create the following tables in MySQL. The first one:
CREATE TABLE IF NOT EXISTS address(
address_id INT NOT NULL AUTO_INCREMENT,
address_region VARCHAR(10) NOT NULL,
address_country VARCHAR(20) NOT NULL,
address_city VARCHAR(30) NOT NULL,
PRIMARY KEY(address_id))ENGINE = InnoDB;
I created it successfully,but when I try to create another table as following
CREATE TABLE IF NOT EXISTS spot(
spot_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
spot_address INT(11) NOT NULL,
spot_name VARCHAR(50) NOT NULL,
spot_desc VARCHAR(500) DEFAULT ' ',
spot_speadd VARCHAR(100) NOT NULL,
spot_viewtime INT DEFAULT 0,
FOREIGN KEY(spot_address)
REFERENCES address(address_id)
ON DELETE SET NULL
ON UPDATE SET NULL);
I get an error:
ERROR 1215 (HY000): Cannot add foreign key constraint
Why is this create table statement failing?
You have NOT NULL constraint on spot_address in the spot table and then try to set it to NULL on delete or update in the parent table address.
You can try this:
CREATE TABLE IF NOT EXISTS address
(
address_id INT NOT NULL AUTO_INCREMENT,
address_region VARCHAR(10) NOT NULL,
address_country VARCHAR(20) NOT NULL,
address_city VARCHAR(30) NOT NULL,
PRIMARY KEY(address_id)
)ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS `spot`
(
`spot_id` INT(11) NOT NULL AUTO_INCREMENT,
`spot_address` INT(11) NOT NULL,
`spot_name` VARCHAR(50) NOT NULL,
`spot_desc` VARCHAR(500) DEFAULT ' ',
`spot_speadd` VARCHAR(100) NOT NULL,
`spot_viewtime` INT(11) DEFAULT '0',
PRIMARY KEY (`spot_id`),
KEY `spot_address` (`spot_address`),
CONSTRAINT `spot_ibfk_1` FOREIGN KEY (`spot_address`) REFERENCES `address` (`address_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

mysql stored procedure not accepting Foreign key Constraint

I had created a table using StoredProcedure in mysql and i had applied primary key constraint also when i tried to apply foreign key constraint..iam getting error 'Cant create table dbname.tablename'..and here is my Simple StoredProcedure
delimiter |
CREATE PROCEDURE SP_CREATE_TABLE_SAMP()
BEGIN
CREATE TABLE anan_user (
user_id bigint(15) NOT NULL AUTO_INCREMENT,
name varchar(70) DEFAULT NULL,
last_name varchar(70) DEFAULT NULL,
gender varchar(10) DEFAULT NULL,
username varchar(40) DEFAULT NULL,
password varchar(40) DEFAULT NULL,
dateofbirth date DEFAULT NULL,
email varchar(100) DEFAULT NULL,
phone varchar(25) DEFAULT NULL,
mobile varchar(25) DEFAULT NULL,
address varchar(250) DEFAULT NULL,
pincode varchar(15) DEFAULT NULL,
state_id bigint(15) DEFAULT NULL,
district_id bigint(15) DEFAULT NULL,
constituency_id bigint(15) DEFAULT NULL,
profile_Img varchar(100) DEFAULT NULL,
is_pwd_changed varchar(10) DEFAULT 'false',
registered_time timestamp NULL DEFAULT NULL,
updated_date timestamp NULL DEFAULT NULL,
PRIMARY KEY ( user_id )
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
END;
how to give foreign key constraints in stored procedures?
Check your syntax against the MySQL reference on foreign key constraints. It would also be helpful for you to post what you tried, and how you're trying to set the foreign key constraint.
An example constraint definition is:
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;
The relevant syntax is the FOREIGN KEY (parent_id) REFERENCES parent(id).