SQL schema will not build - obscure error reference to syntax near ')' - mysql

I'm learning mySQL and running into syntax errors that I can't seem to fix. I get an error that states that "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" line 8 being the declaration of state.
I am checking my code with sqlfiddle and mySQL 5.6 with the following:
CREATE TABLE `customer` (
`customer_id` int(10) NOT NULL,
`first_name` varchar(20) NOT NULL,
`last_name` varchar(20) NOT NULL,
`address` varchar(150) NOT NULL,
`apartment` int(10) DEFAULT NULL,
`city` varchar(30) NOT NULL,
`state` varchar(30) NOT NULL,
`zip` varchar(10) NOT NULL,
`home_phone` varchar(10) DEFAULT NULL,
`cell_phone` varchar(10) DEFAULT NULL,
`other_phone` varchar(10) DEFAULT NULL,
PRIMARY KEY (`customer_id`)
);
CREATE TABLE `donut` (
`donut_id` int(10) NOT NULL,
`name` varchar(60) NOT NULL,
`description` varchar(150) DEFAULT NULL,
`unit_price` int(4) NOT NULL,
PRIMARY KEY (`donut_id`)
);
CREATE TABLE `order` (
`order_id` int(10) NOT NULL,
`customer_id` int(10) NOT NULL,
`order_date` date DEFAULT NULL,
`handling_notes` varchar(300),
PRIMARY KEY (`order_id`),
FOREIGN KEY (`customer_id`)
);
CREATE TABLE `order_line` (
`order_id` int(10) NOT NULL,
`donut_id` int(10) NOT NULL,
`donut_quantity` int(6) NOT NULL,
PRIMARY KEY (`order_id`, `donut_id`)
);

The FOREIGN KEY clause in the CREATE TABLE statement for order misses a REFERENCES clause.
Presumably you want to replace
FOREIGN KEY (`customer_id`)
with:
FOREIGN KEY (`customer_id`)
REFERENCES `customer`
(`customer_id`)

Related

Error code: 1824 Failed to open the refernced table [table exists]

