Cannot Create Table - mysql

I am using this SQL query to create a table but does gives error in my REFRENCES AT line 13. This is my query
CREATE TABLE ITEM (
ID INT NOT NULL,
TYPE VARCHAR (32) NOT NULL,
DESCRIPTION VARCHAR (64) NOT NULL,
SIZE FLOAT NOT NULL,
SIZE_TYPE VARCHAR (4) NOT NULL,
MANUFACTURE VARCHAR (16) NOT NULL,
SECTION VARCHAR (16) NOT NULL,
PRICE FLOAT NOT NULL,
LEVEL INT(1) NOT NULL,
AISLE_ID INT(11) NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (AISLE_ID),
REFERENCES aisle(AISLE_ID)
);

CREATE TABLE ITEM (
ID INT NOT NULL,
TYPE VARCHAR (32) NOT NULL,
DESCRIPTION VARCHAR (64) NOT NULL,
SIZE FLOAT NOT NULL,
SIZE_TYPE VARCHAR (4) NOT NULL,
MANUFACTURE VARCHAR (16) NOT NULL,
SECTION VARCHAR (16) NOT NULL,
PRICE FLOAT NOT NULL,
LEVEL INT NOT NULL,
AISLE_ID INT NOT NULL
PRIMARY KEY (ID) ,
FOREIGN KEY (AISLE_ID) REFERENCES aisle(AISLE_ID)
)

FOREIGN KEY (AISLE_ID),
REFERENCES aisle(AISLE_ID)
Is wrong and should be:
FOREIGN KEY (AISLE_ID) REFERENCES aisle(AISLE_ID)

Related

MySQL v5.7/5.6 Cannot add Foreign Key constraint

I tried putting this through db-fiddle and SQL fiddle Keep getting "Schema Error: Error: ER_CANNOT_ADD_FOREIGN: Cannot add foreign key constraint"
Everything works until I put the last table.
'''
CREATE TABLE User (
User_ID INT NOT NULL,
First_Name VARCHAR(30) NOT NULL,
Last_Name VARCHAR(30) NOT NULL,
Address_1 VARCHAR(100) NOT NULL,
Address_2 VARCHAR(100) NOT NULL,
City VARCHAR(100) NOT NULL,
State VARCHAR(100) NOT NULL,
Zip VARCHAR(25) NOT NULL,
Mobile_Phone VARCHAR(30) NOT NULL,
PRIMARY KEY (User_ID)
);
CREATE TABLE Bagel (
Bagel_ID INT NOT NULL,
Bagel_Name VARCHAR(30) NOT NULL,
Bagel_Description VARCHAR(30) NOT NULL,
Bagel_Price NUMERIC(65,2) NOT NULL,
PRIMARY KEY(Bagel_ID)
);
CREATE TABLE Bagel_Order (
Bagel_Order_ID INT NOT NULL,
User_ID INT NOT NULL,
Order_Date TIMESTAMP,
Delivery_Fee NUMERIC(50 , 2) NOT NULL,
Special_Notes CHAR,
PRIMARY KEY (Bagel_Order_ID),
FOREIGN KEY (User_ID) REFERENCES User (User_ID)
);
CREATE TABLE Bagel_Order_Line_Item (
Bagel_Order_ID INT NOT NULL,
Bagel_ID INT NOT NULL,
Bagel_Quantity INT NOT NULL,
PRIMARY KEY(Bagel_Order_ID, Bagel_ID),
FOREIGN KEY(Bagel_Order_ID) REFERENCES
Bagel_Order_Line_Item(Bagel_Order_ID),
FOREIGN KEY(Bagel_ID) REFERENCES Bagel(Bagel_ID)
);
'''

How to ER Diagram MYSQL

