Cannot add foreign key constraint (mysql-error-1215 - mysql

this is the relational table where am getting my foreign keys from
CREATE TABLE EMPLOYEE(
ENUM DECIMAL(12) NOT NULL,
FNAME VARCHAR(50) NOT NULL,
INITIALS VARCHAR(5) NULL,
LNAME VARCHAR(50) NOT NULL,
DOB DATE NULL,
BLDG DECIMAL(3) NOT NULL,
STREET VARCHAR(50) NOT NULL,
SUBURB VARCHAR(50) NOT NULL,
STATE VARCHAR(5) NOT NULL,
ZIPCODE DECIMAL(4) NOT NULL,
CONSTRAINT EMPLOYEE_PKEY PRIMARY KEY(ENUM) );
and i want to create a new table called EMPNAME but have error when adding my foreign key constraint
CREATE TABLE EMPNAME(
ENUM DECIMAL(12) NOT NULL,
FNAME VARCHAR(50) NOT NULL,
INITIALS VARCHAR(5) NOT NULL,
LNAME VARCHAR(50) NOT NULL,
CONSTRAINT EMPNAME_PKEY PRIMARY KEY(ENUM),
CONSTRAINT EMPNAME_FKEY FOREIGN KEY(ENUM, FNAME, INITIALS, LNAME) REFERENCES EMPLOYEE(ENUM, FNAME, INITIALS, LNAME)
);
i dont get errors if i only set the primary key enum as foreign key but when i make all of the attributes foreign keys, i keep getting error.
thanks

Your reference columns must have indexes in order to be used as foreign keys, so add index(es) on these columns in your employee table:
CREATE TABLE EMPLOYEE (
ENUM DECIMAL (12) NOT NULL,
FNAME VARCHAR (50) NOT NULL,
INITIALS VARCHAR (5) NULL,
LNAME VARCHAR (50) NOT NULL,
DOB DATE NULL,
BLDG DECIMAL (3) NOT NULL,
STREET VARCHAR (50) NOT NULL,
SUBURB VARCHAR (50) NOT NULL,
STATE VARCHAR (5) NOT NULL,
ZIPCODE DECIMAL (4) NOT NULL,
CONSTRAINT EMPLOYEE_PKEY PRIMARY KEY (ENUM),
INDEX `FKEYS` (ENUM, FNAME, INITIALS, LNAME)
);
Then you will be able to add your constraint into second table.

Related

"VARCHAR" is not valid at this position, expecting EOF, ';'

CREATE TABLE Students
(
stud_id INT(10) UNSIGNED not null,
stud_name VARCHAR(30) not null,
stud_phone CHAR(11) not null,
stud_date_of_birth DATE not null,
stud_city CHAR(30) not null,
stud_address CHAR(100) not null,
stud_postcode SMALLINT UNSIGNED not null,
PRIMARY KEY (stud_id)
);
CREATE TABLE Subjects
(
subj_code CHAR(6) not null,
subj_title VARCHAR(30),
PRIMARY KEY (subject_code)
);
CREATE TABLE Subj_Enrolment
(
stud_id INT(10) UNSIGNED not null,
subj_code CHAR(6) not null,
semester SMALLINT UNSIGNED not null,
year YEAR not null,
comment VARCHAR(100) not null,
PRIMARY KEY (stud_id, subj_code, semester, year),
FOREIGN KEY (stud_id) REFERENCES Students(stud_id),
FOREIGN KEY (subj_id) REFERENCES Subjects(subj_code)
);
CREATE TABLE Grades
(
stud_id INT(10) UNSIGNED not null,
subj_code CHAR(10) not null,
semester SMALLINT UNSIGNED not null,
year YEAR not null,
grade CHAR(2) not null,
PRIMARY KEY (stud_id, subj_code, semester, year),
FOREIGN KEY (stud_id) REFERENCES Students(stud_id),
FOREIGN KEY (subj_code) REFERENCES Subjects(subj_code)
);
ALTER TABLE Students
ADD COLUMN gender CHAR(1) CHECK(gender='m' OR gender='f');
ALTER TABLE Subj_Enrolments
DROP COLUMN comment VARCHAR(100);
So on Line 52 or the last line where "DROP COLUMN comment VARCHAR(100);" the error that came up is written on the title, but I do not understand why is this wrong, what mistake am did I cause? I did use VARCHAR for when I am creating my tables but when I want to alter it the first alteration is correct but the second one is wrong
You must not specify column type with a DROP, only the column name, so :
ALTER TABLE Subj_Enrolments
DROP COLUMN comment;
https://dev.mysql.com/doc/refman/8.0/en/alter-table.html

"PropertyID does not exist in the table

Struggling to see what the issue is with adding a foreign key to the Agents table. I'm getting the error "PropertyID does not exist in the table". Here is the relevant code:
CREATE TABLE Properties (
PropertyID INT(8),
Address VARCHAR(50) NOT NULL,
Level TINYINT(1) NOT NULL,
PropertyValue INT(10) NOT NULL,
CONSTRAINT PRIMARY KEY (PropertyID)
);
CREATE TABLE Agents (
AgentID INT(8) AUTO_INCREMENT,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
PropertiesManaged VARCHAR (50) NOT NULL,
PhoneNumber VARCHAR (12),
CONSTRAINT PRIMARY KEY (AgentID),
CONSTRAINT PropertiesManaged FOREIGN KEY (PropertyID) REFERENCES Properties (PropertyID)
);
You have to define the column:
CREATE TABLE Agents (
AgentID INT(8) AUTO_INCREMENT,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
PropertiesManaged VARCHAR (50) NOT NULL,
PhoneNumber VARCHAR (12),
PropertyId INT(8),
----^
CONSTRAINT PRIMARY KEY (AgentID),
CONSTRAINT PropertiesManaged FOREIGN KEY (PropertyID) REFERENCES Properties (PropertyID)
);

data truncation error and int data types

CREATE TABLE Customer (
customerid varchar(10) NOT NULL,
FirstName varchar(20) NOT NULL,
LastName varchar(20) NOT NULL,
StreetAddress varchar (30) NOT NULL,
City varchar (20)NOT NULL,
State varchar (20) NOT NULL,
Zip int(5) NOT NULL,
Hphone int (10) NOT NULL,
Mphone int (10) NOT NULL,
Ophone int (10) NOT NULL,
PRIMARY KEY (customerid));
CREATE TABLE OrderTable (
donutorderid varchar(10) NOT NULL,
customerid varchar (10) NOT NULL,
oderdate datetime (6) NOT NULL,
PRIMARY KEY (donutorderid)
);
CREATE TABLE Donut (
donutid varchar(10),
donutname varchar(20),
description varchar(30),
unitprice numeric,
PRIMARY KEY (donutid)
);
CREATE TABLE OrderLine (
donutorderid varchar (30),
donutid varchar (30),
qty int (10),
PRIMARY KEY (donutorderid,donutid)
);
ALTER TABLE OrderTable ADD INDEX checks (customerid),
ADD CONSTRAINT checks FOREIGN KEY (customerid) REFERENCES Customer (customerid);
ALTER TABLE OrderLine ADD INDEX has (donutorderid),
ADD CONSTRAINT has FOREIGN KEY (donutorderid) REFERENCES OrderTable (donutorderid);
ALTER TABLE OrderLine ADD INDEX available_in (donutid),
ADD CONSTRAINT available_in FOREIGN KEY (donutid) REFERENCES Donut (donutid);
create view CustInfo
AS Select customerid, concat(FirstName,LastName) as FullName from customer;
insert into customer
(customerid, FirstName, LastName, StreetAddress,
City, State, Zip, Hphone, Mphone, Ophone)
values ('123','John','Doe', 'one hoover lane', 'las vegas',
'nevada', 89104, 702555122, 702441111, 702332222);
Data truncation: Out of range value for column 'Hphone' at row 1
When I have 9 digits in my telephone e.g. 702551212 vs 7025551212 it works and Hphone, Mphone, Ophone all have int (10).
You have declared your phone numbers to be integers. That is a very, very bad idea. Declare them to be strings:
CREATE TABLE Customer (
customerid varchar(10) NOT NULL,
FirstName varchar(20) NOT NULL,
LastName varchar(20) NOT NULL,
StreetAddress varchar (30) NOT NULL,
City varchar (20)NOT NULL,
State varchar (20) NOT NULL,
Zip char(5) NOT NULL,
Hphone varchar(32) NOT NULL,
Mphone varchar(32) NOT NULL,
Ophone varchar(32) NOT NULL,
PRIMARY KEY (customerid)
);
It is unclear to me why the phone numbers -- and other columns -- are declared NOT NULL. Not everyone has three phone numbers. In fact, some people have fewer and some more, suggesting that the phone numbers should really be in a separate table.
I also made the zip code a string. After all, leading zeros are very important for zip codes.
But, for your immediate problem, then use single quotes to put the numbers in as strings:
insert into customer(customerid, FirstName, LastName, StreetAddress,
City, State, Zip, Hphone, Mphone, Ophone)
values ('123','John','Doe', 'one hoover lane', 'las vegas',
'nevada', '89104', '702555122', '702441111', '702332222');

Creating tables with foreign keys

I am creating tables where I have foreign keys. This is part of the statements. For some reason I cannot get it to work.
What am i doing wrong?
CREATE TABLE Doctor (
NPI NUMBER PRIMARY KEY NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
PHONE NUMBER NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
CITY VARCHAR(20) NOT NULL );
CREATE TABLE Patient (
SSN NUMBER PRIMARY KEY NOT NULL,
INSURANCE_POLICY_ID NUMBER NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
DOB DATE NOT NULL,
PHONE NUMBER NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL,
FOREIGN KEY (INSURANCE_POLICY_ID) REFERENCES INSURANCE (INSURANCE_POLICY_ID));
You have tagged your question with SQL Server and MySql both, in both of these RDBMS there is no data type called Number but there is a data type to store numbers it is called INT or INTEGER.
Therefore your table definitions should be as follow:
CREATE TABLE Doctor (
NPI INT NOT NULL PRIMARY KEY ,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
PHONE INT NOT NULL, --<-- should be a varchar since most phone numbers have a leading zero
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL );
CREATE TABLE Patient (
SSN INT NOT NULL PRIMARY KEY,
INSURANCE_POLICY_ID INT NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
DOB DATE NOT NULL,
PHONE INT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL,
FOREIGN KEY (INSURANCE_POLICY_ID)
REFERENCES INSURANCE (INSURANCE_POLICY_ID));

error message "ORA-02270: no matching unique or primary key for this column-list"

I am having trouble with mapping things on separate tables of data. There will be tables for DEPARTMENT_LOCATIONS, DEPARTMENT, EMPLOYEE, and PROJECT.
So far all of the tables are created other that PROJECT, because I am receiving the error message for the title of my question.
Below is the information that I used to create the tables that were created without errors:
EMPLOYEE Table:
CREATE TABLE EMPLOYEE
(FNAME VARCHAR(25) NOT NULL,
MINIT CHAR(1),
LNAME VARCHAR(25) NOT NULL,
SSN NUMBER(10) NOT NULL,
BDATE DATE NOT NULL,
ADDRESS VARCHAR(30) NOT NULL,
SEX CHAR(1) NOT NULL,
SALARY DECIMAL(6,2) NOT NULL,
SUPERSSN NUMBER(10),
DNO NUMBER (1) NOT NULL,
PRIMARY KEY (SSN),
FOREIGN KEY (DNO) REFERENCES DEPT_LOCATIONS(DNUMBER));
DEPARTMENT_LOCATIONS Table:
CREATE TABLE DEPT_LOCATIONS
(DNUMBER NUMBER(1) NOT NULL,
DLOCATION VARCHAR(25) NOT NULL,
PRIMARY KEY (DNUMBER));
DEPARTMENT Table:
CREATE TABLE DEPARTMENT
(DNUMBER NUMBER(1) NOT NULL,
DNAME VARCHAR(25) NOT NULL,
MGRSTARTDATE DATE NOT NULL,
MGRSSN NUMBER(10) NOT NULL,
PRIMARY KEY (DNUMBER),
FOREIGN KEY (DNUMBER) REFERENCES DEPT_LOCATIONS(DNUMBER),
FOREIGN KEY (MGRSSN) REFERENCES EMPLOYEE(SSN));
Now when I enter the following information for the PROJECT Table, I receive the error message "ORA-02270: no matching unique or primary key for this column-list.)
CREATE TABLE PROJECT
(PNUMBER NUMBER(2,1) NOT NULL PRIMARY KEY,
PNAME VARCHAR(25) NOT NULL,
PLOCATION VARCHAR(25) NOT NULL,
DNUM NUMBER(1) NOT NULL,
FOREIGN KEY (PLOCATION) REFERENCES DEPT_LOCATIONS(DLOCATION),
FOREIGN KEY (DNUM) REFERENCES DEPT_LOCATIONS(DNUMBER));
Why is this error occurring and what do I need to do to correct it? Thank you.
You have to declare dlocation as unique, inorder to reference it in another table.
CREATE TABLE DEPT_LOCATIONS
(DNUMBER NUMBER(1) NOT NULL,
DLOCATION VARCHAR(25) UNIQUE,
PRIMARY KEY (DNUMBER));