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')
);
Related
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
I'm working on SQL Fiddle and code so far looks like this:
CREATE TABLE IF NOT EXISTS `orders` (
`id` int(6) unsigned NOT NULL,
`date` DATE NOT NULL,
`customerID` int(6) unsigned NOT NULL,
`paymentAmmount` float(6) unsigned NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_customers_customerID` FOREIGN KEY (`customerID`)
REFERENCES `customers` (`customerID`) );
CREATE TABLE IF NOT EXISTS `items` (
`id` int(6) unsigned NOT NULL,
`name` varchar(200) NOT NULL,
PRIMARY KEY (`id`) ) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `customers` (
`customerID` int(6) unsigned NOT NULL,
`firstName` varchar(200) NOT NULL,
`lastName` varchar(200) NOT NULL,
`address` varchar(200) NOT NULL,
#`accountId` int(6) unsigned NOT NULL,
PRIMARY KEY (`customerID`) ) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items-orders` (
`id` int(6) unsigned NOT NULL,
`itemId` int(6) unsigned NOT NULL,
`orderId` int(6) unsigned NOT NULL,
`itemQuantity` int(1) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_items_itemId` (`itemId`),
CONSTRAINT `FK_items_itemId` FOREIGN KEY (`itemId`) REFERENCES `items` (`id`) ) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(6) unsigned NOT NULL,
`email` varchar(200) NOT NULL,
`passwordHash` varchar(200) NOT NULL,
PRIMARY KEY (`id`) ) DEFAULT CHARSET=utf8;
There are two lines that don't work:
KEY `FK_customers_customerID` (`customerID`),
CONSTRAINT `FK_customers_customerID` FOREIGN KEY (`customerID`) REFERENCES `customers` (`id`) ) DEFAULT CHARSET=utf8;
I'm trying to link the 'customerID' var in the 'orders' table to the same var in the 'customers' table.
It worked with the 'items-orders' and 'items' table, but now I keep getting this message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'KEY `FK_customers_customerID` (`customerID`), CONSTRAINT `FK_customers_customer' at line 1
I really don't know how to fix this, any help would be appriciated.
The primary key in customers is CustomerId, so you need to use that:
CONSTRAINT `FK_customers_customerID` FOREIGN KEY (`customerID`)
REFERENCES `customers` (`customerID`)
I am trying to create a database table called wp_tokens with a foreign key relationship to another table called wp_users. However, every time I attempt to run the create table SQL, I get the error "Foreign key constraint is incorrectly formed". I have tried multiple re-edits of the same code, but I just cannot figure out what is going on.
This is the code for wp_users
CREATE TABLE `wp_users` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_login` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_pass` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_nicename` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_url` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_activation_key` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_status` int(11) NOT NULL DEFAULT '0',
`display_name` varchar(250) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY `user_login_key` (`user_login`),
KEY `user_nicename` (`user_nicename`),
KEY `user_email` (`user_email`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci
This is the SQL for wp_tokens
CREATE TABLE `wp_tokens` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`token` varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES wp_users (`ID`)
)
Any help would be greatly appreciated!
The unsigned is important -- the types need to be identical. Try this:
CREATE TABLE `wp_tokens` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
`token` varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES wp_users (`ID`)
)
Here is a rextester.
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;
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;