MySQL "Foreign key constraint is incorrectly formed" - mysql

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;

Related

Alter table foreign key constraint fails

When I try to do this:
ALTER TABLE credit_card ADD CONSTRAINT fk_company FOREIGN KEY (company_id) REFERENCES company (id);
I got the error a foreign key constraint fails, but I don't understand why. The company_id column has same type of company.id column as you can see:
Company table
CREATE TABLE `company` (
`id` bigint NOT NULL AUTO_INCREMENT,
`code` varchar(10) NOT NULL,
`description` varchar(50) NOT NULL,
`currency_id` bigint NOT NULL,
`language_id` bigint DEFAULT NULL,
`address_id` bigint DEFAULT NULL,
`creation_time` datetime NOT NULL,
`modification_time` datetime DEFAULT NULL,
`deleted` tinyint(1) NOT NULL DEFAULT '0',
`cost_center_modifiable` tinyint(1) DEFAULT NULL,
`use_colleague_cost_center` tinyint(1) DEFAULT NULL,
`fixed_guest_company` varchar(50) DEFAULT NULL,
`info_number` int DEFAULT NULL,
`use_kilometric_rate` tinyint(1) DEFAULT NULL,
`use_multiple_km_rate` tinyint(1) DEFAULT NULL,
`email_required` tinyint(1) DEFAULT NULL,
`logo` varchar(255) DEFAULT NULL,
`holding_id` bigint NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `code` (`code`),
KEY `fk_company_address1` (`address_id`),
KEY `fk_company_currency1` (`currency_id`),
KEY `company_language_FK` (`language_id`),
KEY `company_holding_FK` (`holding_id`),
CONSTRAINT `company_currency_FK` FOREIGN KEY (`currency_id`) REFERENCES `currency` (`id`),
CONSTRAINT `company_holding_FK` FOREIGN KEY (`holding_id`) REFERENCES `holding` (`id`),
CONSTRAINT `company_language_FK` FOREIGN KEY (`language_id`) REFERENCES `language` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=142 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
Credit_card table
CREATE TABLE `credit_card` (
`id` bigint NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`pan` varchar(16) NOT NULL,
`account` varchar(50) DEFAULT NULL,
`begin_date` date NOT NULL,
`end_date` date NOT NULL,
`disabled` tinyint(1) NOT NULL DEFAULT '0',
`creation_time` datetime NOT NULL,
`modification_time` datetime DEFAULT NULL,
`company_id` bigint NOT NULL,
PRIMARY KEY (`id`),
KEY `username` (`username`),
KEY `pan` (`pan`),
CONSTRAINT `fk_credit_card_user_ext` FOREIGN KEY (`username`) REFERENCES `users_extension` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=64 DEFAULT CHARSET=utf8mb3
Dont make auto increment columns as foreign keys or unless not sure, dont reference these to autoincrement columns as it may end up making the table with foreign key issue a constraint violation if redundant entries are expected to be made in either of the tables.
Might be the case the related data is already ingested and then the table is altered

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

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

Foreign key referring to primary key in the same table Mysql

# Sql to create userdetails table:
CREATE TABLE `userdetails` (
`user_details_id` int(3) unsigned NOT NULL AUTO_INCREMENT,
`user_group_id` int(1) unsigned NOT NULL,
`name` varchar(50) NOT NULL,
`email_id` varchar(50),
`password` varchar(50) NOT NULL,
`mobile_no` varchar(10) NOT NULL,
`company_id` int(3) unsigned,
`vehicle_id` varchar(10),
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`date_updated` datetime NOT NULL,
`created_by` int(3) unsigned NOT NULL,
`status` enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (`user_details_id`),
UNIQUE KEY `email_id` (`email_id`),
FOREIGN KEY (`user_group_id`) REFERENCES `usergroups` (`user_group_id`),
FOREIGN KEY (`company_id`) REFERENCES `companies` (`company_id`),
FOREIGN KEY (`created_by`) REFERENCES `userdetails` (`user_details_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Focusing on the 'user_details_id' and 'created_by' columns, the conflict arises when created_by is not referring to an existing user_details_id.
Meaning, if I am creating my own profile with the required details, my user_details_id has not been generated yet and hence I do not know what value to input into the created_by field (which is ideally supposed to contain my user_details_id).
I would appreciate if anyone can guide me in the right direction on how to approach such a conflict, and if there is a way to determine what the value of the user_details_id field could be before it's even generated.
Thanks.
You can't handle it by temporarily disabling foreign key checks by setting server variable foreign_key_checks
SET FOREIGN_KEY_CHECKS=0;
INSERT INTO cities userdetails(....);
SET FOREIGN_KEY_CHECKS = 1;
# Sql to create userdetails table:
CREATE TABLE `userdetails` (
`user_details_id` int(3) unsigned NOT NULL AUTO_INCREMENT,
`user_group_id` int(1) unsigned NOT NULL,
`name` varchar(50) NOT NULL,
`email_id` varchar(50),
`password` varchar(50) NOT NULL,
`mobile_no` varchar(10) NOT NULL,
`company_id` int(3) unsigned,
`vehicle_id` varchar(10),
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`date_updated` datetime NOT NULL,
`created_by` int(3) unsigned,
`status` enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (`user_details_id`),
UNIQUE KEY `email_id` (`email_id`),
FOREIGN KEY (`user_group_id`) REFERENCES `usergroups` (`user_group_id`),
FOREIGN KEY (`company_id`) REFERENCES `companies` (`company_id`),
FOREIGN KEY (`created_by`) REFERENCES `userdetails` (`user_details_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Allowing created_by to accept NULL values seems to be a quick fix to this problem as that can imply user is self-created.

Best normalised practice to map phone numbers with user or office

I am designing a DB in MySQL. I have a user table, office table and a phone number table as I intend to allow multiple phone numbers to a user or for an office. so I know I need to add foreign keys in phone number table to office table and to user table rather than the other way round. But how do I do it without a null record?
What is the best-normalized way to make this with neither officeId not userId in the phone number record null?
MY Phone number table
CREATE TABLE `phonenumber` (
`Id` varchar(36) NOT NULL,
`Title` varchar(100) DEFAULT NULL,
`UserId` varchar(36) DEFAULT NULL,
`SchoolId` int(11) DEFAULT NULL,
`CreatedOn` datetime NOT NULL,
`ModifiedOn` datetime NOT NULL,
`CreatedBy` varchar(36) NOT NULL,
`ModifiedBy` varchar(36) NOT NULL,
`Number` varchar(20) NOT NULL,
PRIMARY KEY (`Id`),
KEY `FK_Phone_User` (`UserId`),
KEY `FK_Phone_School` (`SchoolId`),
CONSTRAINT `FK_Phone_School` FOREIGN KEY (`SchoolId`) REFERENCES `school` (`Id`),
CONSTRAINT `FK_Phone_User` FOREIGN KEY (`UserId`) REFERENCES `user` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
My User Table
CREATE TABLE `user` (
`Id` varchar(36) NOT NULL,
`UserName` varchar(20) NOT NULL,
`DisplayName` varchar(100) DEFAULT NULL,
`OfficialName` varchar(100) DEFAULT NULL,
`PasswordHash` varchar(50) DEFAULT NULL,
`CreatedOn` datetime DEFAULT NULL,
`CreatedBy` varchar(36) DEFAULT NULL,
`ModifiedOn` datetime DEFAULT NULL,
`ModifiedBy` varchar(36) DEFAULT NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `UserName` (`UserName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
My School table
CREATE TABLE `school` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(100) NOT NULL,
`AddressId` varchar(36) DEFAULT NULL,
`CreatedOn` datetime NOT NULL,
`ModifiedOn` datetime NOT NULL,
`CreatedBy` varchar(36) NOT NULL,
`ModifiedBy` varchar(36) NOT NULL,
PRIMARY KEY (`Id`),
KEY `FK_School_Address` (`AddressId`),
CONSTRAINT `FK_School_Address` FOREIGN KEY (`AddressId`) REFERENCES `address` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
You simply have to allow for NULL fields during the creation of the Phone_numbers table. What you were warned against is having a whole record with NULLs. You are correctly handling the field that but not saying anything or saying NULL. Here's an example:
CREATE TABLE Phone_numbers(
phone_number VARCHAR(15) NOT NULL,
user_id INT UNSIGNED,
office_id INT UNSIGNED,
PRIMARY KEY (phone_number),
CONSTRAINT RefUsers1 FOREIGN KEY (user_id)
REFERENCES Users(user_id),
CONSTRAINT RefOffices2 FOREIGN KEY (office_id)
REFERENCES Offices(office_id)
)ENGINE=INNODB
;
For this simple model

having trouble with my database using workbench

I am creating a test database to train with and am running into this problem -
"Schema Creation Failed: Can't create table 'db_2_5b129.tbluserassignment' (errno: 150): "
The code for the specific table is -
-- Table tblUserAssignment
CREATE TABLE IF NOT EXISTS `tblUserAssignment` (
`assignment_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT NOT NULL,
`supervisor_id` INT NOT NULL,
`position_id` INT NOT NULL,
`department_id` INT NOT NULL,
`start_date` DATE NOT NULL,
`end_date` DATE NOT NULL,
`date_added` DATE NOT NULL,
`date_modified` DATETIME NOT NULL,
`date_deleted` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`assignment_id`),
INDEX `fk_tblUserAssignment_tblUserPhone1_idx` (`user_id` ASC),
INDEX `fk_tblUserAssignment_tblUserPositions1_idx` (`position_id` ASC),
CONSTRAINT `fk_tblUserAssignment_tblUserPhone1`
FOREIGN KEY (`user_id`)
REFERENCES `tblUserPhone` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tblUserAssignment_tblUserPositions1`
FOREIGN KEY (`position_id`)
REFERENCES `tblUserPositions` (`position_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Can someone look it over and point me in the right direction?
problem is in differences between forign fields type
tblUserPositions.position_id INT UNSIGNED NOT NULL AUTO_INCREMEN
and
tblUserAssignment.position_id INT NOT NULL,`
EDIT 1
see this Use Composite Primary Key as Foreign Key
EDIT 2
this one tested & works fine
CREATE TABLE `tbluserphone` (
`contact_information_id` INT(10) UNSIGNED NOT NULL,
`user_id` INT(11) NOT NULL,
`phone_number` VARCHAR(20) NOT NULL,
`phone_type_id` INT(11) NOT NULL,
`date_added` DATETIME NOT NULL,
`date_modified` DATETIME NOT NULL,
`date_deleted` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`contact_information_id`, `user_id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
CREATE TABLE `tbluserassignment` (
`assignment_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`contact_information_id` INT(10) UNSIGNED NOT NULL,
`user_id` INT(11) NOT NULL,
`supervisor_id` INT(11) NOT NULL,
`position_id` INT(10) UNSIGNED NOT NULL,
`department_id` INT(11) NOT NULL,
`start_date` DATE NOT NULL,
`end_date` DATE NOT NULL,
`date_added` DATE NOT NULL,
`date_modified` DATETIME NOT NULL,
`date_deleted` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`assignment_id`),
INDEX `fk_tblUserAssignment_tblUserPhone1_idx` (`user_id`),
INDEX `fk_tblUserAssignment_tblUserPositions1_idx` (`position_id`),
INDEX `contact` (`contact_information_id`),
CONSTRAINT `FK_tbluserassignment_tbluserpositions`
FOREIGN KEY (`position_id`)
REFERENCES `tbluserpositions` (`position_id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT tbluserassignment_fkz1
FOREIGN KEY (contact_information_id, user_id)
REFERENCES tbluserphone (contact_information_id, user_id)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
(contact_information_id, user_id) - fields should goes same way, like in primary key difinition