Error 1072: Key Column `course_id` does not exist - mysql

I'm trying to make a simple table with foreign keys but it seems there's a problem with the id. I've tried everything but still comes up with an error message please help. Thanks.
CREATE TABLE `staff_info`(
`staff_id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`office_add` VARCHAR(45) NOT NULL,
`email` INT NULL,
`changed` TIMESTAMP,
PRIMARY KEY (`staff_id`),
FOREIGN KEY(`course_id`)
REFERENCES `course_info`(`course_id`)
)
ENGINE = InnoDB;
CREATE TABLE `course_info`(
`course_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`module` VARCHAR(45) NOT NULL,
`StartDate` TIMESTAMP,
`EndDate` TIMESTAMP,
`course_Update` TIMESTAMP,
PRIMARY KEY(`course_id`),
FOREIGN KEY(`student_id`)
REFERENCES student(`student_id`)
)
ENGINE = InnoDB;
CREATE TABLE `student` (
`student_id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`end_name` VARCHAR(45) NOT NULL,
`gender` VARCHAR(45) NOT NULL,
`email` VARCHAR(25) NULL,
`phone` VARCHAR(20) NOT NULL,
`student_update`TIMESTAMP,
PRIMARY KEY(`student_id`)
);

Your table destruct and creation orders are invalid
CREATE TABLE `student` (
`student_id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`end_name` VARCHAR(45) NOT NULL,
`gender` VARCHAR(45) NOT NULL,
`email` VARCHAR(25) NULL,
`phone` VARCHAR(20) NOT NULL,
`student_update`TIMESTAMP,
PRIMARY KEY(`student_id`)
);
CREATE TABLE `course_info`(
`course_id` INT NOT NULL AUTO_INCREMENT,
`student_id` INT,
`name` VARCHAR(45) NOT NULL,
`module` VARCHAR(45) NOT NULL,
`StartDate` TIMESTAMP,
`EndDate` TIMESTAMP,
`course_Update` TIMESTAMP,
PRIMARY KEY(`course_id`),
FOREIGN KEY(`student_id`)
REFERENCES student(`student_id`)
)
CREATE TABLE `staff_info`(
`staff_id` INT NOT NULL AUTO_INCREMENT,
`course_id` INT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`office_add` VARCHAR(45) NOT NULL,
`email` INT NULL,
`changed` TIMESTAMP,
PRIMARY KEY (`staff_id`),
FOREIGN KEY(`course_id`)
REFERENCES `course_info`(`course_id`)
)

set CREATE TABLE student before CREATE TABLE course_info
you use student_id when does not exist studen,
and use course_id when does not exist course

In your SQL Query for TABLE student, you are missing
ENGINE = InnoDB
Because course table is creating FOREIGN KEY from table student.
Also you need to create tables in the order.
student
course_info
staff_info

Related

Syntax error creating a table in my database

