MySQL create table with set attribute from another table? - mysql

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)

Related

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

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

new to sql/mysql cant figure out foriegn keys

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.

Setting Auto_Increment to start from 1000 when creating table

I want my table's primary key to start from the number 1000 instead of the default. I read around here and the answer is to declare the increment value when creating the table as follows:
AUTO_INCREMENT = 1000;
I tried it when I CREATE my table but it returns a syntax error. Of course I could make the changes on PhpMyAdmin but I want to do it on CREATE instead. Please advice what is wrong. Thanks.
CREATE TABLE IF NOT EXISTS Departments
(
Dept_ID int AUTO_INCREMENT=1000, -- the equals sign returns the syntax error.
Dept_Name varchar(255),
Dept_Address varchar(255),
Dept_Tel int,
PRIMARY KEY (Dept_ID)
);
The starting point must be specified as a table option:
CREATE TABLE IF NOT EXISTS Departments
(
Dept_ID int AUTO_INCREMENT,
Dept_Name varchar(255),
Dept_Address varchar(255),
Dept_Tel int,
PRIMARY KEY (Dept_ID)
) AUTO_INCREMENT=1000;
You can see the full syntax for creating a table here