I'm trying to add a foreign key constraint, but I'm getting [Error Code: 1824: Failed to open the refrenced table 'agent_agent']
the table agent_agent exists in my database, I can't get my head around this issue.
I tried creating the constraint using the graphical mode of mysql workbench and I'm getting the same error.
select * from agent_agent; return the data, so the table exists in the DB but:
ALTER TABLE demande_contact
ADD CONSTRAINT dddd
FOREIGN KEY (agent_id_id) REFERENCES agent_agent(id);
is returning an error 1824.
Edit: Here's the 2 tables DDL
CREATE TABLE `agent_agent` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`matricule` varchar(6) NOT NULL,
`nom_et_prenom` varchar(60) NOT NULL,
`fonction` varchar(60) NOT NULL,
`structure` varchar(60) NOT NULL,
`poste_org` varchar(60) DEFAULT NULL,
`metier` varchar(3) DEFAULT NULL,
`csp` varchar(20) DEFAULT NULL,
`en_activite` tinyint(1) NOT NULL,
`timestamp` datetime(6) NOT NULL,
`updated` datetime(6) NOT NULL,
`user_id` int(11) NOT NULL,
`service` varchar(60) DEFAULT NULL,
`centre_cout` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `agent_agent_user_id_bfcb5c50` (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=628 DEFAULT CHARSET=utf8
CREATE TABLE `demande_contact` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`telephone` varchar(40) NOT NULL,
`agent_id_id` int(11) NOT NULL,
`demande_id_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `demande_contact_demande_id_id_0abc00b0_fk_demande_d` (`demande_id_id`),
KEY `agent_id_id_constraint_idx` (`agent_id_id`),
CONSTRAINT `demande_contact_demande_id_id_0abc00b0_fk_demande_d` FOREIGN KEY (`demande_id_id`) REFERENCES `demande_demandetransport` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

How to use "ON UPDATE CASCADE" Correctly in MariaDB 10.1.37 / Ver 15.1?

I am experiencing trouble getting ON UPDATE CASCADE to work with a CONSTRAINT. If I use UPDATE to change the value of customerName in the customer table, it will not change the customerName value in the city table. No error message shows up.
The version of the MariaDB:
Ver 15.1 Distrib 10.1.37-MariaDB
My city table when using SHOW CREATE TABLE city:
city | CREATE TABLE `city` (
`cityId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`city` varchar(50) DEFAULT NULL,
`countryId` int(10) unsigned DEFAULT NULL,
`customerName` varchar(50) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`postalCode` varchar(50) DEFAULT NULL,
`phone` varchar(50) DEFAULT NULL,
`createDate` varchar(50) DEFAULT NULL,
`createdBy` varchar(50) DEFAULT NULL,
`lastUpdateBy` varchar(50) DEFAULT NULL,
PRIMARY KEY (`cityId`),
KEY `customerNameChange01` (`customerName`),
CONSTRAINT `customerNameChange01` FOREIGN KEY (`customerName`)
REFERENCES `customer` (`customerName`)
ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
My customer table SHOW CREATE TABLE customer:
customer | CREATE TABLE `customer` (
`customerId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`customerName` varchar(50) DEFAULT NULL,
`addressId` int(10) unsigned DEFAULT NULL,
`active` int(10) unsigned DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`city` varchar(50) DEFAULT NULL,
`postalCode` varchar(50) DEFAULT NULL,
`phone` varchar(50) DEFAULT NULL,
`createDate` varchar(50) DEFAULT NULL,
`createdBy` varchar(50) DEFAULT NULL,
`lastUpdateBy` varchar(50) DEFAULT NULL,
PRIMARY KEY (`customerId`),
KEY `CustomerName` (`customerName`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
These are the commands I used to create the index and CONSTRAINT:
CREATE INDEX CustomerName ON customer (customerName);
ALTER TABLE city
ADD CONSTRAINT customerNameChange01
FOREIGN KEY (customerName)
REFERENCES customer (customerName)
ON UPDATE CASCADE
ON DELETE SET NULL;
In the customer table, the CustomerName key references an index. Otherwise, I would not have been able to put in the CONSTRAINT in the city table.
Update: The code works fine in DB Fiddle for MariaDB 10.2 and personal testing confirms that the example code from there works in my own database as well.
Thank you for spending your time.

MySQL issue with CREATE TABLE film database

I am trying to create a table with multiple foreign keys which reference other parent tables. 1 of my foreign keys is being accepted 'fk_film_id' however the second is not due to a syntax error. how can I get this to pass?
the body of my tables is below:
create table `Actor_Role` (
`Actor_Role_id` INT AUTO_INCREMENT,
`Actor_Role` VARCHAR(20) NOT NULL,
`Character_Name` VARCHAR(50) NOT NULL,
`Alias_Name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`Actor_Role_id`)
);
create table `Actor` (
`Actor_id` INT AUTO_INCREMENT,
`First_Name` VARCHAR(20) NOT NULL,
`Last_Name` VARCHAR(50) NOT NULL,
`Date_of_Birth` DATE NOT NULL,
`Nationality` VARCHAR(20) NOT NULL,
`Gender` VARCHAR(10) NOT NULL,
`Residence` VARCHAR(20) NOT NULL,
PRIMARY KEY (`Actor_id`)
);
create table `Film` (
`Film_id` INT AUTO_INCREMENT,
`Title` VARCHAR(50) NOT NULL,
`Release_Date` DATE NOT NULL,
`Running_Time` INT NOT NULL,
`Budget` BIGINT NOT NULL,
`Box_Office` BIGINT NOT NULL,
`Rating` INT NOT NULL,
`Language` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Film_id`)
);
create table `Film_Staff` (
`Staff_id` INT AUTO_INCREMENT,
`First_Name` VARCHAR(20) NOT NULL,
`Last_Name` VARCHAR(50) NOT NULL,
`Nationality` VARCHAR(20) NOT NULL,
PRIMARY KEY (`Staff_id`)
);
create table `Staff_Role` (
`Staff_Role_id` INT AUTO_INCREMENT,
`Staff_Role` VARCHAR(20) NOT NULL,
PRIMARY KEY (`Staff_Role_id`)
);
create table `Infinity_Stones` (
`Stone_id` INT AUTO_INCREMENT,
`Colour` VARCHAR(10) NOT NULL,
`Power` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Stone_id`)
);
The table I am trying to make looks like this:
CREATE TABLE Film_Staff_Role(
-> `Film_id` INT NOT NULL,
-> `Role_id` INT NOT NULL,
-> `Staff_id` INT NOT NULL,
-> FOREIGN KEY fk_film_id(Film_id)
-> REFERENCES film(Film_id)
-> ON UPDATE CASCADE
-> ON DELETE RESTRICT
-> FOREIGN KEY fk_Role_id(Role_id)
-> REFERENCES Staff_Role(Staff_Role_id)
-> ON UPDATE CASCADE
-> ON DELETE RESTRICT
-> FOREIGN KEY fk_Staff_id(Staff_id)
-> REFERENCES Film_Staff(Staff_id)
-> ON UPDATE CASCADE
-> ON DELETE RESTRICT
-> )ENGINE=InnoDB;
However I receive the following issue when I attempt to execute:
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 'FOREIGN KEY fk_Staff_Role_id(Staff_Role_id)
REFERENCES Staff_Role(Staff_Role_id' at line 9
How can this be overcome?

MySQL foreign key not found even if it exists

I have 2 tables 'open_invoices' and 'paid_invoices' with the below structures:
CREATE TABLE `open_invoices` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`unique_identifier` varchar(255) NOT NULL,
`insert_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cust_nbr` int(11) NOT NULL,
`location` varchar(10) NOT NULL,
`company` varchar(10) NOT NULL,
`invoice_nbr` int(11) NOT NULL,
`future_letter` varchar(1) NOT NULL,
`invoice_total` decimal(8,2) NOT NULL,
`invoice_payments` decimal(8,2) NOT NULL,
`record_type` int(11) NOT NULL,
`terms_desc` varchar(255) NOT NULL,
`due_date` varchar(8) NOT NULL,
`discount_date` varchar(8) NOT NULL,
`orig_disc_avail` decimal(8,2) NOT NULL,
`cust_po` varchar(10) NOT NULL,
`invoice_date` varchar(8) NOT NULL,
`status` int(11) NOT NULL,
`check_no` varchar(10) NOT NULL,
`last_pay_date` varchar(8) NOT NULL,
`as_of_date` varchar(8) NOT NULL,
`as_of_time` varchar(11) NOT NULL,
`remaining` varchar(1) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_identifier` (`unique_identifier`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `paid_invoices` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date_change` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`invoice_date` varchar(8) NOT NULL,
`unique_identifier` varchar(255) NOT NULL,
`amount_paid` decimal(8,2) NOT NULL,
`amount_left` decimal(8,2) NOT NULL,
`payment_type` varchar(2) NOT NULL,
`last4` varchar(255) NOT NULL,
`transac_id` varchar(255) NOT NULL,
`customer_id` varchar(255) NOT NULL,
`payment_P_F` varchar(1) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (unique_identifier)
REFERENCES open_invoices (unique_identifier)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I'm trying to insert a row in 'paid_invoices' like below:
INSERT INTO `paid_invoices` (`invoice_date`, `unique_identifier`, `amount_paid`, `amount_left`, `payment_type`, `last4`, `transac_id`, `customer_id`, `payment_P_F`) VALUES ('07/19/18', '126_89948_2576', 0, '37.45', '', '', '', '13776', 'P')
I get the below error:
Error Number: 1452
Cannot add or update a child row: a foreign key constraint fails (`accounts_receivables`.`paid_invoices`, CONSTRAINT `paid_invoices_ibfk_1` FOREIGN KEY (`unique_identifier`) REFERENCES `open_invoices` (`unique_identifier`))
INSERT INTO `paid_invoices` (`invoice_date`, `unique_identifier`, `amount_paid`, `amount_left`, `payment_type`, `last4`, `transac_id`, `customer_id`, `payment_P_F`) VALUES ('07/19/18', '126_89948_2576', 0, '37.45', '', '', '', '13776', 'P')
If I search on google, it says that the foreign key 126_89948_2576 was not found in the table 'open_invoices'.
When I do SELECT * FROM open_invoices WHERE id = 2576 I see the row with the foreign key 126_89948_2576 as shown in the picture below :
[![query 1][1]][1]
but when I do this query SELECT * FROM open_invoices WHERE unique_identifier = '126_89948_2576' I get no result see capture below:
[![query 2][2]][2]
I'm sure there is no extra blank space in the value saved for foreign key 126_89948_2576.
What is going on please? Is there a bug in the version of MySQL I'm using?
Server: Localhost via UNIX socket
Server type: MySQL
Server version: 5.6.40 - MySQL Community Server (GPL)
Protocol version: 10
Thanks.
[1]: https://i.stack.imgur.com/L2QDX.png
[2]: https://i.stack.imgur.com/T9Fz5.png
It seems you are using wrong divider symbols, "_" instead of "-" in your query. Try unique_identifier = '126-89948-2576'
Because the underscore _ is a wildcard like the percent %, except that it only looks for one character.
SQL pattern matching enables you to use "_" to match any single character and "%" to match an arbitrary number of characters (including zero characters).
you can use
SELECT * FROM open_invoices WHERE unique_identifier like '%126\_89948\_2576%'

Cannot add foreign key constraint MySql Error

i've a school project and i'm trying to execute my sql script and i've this error
1215 - Cannot add foreign key constraint
Here's my table who generate this error:
CREATE TABLE IF NOT EXISTS `Visiteur` (
`id` char(4) NOT NULL,
`nom` char(30) DEFAULT NULL,
`prenom` char(30) DEFAULT NULL,
`login` char(20) DEFAULT NULL,
`mdp` char(20) DEFAULT NULL,
`adresse` char(30) DEFAULT NULL,
`cp` char(5) DEFAULT NULL,
`ville` char(30) DEFAULT NULL,
`dateEmbauche` date DEFAULT NULL,
`idRang` char(2)NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`idRang`) REFERENCES Rang(`idRang`)
) ENGINE=InnoDB;
And here my references table:
CREATE TABLE IF NOT EXISTS `Rang` (
`idRang` char(2) NOT NULL,
`nomRang` char(15) NOT NULL,
PRIMARY KEY (`idRang`)
) ENGINE=InnoDB;
Sorry for my english, i'm french
Thanks in advance