MySQL Missing comma before start of a new alter operation. (near "ADD" at position 255) - mysql

I have a problem
my code
_________
-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `country` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`alpha2` varchar(2) NOT NULL,
`alpha3` varchar(3) NOT NULL,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `alpha2` (`alpha2`),
UNIQUE KEY `alpha3` (`alpha3`)
) ENGINE=InnoDB;
--
-- `user` table structure
--
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`login` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`avatar` varchar(50) NOT NULL,
CONSTRAINT `user_pk` PRIMARY KEY (`id`),
CONSTRAINT `user_idx_1` UNIQUE INDEX (`login`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `user_address` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`address_line_1` varchar(40) NOT NULL,
`address_line_2` varchar(40) NOT NULL,
`address_line_3` varchar(40) NOT NULL,
`address_line_4` varchar(40) NOT NULL,
`address_line_5` varchar(40) NOT NULL,
`postal_code` varchar(40) NOT NULL,
`city_name` varchar(40) NOT NULL,
`country_code` varchar(3) NOT NULL,
CONSTRAINT `user__address_pk` PRIMARY KEY (`id`),
INDEX `user_address_idx_1` (`user_id`),
INDEX `user_address_idx_2` (`country_code`),
CONSTRAINT `user_address_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
CONSTRAINT `user_address_ibfk_2` FOREIGN KEY (`country_code`) REFERENCES `country` (`alpha3`)
) ENGINE=InnoDB;
ALTER TABLE `user`
ADD COLUMN IF NOT EXISTS `shipping_address` int(11) NULL,
ADD COLUMN IF NOT EXISTS `billing_address` int(11) NULL,
ADD CONSTRAINT IF NOT EXISTS `user_ibfk_2` FOREIGN KEY (`shipping_address`) REFERENCES `user_address` (`id`)
ADD CONSTRAINT IF NOT EXISTS `user_ibfk_3` FOREIGN KEY (`billing_address`) REFERENCES `user_address` (`id`);
CREATE INDEX IF NOT EXISTS `user_idx_2` ON `user`(`shipping_address`);
CREATE INDEX IF NOT EXISTS `user_idx_3` ON `user`(`billing_address`);
CREATE TABLE IF NOT EXISTS `product_types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(50) NOT NULL,
`description` varchar(4000) DEFAULT NULL,
CONSTRAINT `product_types_pk` PRIMARY KEY (`id`),
CONSTRAINT `product_types_idx_1` UNIQUE INDEX (`type`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(15) NOT NULL,
`name` varchar(70) NOT NULL,
`type` int(11) NOT NULL,
`scale` varchar(10) NOT NULL,
`vendor` varchar(50) NOT NULL,
`description` text NOT NULL,
`stock_level` smallint(6) NOT NULL,
`cost` decimal(10,2) NOT NULL,
`price` decimal(10,2) NOT NULL,
CONSTRAINT `product_pk` PRIMARY KEY (`id`),
INDEX `product_idx_2` (`type`),
CONSTRAINT `product_idx_1` UNIQUE INDEX (`code`),
CONSTRAINT `products_ibfk_1` FOREIGN KEY (`type`) REFERENCES `product_types` (`id`)
) ENGINE=InnoDB;
ALTER TABLE `products` ADD FULLTEXT(`description`);
CREATE TABLE IF NOT EXISTS `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_date` timestamp NOT NULL DEFAULT current_timestamp(),
`shipped_date` timestamp DEFAULT NULL,
`status` varchar(15) NOT NULL,
`customer_id` int(11) NOT NULL,
CONSTRAINT `order_pk` PRIMARY KEY (`id`),
INDEX `order_idx_1` (`customer_id`),
CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `order_lines` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
`quantity` smallint(6) NOT NULL,
`cost` decimal(10,2) NOT NULL,
`price` decimal(10,2) NOT NULL,
CONSTRAINT `order_lines_pk` PRIMARY KEY (`id`),
INDEX `order_lines_idx_1` (`order_id`),
CONSTRAINT `order_lines_idx_2` UNIQUE INDEX (`product_id`),
CONSTRAINT `order_lines_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`),
CONSTRAINT `order_lines_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `order_address` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) NOT NULL,
`type` ENUM ('SHIPPING', 'BILLING'),
`address_line_1` varchar(40) NOT NULL,
`address_line_2` varchar(40) NOT NULL,
`address_line_3` varchar(40) NOT NULL,
`address_line_4` varchar(40) NOT NULL,
`address_line_5` varchar(40) NOT NULL,
`postal_code` varchar(40) NOT NULL,
`city_name` varchar(40) NOT NULL,
`country_code` varchar(3) NOT NULL,
CONSTRAINT `order_address_pk` PRIMARY KEY (`id`),
INDEX `order_address_idx_1` (`user_id`),
INDEX `order_address_idx_2` (`country_code`),
CONSTRAINT `order_address_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`),
CONSTRAINT `order_address_ibfk_2` FOREIGN KEY (`country_code`) REFERENCES `country` (`alpha3`)
) ENGINE=InnoDB;
_________________________
ERROR
Static analysis:
1 errors were found during analysis.
Missing comma before start of a new alter operation. (near "ADD" at position 255)
SQL query:
ALTER TABLE `user`
ADD COLUMN IF NOT EXISTS `shipping_address` int(11) NULL,
ADD COLUMN IF NOT EXISTS `billing_address` int(11) NULL,
ADD CONSTRAINT IF NOT EXISTS `user_ibfk_2` FOREIGN KEY (`shipping_address`) REFERENCES `user_address` (`id`)
ADD CONSTRAINT IF NOT EXISTS `user_ibfk_3` FOREIGN KEY (`billing_address`) REFERENCES `user_address` (`id`)
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FOREIGN KEY (`shipping_address`) REFERENCES `user_address` (`id`) ADD CONS...' at line 4

You don't have a comma separating your last two lines of SQL.
Please use this:
ALTER TABLE `user`
ADD COLUMN IF NOT EXISTS `shipping_address` int(11) NULL,
ADD COLUMN IF NOT EXISTS `billing_address` int(11) NULL,
ADD CONSTRAINT IF NOT EXISTS `user_ibfk_2` FOREIGN KEY (`shipping_address`) REFERENCES `user_address` (`id`),
ADD CONSTRAINT IF NOT EXISTS `user_ibfk_3` FOREIGN KEY (`billing_address`) REFERENCES `user_address` (`id`)
SQL will separate the interests of different statements with the Comma, This works similar to a Javascript Semi colon

Related

MySQL "Foreign key constraint is incorrectly formed"

I have the follwing SQL commands:
CREATE TABLE IF NOT EXISTS `users`
(
`id` INTEGER NOT NULL auto_increment,
`username` VARCHAR(255) NOT NULL UNIQUE,
`password` VARCHAR(255) NOT NULL,
`root` TINYINT(1) DEFAULT 0,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
UNIQUE `username_unique` (`username`),
PRIMARY KEY (`id`)
)
engine=innodb;
CREATE TABLE IF NOT EXISTS `domains`
(
`id` INTEGER NOT NULL auto_increment,
`domain` VARCHAR(255) NOT NULL UNIQUE,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
`userid` INTEGER,
UNIQUE `domain_unique` (`domain`),
PRIMARY KEY (`id`),
FOREIGN KEY (`userid`) REFERENCES `users` (`id`) ON DELETE SET NULL ON
UPDATE CASCADE
)
engine=innodb;
CREATE TABLE IF NOT EXISTS `aliases`
(
`id` INTEGER NOT NULL auto_increment,
`source_username` VARCHAR(255) NOT NULL,
`source_domain` VARCHAR(255) NOT NULL,
`destination_username` VARCHAR(255) NOT NULL,
`destination_domain` VARCHAR(255) NOT NULL,
`enabled` TINYINT(1) NOT NULL DEFAULT 1,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`source_domain`) REFERENCES `domains` (`domain`)
)
engine=innodb;
CREATE TABLE IF NOT EXISTS `accounts`
(
`id` INTEGER NOT NULL auto_increment,
`username` VARCHAR(255) NOT NULL,
`domain` VARCHAR(255) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`quota` INTEGER NOT NULL DEFAULT 500,
`enabled` TINYINT(1) NOT NULL DEFAULT 1,
`sendonly` TINYINT(1) NOT NULL DEFAULT 0,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`domain`) REFERENCES `domains` (`domain`)
)
engine=innodb;
if i try to run these i get the message:
"Foreign key constraint is incorrectly formed"
for the tables Alias and Accounts.
Its not possible to use primary keys in alias and accounts
Im using MariaDB 10.2
I hope somebody can tell me what is wrong with these statements.
You need to add a key/index to the domain field of the domains table, in order for it to be used as Foreign Key by another table.
If the table is already created and you want to add an index, use this.
CREATE INDEX domain ON domains(domain);
OR add the index while creating the table -
CREATE TABLE IF NOT EXISTS `domains`
(
`id` INTEGER NOT NULL auto_increment,
`domain` VARCHAR(255) NOT NULL UNIQUE,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
`userid` INTEGER,
UNIQUE `domain_unique` (`domain`),
PRIMARY KEY (`id`),
INDEX domain (domain),
FOREIGN KEY (`userid`) REFERENCES `users` (`id`) ON DELETE SET NULL ON
UPDATE CASCADE
)
engine=innodb;
For further reading (official documentation) regarding Foreign Key Contraint Errors on MariaDB this URL
You define an integer primary key. Use it:
CREATE TABLE IF NOT EXISTS `aliases`
(
`id` INTEGER NOT NULL auto_increment,
`source_username` VARCHAR(255) NOT NULL,
`source_domain_id` INTEGER NOT NULL,
`destination_username` VARCHAR(255) NOT NULL,
`destination_domain` VARCHAR(255) NOT NULL,
`enabled` TINYINT(1) NOT NULL DEFAULT 1,
`createdat` DATETIME NOT NULL,
`updatedat` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`source_domain_id`) REFERENCES `domains` (`id`)
)
engine=innodb;

not able to apply foreign key constraint in mysql

I can run the below query and there are no nulls in the user_id column in the result -
select c.*,r.user_id from chat_group_users c left join roleuser r
on c.user_id = r.user_id;
But I cannot apply the foreign key constraint and mysql just says
Error Code: 1215. Cannot add foreign key constraint
The command i wrote -
ALTER TABLE chat_group_users
ADD CONSTRAINT fk_roleuser FOREIGN KEY (user_id) REFERENCES roleuser (user_id)
ON DELETE cascade ON UPDATE cascade;
The table structures -
CREATE TABLE `roleuser` (
`User_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`UserNM` varchar(50) NOT NULL,
`UserPS` varchar(150) NOT NULL,
`r_username` varchar(50) DEFAULT NULL,
`UGroup` varchar(5) NOT NULL DEFAULT 'Usar',
`FirstName` varchar(45) NOT NULL,
`LastName` varchar(45) NOT NULL,
PRIMARY KEY (`User_ID`),
UNIQUE KEY `Index_2` (`UserNM`),
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=latin1;
CREATE TABLE `chat_group_users` (
`auto_id` int(11) NOT NULL AUTO_INCREMENT,
`group_id` int(11) DEFAULT NULL,
`user_id` int(10) DEFAULT NULL,
`is_admin` varchar(5) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`auto_id`),
UNIQUE KEY `unique_user_group` (`user_id`,`group_id`),
KEY `fc_group_id` (`group_id`),
CONSTRAINT `fc_group_id` FOREIGN KEY (`group_id`)
REFERENCES `chat_group`(`auto_id`)
ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
missed the unsigned on the chat_group_users -
`User_ID` int(10) unsigned NOT NULL AUTO_INCREMENT, ...

