How do I start auto increment from a specific point? - sql

CREATE TABLE `batchinfo` (
`rowid` int(11) NOT NULL AUTO_INCREMENT,
`datapath` mediumtext,
`analysistime` varchar(50) DEFAULT NULL,
`reporttime` varchar(50) DEFAULT NULL,
`lastcalib` varchar(50) DEFAULT NULL,
`analystname` varchar(150) DEFAULT NULL,
`reportname` varchar(150) DEFAULT NULL,
`batchstate` varchar(150) DEFAULT NULL,
`instrument` varchar(20) DEFAULT NULL,
PRIMARY KEY (`rowid`),
UNIQUE KEY `rowid_UNIQUE` (`rowid`)
) ENGINE=InnoDB AUTO_INCREMENT=15034 DEFAULT CHARSET=latin1
I want to start the auto incremenet from 20000.
How do I do that? Can I edit the table some how to start incrementing from 20000?

ALTER TABLE batchinfo AUTO_INCREMENT = 20000;
See also Autoincrement

See the last line of your query:
AUTO_INCREMENT=15034
Change it to:
AUTO_INCREMENT=20000
Easy as that! :)
CREATE TABLE `batchinfo` (
`rowid` int(11) NOT NULL AUTO_INCREMENT,
`datapath` mediumtext,
`analysistime` varchar(50) DEFAULT NULL,
`reporttime` varchar(50) DEFAULT NULL,
`lastcalib` varchar(50) DEFAULT NULL,
`analystname` varchar(150) DEFAULT NULL,
`reportname` varchar(150) DEFAULT NULL,
`batchstate` varchar(150) DEFAULT NULL,
`instrument` varchar(20) DEFAULT NULL,
PRIMARY KEY (`rowid`),
UNIQUE KEY `rowid_UNIQUE` (`rowid`)
) ENGINE=InnoDB AUTO_INCREMENT=20000 DEFAULT CHARSET=latin1;
INSERT INTO batchinfo (datapath) values('test');
SELECT * FROM batchinfo;

I don't know how to do it from the CREATE statement, but after that you can do this:
ALTER TABLE `batchinfo` AUTO_INCREMENT = 20000;

Related

MySQL: All parts of primary key must be NOT NULL; if you need NULL in a key, use UNIQUE instead fivem

can someone fix the whole sql for me because i don't really understand sql
this is for my fivem server
https://www.mediafire.com/file/4hqnihgpxu7gfsd/mdt.sql/file
CREATE TABLE IF NOT EXISTS `mdt_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cid` VARCHAR(20) NOT NULL,
`information` MEDIUMTEXT DEFAULT NULL,
`tags` TEXT NOT NULL,
`gallery` TEXT NOT NULL,
`jobtype` VARCHAR(25) DEFAULT 'police',
`pfp` TEXT DEFAULT NULL,
`fingerprint` VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (`cid`),
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_bulletin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` TEXT NOT NULL,
`desc` TEXT NOT NULL,
`author` varchar(50) NOT NULL,
`time` varchar(20) NOT NULL,
`jobtype` VARCHAR(25) DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_reports` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` varchar(50) DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`type` varchar(50) DEFAULT NULL,
`details` text DEFAULT NULL,
`tags` text DEFAULT NULL,
`officersinvolved` text DEFAULT NULL,
`civsinvolved` text DEFAULT NULL,
`gallery` text DEFAULT NULL,
`time` varchar(20) DEFAULT NULL,
`jobtype` varchar(25) DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_bolos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` varchar(50) DEFAULT NULL,
`title` varchar(50) DEFAULT NULL,
`plate` varchar(50) DEFAULT NULL,
`owner` varchar(50) DEFAULT NULL,
`individual` varchar(50) DEFAULT NULL,
`detail` text DEFAULT NULL,
`tags` text DEFAULT NULL,
`gallery` text DEFAULT NULL,
`officersinvolved` text DEFAULT NULL,
`time` varchar(20) DEFAULT NULL,
`jobtype` varchar(25) NOT NULL DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_convictions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cid` varchar(50) DEFAULT NULL,
`linkedincident` int(11) NOT NULL DEFAULT 0,
`warrant` varchar(50) DEFAULT NULL,
`guilty` varchar(50) DEFAULT NULL,
`processed` varchar(50) DEFAULT NULL,
`associated` varchar(50) DEFAULT '0',
`charges` text DEFAULT NULL,
`fine` int(11) DEFAULT 0,
`sentence` int(11) DEFAULT 0,
`recfine` int(11) DEFAULT 0,
`recsentence` int(11) DEFAULT 0,
`time` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_incidents` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` varchar(50) NOT NULL DEFAULT '',
`title` varchar(50) NOT NULL DEFAULT '0',
`details` text NOT NULL,
`tags` text NOT NULL,
`officersinvolved` text NOT NULL,
`civsinvolved` text NOT NULL,
`evidence` text NOT NULL,
`time` varchar(20) DEFAULT NULL,
`jobtype` varchar(25) NOT NULL DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_logs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` text NOT NULL,
`time` varchar(20) DEFAULT NULL,
`jobtype` varchar(25) DEFAULT 'police',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_vehicleinfo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`plate` varchar(50) DEFAULT NULL,
`information` text NOT NULL ,
`stolen` tinyint(1) NOT NULL DEFAULT 0,
`code5` tinyint(1) NOT NULL DEFAULT 0,
`image` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `mdt_impound` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`vehicleid` int(11) NOT NULL,
`linkedreport` int(11) NOT NULL,
`fee` int(11) DEFAULT NULL,
`time` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
made changes in mdt_data table and mdt_vehicleinfo table

