This is what I have written:
create table employee (
emp_id int,
emp_first_name varchar(20),
emp_last_name varchar(20),
dept_id int,
email varchar(255),
primary key(emp_id, dept_id)
);
create table department(
dept_id int primary key,
dept_name varchar(255),
gm_no int,
foreign key(dept_id) references employee(dept_id)
);
but when I try to create the department table, it gives me the error(ERROR 1215 (HY000): Cannot add foreign key constraint)
all help is appreciated. thank you.
You have the logic backwards. The dept_id is defined in the department table as the primary key. The foreign key constraint belongs in the tables that reference the department:
create table department(
dept_id int primary key,
dept_name varchar(255),
gm_no int
);
create table employee (
emp_id int,
emp_first_name varchar(20),
emp_last_name varchar(20),
dept_id int,
email varchar(255),
primary key(emp_id, dept_id)
foreign key (dept_id) references department (dept_id)
);
Note that this data structure seems suspicious. I would expect a company called employee to have a unique id for empid. Period.
Employees are obvious in departments, but that could change over time. You would then want another table, say employeeDepartment that captured the changing relationship over time.
Your data model, for instance, would get confusing if an employee starts in department A, switches to B, and then returns to A.
The dept_id in the employee table is not key of any type. Make the dept_in column a KEY, UNIQUE KEY, or PRIMARY KEY on the employee. In your situation, just making it UNIQUE KEY is enough.
As #Sharofiddin said:
create table employee (
emp_id int,
emp_first_name varchar(20),
emp_last_name varchar(20),
dept_id int,
email varchar(255),
primary key(emp_id),
UNIQUE KEY (dept_id) /* add this line and run the query again */
);
create table department(
dept_id int primary key,
dept_name varchar(255),
gm_no int,
foreign key(dept_id) references employee(dept_id)
);
Thank you.
Related
I'm currently working on a mysql project for my class and I'm wondering if the PK-FK of a subclass table can be referenced as FK for another table instead of the PK of the parent class.
Let's say I have the subclass table employees written as:
CREATE TABLE employees (
Person_ID int PRIMARY KEY,
Designation varchar(50),
FOREIGN KEY (Person_ID) REFERENCES persons(Person_ID));
As for the parent class,
CREATE TABLE persons (
Person_ID int NOT NULL AUTO_INCREMENT,
Last_Name varchar(255),
Middle_Name varchar(255),
First_Name varchar(255),
PRIMARY KEY(Person_ID));
Let's say I want to create another table works whose FK references Person_ID from the subclass employees, not the parent class persons.
CREATE TABLE works (
project_ID PRIMARY KEY,
date_started date,
FOREIGN KEY (Person_ID) REFERENCES employees(Person_ID));
Thanks!
Yes, that's technically possible (I guess you tested that already) and is logically also OK in such a case as yours. In fact referencing persons would be wrong if, logically, only employees can take part in the "works on" relation -- the FK constraint wouldn't ensure that they're only employees.
It is no Problem, but you have always check if the id exist.
Ring connection can cause problems.
CREATE TABLE persons (
Person_ID int AUTO_INCREMENT,
Last_Name varchar(255),
Middle_Name varchar(255),
First_Name varchar(255),
PRIMARY KEY(Person_ID));
✓
CREATE TABLE employees (
Person_ID int PRIMARY KEY,
Designation varchar(50),
foreign key (Person_ID) REFERENCES persons(Person_ID));
✓
CREATE TABLE works (
project_ID Int PRIMARY KEY,
date_started date,
Person_ID int,
FOREIGN KEY (Person_ID) REFERENCES employees(Person_ID));
INSERT INTo persons VALUES (NULL,'A','B','C')
INSERT INTO employees VALUES (1,'test')
INSERT INTO works VALUES (1,NOW(),1)
db<>fiddle here
This is the task:
Create a table payment with attributes: p_id, c_id, staff_id, amount, payment_date where p_id is primary key and c_id and staff_id is foreign key which refer to table customer and staff respectively. Display the p_id and staff_id from the table payment where payment_date is 8 august 2020
Please help I am getting this error even though my code seems right to me
create table payment(
p_id int primary key,
amount int,
payment_date date,
c_id int,
staff_id int
);
create table customer(
cus_id int primary key,
c_id int,
foreign key (c_id) references payment(c_id)
);
ERROR 1215 (HY000): Cannot add foreign key constraint
You have your foreign key relationship backwards. payment should have FK to customer and staff.
create table customer (
c_id int primary key,
-- other columns
);
create table staff (
s_id int primary key
-- other columns
);
create table payment(
p_id int primary key,
amount int,
payment_date date,
c_id int,
staff_id int,
foreign key (c_id) references customer(c_id),
foreign key (staff_id) references staff(s_id)
);
For example, I'm creating two tables as shown below:
create table A (
department_id int,
college_id int,
constraint Pk_name primary key(department_id,college_id)
);
create table B (
student_name varchar(75),
department_id int,
college_id int,
foreign key(department_id,college_id) references A(Pk_name)
);
Can I write like this?
I don't think so because there's no way that the RDBMS could know whether PK_name is a column or a constraint name so I suggest if you stick with the usual :
create table A (
department_id int,
college_id int,
constraint Pk_name primary key(department_id,college_id)
);
create table B (
student_name varchar(75),
department_id int,
college_id int,
foreign key(department_id,college_id) references A(department_id,college_id)
);
I will update the answer once I find an other answer .
My two empty tables:
CREATE TABLE person (
person_id SMALLINT UNSIGNED,
fname VARCHAR(20),
lname VARCHAR(20),
gender ENUM('m', 'f'),
birth_date DATE,
street VARCHAR(30),
city VARCHAR(20),
state VARCHAR(20),
country VARCHAR(20),
postal_code VARCHAR(20),
CONSTRAINT pk_person PRIMARY KEY (person_id)
);
create TABLE favorite_food (
person_id SMALLINT UNSIGNED,
food VARCHAR(20),
CONSTRAINT pk_favorite_food PRIMARY KEY (person_id, food),
CONSTRAINT fk_fav_food_person_id FOREIGN KEY (person_id) REFERENCES person (person_id)
);
Needed modification:
ALTER TABLE person MODIFY person_id SMALLINT UNSIGNED AUTO_INCREMENT;
Result:
ERROR 1833 (HY000): Cannot change column 'person_id': used in a
foreign key constraint 'fk_fav_food_person_id' of table
'tom.favorite_food'
Why is this and is there a way around this without dropping the tables and redefining them?
This is probably because there already is data in person.person_id in any rows (NOT NULL). You can circumvent this by disabling foreign key checks
I believe the best way to handle this is to drop the foreign key reference from favorite_food, alter the column in person and then recreate the foreign key reference. That will properly recreate the index on which the key depends.
I have to create a number of tables for my project and each one of them has a foreign key besides their primary key. I know how to create a table on putty without the foreign key but if they are all depends on each other, then how to create them individually ?
For example, if I want to create table A, B and C
CREATE TABLE PROFESSOR
(
SSN numeric(9) primary key,
PNAME varchar(20),
CITY varchar(20),
STREETADDRESS varchar(50),
STATE char(2),
ZIP numeric(5),
AREACODE numeric(3),
PHONENUMBER numeric(7),
SEX char(1),
TITLE char(4),
SALARY float(9),
foreign key (DNUM) references DEPARTMENT(DNUM)
);
CREATE TABLE DEPARTMENT
(
DNUM numeric(1) primary key,
DNAME varchar(20),
DPHONE numeric(10),
OFFICELOCATION varchar(20),
foreign key (CWID) references STUDENT(CWID)
);
CREATE TABLE STUDENT
(
CWID numeric(9) primary key,
FNAME varchar(20),
LNAME varchar(20),
SADDRESS varchar(50),
SPHONE numeric(10),
foreign key (DNUM) references DEPARTMENT(DNUM)
);
I'm using Putty to create table and I have to run them separately. The thing is if I run the scrip separately for each table, it's going to give me an error, because the table that has the foreign key does not create yet. How am I going to fix it ? and is there a work around way on solving this problem ? Thank you.
If I run the script for creating table separately, it will give me an error