mysql error 1072 - mysql

COMMIT WORK
;
DROP DATABASE IF EXISTS Campaign_Database;
CREATE DATABASE Campaign_Database;
USE Campaign_Database;
DROP TABLE IF EXISTS CONTRIBUTOR
;
DROP TABLE IF EXISTS CAMPAIGN
;
DROP TABLE IF EXISTS PLEDGE
;
CREATE TABLE CONTRIBUTOR
( CONTRIBUTOR_ID VARCHAR(50) primary key,
Address char(50) not null,
City char(50) not null,
State char(2) not null,
Zip INTEGER(6) not null,
Home_Phone char (15)
)ENGINE = InnoDB;
;
CREATE TABLE CAMPAIGN
( CAMPAIGN_ID VARCHAR(50) primary key,
Chair_First_Name char (50) not null,
Chair_Last_Name char (50) not null,
Launch_Date char (11) not null,
End_Date char (11) not null,
Fundraising_Goal INTEGER NOT NULL
)ENGINE = InnoDB;
;
CREATE table PLEDGE
(
FOREIGN KEY (CONTRIBUTOR_ID) REFERENCES CONTRIBUTOR (CONTRIBUTOR_ID),
FOREIGN KEY (CAMPAIGN_ID) REFERENCES CAMPAIGN (CAMPAIGN_ID),
Date_Pledged char(11) not null primary key,
Amount INTEGER NOT NULL,
PAID char(1) not null
CHECK (PAID in ('Y','N')),
Payment_Date char(11)
)ENGINE = InnoDB;
;
COMMIT WORK
;
This current is the mysql coding I am using for a finals assignment.
I have tried several methods but was not successful in creating the two foreign keys within the 'pledge' table.
I would be appreciated if I can at least know what went wrong. Thank you.

You need a CONTRIBUTOR_ID, CAMPAIGN_ID fields in the PLEDGE table.
CREATE table PLEDGE
(
CONTRIBUTOR_ID VARCHAR(50),
CAMPAIGN_ID VARCHAR(50),
FOREIGN KEY (CONTRIBUTOR_ID) REFERENCES CONTRIBUTOR (CONTRIBUTOR_ID),
FOREIGN KEY (CAMPAIGN_ID) REFERENCES CAMPAIGN (CAMPAIGN_ID),
Date_Pledged char(11) not null primary key,
Amount INTEGER NOT NULL,
PAID char(1) not null
CHECK (PAID in ('Y','N')),
Payment_Date char(11)
)ENGINE = InnoDB;
;
In this statement
FOREIGN KEY (CONTRIBUTOR_ID) REFERENCES CONTRIBUTOR (CONTRIBUTOR_ID),
The first CONTRIBUTOR_ID refers to the table where the contraint is defined and the second refers to the table where it is pointing to.

Related

MYSQL Foreign key incorrectly formed