SQL query for getting subscription dates

I have these tables into which I would like to check user subscription period:
Entities:
CREATE TABLE `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`city` varchar(30) DEFAULT NULL,
`CONTENT` text DEFAULT NULL,
`country` varchar(50) DEFAULT NULL,
`created_at` datetime(6) DEFAULT NULL,
`discount` decimal(8,2) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`first_name` varchar(50) DEFAULT NULL,
`grand_total` decimal(8,2) DEFAULT NULL,
`item_discount` decimal(8,2) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`line1` varchar(50) DEFAULT NULL,
`line2` varchar(50) DEFAULT NULL,
`middle_name` varchar(50) DEFAULT NULL,
`mobile` varchar(15) DEFAULT NULL,
`promo` varchar(100) DEFAULT NULL,
`province` varchar(50) DEFAULT NULL,
`session_id` int(11) DEFAULT NULL,
`shipping` decimal(8,2) DEFAULT NULL,
`status` varchar(100) DEFAULT NULL,
`sub_total` decimal(8,2) DEFAULT NULL,
`tax` decimal(8,2) DEFAULT NULL,
`token` varchar(100) DEFAULT NULL,
`total` decimal(8,2) DEFAULT NULL,
`updated_at` datetime(6) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`phone` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=latin1
CREATE TABLE `subscription` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` decimal(19,2) DEFAULT NULL,
`created_at` datetime(6) DEFAULT NULL,
`currency` varchar(20) DEFAULT NULL,
`duration` bigint(20) DEFAULT NULL,
`end_at` datetime(6) DEFAULT NULL,
`error` varchar(200) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
`product` varchar(20) DEFAULT NULL,
`run_at` datetime(6) DEFAULT NULL,
`start_at` datetime(6) DEFAULT NULL,
`status` varchar(20) DEFAULT NULL,
`updated_at` datetime(6) DEFAULT NULL,
`title` varchar(20) DEFAULT NULL,
`parent_transaction_id` int(11) DEFAULT NULL,
`parent_transactionId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23443556 DEFAULT CHARSET=latin1
CREATE TABLE `payment_transactions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` decimal(19,2) DEFAULT NULL,
`code` varchar(100) DEFAULT NULL,
`CONTENT` text DEFAULT NULL,
`created_at` datetime(6) DEFAULT NULL,
`currency` varchar(20) DEFAULT NULL,
`error` varchar(200) DEFAULT NULL,
`external_id` varchar(255) DEFAULT NULL,
`gateway` varchar(20) DEFAULT NULL,
`mode` int(11) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
`reconciled_at` datetime(6) DEFAULT NULL,
`reference_transaction_id` int(11) DEFAULT NULL,
`status` varchar(20) DEFAULT NULL,
`type` varchar(20) DEFAULT NULL,
`unique_transactionId` varchar(50) DEFAULT NULL,
`updated_at` datetime(6) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`unique_transaction_id` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=119 DEFAULT CHARSET=latin1
I'm new to database table design so I need to ask what table relations relations should I use to get the user subscription period using user id?
Is it a good idea to use Orders table as a main table and make SQL queries with JOIN to get the subscription period using user id?
JSFFiddle: http://sqlfiddle.com/#!9/262df6/1
As per your requirement, you need to get the user subscription period using user id.
You can get user subscription period and other data from SUBSCRIPTION table by joining ORDERS table, as they have order_id you can directly join the two tables by querying for user_id in where condition.
select subs.* from subscription subs
left join orders odr on odr.id = subs.order_id
where odr.user_id = 221 -- pass the required user id
specify the required columns and query for the userid in where condition
verified with JSFiddle you shared by adding a record in subscription table : http://sqlfiddle.com/#!9/b834fc/2
The main thing to consider here is how you'll normally be wanting to access these tables in the future. Based on what you've posted, I'm going to assume there are users (I don't see a users table), and that they place orders and also have subscriptions.
The thing these share in common - the relation between the two - is the user. Having the user be the focal point also creates a 1-to-many relationship, as opposed to a many-to-many relationship, which is much easier to manage. So, rather than having orders tied to subscriptions, which could get confusing, I would recommend having them both tied to the user. What if a single user has multiple orders, and multiple subscriptions? Which order would you relate to which subscription?
If you have a users table, then I would structure it so each user gets their own unique primary key. Let's call it user_id. Then, have the user_id in both the subscriptions (relating the subscriptions back to the user) and also the orders (relating orders back to the user).
With the user_id relating the two, you could relate the orders and subscriptions easily.

Cannot add or update a child row: a foreign key constraint fails (Clubs and Users)

User Table
CREATE TABLE IF NOT EXISTS `users` (
`userId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`firstname` varchar(25) NOT NULL,
`lastname` varchar(25) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(32) NOT NULL,
`addressLine1` varchar(80) NOT NULL,
`addressLine2` varchar(80) NOT NULL,
`town` varchar(30) NOT NULL,
`county` varchar(30) NOT NULL,
PRIMARY KEY (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=55 ;
Club Table
CREATE TABLE IF NOT EXISTS `clubs` (
`clubId` int(11) NOT NULL AUTO_INCREMENT,
`clubName` varchar(100) NOT NULL,
`startTime` varchar(5) NOT NULL,
`finishTime` varchar(5) NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`clubId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
memberid Table
CREATE TABLE IF NOT EXISTS `membersid` (
`memberId` int(11) NOT NULL AUTO_INCREMENT,
`clubId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
PRIMARY KEY (`memberId`,`clubId`,`userId`),
UNIQUE KEY `memberId` (`memberId`),
KEY `clubId` (`clubId`),
KEY `userId` (`userId`),
KEY `clubId_2` (`clubId`),
KEY `clubId_3` (`clubId`),
KEY `userId_2` (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
DAO
String query = "INSERT INTO membersid( userId, clubId ) VALUES ( ?,? )";
Keep getting error Cannot add or update a child row: a foreign key constraint fails
If someone could be that would be great:)
Your keys in the membersid table (please change that name to members) are messed up. Try
CREATE TABLE IF NOT EXISTS members
(
`memberId` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`clubId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
UNIQUE KEY (`clubId`,`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

SQL delete based on query

How do I delete all laptops made by a manufacturer that doesn’t make PCs?
I currently have the following tables:
CREATE TABLE `Device` (
`model` varchar(50) NOT NULL DEFAULT '',
`speed` double DEFAULT NULL,
`RAM` int(11) DEFAULT NULL,
`HD` int(11) DEFAULT NULL,
`list_price` double DEFAULT NULL,
`type` varchar(45) NOT NULL,
`screen` varchar(45) DEFAULT NULL,
PRIMARY KEY (`model`,`type`)
);
INSERT INTO `Device` VALUES
('GATE TOP',2.5,2000,500,1000,'LAPTOP','12'),
('GATE TOWER',2.5,2000,500,1000,'DESKTOP',NULL),
('HELLO TOWER',3.5,8000,1000,1299,'DESKTOP',NULL),
('MACBOOK AIR',2.5,2048,500,599,'LAPTOP','11'),
('MACBOOK PRO',3.5,8000,1000,1299,'LAPTOP','19'),
('PAAP',2.5,2048,500,599,'DESKTOP',NULL),
('TOWER',3,3100,400,2499,'DESKTOP',NULL),
('ULTRA TOWER',6,5000,1000,8999,'DESKTOP',NULL),
('VAIO',2.5,2000,500,569,'LAPTOP','12'),
('VAIO TOWER',2.5,2000,500,569,'DESKTOP',NULL);
CREATE TABLE `Product` (
`manu_Name` varchar(50) NOT NULL DEFAULT '',
`model` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`manu_Name`,`model`),
KEY `Product` (`model`),
CONSTRAINT `Product_ibfk_1` FOREIGN KEY (`manu_Name`)
REFERENCES `Manufacturer` (`name`),
CONSTRAINT `Product_ibfk_2` FOREIGN KEY (`model`)
REFERENCES `Device` (`model`)
);
INSERT INTO `Product` VALUES
('GATEWAY','GATE TOP'),('GATEWAY','GATE TOWER'),
('ACER','HELLO TOWER'),('APPLE','MACBOOK AIR'),
('APPLE','MACBOOK PRO'),('ACER','PAAP'),
('DELL','TOWER'),('SONY','VAIO'),
('SONY','VAIO TOWER');
CREATE TABLE `Manufacturer` (
`name` varchar(50) NOT NULL,
`country` varchar(50) DEFAULT NULL,
`phone` varchar(10) DEFAULT NULL,
PRIMARY KEY (`name`)
);
INSERT INTO `Manufacturer` VALUES
('ACER','TAIWAN','9024801111'),
('APPLE','UNITED STATES','9028189125'),
('DELL','UNITED STATES','9025551234'),
('GATEWAY','UNITED STATES','8705551698'),
('SONY','JAPAN','0123456789'),
('TOSHIBA','JAPAN','1235553560');
The delete statement would be something like this:
delete from `Device` where `model` in (
select `model` from `Product` where `manu_Name` in ('APPLE')
);

mass replace of values in more tables by a highest value

I have a table with OWN_ID and OWN_Email -
own_id | own_email
-----------------------------------------------
3ace7cf80edd | email#example.com
3acf6af33ff7 | email#example.com
3acda2524e00 | email#example.com
3ad75583c9a7 | spam#example.com
3ad74b018999 | spam#example.com
etc.
the problem is that it should contain only a single ID per Email, also I need to replace all OWN_ID values in another table by highest OWN_ID value of the OWN_Email
sql create:
CREATE TABLE `blahblah.`eventowner` (
`OWN_ID` varchar(12) COLLATE utf8_czech_ci NOT NULL DEFAULT '',
`OWN_Email` varchar(120) COLLATE utf8_czech_ci DEFAULT NULL,
`OwnDateFormat` varchar(16) COLLATE utf8_czech_ci DEFAULT NULL,
`OwnWeekStart` int(11) DEFAULT NULL,
`OwnDayStart` int(11) DEFAULT NULL,
`OwnDayEnd` int(11) DEFAULT NULL,
PRIMARY KEY (`OWN_ID`),
KEY `OwnerEmailIndex` (`OWN_Email`),
KEY `OwnerIndex` (`OWN_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci
CREATE TABLE `blahblah`.`event` (
`EVN_ID` varchar(128) COLLATE utf8_czech_ci DEFAULT NULL,
`EVNGRP_ID` varchar(12) COLLATE utf8_czech_ci DEFAULT NULL,
`EVNOWN_ID` varchar(12) COLLATE utf8_czech_ci DEFAULT NULL,
`EVNRCR_ID` varchar(12) COLLATE utf8_czech_ci DEFAULT NULL,
`Evn_EditCounter` int(11) DEFAULT NULL,
`Evn_Created` int(11) DEFAULT NULL,
`Evn_Modified` int(11) DEFAULT NULL,
`EvnFolder` varchar(128) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnTitle` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnNote` text COLLATE utf8_czech_ci,
`EvnLocation` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnPriority` int(11) DEFAULT NULL,
`EvnComplete` int(11) DEFAULT NULL,
`EvnColor` varchar(2) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnClass` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnShareType` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnTimeFormat` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnType` varchar(127) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnStatus` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnOrganizer` varchar(80) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnContact` varchar(80) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnURL` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnStartDate` int(11) DEFAULT NULL,
`EvnStartTime` int(11) DEFAULT NULL,
`EvnEndDate` int(11) DEFAULT NULL,
`EvnEndTime` int(11) DEFAULT NULL,
`EvnRID` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnUID` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnExpire` int(11) DEFAULT NULL,
`EvnSequence` int(11) DEFAULT NULL,
`EvnFlags` int(11) DEFAULT NULL,
KEY `EventGroupClassIndex` (`EVNGRP_ID`,`EvnClass`),
KEY `EventGroupFolderIndex` (`EVN_ID`,`EVNGRP_ID`,`EvnFolder`),
KEY `EventRIDIndex` (`EvnRID`),
KEY `EventUIDIndex` (`EvnUID`),
KEY `EventGroupRCRIndex` (`EVN_ID`,`EVNGRP_ID`,`EVNRCR_ID`),
KEY `EventExpireIndex` (`EvnExpire`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci
delete t1 from table t1, table t2
where t1.own_email= t2.own_email
and t1.own_id < t2.own_id
update othertable, table
set othertable.own_id= table.own_id
where othertable.own_email= table.own_email;
Could accomplish this fairly simple using a transaction that nukes the whole table.
BEGIN;
SELECT own_email, max(own_Id) INTO temptable FROM table GROUP BY own_email
TRUNCATE table;
INSERT INTO table SELECT * FROM temptable;
DELETE temptable;
COMMIT;
But, that Isn't the best way -- you can also do it by deleting just the right rows.
DELETE FROM table AS d WHERE EXISTS (
SELECT own_id FROM table AS t
WHERE NOT EXISTS (
SELECT max(own_id) FROM table AS i
GROUP BY own_email
WHERE t.own_email = i.own_email
)
AND d.own_id = t.own_id
AND d.own_email = t.own_email
);
Here we delete all own_ids, that aren't the max(own_id) for an E-mail.
This can be accomplished with NOT IN (understanding the caveat on nulls), or an anti-join on the table being deleted from