new to sql/mysql cant figure out foriegn keys - mysql

CREATE DATABASE FINAL_PROJECT;
USE FINAL_PROJECT;
CREATE TABLE MOVIE_RATING
(RATING_ID VARCHAR (1) PRIMARY KEY,
RATING VARCHAR(4));
CREATE TABLE MOVIE
(MOVIE_ID VARCHAR (2) PRIMARY KEY,
TITLE VARCHAR(20),
YEAR_RELEASED VARCHAR(4),
RATING_ID VARCHAR(1),
BW VARCHAR(4),
SUBTITLES VARCHAR(5));
CREATE TABLE CAST
(CAST_ID VARCHAR (2) PRIMARY KEY,
LAST_NAME VARCHAR(20),
FIRST_NAME VARCHAR(4),
DOB VARCHAR(20),
DOD VARCHAR(20));
CREATE TABLE MOVIE_CAST
(MOVIE_ID VARCHAR (2),
CAST_ID VARCHAR(2),
PERSONA VARCHAR(4));
I need to code a foreign keys for the tables here currently have no clue as to what to do for the following:
In the movie table I need to reference the rating id that is
present in the rating table.
In the cast table i need to reference movie id and cast id
from the movie table and cast table

This command will crate a foreign key:
alter table movie
add foreign key (rating_id)
references rating(id);
I recommend renaming the cast table to something else, because cast is a built-in function. Maybe movie_cast.

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

How to make the primary key as varchar

Hi guys so i dont know how to make it as varchar because is want to input BSIT as my primary key and i cant figure it out how to make it as characters please help me this is my code btw
create table CourseTBL
(
CourseID int primary key,
Course varchar(50),
Description varchar(50),
)
It's better to create a different CODE column to use WHERE condition on it, because key is something meaningless and is intended for referential integrity only. For PK add a constraint as an another element of CREATE TABLE statement. Like this:
create table CourseTBL
(
CourseID varchar(50),
Course varchar(50),
Description varchar(50),
primary key( CourseID )
)
My preference is to add course code column
Create table CourseTBL
(
CourseID int primary key,
CourseName varchar(50),
CourseCode varchar(10),
CourseDescription varchar(50),
)

How to reference a entity twice in the same table but with different variables?

I really hope I have used the right vocabulary, since I usually don't do this in English.
My problem is the following: I want to create a table that references to the same entity in another table twice, but with different variables.
In detail, I want to create a table called "matches" that references twice to the entity "name" in a table called "club", since a sports match obviously has two teams participating. This is what I tried to do:
CREATE TABLE aclub (
clubname varchar(100),
stadium varchar(100),
foundingdate integer,
PRIMARY KEY (clubname, stadium) ) ;
CREATE TABLE amatches (
matchnr integer PRIMARY KEY,
place varchar(100) REFERENCES aclub(stadium),
clubname1 varchar(100) REFERENCES aclub(clubname),
clubname2 varchar(100) REFERENCES aclub(clubname) ) ;
The error I get is the following:
ERROR: there is no unique constraint matching given keys for referenced table "aclub"
CREATE TABLE aclub (
id INT AUTO_INCREMENT PRIMARY KEY,
clubname varchar(100),
stadium varchar(100),
foundingdate integer,
UNIQUE (clubname),
UNIQUE (stadium)
) ;
CREATE TABLE amatches (
matchnr INTEGER AUTO_INCREMENT PRIMARY KEY,
club1 INTEGER REFERENCES aclub(id),
club2 INTEGER REFERENCES aclub(id),
place INTEGER REFERENCES aclub(id)
) ;
So you are have three references to the aclub table as in your question. But now we are using integers instead of varchar and that reduces redundancy
Use synthetic primary keys. This really makes it easier to design and use the data model:
CREATE TABLE aclub (
aclubId int auto_increment PRIMARY KEY,
clubname varchar(100),
stadium varchar(100),
foundingdate integer,
UNIQUE (clubname, stadium)
) ;
CREATE TABLE amatches (
matchnr integer auto_increment PRIMARY KEY,
aclubid integer,
foreign key (aclubid) references aclub(aclubid)
) ;
Your specific problem is that all the columns in a primary key need to be included in the foreign key definition. But why bother? Just use an auto-incremented id for the key and look up the information you want for the other fields.

What's the right way to insert data to a table with a relationship using plain mysql

This is the database I've created so far
CREATE TABLE app_user(
id INT AUTO_INCREMENT,
username VARCHAR(20),
password VARCHAR(40),
first_name VARCHAR(20),
last_name VARCHAR(20),
PRIMARY KEY(id)
);
CREATE TABLE app_videos(
id INT AUTO_INCREMENT,
video_path VARCHAR(20),
description VARCHAR(20),
likes INT(20),
dislikes INT(20),
PRIMARY KEY(id)
);
CREATE TABLE vid_uid
(
video_id INT,
user_id INT
);
ALTER TABLE vid_uid ADD FOREIGN KEY(video_id) REFERENCES app_videos(id); (..)
This far I've only managed to insert data, by using two queries.
One of the inserts data to the app_users/app_videos table and the other one inserts the data into the relationship table.
How can I add data to a manytomany relationship without using several queries?
is any "right" way to do it?

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);