Duplicate key name 'unique_id' - mysql

Here is the sql, however, there is an error says "*#1061 - Duplicate key name 'unique_id'* ", what is the problem.
create table `users`(
uid int(11) auto_increment,
unique_id varchar(23) not null unique,
name varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null,
PRIMARY KEY (`unique_id`),
UNIQUE KEY `uid` (`uid`),
UNIQUE KEY `unique_id` (`unique_id`),
UNIQUE KEY `email` (`email`)
)ENGINE=InnoDB AUTO_INCREMENT=877888 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

remove this line
UNIQUE KEY `unique_id` (`unique_id`),
since unique_id is already Primary Key. and Primary Keys are unique.
full CREATE TABLE statement
create table `users`
(
uid int(11) auto_increment,
unique_id varchar(23) not null,
name varchar(50) not null,
email varchar(100) not null unique, -- specified here
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null,
PRIMARY KEY (`unique_id`),
UNIQUE KEY `uid` (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=877888
DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SQLFiddle Demo

Related

How to convert MySQL query to equivalent Oracle sql Query

I want to convert below MY SQL query to Oracle SQL query, could someone please help ?
Below instructor table is parent entity, and instructor_detail table child entity.
CREATE TABLE `instructor_detail` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`youtube_channel` varchar(128) DEFAULT NULL,
`hobby` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
CREATE TABLE `instructor` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) DEFAULT NULL,
`last_name` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`instructor_detail_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_DETAIL_idx` (`instructor_detail_id`),
CONSTRAINT `FK_DETAIL` FOREIGN KEY (`instructor_detail_id`) REFERENCES `instructor_detail` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
I have tried below but not working
CREATE TABLE instructor (
id numeric(11) NOT NULL PRIMARY KEY,
first_name varchar(45) DEFAULT NULL,
last_name varchar(45) DEFAULT NULL,
email varchar(45) DEFAULT NULL,
instructor_detail_id numeric(10) not null
);
CREATE TABLE instructor_detail (
id NUMERIC(11) NOT NULL PRIMARY KEY,
youtube_channel varchar(128) DEFAULT NULL,
hobby varchar(45) DEFAULT NULL,
CONSTRAINT fk_instructor
FOREIGN KEY (instructor_detail_id)
REFERENCES instructor(instructor_detail_id)
);
error : Error report -
ORA-00904: "INSTRUCTOR_DETAIL_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Appreciated
Oracle 11 doesn't support auto-generated primary keys, so you need a trigger for that. And there are some different rules on cascading constraints.
But otherwise:
CREATE TABLE instructor_detail (
id int PRIMARY KEY,
youtube_channel varchar2(128) DEFAULT NULL,
hobby varchar2(45) DEFAULT NULL
) ;
CREATE TABLE instructor (
id int NOT NULL PRIMARY KEY,
first_name varchar2(45) DEFAULT NULL,
last_name varchar2(45) DEFAULT NULL,
email varchar2(45) DEFAULT NULL,
instructor_detail_id int,
CONSTRAINT FK_DETAIL FOREIGN KEY (instructor_detail_id) REFERENCES instructor_detail (id)
) ;
CREATE INDEX idx_instructor_instruct_detail_id ON instructor(instructor_detail_id);
Here is a db<>fiddle.

Add a foregin key to a different table

I'm trying to add username from my st_accounts column into my table results using mySQL.
st_accounts table
`id` int(11) NOT NULL, //Primary Key
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(100) NOT NULL,
`img` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
results table
`score_ID` int(11) NOT NULL, //Primary key
`score` int(20) NOT NULL,
`date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I think you want:
create table accounts (
account_id int auto_increment primary key,
. . .
);
create table results (
result_id int auto_increment primary key,
account_id int not null
score int NOT NULL,
score_date date NOT NULL,
constraint fk_results_accounts foreig key (accounts_id) references accounts(account_id)
) ;
Notes:
Declare the primary key explicitly, not in a comment.
My convention is to name the primary key after the name of the table (in singular) with _id after it.
The foreign key has the same name as the primary key -- self documenting.
I made the integer primary keys auto_increment, so the database can assign unique values.

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.

MySQL table with a varchar column as foreign key

I am trying to create a table with a varchar column as foreign key but MySql gives me an error while creating the table. My query is like this:
CREATE TABLE network_classes (
id TINYINT(1) UNSIGNED NOT NULL AUTO_INCREMENT,
category VARCHAR(80) NOT NULL,
PRIMARY KEY(id),
KEY `key_1` (`id`,`category`)
)
ENGINE=InnoDB;
CREATE TABLE networks (
id TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
category VARCHAR(80) NOT NULL,
director_id TINYINT(3) UNSIGNED NULL,
director_name VARCHAR(100) NULL,
description VARCHAR(1000) NULL,
last_modified TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
user_id SMALLINT UNSIGNED NULL,
PRIMARY KEY(id),
KEY `networks_fk1` (`category`),
CONSTRAINT `networks_fk1` FOREIGN KEY (`category`) REFERENCES `network_classes` (`category`) ON DELETE NO ACTION,
INDEX networks_index2471(name),
INDEX networks_index2472(director_id, director_name)
)
ENGINE=InnoDB;
and I get this error:
[Err] 1215 - Cannot add foreign key constraint
I am using MySQL 5.6.12. How can I rewrite my query to fix it?
You can only have a foreign key referencing a unique field. Modify your network_classes table so that the category field is unique, like below
CREATE TABLE network_classes (
id TINYINT(1) UNSIGNED NOT NULL AUTO_INCREMENT,
category VARCHAR(80) NOT NULL,
PRIMARY KEY(id),
UNIQUE KEY `category_UNIQUE` (`category`),
KEY `key_1` (`id`,`category`)
)
ENGINE=InnoDB;
CREATE TABLE networks (
id TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
category VARCHAR(80) NOT NULL,
director_id TINYINT(3) UNSIGNED NULL,
director_name VARCHAR(100) NULL,
description VARCHAR(1000) NULL,
last_modified TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
user_id SMALLINT UNSIGNED NULL,
PRIMARY KEY(id),
KEY `networks_fk1` (`category`),
CONSTRAINT `networks_fk1` FOREIGN KEY (`category`) REFERENCES `network_classes` (`category`) ON DELETE NO ACTION,
INDEX networks_index2471(name),
INDEX networks_index2472(director_id, director_name)
)
ENGINE=InnoDB;
You should then be able to add the foreign key you want
column types in the table and the referenced table do not match for
constraint
Why 2 varchar columns with same size not match in type? And of course the answer is obvious collation. Turns out that in the new table the column was UTF-8 instead of ASCII as in the referenced table. Changed to ascii and done.
The target of a FOREIGN KEY constraint needs to be indexed. Usually, it is a PRIMARY KEY so this isn't an issue, but in your case it's not (although it's part of a composite key, that's not enough)
Create an index on your network_classes.category field:
CREATE INDEX category_idx ON network_classes(category);
Then re-create your networks table.
For me, it was the charset that was missing. Just providing an example for reference.
CREATE TABLE `client` (
id int NOT NULL,
name varchar(255) NOT NULL,
email varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
created_at datetime NOT NULL,
created_by varchar(50) NOT NULL DEFAULT 'admin',
updated_at datetime NOT NULL,
updated_by varchar(50) NOT NULL DEFAULT 'admin',
PRIMARY KEY (id),
UNIQUE KEY name_UNIQUE (name),
KEY name_idx (name)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
create table `usecase` (`id` int NOT NULL AUTO_INCREMENT, `client` varchar(255) NOT NULL, `name` varchar(255), `description` varchar(1000), `rule_file` varchar(255), `parsed_rule_file` varchar(255), `archived` BOOLEAN DEFAULT 0, `state` ENUM('INITIATED','PARSED','UPLOADED', 'COMPLETE','FAILED'), `digest` varchar(255), `created_by` varchar(128) DEFAULT NULL, `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), `updated_by` varchar(128) DEFAULT NULL, `updated_at` datetime(3) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uniq_usecase_id` (`client`,`name`), FOREIGN KEY (`client`) REFERENCES client(`name`))ENGINE=InnoDB DEFAULT CHARSET=latin1;
I had missed the line
DEFAULT CHARSET=latin1
in
CONSTRAINT `networks_fk1` FOREIGN KEY (`category`)
REFERENCES `network_classess` (`category`) ON DELETE NO ACTION,
you have used network_classess instead of network_classes (as in your create table script), so that table not exists.
EDIT
Name of your constraint is the same of key (networks_fk1) change ones.
I read better your DDL.
Your table network_classes has a primary key ID, so is correct put as foreign key a field to link your id field, the category field mustn't appear in your table network, but I think you must put fk_network_class (as int) linked to id (of network_classes)

DB Structure : Ok or not?

I am developing an application management business (sales, suppliers, customers, products, ...) for a new company. To begin, I need to create a database. Could you please tell me if the BD scheme bellow is good and optimized ?
CREATE TABLE IF NOT EXISTS `company` (
id UNSIGNED INT NOT NULL auto_increment,
`SIRET` varchar(50) NOT NULL,
`nom` varchar(50) NOT NULL,
`description` varchar(500) NOT NULL,
`enable` ENUM('YES', 'NO') DEFAULT 'YES',
`level` int(1) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY SIRET (SIRET)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
// contactType can be one of the 3 values : email, phone, fax
CREATE TABLE IF NOT EXISTS `contactType` (
id UNSIGNED INT NOT NULL auto_increment,
`contactType` ENUM('email', 'phonenumber', 'faxnumber')
`mobile` ENUM('YES', 'NO') default 'NO',
PRIMARY KEY (id),
UNIQUE KEY type (contactType)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `contacts` (
id UNSIGNED INT NOT NULL auto_increment,
`SIRET` varchar(50) NOT NULL,
`contactType` varchar(50) NOT NULL, // A reference to contactType just above
`contactref` varchar(50) NOT NULL, // Phone number, fax number or email adress
PRIMARY KEY (id),
UNIQUE KEY type (SIRET)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `customers` (
id UNSIGNED INT NOT NULL auto_increment,
`SIRET` varchar(50) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY type (SIRET)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `supplier` (
id UNSIGNED INT NOT NULL auto_increment,
`SIRET` varchar(50) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY type (SIRET)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `industry` (
id UNSIGNED INT NOT NULL auto_increment,
`industry` varchar(250) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY type (activite)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `entreprisePerIndustry` (
id UNSIGNED INT NOT NULL auto_increment,
`industry_id` varchar(250) NOT NULL, // Chemical, Computer, Consulting, ...
FOREIGN KEY (industry_id) REFERENCES industry(id) ON DELETE CASCADE,
`SIRET` varchar(50) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY type (industry_id)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Below are few TIPS which can helps you to create better DB
Change table engine from MyISAM to InnoDB if you want make foreign key constrains to work.
AUTO INCREMENT DataType should be UNSIGNED INT. this will double the range.
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT`
if a column value chosen from a list of permitted values then change dataType to ENUM .In your case enable, level can be turned into ENUM dataType
`enable` ENUM( 'y', 'n' ) NOT NULL DEFAULT 'y' COMMENT 'y:yes; n:no' `
add foreign key relation to
contacts.contactType with contactType.id
entreprisePerIndustry .industry with industry.id
update
I have created basic and optimized table structure ( AFAIK ).
--
-- Table structure for table `tbl_company`
--
CREATE TABLE IF NOT EXISTS `tbl_company` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`siret` varchar(50) NOT NULL,
`nom` int(11) NOT NULL,
`description` varchar(250) NOT NULL,
`enable` enum('y','n') NOT NULL DEFAULT 'y' COMMENT 'y:yes; n:no',
`level` enum('1','2') NOT NULL DEFAULT '1',
`last_updated_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Table structure for table `tbl_contact`
--
CREATE TABLE IF NOT EXISTS `tbl_contact` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`siret` varchar(50) NOT NULL,
`contact_type` enum('email','phone','fax') NOT NULL DEFAULT 'email',
`contact_ref` varchar(100) NOT NULL COMMENT 'Phone number, fax number or email adress',
`last_updated_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `siret` (`siret`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
...
...
complate structure is here