Using an ISA relationship in mysql - mysql

i currently have 3 entities. Employees, teachers, and custodians. All teachers and custodians are employees. All employees have an id, name, and email. I have tried connected custodians and teachers to Employees using an ISA relationship with primary and foreign keys. I am attempting to create the tables, so that when i create a "teacher" or a "custodian" the Employee table is also populated. I currently have only an id for teacher/custodian linked to the employee. am i doing this wrong? thank you!
CREATE TABLE employees(
id INT UNSIGNED,
first_name VARCHAR(30),
last_name VARCHAR(30),
email VARCHAR(60),
Primary Key (id));
CREATE TABLE teacher(
teach_id INT UNSIGNED,
)
CREATE TABLE teachemp(
id INT UNSIGNED,
teach_id INT UNSIGNED,
PRIMARY KEY(id,teach_id),
FOREIGN KEY (id) REFERENCES employees(id),
FOREIGN KEY (teach_id) REFERENCES teacher(teach_id)
)
In other words, when i create insert an employee and that employee is a teacher. i want the teacher table to also be populated. am i doing this correctly? or should i create a teacher and the employee table be populated?
Thank you

This is what you need...
Follow this Trigger Example for SQL.
and this Trigger for MYSQL

Related

Can a primary key of a subclass table be referenced as a foreign key?

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

SQL-linking two tables with a third table

I have two tables Students and Courses.
I have to create a mapping table to link these two tables with a set of information.
For eg: Mary Murphy from **Students ** table is to be linked with Courses table with web designing course that she is taking.
How can I do this??
Thank you in advance.
You would use a third table, studentCourses would be a reasonable name. It would look something like this:
create table studentCourses (
studentCourseId int auto_increment primary key,
studentId int,
courseId int,
constraint fk_studentcourses_student foreign key (studentId) references students (studentId),
constraint fk_studentcourses_course foreign key (courseId) references students (courseId)
);
You might include other information, such as the enrollment date.

Simple attendance list SQL schema architecture advice

I have a sport club presence list on paper which I have to bring online.
Basically, I have everything in handle except the SQL schema architecture.
I have never designed a DataBase and I'm not sure if it's the right way. I appreciate any help!
If the person is present we make a x with a pen on paper :)
To do to look the same like on the paper but in an app I try to develop a Java Vaadin App with Spring Data JPA-Hibernate.
On paper and in the future APP look like this:
And this is the MySQL schema:
create table person(
id int PRIMARY KEY AUTO_INCREMENT,
first_name varchar(20)
);
create table isPresent(
id int PRIMARY KEY AUTO_INCREMENT,
isPresent boolean
);
create table persons_isPresent(
person_id int,
isPresent_id int,
FOREIGN KEY (person_id)
REFERENCES person(id),
FOREIGN KEY (isPresent_id)
REFERENCES isPresent(id)
);
create table training(
id int PRIMARY KEY AUTO_INCREMENT,
person_id int,
isPresent_id int,
training_time datetime,
FOREIGN KEY (person_id)
REFERENCES person(id),
FOREIGN KEY (isPresent_id)
REFERENCES isPresent(id)
);
I have never designed a DataBase and I'm not sure if it's the right way. I appreciate any help!
It seems unnecessary to have ispresent ID's.
I would keep your Person table as is.
Create a Training table with an ID and time, and an TrainingAttendance table with a Training ID and a Person ID. If you want to check a person's attendance at a training, check if a record exists in TrainingAttendance with the Person's ID and the Training ID.
CREATE TABLE PERSON (
PERSON_ID INTEGER NOT NULL PRIMARY KEY,
FIRST_NAME VARCHAR(20) NOT NULL);
CREATE TABLE TRAINING (
TRAINING_ID INTEGER NOT NULL PRIMARY KEY
TRAINING_TIME DATETIME NOT NULL);
CREATE TABLE TRAINING_ATTENDANCE (
TRAINING_ID INTEGER NOT NULL,
PERSON_ID INTEGER NOT NULL,
FOREIGN KEY(PERSON_ID) REFERENCES PERSON(PERSON_ID),
FOREIGN KEY(TRAINING_ID) REFERENCES TRAINING(TRAINING_ID),
PRIMARY KEY(TRAINING_ID, PERSON_ID));
I think it might be cleaner to think about training sessions independently. Then you could achieve that with three tables [training]<-[attendee]->[person], where the attendee is a binding table. So maybe something like this:
create table training(
id int PRIMARY KEY AUTO_INCREMENT,
training_time datetime
);
create table person(
id int PRIMARY KEY AUTO_INCREMENT,
first_name varchar(20)
);
create table attendee(
id int PRIMARY KEY AUTO_INCREMENT,
person_id int,
training_id int,
FOREIGN KEY (person_id) REFERENCES person(id),
FOREIGN KEY (training_id) REFERENCES training(id)
);
I tried it out with some simple inserts/select and it might do the trick for you?
insert INTO person (first_name) VALUES ('Pavel');
insert INTO training (training_time) VALUES (NOW());
insert INTO attendee (person_id, training_id) VALUES (1,1);
select person.first_name, training.training_time
from attendee, person, training
where person.id = attendee.person_id
AND training.id = attendee.training_id;