I'm trying to create simple library database in mysql. I have 5 tables students, entry, book, typebook, author. When I was trying to make er diagram with mysql reverse engineer my tables doesn't have any relationships on er diagram. But almost every databases have relationships on er diagram in the internet. What am i doing wrong and how to fix it?
CREATE TABLE student(
`stuNo` INT NOT NULL,
`stuname` VARCHAR(45) NULL,
`stusurname` VARCHAR(45) NULL,
`class` INT NULL,
`age` INT NULL,
PRIMARY KEY (`stuNo`));
CREATE TABLE entry(
`stuNo` INT NOT NULL,
`entryno` INT NOT NULL,
`bookno` INT NOT NULL,
`borrowdate` DATE NULL,
`returndate` DATE NULL,
PRIMARY KEY (`bookno`));
CREATE TABLE book(
`bookno` INT NOT NULL,
`bookname` VARCHAR(45) NULL,
`authorno` INT NOT NULL,
`typeno` INT NOT NULL,
PRIMARY KEY (authorno , typeno));
CREATE TABLE typebook (
`typeno` INT NOT NULL,
`typename` VARCHAR(45) NULL,
PRIMARY KEY (`typeno`));
CREATE TABLE author(
`authorno` INT NOT NULL,
`authorname` VARCHAR(45) NULL,
`autorname` VARCHAR(45) NULL,
PRIMARY KEY (`authorno`));
I had to switch the order of the CREATE TABLE
What changes is the order you have to insert data.
For example
If you want to INSERT a book , you first have to insert the typebook and author, that corresponds to the book and so on.
CREATE TABLE student(
`stuNo` INT NOT NULL,
`stuname` VARCHAR(45) NULL,
`stusurname` VARCHAR(45) NULL,
`class` INT NULL,
`age` INT NULL,
PRIMARY KEY (`stuNo`));
CREATE TABLE typebook (
`typeno` INT NOT NULL,
`typename` VARCHAR(45) NULL,
PRIMARY KEY (`typeno`));
CREATE TABLE author(
`authorno` INT NOT NULL,
`authorname` VARCHAR(45) NULL,
`autorname` VARCHAR(45) NULL,
PRIMARY KEY (`authorno`));
CREATE TABLE book(
`bookno` INT NOT NULL,
`bookname` VARCHAR(45) NULL,
`authorno` INT NOT NULL,
`typeno` INT NOT NULL,
PRIMARY KEY (authorno , typeno),
INDEX(bookno),
FOREIGN KEY (typeno)
REFERENCES typebook(typeno),
FOREIGN KEY (authorno)
REFERENCES author(authorno));
CREATE TABLE entry(
`stuNo` INT NOT NULL,
`entryno` INT NOT NULL,
`bookno` INT NOT NULL,
`borrowdate` DATE NULL,
`returndate` DATE NULL,
PRIMARY KEY (`bookno`),
FOREIGN KEY (stuNo)
REFERENCES student(stuNo),
FOREIGN KEY (bookno)
REFERENCES book(bookno)
);
Results in
The problem is that your tables do not have any FOREIGN KEYS set. MySQL has no idea that the authorno column from one table should be linked to the authorno column from another table. So no relationship links are generated.
To generate the relationship links to your ER diagram, you have to use FOREIGN KEY entries in your CREATE TABLE query to specify which column from one table is referencing a column from a different table:
CREATE TABLE author(
`authorno` INT NOT NULL,
`authorname` VARCHAR(45) NULL,
`autorname` VARCHAR(45) NULL,
PRIMARY KEY (`authorno`)
);
CREATE TABLE book(
`bookno` INT NOT NULL,
`bookname` VARCHAR(45) NULL,
`authorno` INT NOT NULL,
`typeno` INT NOT NULL,
PRIMARY KEY (bookno),
FOREIGN KEY (authorno) REFERENCES author(authorno)
);
With the added FOREIGN KEY entries MySQL now knows that the authorno column in book must reference a value from the authorno column of the author table. When generating the ER diagram you should get the lines between the tables to see the relationship.

I cannot create the database GradeBook with the foreign key. When I try to add it I get error1046: choose database

