I am currently learning to use MySQL and a bit stuck and wondering if someone can help. Below is one of my tables for CD information. It works but I need to change the Genre field so that instead of a user typing in a Genre, they can pick from a preselected list of values. I am really unsure how to do this and wondering if someone can help steer me on this.
CREATE TABLE CD (
Unique_CD_Identification_Number INT NOT NULL,
CD_Title VARCHAR(23) NOT NULL,
CD_Year YEAR(4) NOT NULL,
CD_Price DECIMAL(2,2) NOT NULL,
CD_Description VARCHAR(56) NOT NULL,
CD_Genre VARCHAR(10) NOT NULL,
Unique_Production_Number INT,
FOREIGN KEY (Unique_Production_Number) REFERENCES Production(Unique_Production_Number),
PRIMARY KEY (Unique_CD_Identification_Number)
);
Make CD_Genre instead CD_GenreId. Then this should be a foreign reference into a Genres table:
CREATE TABLE Genres (
GenreId int not null auto_increment primary key,
Name varchar(255) not null
);
CREATE TABLE CD (
Unique_CD_Identification_Number INT NOT NULL,
CD_Title VARCHAR(23) NOT NULL,
CD_Year YEAR(4) NOT NULL,
CD_Price DECIMAL(2,2) NOT NULL,
CD_Description VARCHAR(56) NOT NULL,
CD_GenreId int NOT NULL,
Unique_Production_Number INT,
FOREIGN KEY (Unique_Production_Number) REFERENCES Production(Unique_Production_Number),
FOREIGN KEY (CD_GenreId) REFERENCES Genres(GenreId),
PRIMARY KEY (Unique_CD_Identification_Number)
);
You can then use the Genres table to populate your list.
Related
For some reason my code won't let me create the projects table due to the foreign key error. I have tried a few different things and just cant seem to get it to work, ive tried looking on here for solutions but cant seem to do it. Any help would be much appreciated.
CREATE TABLE PEOPLE (
NAME VARCHAR(32) NOT NULL,
GENDER ENUM('Male', 'Female', 'Other'),
DOB DATE NOT NULL,
SALARY VARCHAR(16) NOT NULL,
PROJECT VARCHAR(32) NOT NULL,
BUSINESS_NAME VARCHAR(32) NOT NULL,
PRIMARY KEY(NAME, PROJECT)
);
CREATE TABLE PEOPLE_EMAILS (
NAME_ID VARCHAR(32) NOT NULL,
EMAIL VARCHAR(64) NOT NULL,
PRIMARY KEY(EMAIL),
FOREIGN KEY(NAME_ID) REFERENCES PEOPLE(NAME)
);
CREATE TABLE PEOPLE_PHONE (
NAME_ID2 VARCHAR(32) NOT NULL,
PHONE_NUMBER VARCHAR(32) NOT NULL,
PRIMARY KEY (PHONE_NUMBER),
FOREIGN KEY(NAME_ID2) REFERENCES PEOPLE(NAME)
);
CREATE TABLE PROJECTS (
PROJECT_NAME VARCHAR(32) NOT NULL,
PROJECT_LOCATION VARCHAR(32) NOT NULL,
BUDGET VARCHAR(16) NOT NULL,
FOREIGN KEY(PROJECT_NAME) REFERENCES PEOPLE(PROJECT)
);
Presumably, you want the foreign key the other way around. You would expect people to reference projects instead of projects to reference people.
This means that you need to create the projects table first, and then the people table. Also, you need a proper primary key on projects so you can reference it in people (I assumed project_name).
CREATE TABLE PROJECTS (
PROJECT_NAME VARCHAR(32) NOT NULL,
PROJECT_LOCATION VARCHAR(32) NOT NULL,
BUDGET VARCHAR(16) NOT NULL,
PRIMARY KEY (PROJECT_NAME)
);
CREATE TABLE PEOPLE (
NAME VARCHAR(32) NOT NULL,
GENDER ENUM('Male', 'Female', 'Other'),
DOB DATE NOT NULL,
SALARY VARCHAR(16) NOT NULL,
PROJECT VARCHAR(32) NOT NULL,
BUSINESS_NAME VARCHAR(32) NOT NULL,
PRIMARY KEY(NAME, PROJECT),
FOREIGN KEY(PROJECT) REFERENCES PROJECTS(PROJECT_NAME)
);
CREATE TABLE PEOPLE_EMAILS (
NAME_ID VARCHAR(32) NOT NULL,
EMAIL VARCHAR(64) NOT NULL,
PRIMARY KEY(EMAIL),
FOREIGN KEY(NAME_ID) REFERENCES PEOPLE(NAME)
);
CREATE TABLE PEOPLE_PHONE (
NAME_ID2 VARCHAR(32) NOT NULL,
PHONE_NUMBER VARCHAR(32) NOT NULL,
PRIMARY KEY (PHONE_NUMBER),
FOREIGN KEY(NAME_ID2) REFERENCES PEOPLE(NAME)
);
Demo on DB Fiddle
so pretty new to SQL I created 2 tables which I wanted to be related to one another but I'm getting an error "#1215 - Cannot add foreign key constraint" can someone point me to the right direction of this problem?
CREATE TABLE movie(
id INT(1) NOT NULL AUTO_INCREMENT,
nearname VARCHAR(25) NOT NULL,
release_date DATE NOT NULL,
lang VARCHAR(10) NOT NULL,
PRIMARY KEY(id),
CONSTRAINT same_movie FOREIGN KEY(id) REFERENCES movie_cast(movie_id)
);
CREATE TABLE movie_cast(
movie_id INT(1) NOT NULL AUTO_INCREMENT,
director_name VARCHAR(20) NOT NULL,
actor_name VARCHAR(20) NOT NULL,
actress_name VARCHAR(20) NOT NULL,
PRIMARY KEY(movie_id),
CONSTRAINT same_movie FOREIGN KEY(movie_id) REFERENCES movie(id)
);
You need to refer to the same column name as the primary key. In this case, it is called id:
CONSTRAINT same_movie FOREIGN KEY(movie_id) REFERENCES movie_cast(id)
Of course, your DDL doesn't define movie_cast. So, I am guessing the second table should be something like:
CREATE TABLE movie_cast (
id INT NOT NULL AUTO_INCREMENT,
movie_id int not null,
cast_name varchar(255)
PRIMARY KEY(id),
CONSTRAINT fk_movie_cast_movie FOREIGN KEY(movie_id) REFERENCES movie(movie_id)
);
My question is about MySQL, I keep getting an error (Error 1215: Cannot add Foreign key Constraint) while trying to forward engineer a schema to a db server, I've got two parent tables:
CREATE TABLE IF NOT EXISTS alunos (
idAluno INT NOT NULL AUTO_INCREMENT,
NomeAluno VARCHAR(100) NOT NULL,
nifAluno VARCHAR(15) NOT NULL,
moradaAluno VARCHAR(255) NOT NULL,
telefoneAluno VARCHAR(9) NOT NULL,
emailAluno VARCHAR(255) NOT NULL DEFAULT "Nao fornecido",
PRIMARY KEY(idAluno, nifAluno)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS cursos (
idCurso INT NOT NULL AUTO_INCREMENT,
nomeCurso VARCHAR(50) NOT NULL,
horas INT NOT NULL,
PRIMARY KEY(idCurso, nomeCurso)
) ENGINE=INNODB;
And this is my child table:
CREATE TABLE IF NOT EXISTS inscritos (
id INT NOT NULL AUTO_INCREMENT,
Nome VARCHAR(100) NOT NULL,
Morada VARCHAR(255) NOT NULL,
Naturalidade VARCHAR(45) NOT NULL,
NIF VARCHAR(15) NOT NULL,
email VARCHAR(255) NOT NULL DEFAULT "Nao fornecido",
Telefone VARCHAR(9) NOT NULL,
Curso VARCHAR(50) NOT NULL,
Horas INT NOT NULL,
Inicio DATE NOT NULL,
Validade DATE NOT NULL,
Atividade VARCHAR(45) NOT NULL,
PRIMARY KEY(id),
INDEX(NIF),
INDEX(Curso),
FOREIGN KEY(NIF)
REFERENCES alunos(nifAluno)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY(Curso)
REFERENCES cursos(nomeCurso)
ON UPDATE RESTRICT ON DELETE RESTRICT
) ENGINE=INNODB;
I've looked through the code over and over and I can't seem to find the error when assigning the foreign keys.
Thanks in advance.
Because, NIF and Curso aren't primary/unique key in inscritos table. Creating index doesn't mean you are creating key on same column. So, just for your information. The referenced columns in the Parent table must be the left-most columns of a key. Best if the key is PRIMARY KEY or UNIQUE KEY.
As #Bill commented, he has an answer where he has prepared a checklist, you may refer to make sure, you won't get any other error.
sorry for maybe stupid question, but I stack with my sql query. Tried out many methods to avid it but still have error 150. I've created 3 tables and one with foreighn keys:
Table users
create table users(
id int(11) primary key auto_increment,
unique_id varchar(23) not null unique,
name varchar(50) not null,
cname varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null
);
Table behaviours
CREATE TABLE behaviours (
id int(2) AUTO_INCREMENT PRIMARY KEY,
bName varchar(100) NOT NULL
);
Table severytys
CREATE TABLE severitys (
id int(2) AUTO_INCREMENT PRIMARY KEY,
severity varchar(10) NOT NULL
);
And here it shows errors:
CREATE TABLE child_behaviours(
id int(11) primary key auto_increment,
name varchar(50) not null,
cname varchar(50) not null,
bName varchar(100) NOT NULL,
severity varchar(10) NOT NULL,
start_at datetime,
stop_at datetime null,
FOREIGN KEY (id) REFERENCES users(id),
FOREIGN KEY (bName) REFERENCES behaviours(bName),
FOREIGN KEY (severity) REFERENCES severitys(severity)
);
Will be really pleasent to have an answer to this issue
You can only create a FK on a column that is both UNIQUE and NOT NULL. This is usually, but not always, the primary key of the referenced table.
If you reference anything other than the PK, you need a really really good reason.
(I'd even add to that, that you need a really really good reason to use anything other than an INT or a couple INTs as a PK...)
Now, child_behaviours has several errors:
Duplication of columns already present in users. If you reference users by its id, there is no need to duplicate the columns.
Reference to behaviors(bName) instead of using its id
same thing for reference to severity
Also, there is the generic mistake of using "id" columns. Please call them "user_id", "severity_id", etc, and then call them the same in your references. This will make your life much easier, and avoid stuff like:
SELECT foo.id AD foo_id, bar.id AS bar_id FROM foo JOIN bar ON ...
You have to refer to a primary key, when building a foreign key
CREATE TABLE child_behaviours(
userId int not null,
behaviourId int NOT NULL,
severityId int NOT NULL,
start_at datetime not null,
stop_at datetime,
FOREIGN KEY (userId) REFERENCES users(id),
FOREIGN KEY (behaviourId) REFERENCES behaviours(id),
FOREIGN KEY (severityId) REFERENCES severitys(id)
);
I want to implement connection as friends of friends using MySQL
CREATE TABLE user (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
email VARCHAR(60) NOT NULL,
....
PRIMARY KEY (id)
);
CREATE TABLE friendof (
user_a_id MEDIUMINT NOT NULL,
user_b_id MEDIUMINT NOT NULL,
PRIMARY KEY(user_a_id, user_b_id),
FOREIGN KEY(user_a_id) REFERENCES user(id),
FOREIGN KEY(user_b_id) REFERENCES user(id),
KEY reverseLookup (user_b_id),
KEY lookup (user_a_id),
);
Was that really so hard?