In below query I need to change IMEI_CODE and REGISTRATION_ID unique index as non unique index using alter query.How to achieve that
CREATE TABLE `ringee_user` (
`RINGEE_USER_ID` BIGINT(20) NOT NULL,
`USER_NAME` VARCHAR(40) NOT NULL,
`IMEI_CODE` VARCHAR(45) NOT NULL,
`REGISTRATION_ID` VARCHAR(200) NOT NULL,
`MOBILE_NUMBER` VARCHAR(14) NOT NULL,
`CREATED_DTTM` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`MODIFIED_DTTM` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`IS_DELETE` TINYINT(1) NULL DEFAULT '0',
PRIMARY KEY (`RINGEE_USER_ID`),
UNIQUE INDEX `REG_ID_UNIQUE` (`REGISTRATION_ID`),
UNIQUE INDEX `MOBILE_NUMBER_UNIQUE` (`MOBILE_NUMBER`),
UNIQUE INDEX `IMEI_CODE_UNIQUE` (`IMEI_CODE`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
ALTER TABLE `ringee_user`
CHANGE COLUMN `IS_DELETE` `IS_DELETE` TINYINT(1) NULL DEFAULT '0' AFTER `MOBILE_NUMBER`;
Related
I was attempted to execute following queries:
CREATE TABLE `lob_sections` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`section_name` varchar(600) NOT NULL,
`lob_type` varchar(64) NOT NULL,
`agency_id` varchar(64) NOT NULL,
`display_order` tinyint(2) NOT NULL DEFAULT 1,
`active` tinyint(1) NOT NULL DEFAULT 1,
`created_date` timestamp NOT NULL DEFAULT current_timestamp(),
`last_modified_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
UNIQUE KEY `unq_lob_sections` (`agency_id`,`lob_type`,`section_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `lob_custom_fields` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`section_id` int(11) NOT NULL,
`field_label` varchar(1400) NOT NULL,
`field_type` varchar(20) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT 1,
`display_order` tinyint(3) NOT NULL DEFAULT 1,
`required` tinyint(1) NOT NULL DEFAULT 0,
`created_date` timestamp NOT NULL DEFAULT current_timestamp(),
`last_modified_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
CONSTRAINT unq_section_field_label UNIQUE (section_id, field_label),
CONSTRAINT `fk_section_id` FOREIGN KEY (`section_id`) REFERENCES `lob_sections` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
lob_sections table is created successfully, but lob_custom_fields is not creating, it is throwing me following error:
#1005 - Can't create table abc_db.lob_custom_fields (errno: 150 "Foreign key constraint is incorrectly formed") (Details…)
When I click on Details, then it shows me reason "Create table abc_db.lob_custom_fieldswith foreign keyfk_section_id constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns.------------".
If I remove the line "CONSTRAINT unq_section_field_label UNIQUE (section_id, field_label)," from create table statement of lob_custom_fields table, then it created successfully.
How to add unique index in lob_custom_fields table? Create-Alter both are showing same error when I attempt to add unique index. Any help will be highly appreciated.
The error message says
Specified key was too long; max key length is 3072 bytes
The InnoDB internal maximum key length is 3500 bytes, but MySQL itself restricts this to 3072 bytes. This limit applies to the length of the combined index key in a multi-column index.
that is for mysql 8
so you must define
field_label varchar(1022)
That fits
CREATE TABLE `lob_sections` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`section_name` varchar(600) NOT NULL,
`lob_type` varchar(64) NOT NULL,
`agency_id` varchar(64) NOT NULL,
`display_order` tinyint(2) NOT NULL DEFAULT 1,
`active` tinyint(1) NOT NULL DEFAULT 1,
`created_date` timestamp NOT NULL DEFAULT current_timestamp(),
`last_modified_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
UNIQUE KEY `unq_lob_sections` (`agency_id`,`lob_type`,`section_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `lob_custom_fields` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`section_id` int(11) NOT NULL,
`field_label` varchar(1022) NOT NULL,
`field_type` varchar(20) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT 1,
`display_order` tinyint(3) NOT NULL DEFAULT 1,
`required` tinyint(1) NOT NULL DEFAULT 0,
`created_date` timestamp NOT NULL DEFAULT current_timestamp(),
`last_modified_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
CONSTRAINT unq_section_field_label UNIQUE (section_id, field_label),
CONSTRAINT `fk_section_id` FOREIGN KEY (`section_id`) REFERENCES `lob_sections` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
db<>fiddle here
I have 3 tables:
CREATE TABLE `channels` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`status` enum('active','inactive') NOT NULL DEFAULT 'active’,
`description` text DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=485 DEFAULT CHARSET=utf8;
CREATE TABLE `categories` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`content` text DEFAULT NULL,
`description` text DEFAULT NULL,
`status` enum('active','inactive') NOT NULL DEFAULT 'active',
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=128 DEFAULT CHARSET=utf8;
CREATE TABLE `events` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`content` text DEFAULT NULL,
`timezone` varchar(255) DEFAULT NULL,
`recurring` tinyint(1) NOT NULL DEFAULT 0,
`all_day` tinyint(1) NOT NULL DEFAULT 0,
`starts_at` timestamp NULL DEFAULT NULL,
`ends_at` timestamp NULL DEFAULT NULL,
`started_at` timestamp NULL DEFAULT NULL,
`completed_at` timestamp NULL DEFAULT NULL,
`status` enum('active','inactive','in_progress','complete') DEFAULT 'active',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=82 DEFAULT CHARSET=utf8;
I want to alter the events table to add more fields and add foreign keys to channel, event_type and client_event_type.
I've tried this:
alter table `events`
add `channel` int unsigned not null,
add `event_type` int unsigned not null,
add `client_event_type` int unsigned not null,
add constraint `events_channel_foreign` foreign key `channel` references `channels`(`id`),
add constraint `events_event_type_foreign` foreign key `event_type` references `categories`(`id`),
add constraint `events_clientEventType_foreign` foreign key `client_event_type` references `categories`(`id`),
It comes up with :
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 'references channels(id), add constraint
events_event_type_foreign foreig' at line 5
What am I doing wrong?
Thanks
You're missing parantheses:
alter table `events`
add `channel` int unsigned not null,
add `event_type` int unsigned not null,
add `client_event_type` int unsigned not null,
add constraint `events_channel_foreign` foreign key (`channel`) references `channels`(`id`),
add constraint `events_event_type_foreign` foreign key (`event_type`) references `categories`(`id`),
add constraint `events_clientEventType_foreign` foreign key (`client_event_type`) references `categories`(`id`)
I am trying to add a foreign key between two different sets of tables, the first set is customer and shopping_cart. I tried looking at the other posts regarding this error but I couldn't get it to work after looking at them.
CREATE TABLE `usale`.`customer` (
CREATE TABLE `usale`.`customer` (
`customer_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`email` VARCHAR(50) NULL DEFAULT NULL,
`active` TINYINT(1) NOT NULL DEFAULT TRUE,
`create_date` DATETIME NOT NULL,
`last_update` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`student_student_id` BIGINT NOT NULL,
`student_school_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`customer_id`, `student_student_id`, `student_school_id`)),
INDEX `fk_customer_student1_idx` (`student_student_id` ASC,`student_school_id` ASC),
INDEX `fk_customer_shopping_cart1_idx` (`shopping_cart_cart_id` ASC),
CONSTRAINT `fk_customer_student1`
FOREIGN KEY (`student_student_id`)
REFERENCES `usale`.`student` (`student_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_customer_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart` (`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;`customer_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`email` VARCHAR(50) NULL DEFAULT NULL,
`active` TINYINT(1) NOT NULL DEFAULT TRUE,
`create_date` DATETIME NOT NULL,
`last_update` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`student_student_id` BIGINT NOT NULL,
`student_school_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`customer_id`, `student_student_id`, `student_school_id`))
CREATE TABLE IF NOT EXISTS `frankapp`.`shopping_cart` (
`cart_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cart_total` DECIMAL(12,2) NULL,
PRIMARY KEY (`cart_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
ALTER TABLE customer ADD CONSTRAINT `fk_customer_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart`(`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
I'm getting the error: "ERROR: Cannot add foreign key constraint
Error Code: 1215"
For the second set of tables they are named shopping_cart and shopping_cart_item
CREATE TABLE IF NOT EXISTS `usale`.`shopping_cart_item` (
`shopping_cart_item_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
`inventory_item_inventory_item_id` BIGINT UNSIGNED NOT NULL,
`inventory_item_student_id` BIGINT NOT NULL,
`inventory_item_inventory_type` VARCHAR(45) NOT NULL,
`inventory_item_student_student_id` BIGINT NOT NULL,
`inventory_item_student_customer_id` BIGINT NOT NULL,
`inventory_item_student_school_id` BIGINT NOT NULL,
`inventory_item_edition_edition_id` BIGINT UNSIGNED NOT NULL,
`inventory_item_edition_author_id` BIGINT NOT NULL,
PRIMARY KEY (`shopping_cart_item_id`, `shopping_cart_cart_id`, `inventory_item_inventory_item_id`, `inventory_item_student_id`, `inventory_item_inventory_type`, `inventory_item_student_student_id`, `inventory_item_student_customer_id`, `inventory_item_student_school_id`, `inventory_item_edition_edition_id`, `inventory_item_edition_author_id`),
INDEX `fk_shopping_cart_items_shopping_cart1_idx` (`shopping_cart_cart_id` ASC),
INDEX `fk_shopping_cart_item_inventory_item1_idx` (`inventory_item_inventory_item_id` ASC, `inventory_item_student_id` ASC, `inventory_item_inventory_type` ASC, `inventory_item_student_student_id` ASC, `inventory_item_student_customer_id` ASC, `inventory_item_student_school_id` ASC, `inventory_item_edition_edition_id` ASC, `inventory_item_edition_author_id` ASC))
CREATE TABLE IF NOT EXISTS `frankapp`.`shopping_cart` (
`cart_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cart_total` DECIMAL(12,2) NULL,
PRIMARY KEY (`cart_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
ALTER TABLE shopping_cart_item ADD CONSTRAINT `fk_shopping_cart_items_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart`(`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
Once again I am getting error 1215: "ERROR: Cannot add foreign key constraint
Error Code: 1215"
I'm not sure why this query is taking 4 minutes to complete:
SELECT
su.sid,u.uid,u.display_name,u.locale
FROM user u
LEFT JOIN subscription_user su ON su.uid = u.uid
ORDER BY u.display_name DESC
LIMIT 0,25;
Well, I know it's due to the order, remove it and it's very fast. If I change to using INNER JOIN instead it's fast but the issue is not all users may be in the subscription_user table.
CREATE TABLE `user` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`password` varchar(100) DEFAULT NULL,
`user_type` varchar(10) NOT NULL DEFAULT 'user',
`display_name` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
`locale` varchar(8) DEFAULT 'en',
`last_login` datetime DEFAULT NULL,
`auth_type` varchar(10) DEFAULT NULL,
`auth_data` varchar(500) DEFAULT NULL,
`inactive` tinyint(4) NOT NULL DEFAULT '0',
`receive_email` tinyint(4) NOT NULL DEFAULT '1',
`stateid` int(10) DEFAULT NULL,
`owner_group_id` int(11) DEFAULT NULL,
`signature` varchar(500) DEFAULT NULL,
`raw_signature` varchar(500) DEFAULT NULL,
`round_robin` smallint(5) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`uid`),
UNIQUE KEY `email` (`email`),
KEY `stateid` (`stateid`) USING BTREE,
KEY `user_type` (`user_type`) USING BTREE,
KEY `name` (`display_name`)
) ENGINE=InnoDB AUTO_INCREMENT=28343 DEFAULT CHARSET=latin1;
CREATE TABLE `subscription_user` (
`sid` varchar(50) NOT NULL,
`uid` int(11) NOT NULL,
`deleted` tinyint(4) NOT NULL DEFAULT '0',
`forum_user` varchar(50) NOT NULL,
PRIMARY KEY (`sid`,`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
When you have an SQL query, the index can only really help you if the first column in the index is part of the query.
Your query joins su.uid = u.uid and the optimizer will not be able to use that to reference the first column in the subscription primary key index.
You should either reverse the order of the columns in the primary key, or alternatively, you should add a foreign key index, or an independent index on the uid
I have two mysql tables and i want to merge the results of these two tables based on the common column rev_id. The merged results should be sorted by the date of two tables.
Please help me.
CREATE TABLE `reply` (
`id` int(3) NOT NULL auto_increment,
`name` varchar(25) NOT NULL default '',
`member_id` varchar(45) NOT NULL,
`rev_id` int(3) NOT NULL default '0',
`description` text,
`post_date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`flag` char(2) NOT NULL default 'N',
PRIMARY KEY (`id`),
KEY `member_id` (`member_id`)
) ENGINE=MyISAM;
CREATE TABLE `comment` (
`com_id` int(8) NOT NULL auto_increment,
`rev_id` int(5) NOT NULL default '0',
`member_id` varchar(50) NOT NULL,
`comm_desc` text NOT NULL,
`date_created` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`com_id`),
KEY `member_id` (`member_id`)
) ENGINE=MyISAM;
Try this:
SELECT * FROM reply LEFT JOIN comment ON reply.rev_id = comment.rev_id ORDER BY reply.post_date, comment.date_created