CREATE SCHEMA GRADEBOOK;
CREATE TABLE GRADEBOOK.PERSON
(PERSON_ID INT NOT NULL,
Fname varchar(15) NOT NULL,
Minit varchar (1),
Lname varchar(15) NOT NULL,
B_date date NOT NULL,
SEX varchar(1) NOT NULL,
ADDRESS varchar(50) NOT NULL,
TELEPHONE VARCHAR(12),
STATUS varchar(10) NOT NULL,
PRIMARY KEY (PERSON_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.STUDENT
(STUDENT_ID INT NOT NULL,
SUBJECT_ID VARCHAR (30) NOT NULL,
PRIMARY KEY (STUDENT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.ALUMNUS
(STUDENT_ID INT NOT NULL,
GRAD_YEAR date NOT NULL,
PRIMARY KEY(STUDENT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.SUBJECT
(SUBJECT_ID VARCHAR (30) NOT NULL,
SUBJECT_NAME VARCHAR (20) NOT NULL,
CLASS_ID VARCHAR (20) NOT NULL,
PRIMARY KEY (SUBJECT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.HOUSE
(HOUSE_ID INT NOT NULL,
HOUSE_NAME VARCHAR (20) NOT NULL,
HOUSE_COLOR VARCHAR (20) NOT NULL,
PRIMARY KEY (HOUSE_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.CLUB
(CLUB_ID INT auto_increment NOT NULL,
CLUB_name varchar(20) NOT NULL,
PRIMARY KEY (CLUB_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.HOMEROOM
(HOMEROOM_ID VARCHAR(20) NOT NULL,
CLASS_ID VARCHAR(20) NOT NULL,
PRIMARY KEY( HOMEROOM_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.CLASS
(CLASS_ID VARCHAR(20) NOT NULL,
CLASS_name VARCHAR(20) NOT NULL,
HOMEROOM_ID VARCHAR(20) NOT NULL,
PRIMARY KEY (CLASS_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.TEACHER
(TEACHER_ID INT NOT NULL,
HOOMROOM VARCHAR(20) NOT NULL,
SUBJECT_ID VARCHAR(20) NOT NULL,
YEAR INT NOT NULL,
TERM VARCHAR(20) NOT NULL,
PRIMARY KEY(TEACHER_ID))
ENGINE=INNODB;

Having trouble in Online Movie ticketing database design

#J2D8T i have done few changes in my code please tell if its correct also is it properly normalized? thanks in advance.
use [fadi1]
create table Customer (
Cnic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar (30) not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30)
);
create table Adimn (
Anic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar (30) not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30)
);
create table Movie (
M_id int primary key not null,
name varchar(25) not null,
Mdescription varchar(500) not null,
imagename varchar (100) not null,
Actors varchar (100) not null,
);
create table Venue(
V_id int primary key not null,
V_Name varchar(25) not null,
Maxcapacity int not null,
);
create table Shows (
showid int primary key IDENTITY(1,1) not null,
V_id int not null,
M_id int not null ,
date_time datetime not null unique,
remaining_tickets int not null,
foreign key (v_id) references Venue(v_id),
foreign key (M_id) references Movie(M_id),
);
create table Tickets(
showid int not null,
Seat_no int not null, -- (maxcap-remaining tickets)+1 from capacity table of given pk of that table
T_id int primary key IDENTITY(1,1) not null, --(p.k)
price int not null,
foreign key (showid) references Shows(showid)
);
create table Booking (
showid int not null,
T_id int not null unique,
Cnic varchar(17) not null,
booking_time datetime not null,
foreign key (showid) references Shows(showid),
foreign key (T_id) references Tickets(T_id),
foreign key (Cnic) references Customer(Cnic),
constraint pk_IDBooking primary key(T_id,showid),
);
please inform me as soon as possible because i am not quite far from the deadline :D
At a minimum, there is no password data type in SQL Server. Since you have provided no details about how you're storing data, I converted it to varchar(50).
SQLFiddle
create table Customer (
Cnic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar(50) not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30) ,
)
create table Adimn (
Anic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar(50) not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30) ,
)
create table Movie (
M_id int not null,
name varchar(25) not null,
Mdescription varchar(500) not null,
imagename varchar (100) not null,
primary key (M_id)
);
create table Actors(
actor_id varchar(25) primary key not null ,
actor varchar(30)not null,
M_id int not null,
Act_role varchar (10)not null,
amountspent int ,
foreign key (M_id) REFERENCES Movie(M_id)
);
create table Venue(
V_id int primary key not null,
M_id int not null,--F.k
V_Name varchar(25)not null
);
create table Capacity(
M_id int not null,
v_id int not null,
date_time varchar (15) not null,
remaining_tickets int not null,
max_capacity int not null,
primary key (M_id, v_id ,date_time )
);
create table Booking (
v_id int not null, --key
T_id int not null,
M_id int not null,--key
Cnic varchar(30) primary key not null,--(p.k))
date_time varchar (15) not null, -- (f.k )
);
create table Tickets(
Seat_no int primary key not null, -- (p.k))
T_id int not null,
M_id int not null, --(f.k)
price int not null,
);
I edited the answer to include only the code with some comments hope this solves your problem.
Edit from your original post:
create table Customer (
Cnic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword password not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30)
);
create table Adimn (
Anic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar (30) not null, // changed to varchar
email varchar (40) ,
contactno varchar (15) ,
city varchar (30)
);
create table Movie (
M_id int primary key not null,
name varchar(25) not null,
Mdescription varchar(500) not null,
imagename varchar (100) not null
);
create table Actors(
actor_id varchar(25) primary key not null ,
actor varchar(30)not null,
M_id int not null,
Act_role varchar (10)not null,
amountspent int ,
foreign key (M_id) REFERENCES Movie(M_id)
);
create table Venue(
V_id int primary key not null,
M_id int not null,--F.k
V_Name varchar(25) not null,
foreign key (M_id) references Movie(M_id)
);
create table Capacity(
v_id int not null, // FK
M_id int not null unique, //FK
date_time varchar (15) not null unique,
remaining_tickets int not null,
max_capacity int not null,
foreign key (v_id) references Venue(v_id),
foreign key (M_id) references Movie(M_id),
constraint pk_IDCapacity PRIMARY KEY (M_id,v_id,date_time) // this will create composite key
);
create table Tickets(
Seat_no int primary key not null, -- (p.k))
T_id int not null,
M_id int not null, --(f.k)
price int not null,
foreign key (M_id) references Capacity(M_id)
);
create table Booking (
v_id int not null, //FK
T_id int not null, //FK
M_id int not null,//FK
Cnic varchar(30) primary key not null // Will this stil be neccessary
date_time varchar (15) not null, //FK
foreign key (v_id) references Venue(v_id),
foreign key (M_id) references Movie(M_id),
foreign key (date_time) references Capacity(date_time),
foreign key (T_id) references Tickets(T_id),
constraint pk_IDBooking primary key(v_id,M_id,T_id,date_time) //Can be in any order the PK's
);
Could probably be more compacted with normalization but it should work based on the code you have provided.

#1005 - Can't create table 'workswell_database.skoleni' Novice user with MySQL

sorry for my stupid question,but I do database in MySQL and was so happy,when i finished.Unfortunately,I had found a lot of mistakes in my database n when i will properly fix a bug,so i've another. Also,this is my Database and that guy what really boring right now,please write about my bugs and solution for them.I novice as a programmer and this database i do about 4 days !
CREATE TABLE`Skoleni` (
`sk_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`Cena` MEDIUMINT NOT NULL,
`Obsazenost` text NOT NULL,
`Kapacita` datetime NOT NULL,
`Popis` text,
`Prerekvizity` text,
`Certifikat` MEDIUMINT NOT NULL,
PRIMARY KEY (`sk_id`),
FOREIGN KEY (`sk_id`) REFERENCES Skoleni_has_Termin(`Skoleni_sk_id`),
FOREIGN KEY (`sk_id`) REFERENCES Skoleni_has_Uzivatel(`Skoleni_sk_id`)
);
CREATE TABLE `Skoleni_has_Termin`(
`Skoleni_sk_id`INT NOT NULL UNIQUE,
`Termin_ter_id` INT NOT NULL UNIQUE,
PRIMARY KEY (`Skoleni_sk_id`)
);
CREATE TABLE `Termin` (
`ter_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`Datum_cas` DATETIME NOT NULL,
`Misto_mo_id` INT NOT NULL,
PRIMARY KEY (`ter_id`),
FOREIGN KEY (`ter_id`) REFERENCES Skoleni_has_Termin(`Termin_ter_id`)
);
CREATE TABLE `Misto` (
`mo_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`ulice` MEDIUMINT NOT NULL,
`cislo_popisne` MEDIUMINT NOT NULL,
`lat` FLOAT (10,6) NOT NULL,
`lng` FLOAT (10,6) NOT NULL,
PRIMARY KEY (`mo_id`)
)ENGINE = MYISAM;
SELECT TABLE.Misto(
INSERT (`ulice`, `cislo_popisne`, `lat`, `lng`),
VALUES (`dr_Zikmunda_Wintra_376_5``16000 Praha 6 Bubenec``14.407438``50.101049`)
);
CREATE TABLE `Skoleni_has_Uzivatel` (
`Skoleni_sk_id` INT NOT NULL UNIQUE,
`Uzivatel_uziv_id` INT NOT NULL UNIQUE,
PRIMARY KEY (`Skoleni_sk_id`)
);
CREATE TABLE `Uzivatel` (
`uziv_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`Jmeno` VARCHAR(30) NOT NULL,
`Typ` MEDIUMINT UNIQUE,
`Heslo` VARCHAR(32) NOT NULL,
`Potvrzeni` VARCHAR(1) NOT NULL,
PRIMARY KEY (`uziv_id`),
FOREIGN KEY (`uziv_id`) REFERENCES Skoleni_has_Uzivatel(`Uzivatel_uziv_id`)
);
CREATE TABLE `Skoleni_has_Lektor` (
`Skoleni_sk_id` INT NOT NULL UNIQUE,
`Lektor_lek_id` INT NOT NULL UNIQUE,
PRIMARY KEY (`Lektor_lek_id`)
);
CREATE TABLE `Lektor` (
`lek_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`Titul'pred'` VARCHAR(10) NOT NULL,
`Jmeno` VARCHAR(20) NOT NULL,
`Prijmeni` VARCHAR(20) NOT NULL,
`Titul'za'` VARCHAR(10),
`Firma` VARCHAR(30),
`Rodne cislo` CHAR(11) NOT NULL,
`Datum narozeni` DATE NOT NULL,
PRIMARY KEY (`lek_id`)
);
CREATE TABLE `Firma` (
`fir_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`E-mail` VARCHAR(15) NOT NULL,
`Telefon` VARCHAR(15) NOT NULL,
`Web` VARCHAR(30),
`IC` CHAR(8) NOT NULL,
`DIC` VARCHAR(12) NOT NULL,
`Misto_mo_id` INT NOT NULL,
PRIMARY KEY (`fir_id`),
PRIMARY KEY (`Misto_mo_id`),
FOREIGN KEY (`Misto_mo_id`) REFERENCES Firma(`Misto_mo_id`)
);
The tables `skoleni_has_termin' and 'skoleni_has_lektor' are most likely not needed here. I would bring dates to trainings table and leave just lektor_id in skoleni table as relation between trainings and lecturers is 1:n not m:n (unless one training can have more than one lecturer)