Cannot add foreign key constraint

I have created a database "webportal". and this is my "user" table script
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`firstName` varchar(255) DEFAULT NULL,
`lastName` varchar(255) DEFAULT NULL,
`dateRegistered` DATE DEFAULT NULL,
`skypeID` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
and this one is "catalog" table script.
DROP TABLE IF EXISTS `catalog`;
create table catalog(
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`link` VARCHAR(100) NOT NULL,
`comment` VARCHAR(100) NOT NULL,
`inserDate` DATE DEFAULT NULL,
`content` longblob NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_catalog` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and when I try to execute the second script in the command line, I get this error...
ERROR 1215 (HY000): Cannot add foreign key constraint.
What is wrong with this code?
It seems you are using a old version of MySQL, you can add a INDEX clause to foreign key field to fix the problem:
DROP TABLE IF EXISTS `catalog`;
create table `catalog`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`link` VARCHAR(100) NOT NULL,
`comment` VARCHAR(100) NOT NULL,
`inserDate` DATE DEFAULT NULL,
`content` longblob NOT NULL,
PRIMARY KEY (`id`),
INDEX (`user_id`),
CONSTRAINT `fk_catalog` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

mysql composite foreign key referencing more than 2 attributes

Hi I have a table with 3 attributes as primary key
Products:
PRIMARY KEY (product_name,category,product_type).
Now i am referencing this composite primary key in another table
order_details:
foreign key(product_name,product_type,category) references products(product_name,product_type,category)
however I am getting an error in the console saying missing parenthesis and I am not able to add the foreign key. But if i add only 2 column names in the reference(example : "foreign key(product_name,product_type,category) references products(product_name,product_type)
") the query is not giving an error.
Please help me to resolve this issue. Please find my code below
CREATE TABLE `products` (
`product_name` varchar(45) NOT NULL,
`product_type` varchar(45) NOT NULL,
`category` varchar(45) NOT NULL,
`product_desc` varchar(150) DEFAULT NULL,
`unit_price` int(11) NOT NULL,
`supplier_id` int(11) NOT NULL,
`units_in_stock` int(11) NOT NULL,
PRIMARY KEY (`product_name`,`category`,`product_type`),
INDEX (product_name,category,product_type),
CONSTRAINT `supplier_prod_table_fkey` FOREIGN KEY (`supplier_id`) REFERENCES
`supplier` (`supplier_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=INNODB;
CREATE TABLE `order_details` (
`order_id` int(11) NOT NULL AUTO_INCREMENT,
`product_name` varchar(45) NOT NULL,
`product_type` varchar(45) NOT NULL,
`category` varchar(45) NOT NULL,
`quantity` int(11) DEFAULT NULL,
CONSTRAINT `orderid_fkey` FOREIGN KEY (`order_id`) REFERENCES `orders`
(`order_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
PRIMARY KEY (`order_id`,`product_name`,`product_type`,`category`),
INDEX (product_name,product_type,category),
foreign key(product_name,product_type,category) references products(product_name,product_type,category)
);
Fields in REFERENCES and in correspondent indices must go in the same order in both tables, i.e. (product_name, category, product_type).
CREATE TABLE `order_details` (
`order_id` int(11) NOT NULL AUTO_INCREMENT,
`product_name` varchar(45) NOT NULL,
`product_type` varchar(45) NOT NULL,
`category` varchar(45) NOT NULL,
`quantity` int(11) DEFAULT NULL,
CONSTRAINT `orderid_fkey` FOREIGN KEY (`order_id`) REFERENCES `orders`
(`order_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
PRIMARY KEY (`order_id`,`product_name`,`product_type`,`category`),
INDEX (product_name,category,product_type),
FOREIGN KEY(product_name,category,product_type) REFERENCES products(product_name,category,product_type)
);

Cannot create foreign key in MySQL error: 150

I have created the following tables and have no idea why my foreign key constraint script is not working.
CREATE TABLE IF NOT EXISTS `project` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`project_id` varchar(60) NOT NULL,
`project_name` varchar(500) NOT NULL,
`cons_bal` int(11) NOT NULL,
`non_cons_bal` int(11) NOT NULL,
`budget_head` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
And here is my another table:
CREATE TABLE IF NOT EXISTS `project_map` (
`id` int(11) NOT NULL,
`project_id` varchar(60) NOT NULL,
`head` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And here is my add constraint line:
alter table project_map add constraint p_map_fk001 foreign key (`project_id`) references project(`project_id`)
Any help would be nice. Thank you.
In the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.
Refer: http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
So try:
CREATE TABLE `project` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`project_id` varchar(60) NOT NULL,
`project_name` varchar(500) NOT NULL,
`cons_bal` int(11) NOT NULL,
`non_cons_bal` int(11) NOT NULL,
`budget_head` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `project_id` (`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `project_map` (
`id` int(11) NOT NULL,
`project_id` varchar(60) NOT NULL,
`head` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `project_id` (`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
ALTER TABLE `project_map` ADD CONSTRAINT
`p_map_fk001` FOREIGN KEY (`project_id`)
REFERENCES project(`project_id`);