Have an error where my first foreign key constraint is incorrectly formed. I'm waiting to hear back from my lecturer if she can spot the issue but thought I'd ask here
My SQL query is as follows
CREATE DATABASE rugby;
USE rugby;
CREATE TABLE address(
address_ID INT AUTO_INCREMENT NOT NULL,
address_name VARCHAR(50) NOT NULL,
PRIMARY KEY (address_ID)
) ENGINE=INNODB;
CREATE TABLE team(
team_ID INT AUTO_INCREMENT NOT NULL,
team_Name VARCHAR(25) NOT NULL,
team_Year INT NOT NULL,
PRIMARY KEY(team_ID)
) ENGINE=INNODB;
CREATE TABLE person(
person_ID INT AUTO_INCREMENT NOT NULL,
name VARCHAR(30) NOT NULL,
phone INT NOT NULL,
address_ID INT NOT NULL,
email VARCHAR(50),
photo BLOB,
PRIMARY KEY(person_ID),
FOREIGN KEY(address_ID) REFERENCES address(address_ID) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=INNODB;
I have followed how we were taught but can't spot the issue.
Any help appreciated
In table person you have defined address_ID as NOT NULL, but
when you define the FOREIGN KEY you set:
ON DELETE SET NULL
which contradicts the NOT NULL definition.

Database creation and double checking for mistakes

I created a database in one of my project but I'm not sure if I still doing it right. I'm still learning sql and I stuggle in some of the tables I make. The operations are the following.
I can add officer with it's assign division.
I have to check every division what their plan and their corresponding items with milestone(months) each year.
I can also check how many items per milestone(months) & vice Versa.
Tables are officer, division, plan, items, milestone, when I try to create this table in http://sqlfiddle.com I got an error of Can't create database 'db_9_85e4c'; database exists when I try to alter and create a foreign key in officer & division table.
Here's my tables:
Officer Table
create table officer
(
id int not null auto_increment
primary key,
name varchar(100) not null,
s_position varchar(50) null,
f_position varchar(100) not null,
constraint officer_name_uindex
unique (name),
constraint officer_position_uindex
unique (f_position),
constraint officer_division_division_id_fk
foreign key (id) references test.division (division_id)
on update cascade on delete cascade
)
;
Division Table
create table division
(
division_id int not null
primary key,
acronym varchar(50) not null,
sequence_id int not null,
decription varchar(50) not null,
constraint division_sequesnce_id_uindex
unique (sequence_id),
constraint division_acronym_uindex
unique (acronym),
constraint division_decription_uindex
unique (decription),
constraint division_ppmp_ppmp_id_fk
foreign key (division_id) references test.ppmp (ppmp_id)
on update cascade on delete cascade
)
;
Plan Table
create table ppmp
(
ppmp_id int not null auto_increment
primary key,
year varchar(50) not null
)
;
Items Table
create table items
(
id int not null auto_increment
primary key,
description varchar(100) not null,
unit varchar(50) not null,
quantity int not null,
budget double not null,
mode varchar(50) not null,
constraint items_milestone_id_fk
foreign key (id) references test.milestone (id)
on update cascade on delete cascade,
constraint items_ppmp_ppmp_id_fk
foreign key (id) references test.ppmp (ppmp_id)
on update cascade on delete cascade
)
;
Milestone Table
create table milestone
(
id int not null auto_increment
primary key,
january int null,
february int null,
march int null,
april int null,
may int null,
june int null,
july int null,
august int null,
september int null,
october int null,
november int null,
decemeber int null
)
;
Create the schema with the tables in this order
Milestone
ppmp
items
division
officer
the problem is happening because Officer have a foreing key to division_id but that table and column does not exists yet.

Syntax Error in MySQL - Same syntax worked in earlier table

I'm working on building a small database and am running into a syntax error on these two lines in my last table.
They're foreign keys and those lines didn't produce any errors in the tables where they originally appeared.
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
DONUT_ID VARCHAR(10) NOT NULL UNIQUE,
These two lines are the PK and FK in the table, and I think this is where I'm running into the problem because I used the same syntax as I did in earlier tables that aren't generating any errors.
Here is the full code for four tables:
CREATE TABLE CUSTOMER (
CUST_ID INT NOT NULL AUTO_INCREMENT UNIQUE,
CUST_LNAME VARCHAR(25) NOT NULL,
CUST_FNAME VARCHAR(25) NOT NULL,
CUST_INITIAL CHAR(1),
CUST_STREET_NO VARCHAR(6),
CUST_STREET_NAME VARCHAR(25),
CUST_APT_NO VARCHAR(10),
CUST_CITY VARCHAR(25),
CUST_STATE CHAR(2),
CUST_ZIP_CODE CHAR(5),
CUST_HOME_AC CHAR(3),
CUST_HOME_PHONE CHAR(8),
PRIMARY KEY (CUST_ID)
)ENGINE = InnoDB;
CREATE TABLE INVOICE (
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
CUST_ID INTEGER NOT NULL,
INV_DATE DATE NOT NULL,
SPECIAL_HANDLING VARCHAR(35),
PRIMARY KEY (INVOICE_ID),
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER(CUST_ID) ON UPDATE CASCADE
)ENGINE = InnoDB;
CREATE TABLE PRODUCT (
DONUT_ID VARCHAR(10) NOT NULL UNIQUE,
DONUT_NAME VARCHAR(25) NOT NULL,
DONUT_DESC VARCHAR(35) NOT NULL,
DONUT_PRICE DECIMAL(13,2) NOT NULL,
PRIMARY KEY (DONUT_ID)
)ENGINE = InnoDB;
CREATE TABLE INVOICE LINE ITEM (
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
DONUT_ID VARCHAR(10) NOT NULL UNIQUE,
DONUT_QTY INTEGER NOT NULL,
PRIMARY KEY (INVOICE_ID, DONUT_ID),
FOREIGN KEY (INVOICE_ID) REFERENCES INVOICE(INVOICE_ID) ON UPDATE CASCADE,
FOREIGN KEY (DONUT_ID) REFERENCES PRODUCT(DONUT_ID) ON UPDATE CASCADE
)ENGINE = InnoDB;
The fourth CREATE TABLE statement will fail because of the spaces in the table name:
CREATE TABLE INVOICE LINE ITEM ...
You should use a different name, without spaces. For example you could replace the spaces with underscores and use snake-case:
CREATE TABLE INVOICE_LINE_ITEM ...

Cannot add foreign key constraint in MySQL

MySQL:
CREATE SCHEMA IF NOT EXISTS otes_db;
USE otes_db;
CREATE TABLE IF NOT EXISTS school(
school_code CHAR(3),
school_desc VARCHAR(255) NOT NULL,
school_head INT UNSIGNED NOT NULL,
PRIMARY KEY(school_code),
FOREIGN KEY(school_head) REFERENCES user(user_id)
ON DELETE CASCADE
ON UPDATE CASCADE
)ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS user(
user_id INT UNSIGNED,
user_lname VARCHAR(255) NOT NULL,
user_fname VARCHAR(255) NOT NULL,
user_mname VARCHAR(255),
user_email VARCHAR(255) UNIQUE NOT NULL,
user_password VARCHAR(255) NOT NULL,
user_type ENUM('CEO', 'VP', 'HEAD', 'CHAIR', 'STUD') NOT NULL,
user_isActivated ENUM('Y', 'N') DEFAULT 'N',
PRIMARY KEY(user_id)
);
Based on this thread, I have checked for the following:
InnoDB Engine is set in school table
The referenced key is a primary key (user_id)
The data types are the same (INT UNSIGNED)
However, I'm still getting the error with the school_head and user_id. Anything I missed?
You need to create the user table first

"Key column doesn't exist in table" when trying to define FOREIGN KEY

I'm trying to setup a MySQL-Script that creates 5 tables. In three tables, there is a FOREIGN KEY, and for all three of them the same error appears:
Error Code: 1072. Key column ... doesn't exist in table
whereas ... are Gebaeude, Dept_Name and Mat_Nr
Here's the script
use cs261_24;
drop table if exists Professor;
drop table if exists Departement;
drop table if exists Gebaeude;
drop table if exists Student;
drop table if exists Pruefung;
CREATE TABLE Gebaeude (
Gebaeude VARCHAR(20) NOT NULL PRIMARY KEY,
Hauswart VARCHAR(20) NOT NULL,
Adresse VARCHAR(20) NOT NULL
)engine = innodb;
CREATE TABLE Professor (
P_ID INTEGER PRIMARY KEY CHECK (P_ID > 0),
P_Name VARCHAR(20) NOT NULL,
Dept_Name VARCHAR(20) NOT NULL,
Raum INTEGER UNIQUE CHECK (Raum > 0),
Tel INTEGER(10) UNIQUE CHECK (Tel > 210000000),
FOREIGN KEY (Gebaeude) REFERENCES Gebaeude (Gebaeude)
)engine = innodb;
CREATE TABLE Departement (
Dept_Name VARCHAR(20) NOT NULL PRIMARY KEY,
Vorsteher VARCHAR(20) NOT NULL
)engine = innodb;
CREATE TABLE Student (
Mat_Nr INTEGER(8) PRIMARY KEY CHECK (Mat_Nr > 0),
S_Name VARCHAR(20) NOT NULL,
Semester INTEGER CHECK(Semester > 0),
FOREIGN KEY (Dept_Name) REFERENCES Departement (Dept_Name)
)engine = innodb;
CREATE TABLE Pruefung (
Pr_ID INTEGER PRIMARY KEY CHECK(Pr_ID > 0),
Fach VARCHAR(20) NOT NULL,
Pruefer VARCHAR(20) NOT NULL,
Note FLOAT CHECK (Note >= 1 AND Note <= 6),
FOREIGN KEY (Mat_Nr) REFERENCES Student (Mat_Nr)
)engine = innodb;
Why? I work with MySQL Workbench, and I clearly see the created tables, plus the specific columns are marked as primary keys!
You are doing it wrong, take a look to this example
http://www.sqlfiddle.com/#!2/a86cf
your FK line should be more like this:
FOREIGN KEY (field_that_will_be_Fk) REFERENCES Table_to_reference (field_to_reference)
I.E=
CREATE TABLE Gebaeude (
Gebaeude VARCHAR(20) NOT NULL PRIMARY KEY,
Hauswart VARCHAR(20) NOT NULL,
Adresse VARCHAR(20) NOT NULL
)engine = innodb;
CREATE TABLE Professor (
Gebaeude_FK varchar(20) NOT NULL,
P_ID INTEGER PRIMARY KEY CHECK (P_ID > 0),
P_Name VARCHAR(20) NOT NULL,
Dept_Name VARCHAR(20) NOT NULL,
Raum INTEGER UNIQUE CHECK (Raum > 0),
Tel INTEGER(10) UNIQUE CHECK (Tel > 210000000),
FOREIGN KEY (Gebaeude_FK) REFERENCES Gebaeude (Gebaeude)
)engine = innodb;
You have to create the column in each of the tables before you can constrain them as a foreign key.
See this link for reference: Link