I have created the following script to set up my MySQL database:
CREATE DATABASE IF NOT EXISTS magicc_hat;
USE magicc_hat;
CREATE TABLE people (
personID INT NOT NULL AUTO_INCREMENT,
firstName VARCHAR(45) NOT NULL,
lastName VARCHAR(45),
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (personID)
);
CREATE TABLE categories (
categoryID INT NOT NULL AUTO_INCREMENT,
categoryName VARCHAR(45) NOT NULL,
description TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (categoryID)
);
CREATE TABLE homes (
homeID INT NOT NULL AUTO_INCREMENT,
homeName VARCHAR(45) NOT NULL,
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (homeID)
};
CREATE TABLE items (
itemID INT NOT NULL AUTO_INCREMENT,
itemName VARCHAR(100) NOT NULL,
identifier VARCHAR(100),
quantity INT NOT NULL,
categoryID INT NOT NULL,
homeID INT NOT NULL,
itemStatus ENUM('normal', 'broken', 'missing') NOT NULL DEFAULT 'normal',
description TEXT,
image VARCHAR(45),
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (itemID),
FOREIGN KEY (categoryID) REFERENCES categories(categoryID),
FOREIGN KEY (homeID) REFERENCES homes(homeID)
};
CREATE TABLE projects (
projectID INT NOT NULL AUTO_INCREMENT,
projectName VARCHAR(45) NOT NULL,
description TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (projectID)
);
CREATE TABLE checkouts (
checkoutID INT NOT NULL AUTO_INCREMENT,
itemID INT NOT NULL,
personID INT NOT NULL,
projectID INT,
quantity INT NOT NULL,
outDateTime DATETIME,
inDateTime DATETIME,
outNotes TEXT,
inNotes TEXT,
checkedIn BOOL,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (checkoutID),
FOREIGN KEY (itemID) REFERENCES items(itemID),
FOREIGN KEY (personID) REFERENCES people(personID),
FOREIGN KEY (projectID) REFERENCES projects(projectID)
);
However, when I run this script I get the following error messages:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '}' at line 8
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '}' at line 17
ERROR 1215 (HY000): Cannot add foreign key constraint
I suspect it has something to do with using the bool datatype since that is what is happening at lines 8 and 17; however I have not been able to find any solutions to this issue online. Is someone able to see what might be causing the problem? I'm running MySQL 5.6.11 on my local Windows machine.
Thanks!
The characters need to be the same. Use parentheses for both
CREATE TABLE homes (
};
In this case: It's showing you the incorrect character in the error message:
check the manual that corresponds to your MySQL server version for the right
syntax to use near '}' at line 8
Get rid of the curly braces and replace with (
Works great : http://sqlfiddle.com/#!8/4ad37
Try this one, I tested it and it created the database successfully. The issues were caused due to the opening and closing brackets not matching each other.
CREATE DATABASE IF NOT EXISTS magicc_hat;
USE magicc_hat;
CREATE TABLE people (
personID INT NOT NULL AUTO_INCREMENT,
firstName VARCHAR(45) NOT NULL,
lastName VARCHAR(45),
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (personID)
);
CREATE TABLE categories (
categoryID INT NOT NULL AUTO_INCREMENT,
categoryName VARCHAR(45) NOT NULL,
description TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (categoryID)
);
CREATE TABLE homes (
homeID INT NOT NULL AUTO_INCREMENT,
homeName VARCHAR(45) NOT NULL,
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (homeID)
);
CREATE TABLE items (
itemID INT NOT NULL AUTO_INCREMENT,
itemName VARCHAR(100) NOT NULL,
identifier VARCHAR(100),
quantity INT NOT NULL,
categoryID INT NOT NULL,
homeID INT NOT NULL,
itemStatus ENUM('normal', 'broken', 'missing') NOT NULL DEFAULT 'normal',
description TEXT,
image VARCHAR(45),
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (itemID),
FOREIGN KEY (categoryID) REFERENCES categories(categoryID),
FOREIGN KEY (homeID) REFERENCES homes(homeID)
);
CREATE TABLE projects (
projectID INT NOT NULL AUTO_INCREMENT,
projectName VARCHAR(45) NOT NULL,
description TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (projectID)
);
CREATE TABLE checkouts (
checkoutID INT NOT NULL AUTO_INCREMENT,
itemID INT NOT NULL,
personID INT NOT NULL,
projectID INT,
quantity INT NOT NULL,
outDateTime DATETIME,
inDateTime DATETIME,
outNotes TEXT,
inNotes TEXT,
checkedIn BOOL,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (checkoutID),
FOREIGN KEY (itemID) REFERENCES items(itemID),
FOREIGN KEY (personID) REFERENCES people(personID),
FOREIGN KEY (projectID) REFERENCES projects(projectID)
);
you have 2 syntax errors as below...
And since below tables are not created, you are facing foreign key error..
run below sqls your problem will be solved :)
CREATE TABLE homes (
homeID INT NOT NULL AUTO_INCREMENT,
homeName VARCHAR(45) NOT NULL,
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (homeID)
); /* previously }; */
CREATE TABLE items (
itemID INT NOT NULL AUTO_INCREMENT,
itemName VARCHAR(100) NOT NULL,
identifier VARCHAR(100),
quantity INT NOT NULL,
categoryID INT NOT NULL,
homeID INT NOT NULL,
itemStatus ENUM('normal', 'broken', 'missing') NOT NULL DEFAULT 'normal',
description TEXT,
image VARCHAR(45),
notes TEXT,
archived BOOL NOT NULL DEFAULT '0',
PRIMARY KEY (itemID),
FOREIGN KEY (categoryID) REFERENCES categories(categoryID),
FOREIGN KEY (homeID) REFERENCES homes(homeID)
); /* previously }; */
Related
I got this error from mysql its not creating my table as suppose since there is an error from it the code is the following
CREATE TABLE customers (
customer_number INT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
address VARCHAR(60) NOT NULL,
shipping_adress VARCHAR(60) NOT NULL,
billing_adress VARCHAR(60) NOT NULL,
balance_due DATE NOT NULL,
UNIQUE (customer_number)
);
CREATE TABLE orders (
order_number INT PRIMARY KEY NOT NULL,
customer_number VARCHAR(20) NOT NULL,
order_date DATE NOT NULL,
order_total_cost INT NOT NULL,
FOREIGN KEY (customer_number) REFERENCES customers(customer_number)
);
CREATE TABLE inventory (
product_number INT PRIMARY KEY,
product_name VARCHAR(60) NOT NULL,
product_measure FLOAT NOT NULL,
product_price FLOAT NOT NULL,
);
CREATE TABLE ordered_products (
ordered_id INT PRIMARY KEY,
order_number INT NOT NULL,
product_number INT NOT NULL,
quantity INT NOT NULL CHECK (QUANTITY > 0),
unit_price FLOAT NOT NULL,
FOREIGN KEY (order_number) REFERENCES orders(order_number),
FOREIGN KEY (product_number) REFERENCES inventory(product_number)
);
and the errror is as follows
ERROR 1064 (42000) at line 20: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 6
customer_number is a INT in customers table but VARCHAR(20) in the orders table
Ran on MySQL Compiler and it was successful
CREATE TABLE customers (
customer_number INT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
address VARCHAR(60) NOT NULL,
shipping_adress VARCHAR(60) NOT NULL,
billing_adress VARCHAR(60) NOT NULL,
balance_due DATE NOT NULL
);
CREATE TABLE orders (
order_number INT PRIMARY KEY NOT NULL,
customer_number INT NOT NULL,
order_date DATE NOT NULL,
order_total_cost INT NOT NULL,
FOREIGN KEY (customer_number) REFERENCES customers(customer_number)
);
CREATE TABLE inventory (
product_number INT PRIMARY KEY,
product_name VARCHAR(60) NOT NULL,
product_measure FLOAT NOT NULL,
product_price FLOAT NOT NULL
);
CREATE TABLE ordered_products (
ordered_id INT PRIMARY KEY,
order_number INT NOT NULL,
product_number INT NOT NULL,
quantity INT NOT NULL CHECK (QUANTITY > 0),
unit_price FLOAT NOT NULL,
FOREIGN KEY (order_number) REFERENCES orders(order_number),
FOREIGN KEY (product_number) REFERENCES inventory(product_number)
);
So the thing is I added a extra comma in the
CREATE TABLE inventory (
product_number INT PRIMARY KEY,
product_name VARCHAR(60) NOT NULL,
product_measure FLOAT NOT NULL,
product_price FLOAT NOT NULL,
);
So I just removed the comma from
product_price FLOAT NOT NULL
and it worked.
Commas can really be a pain if not using a mysql workbench
Using these tables
CREATE TABLE people(
idPeople INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
email VARCHAR(100),
PRIMARY KEY(idPeople)
);
CREATE TABLE country_details (
idCountry INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
country VARCHAR(45)
);
CREATE TABLE registration_details(
idRegistration INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
register_date DATETIME NOT NULL
);
CREATE TABLE registration(
idRegistration INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
idPeople INT UNSIGNED NOT NULL DEFAULT 0,
idCountry INT UNSIGNED NOT NULL DEFAULT 0,
register_date_id INT UNSIGNED NOT NULL DEFAULT 0,
name VARCHAR(45) NOT NULL DEFAULT '',
email VARCHAR(45) NOT NULL DEFAULT '',
register_date VARCHAR(45) NOT NULL DEFAULT '',
country VARCHAR(45) NOT NULL DEFAULT '',
FOREIGN KEY(idCountry) REFERENCES country_details(idCountry),
FOREIGN KEY(register_date_id) REFERENCES
registration_details(register_date_id),
FOREIGN KEY(idPeople) REFERENCES people(idPeople)
);
I am getting the error:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (peopledb.registration, CONSTRAINT registration_ibfk_1 FOREIGN KEY (idCountry) REFERENCES country_details (idCountry))
Anyone know what could be happening?
Foreign keys ensure that a parent table (in your case country_details) holds a value that is replicated in the child table (registration).
What's happening is that you're trying to input data into registration that attempts to reference a country id that doesn't exist in the country_details table.
I am new to relational database architecture creation. I am creating a very basic one where I have two data tables.
CREATLE TABLE Authors (
idAuthors INT NOT NULL auto_increment,
Name VARCHAR(50) NOT NULL,
Birthplace VARCHAR(50) NOT NULL,
PRIMARY KEY (idAuthors));
CREATLE TABLE Shops(
idShops NOT NULL auto_increment,
Name_shop VARCHAR(50) NOT NULL,
Name_authors VARCHAR(50) NOT NULL,
Location VARCHAR(50) NOT NULL
PRIMARY KEY (idShops));
My question is if you should have a foreign key in each table or an intermediate table that would be:
CREATLE TABLE intermediate(
PRIMARY KEY (idShops),
PRIMARY KEY (idAuthors));
Is this correct?
Thank you!
Something like
CREATE TABLE Authors (
idAuthors INT NOT NULL auto_increment,
NameAuthor VARCHAR(50) NOT NULL DEFAULT 'unknown',
BirthPlace VARCHAR(50) NOT NULL DEFAULT 'not defined',
PRIMARY KEY (idAuthors)
);
CREATE TABLE Shops(
idShops INT NOT NULL auto_increment,
NameShop VARCHAR(50) NOT NULL 'unspecified',
Location VARCHAR(50) NOT NULL 'not specified',
PRIMARY KEY (idShops)
);
CREATE TABLE Authors_Shops (
idAuthors INT NOT NULL,
idShops INT NOT NULL,
PRIMARY KEY (idAuthors, idShops),
CONSTRAINT fk_Authors FOREIGN KEY (idAuthors) REFERENCES Authors (idAuthors),
CONSTRAINT fk_Shops FOREIGN KEY (idShops) REFERENCES Shops (idShops)
);
I tried looking up the reasons why it's failing but I can't find the different data types issues mentioned in the solutions in my tables.
I'm getting the error when I'm trying to run the following:
comments (id int NOT NULL AUTO_INCREMENT,
user_id varchar(10) NOT NULL,
blog_id int NOT NULL,
comment varchar(150) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(user_id) REFERENCES user_details(id),
FOREIGN KEY(blog_id) REFERENCES blog(id));
existing data bases for primary keys:
user_details (id varchar(10) NOT NULL ,
name varchar(70) NOT NULL,
email varchar(40) NOT NULL,
salt varchar(40) NOT NULL,
masked_password varchar(40) NOT NULL,
is_active varchar(10) DEFAULT 'False');
blog (id int NOT NULL AUTO_INCREMENT,
category_id int NOT NULL,
title varchar(240) NOT NULL,
body longtext NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(category_id) REFERENCES blog_catagories(id));
Let me know where I'm going wrong
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)