need help creating a simple many-to-many relationship using mysql - 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);

Related

Transferring from sql developer to mysql

So i have a few tables in my database in Sql developer which I still have the queries from. If I try to put it in mysql it comes out with some errors so i'm wondering what is different and why is it not working like what would I need to change.
These are some of the tables I created in sql which i'm trying to create now in mysql:
create table EspecialidadesMedicas(
IdEspecialidad number(4) constraint pk_EspecialidadesMedicas primary key,
DescripcionEspecialidad varchar2(30));
create table Doctores(
IdDoctor number(5) constraint pk_Doctores primary key,
NombreDoctor varchar2(30),
Salario number(12,2),
Especialidad constraint fk1_Doctores references EspecialidadesMedicas);
create table Consultorios(
IdConsultorio number(4) constraint pk_Consultorios primary key,
Tamano varchar2(30),
Construido date);
Your first statement will become this:
create table EspecialidadesMedicas(
IdEspecialidad int primary key,
DescripcionEspecialidad varchar(30)
);
Your second statement is likely to become this:
create table Doctores(
IdDoctor int primary key,
NombreDoctor varchar(30),
Salario decimal(12,2),
Especialidad int,
constraint fk1_Doctores foreign key (Especialidad) references EspecialidadesMedicas (IdEspecialidad)
);
Your third statement will turn out to be:
create table Consultorios(
IdConsultorio int primary key,
Tamano varchar(30),
Construido date
);
Try this out in MySQL.
Your first table would look like this in MySQL:
CREATE TABLE `de`.`EspecialidadesMedicas` (
`IdEspecialidad` INT NOT NULL AUTO_INCREMENT,
`DescripcionEspecialidad` VARCHAR(30) NULL,
PRIMARY KEY (`IdEspecialidad`));
As you see, there are quite some differences in syntax, data types and features which can not all be covered here. (e.g. AUTO INCREMENT)
The easiest way to get into this is to use a toollike MySQL Workbench. It allows you to use a GUI to create your tables and displays the executed SQL. This way you see the differences.
I also suggest to read some migration tutorials like these:
https://blog.toadworld.com/2017/03/17/migrating-from-oracle-to-mysql
http://www.sqlines.com/oracle-to-mysql

How to create a weak entity in mySql?

I want to know how can I create weak entities in mySql by Creating tables, I have the code like this:
CREATE TABLE users(
user_Id int AUTO_INCREMENT,
full_Name varchar(60),
email varchar(30),
password varchar(30),
reg_Date timestamp
);
CREATE TABLE personal_Infos(
...
);
These are the tables, all I want to Know is how can I Connect to each-other with foreign key and should I create another primary key at the second table? (Second table has some more informations for the first table)
should I create another primary key at the second table?
Yes you do need to create a primary key in your second table personal_Infos table and that primary key will become foreign key in your users table.
how can I Connect to each-other with foreign key ?
Suppose your primary key in personal_Infos table is P_id, then you can add it as foreign key in your users table like shown below
CREATE TABLE users(
user_Id int AUTO_INCREMENT,
full_Name varchar(60),
email varchar(30),
password varchar(30),
reg_Date timestamp,
p_id int not null,
FOREIGN KEY (P_Id) REFERENCES personal_Infos(P_Id)
);

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?

MySQL create table with set attribute from another table?

I am a beginner at this but is it possible (in MySQL workbench) to create an attribute that is a set depended on an attribute from another table?
CREATE TABLE danes (
id INT PRIMARY KEY,
name VARCHAR(50),
nationality VARCHAR(20),
gender CHAR(1),
degree SET ?????????????????? from degree(level)
);
CREATE TABLE degree (
level VARCHAR(10),
subject VARCHAR(20),
institutionawarding VARCHAR(20),
yearawarded DATE,
PRIMARY KEY (level, subject)
);
never mind I got it
I am guessing that you want another table, a junction table:
CREATE TABLE DaneDegrees (
DanesId INT REFERENCES danes(id),
Level VARCHAR(10),
Subject VARCHAR(20),
FOREIGN KEY fk_level_subject(level, subject) REFERENCES Degree(level, Subject)
);
I would, however, have an INT AUTO_INCREMENT PRIMARY KEY in both Danes and Degrees.
apparently you cant just say
<attribute_name> set(get set from another tabe)
the set thing doesn't work that way
eventhough you can apparently update it so it actually can
update table_name
attribute = (select * from another table_name)

MYSQL in PHPMYADMIN - create table relationship from one table

I want to create a table relationship with MYSQL PHPMYADMIN.
I have this Create table:
CREATE TABLE students(code_students int(8)not null AUTO_INCREMENT,
name_students varchar(25),
age_students int(3),
degree_program varchar(25),
code_advisor int(8)not null,
primary key(code_students, code_advisor)
);
and i want to make a create table named advise relationship between code_students, code_advisor.
Ok this is my tryout.
CREATE TABLE advise (
code_students int(8),
code_advisor int(8),
primary key(code_students, code_advisor),
foreign key(code_students)references students(code_students),
foreign key(code_advisor)references students(code_advisor)
);
mySQL says :
A FOREIGN KEY constraint that references a non-UNIQUE key is not standard SQL. It is an InnoDB extension to standard SQL
Try adding the UNIQUE keyword to your first table:
CREATE TABLE students(
code_students int(8)not null unique AUTO_INCREMENT,
name_students varchar(25),
age_students int(3),
degree_program varchar(25),
code_advisor int(8)not null unique,
primary key(code_students, code_advisor)
);
Check out this sqlFiddle and see how it works, then try removing the UNIQUE keywords and see that you get the same error that you mentioned. ( Hit the Build Schema button )
http://sqlfiddle.com/#!2/46b69