Adding multi columns and foreign key - mysql

I`m a beginner in structured query language . I want to add multi columns with different foreign key.Like the example:
drop schema humman;
create schema humman;
CREATE TABLE humman.father (
id int not null auto_increment,
firstname varchar(200) not null,
primary key(id)
);
create table humman.mather(
id int not null auto_increment,
FirstName varchar(200),
primary key(id)
);
CREATE TABLE humman.child (
id int not null auto_increment,
firstname varchar(200) not null,
primary key(id)
);
use `humman`;
alter table humman.child
ADD `parentId` int ,
ADD `motherId` int,
ADD foreign key (`parentId`) references father(`id`),
ADD foreign key (`motherId`) references mother(`id`);
Error code: 1215 Cannot add foreign key CONSTRAINT

Your code is good except for a typo, you spelt "mother" as "mather" in your second table definition;
create table humman.mather(
id int not null auto_increment,
FirstName varchar(200),
primary key(id)
);
correct that and it should work.

Related

#1005 - Can't create table `musicplayer`.`Albums` (errno: 150 "Foreign key constraint is incorrectly formed")

I'm trying to define some tables with SQL, but when I execute it I have an error. Below there is my code and the error which I receive.
CREATE TABLE Artists
(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
description VARCHAR(255)
);
CREATE TABLE Albums
(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
releasedate DATE,
artistid INT,
genreid INT,
picture VARCHAR(255),
CONSTRAINT `fk_albums_artists`
FOREIGN KEY (artistid) REFERENCES Artists(id),
CONSTRAINT `fk_albums_genres`
FOREIGN KEY (genreid) REFERENCES Genres(id)
);
CREATE TABLE Tracks
(
id INT(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
playtime INT NOT NULL,
albumid INT,
CONSTRAINT `fk_tracks_album`
FOREIGN KEY (albumid) REFERENCES Albums(id)
);
CREATE table Genres
(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL
);
Error in my SQL query:
MySQL said: Documentation
1005 - Can't create table musicplayer.Albums (errno: 150 "Foreign
key
constraint is incorrectly formed") (Details…)
Declare the tables in order, so the tables are defined before they are referenced:
CREATE TABLE Artists (
id int AUTO_INCREMENT PRIMARY KEY NOT null,
name varchar(255) not null,
description varchar(255)
);
CREATE table Genres(
id int AUTO_INCREMENT PRIMARY KEY NOT null,
name varchar(255) not null
);
CREATE TABLE Albums (
id int AUTO_INCREMENT PRIMARY KEY NOT null,
name varchar(255) not null,
releasedate date,
artistid int,
genreid int,
picture varchar(255),
CONSTRAINT `fk_albums_artists` FOREIGN KEY (artistid) REFERENCES Artists(id),
CONSTRAINT `fk_albums_genres` FOREIGN KEY (genreid) REFERENCES Genres(id)
);
CREATE TABLE Tracks(
id int(11) AUTO_INCREMENT PRIMARY KEY NOT null,
name varchar(255) not null,
playtime int not null,
albumid int,
CONSTRAINT `fk_tracks_album` FOREIGN KEY (albumid) REFERENCES Albums(id)
);
Here is a db<>fiddle.
You can still have tables that reference each other. However, you will need to declare such foreign key constraints using ALTER TABLE rather than in the CREATE TABLE statement. However, you do not need to do this for these table definitions.

trying to relate two table together

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

SQLfiddle Errno: 150 - Foreign Key Issue

When I try to set a Foreign key, it throws the Error number 150.
Schema Creation Failed: Can't create table 'db_2_f856e.urlnames'
This is the code:
create table images(
id int auto_increment primary key
,gender varchar(6)
,pattern varchar(50)
,item_name varchar(25)
,url_id int(250)
)//
create table urlnames(
url_id_no int(250)
,url varchar(250)
,foreign key (url_id_no) references images(url_id)
)//
Can someone explain why it is not working?
Thanks
Your data structure doesn't make sense. I think you want:
create table urlnames(
url_id_no int auto_increment primary key,
url varchar(250)
);
create table images(
image_id int auto_increment primary key,
gender varchar(6),
pattern varchar(50),
item_name varchar(25),
url_id int(250) references urlnames(url_id_no)
);
Any column referenced by a foreign key reference needs to be a primary key or unique key. And, urlnames should have its id column declared as a primary key.
Here is a SQL Fiddle example.

Foreign key constraints seem ignored by mysql

I am just making a foreign key reference of parent table in a child table. When I try to delete the row from the parent table whose reference is there in child table, surprisingly it allows me to delete it. I have tried to create child table explicitly by writing on delete restrict and also without it, but to no help. Any ideas why this is happening? Below is the code I am using while creating the tables.
CREATE TABLE region
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(50) NOT NULL
);
CREATE TABLE aggregator
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(50) NOT NULL
);
CREATE TABLE gateway
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(50) NOT NULL,
region_id int ,
aggregator_id int ,
is_public boolean DEFAULT 0 NOT NULL,
FOREIGN KEY (region_id) REFERENCES region(id),
FOREIGN KEY (aggregator_id) REFERENCES aggregator(id)
);
Both parent and child table need to be INNODB tables.
Try:
CREATE TABLE region
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(50) NOT NULL
) ENGINE=INNODB;
CREATE TABLE aggregator
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(50) NOT NULL
) ENGINE=INNODB;
CREATE TABLE gateway
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(50) NOT NULL,
region_id int ,
aggregator_id int ,
is_public boolean DEFAULT 0 NOT NULL,
FOREIGN KEY (region_id) REFERENCES region(id),
FOREIGN KEY (aggregator_id) REFERENCES aggregator(id)
) ENGINE=INNODB;
Do mean to say that if you delete parent table and child table deletes automatic? If yes then once go through cascading rules.

sql Error 1064 (42000) Syntax Error

CREATE TABLE IF NOT EXISTS message(
id INT NOT NULL auto_increment,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
PRIMARY KEY ('id')
FOREIGN KEY ('userid') REFERENCES users('id'));
I was just wondering if someone could help me in identifying a syntax error as I can not create a table.
Try to put , after the primary key declaration.
Update: I guess it should be
CREATE TABLE IF NOT EXISTS message (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id));
I'm assuming this is for MS SQL Server? If you get MS SQL Server Studio, you can script stuff which gives you an idea:
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[message]') AND type in (N'U'))
CREATE TABLE message(
id INT IDENTITY NOT NULL,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (userid) REFERENCES users(id))
GO
You query should be as below
CREATE TABLE IF NOT EXISTS message (
id INT auto_increment PRIMARY KEY,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id));
Provided you have id as a primary key in users table.
CREATE TABLE users (id INT PRIMARY KEY)
Your query should look like this:
CREATE TABLE IF NOT EXISTS message(
id INT NOT NULL auto_increment,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
PRIMARY KEY ('id'),
FOREIGN KEY ('userid') REFERENCES users('id')
) Engine=InnoDB;
Note the , after PRIMARY KEY ('id').
Small trick
You don't have to specify foreign keys in table definitions. It's practical when you do it like this (because dump may export tables in order that foreign keys will fail on creating/inserting):
CREATE TABLE 1; -- With references to table 2
CREATE TABLE 2;
INSERT INTO 1;
INSERT INTO 2;
ALTER TABLE 1 ADD FOREIGN KEY (user_id) REFERENCES 2 2(id);
Try to change the name of your table, May be message is an in-built keyword in MySQL.
Update to this: I guess it should be
CREATE TABLE IF NOT EXISTS myMessage (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id));