I'm getting error after trying to create a table for my data base
This is my script
CREATE TABLE `users` (
`id` int PRIMARY KEY NOT NULL AUTO_INCREMENT,
`user_role_id` int NOT NULL,
`first_name` varchar(250) NOT NULL,
`last_name` varchar(250) NOT NULL,
`name` varchar(250),
`email` varchar(250) NOT NULL,
`dni_type` varchar(32) NOT NULL,
`dni` int NOT NULL,
`phone` int,
`address` varchar(250),
`city` varchar(125),
`state` varchar(125),
`country` varchar(32),
`zip` varchar(32),
`created_at` datetime,
`created_by` int,
`updated_at` datetime,
`updated_by` int,
`deleted_at` datetime,
`deleted_by` int
);
I'm getting
#1064 - Something is wrong in your syntax near 'CREATE TABLE `users` (
`id` int PRIMARY KEY NOT NULL AUTO_INCREMENT,
`u...' on line 2
Why is the error generated, and how can I avoid it in the future?
CREATE TABLE `users` (
`id` int PRIMARY KEY NOT NULL AUTO_INCREMENT
Don't use `
CREATE TABLE `users` (
id int PRIMARY KEY NOT NULL AUTO_INCREMENT

MySQL create table with condition on column

I want to create a customer table and want to restrict the SponsorID column not null if CustomerType=Guest.
CREATE TABLE `customers` (
`CustomerID` INT(11) NOT NULL AUTO_INCREMENT,
`CustomerType` VARCHAR(45) NOT NULL,
`WM_Status` TINYINT(1) NOT NULL,
`SponsorID` INT(11) NULL,
`F_Name` VARCHAR(45) NOT NULL,
`L_Name` VARCHAR(45) NOT NULL,
`Street` VARCHAR(80) NULL DEFAULT NULL,
`City` VARCHAR(45) NULL DEFAULT NULL,
`State` CHAR(4) NULL DEFAULT NULL,
`Zip` INT(11) NULL DEFAULT NULL,
`WorkNum` VARCHAR(45) NULL DEFAULT NULL,
`HomeNum` VARCHAR(45) NOT NULL,
`DOB` DATE NOT NULL,
PRIMARY KEY (`CustomerID`),
UNIQUE INDEX `CustomerID_UNIQUE` (`CustomerID` ASC) ,
INDEX `SponsorID` (`SponsorID` ASC) ,
CONSTRAINT `SponsorID`
FOREIGN KEY (`SponsorID`)
REFERENCES `project1_team3`.`customers` (`CustomerID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
Please suggest.
Any help would be appreciated.
Thanks,
You could add a constraint like this one.
CONSTRAINT SponsorIDCheck CHECK ((`CustomerType` != 'Guest') OR (`CustomerType` = 'Guest' AND `SponsorID` IS NOT NULL))

Cannot add foreign key SQL fiddle

I'm having issues with the foriegn key in my table. If any can point out the error, please do, I have no idea what it is.
CREATE TABLE IF NOT EXISTS `user_account` (
`accountID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(20) NOT NULL,
`typeID` int(10) UNSIGNED NOT NULL,
`email` VARCHAR(100) NOT NULL,
`password` VARCHAR(100) NOT NULL,
PRIMARY KEY (`accountID`),
FOREIGN KEY (`typeID`) REFERENCES account_type(`typeID`)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `account_type`(
`typeID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`typeName` VARCHAR (20) NOT NULL,
`description` VARCHAR (255),
PRIMARY KEY (`typeID`),
) ENGINE = InnoDB;
As far as I'm concerned, my datatypes are fine.
Thanks for the help.
Just do them in the opposite order after you fix the TYPO extra comma in the first table below.
CREATE TABLE IF NOT EXISTS `account_type`(
`typeID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`typeName` VARCHAR (20) NOT NULL,
`description` VARCHAR (255),
PRIMARY KEY (`typeID`)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `user_account` (
`accountID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(20) NOT NULL,
`typeID` int(10) UNSIGNED NOT NULL,
`email` VARCHAR(100) NOT NULL,
`password` VARCHAR(100) NOT NULL,
PRIMARY KEY (`accountID`),
FOREIGN KEY (`typeID`) REFERENCES account_type(`typeID`)
) ENGINE = InnoDB;

Not able to create a Foreign Constraint in Mysql

Here is the script I am trying to run
CREATE TABLE IF NOT EXISTS `store` (
`store_id` INT NOT NULL AUTO_INCREMENT,
`store_name` VARCHAR(1024) NOT NULL,
`store_user` INT NOT NULL,
`store_address` INT NOT NULL,
`store_type` INT NOT NULL,
`created_date` DATETIME NOT NULL,
`updated_date` DATETIME NOT NULL,
PRIMARY KEY (`store_id`)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `store_address` (
`address_id` INT NOT NULL AUTO_INCREMENT,
`address_line_1` VARCHAR(1024) NOT NULL,
`address_line_2` VARCHAR(1024) NOT NULL,
`address_line_3` VARCHAR(1024) NULL,
`city` VARCHAR(45) NOT NULL,
`locality` VARCHAR(100) NOT NULL,
`pincode` CHAR(6) NOT NULL,
`latitude` DECIMAL(8,6) NULL,
`longitude` DECIMAL(9,6) NULL,
`state` VARCHAR(45) NOT NULL,
`created_date` DATETIME NOT NULL,
`updated_date` DATETIME NOT NULL,
PRIMARY KEY (`address_id`),
CONSTRAINT `FK_STR_STR_ADR`
FOREIGN KEY (`address_id`)
REFERENCES `store` (`store_address`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I get this error - Error Code: 1215. Cannot add foreign key constraint
No clue what is wrong with the DDL
Two issues:
1. use unsigned int for the primary keys (Edit: this is a good idea but was not the problem)
2. the store.store address must be a key in the store table in order to use it in a foreign key constraint in the store_address table
CREATE TABLE IF NOT EXISTS `store` (
`store_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`store_name` VARCHAR(1024) NOT NULL,
`store_user` INT NOT NULL,
`store_address` int(10) unsigned NOT NULL DEFAULT 0,
`store_type` INT NOT NULL,
`created_date` DATETIME NOT NULL,
`updated_date` DATETIME NOT NULL,
PRIMARY KEY (`store_id`),
KEY (`store_address`)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `store_address` (
`address_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`address_line_1` VARCHAR(1024) NOT NULL,
`address_line_2` VARCHAR(1024) NOT NULL,
`address_line_3` VARCHAR(1024) NULL,
`city` VARCHAR(45) NOT NULL,
`locality` VARCHAR(100) NOT NULL,
`pincode` CHAR(6) NOT NULL,
`latitude` DECIMAL(8,6) NULL,
`longitude` DECIMAL(9,6) NULL,
`state` VARCHAR(45) NOT NULL,
`created_date` DATETIME NOT NULL,
`updated_date` DATETIME NOT NULL,
PRIMARY KEY (`address_id`),
CONSTRAINT `FK_STR_STR_ADR` FOREIGN KEY (`address_id`) REFERENCES `store` (`store_address`)
)ENGINE = InnoDB;

Creating a MySQL Table but get error

I get
errnno 150: InnoDB Documentation
Supports transactions, row-level locking, and foreign keys
Here is the SQL:
-- Table structure for table `Serving_Info`
--
CREATE TABLE IF NOT EXISTS `Serving_Info` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`Food_Value` varchar(40) NOT NULL,
`Food_name` varchar(40) NOT NULL,
`Served_On` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`Oncall` varchar(128) NOT NULL,
Foreign key(`Oncall`) REFERENCES `employees`(`name`),
Foreign key(`Food_name`) REFERENCES `Foods`(`Food_name`),
PRIMARY KEY (`Oncall`,`Food_name`, `Served_On`)
);
Any idea what is causing the error? I have tried making id part of the primary key but that isn't solving the problem either.
Here are the statements for the tables I am referencing:
CREATE TABLE IF NOT EXISTS `employees` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
`email` varchar(64) NOT NULL,
`phone_number` varchar(16) NOT NULL,
`username` varchar(16) NOT NULL,
`password` varchar(32) NOT NULL,
`confirmcode` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id_user`)
);
--
-- Table structure for table `Foods`
--
CREATE TABLE IF NOT EXISTS `Foods` (
`Food_name` varchar(40) NOT NULL,
`CostPerRefill` double NOT NULL,
PRIMARY KEY (`Food_name`)
);
I just tried the below but I get the same error:
--
-- Table structure for table `Serving_Info`
--
CREATE TABLE IF NOT EXISTS `Serving_Info` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`Food_Value` varchar(40) NOT NULL,
`Food_name` varchar(40) NOT NULL,
`Served_On` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`Oncall` varchar(128),
Foreign key(`Oncall`) REFERENCES `employees`(`name`),
Foreign key(`Food_name`) REFERENCES `Foods`(`Food_name`),
PRIMARY KEY (`id`),
UNIQUE (`Oncall`,`Food_name`, `Served_On`)
);
Your problem is that the reference is to employees.name rather than employees.id_user. Try this:
CREATE TABLE IF NOT EXISTS `Serving_Info` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`Food_Value` varchar(40) NOT NULL,
`Food_name` varchar(40) NOT NULL,
`Served_On` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`Oncall` int,
PRIMARY KEY (`id`),
Foreign key(`Oncall`) REFERENCES `employees`(`id_user`),
Foreign key(`Food_name`) REFERENCES `Foods`(`Food_name`),
UNIQUE (`Oncall`,`Food_name`, `Served_On`)
);
Otherwise, change the employees table so name has an index:
CREATE TABLE IF NOT EXISTS `employees` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL ,
`email` varchar(64) NOT NULL,
`phone_number` varchar(16) NOT NULL,
`username` varchar(16) NOT NULL,
`password` varchar(32) NOT NULL,
`confirmcode` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id_user`),
KEY (name)
);
In practice, if you are going to use name this way, you should probably make that unique rather than just key.