need help creating a simple many-to-many relationship using mysql

So I got used to using doctrine so much that I forgot the standard mysql syntax, which I need for an exam.
I want to create a test database called 'youtube'
with an app_users table and an app_videos table.
In "real life" this would be a oneToMany relationship, but I need to
study to create a manyToMany relationship.
so its
"many app_users" -> have "many app_videos"
and the other way around.
so first I created a database;
CREATE DATABASE youtube;
use youtube;
then I created the users and videos table
CREATE TABLE app_users(
id INT AUTO_INCREMENT,
username VARCHAR(20),
password VARCHAR(40),
first_name VARCHAR(20),
last_name VARCHAR(20),
video_id INT,
PRIMARY KEY(id)
);
CREATE TABLE app_videos(
id INT AUTO_INCREMENT,
video_path VARCHAR(20),
description VARCHAR(20),
likes INT(20),
dislikes INT(20),
user_id INT,
PRIMARY KEY(id)
);
then I created a relationship table
CREATE TABLE vid_uid
(
id INT AUTO_INCREMENT,
video_id INT,
user_id INT,
PRIMARY KEY(id)
);
now what I need to do is somehow connect the columns to eachother but my syntax never works
I know I need to change this
ALTER TABLE app_users
ADD FOREIGN KEY(video_id)
REFERENCES app_videos(video_id);
to something that works :/
You need to change app_videos(video_id) to app_videos(id). Also you want to alter the table vid_uid (not app_users) since thats the one that as references to external keys:
ALTER TABLE vid_uid
ADD FOREIGN KEY(video_id)
REFERENCES app_videos(id);
You'll also need to do:
ALTER TABLE vid_uid
ADD FOREIGN KEY(user_id)
REFERENCES app_users(id);

SQL Statement to write table?

working on a social networking project. just have to put together the sql statement to create the tables and need a little help with how this would be done, and how the relationships would work between the tables.
drop table users;
drop table intrest;
drop table friendships;
create table users(
id INT,
Fname char(15),
Lname char(15),
email char(20),
street char(15),
state char(2),
zip INT,
age INT,
gender char (2),
phone INT,
Password char(15),
primary key (id);
create table Instrests(
id INT,
create table friendships(
this can be done with just three tables
so far i have this done.
drop table users;
drop table intrest;
drop table friendships;
create table users(
id INT,
Fname char(15),
Lname char(15),
email char(20),
street char(15),
state char(2),
zip INT,
age INT,
gender char (2),
phone INT,
User_password char(15),
primary key (id)
foriegn key (intrest_id) references intrest(id)
);
create table Intrests(
id INT,
description char(30),
Primary key (id),
foreign key (users_id) references users(id)
);
create table User_intrest(
foreign key (user_id) references user(id),
foreign key (intrest_id) references intrest(id)
);
create friendships(
User_1_id INT,
User_2_id INT,
status Char(20),
foreign key (user_id) references user(id)
);
It looks like you're asking how to create the Interests and Friendships tables? And how they relate?
First, you'll need to spell Interests the same in the drop and create statement. You're right that Interests will link back to the Users by the ID. You'll probably want both an Interests Table, with an InterestID and description, and a linking table, with both the UserID and InterestID. Otherwise, you'll have lots of duplicate interests listed, one for each user.
The friendships table may be just a linking table, linking two userIDs together.
Try to imagine the data you need, and create the tables based on that:
User - 1, name - Joe, other info...
User - 2, name - Kris, other info..
User - 3, name - Lee, other info...
Interest - 1, name - reading
Interest - 2, name - parasailing
Interest - 3, name - skimboarding
UserInterest - User 1, Interest 2
UserInterest - User 1, Interest 3
UserInterest - User 2, Interest 2
Friendship - User 1, User 2
That tells you that Joe and Kris are friends and they both like parasailing, although Joe also likes skimboarding.
This doesn't tell you how to create the tables, but perhaps it will point you in the right direction. If this is a homework assignment, and it looks like it, you still want to do the work yourself.
You will need a an intermediate table between Users and Interests if you plan on having a many to many relationship between the two tables.
Also for Friendship, I would recomend a intermediate table between Friendship and Users as well, with that table having a link to a "Friendship Type" table to identify the friendship/relationship.
And if ThursdayGeek is correct and this is a homework assignment then I will stop here with my suggestions because there is more to the intermediate tables that you need to learn about before you implement them.
drop table users;
drop table intrest;
drop table friendships;
create table users(
id INT,
Fname char(15),
Lname char(15),
email char(20),
street char(15),
state char(2),
zip INT,
age INT,
gender char (2),
phone INT,
User_password char(15),
primary key (id)
foriegn key (intrest_id) references intrest(id)
);
create table Intrests(
id INT,
description char(30),
Primary key (id),
foreign key (users_id) references users(id)
);
create table User_intrest(
foreign key (user_id) references user(id),
foreign key (intrest_id) references intrest(id)
);
create friendships(
User_1_id INT,
User_2_id INT,
status Char(20),
foreign key (user_id) references user(id)
);
sorry not really good at this, anyways, this is what I have done for the sql statements so far.