Task - DDL & ER Diagram - Order Management System #2
Replies: 43 comments 1 reply
CREATE TABLE `employees`(
`employeeNumber` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`lastName` varchar(50) NOT NULL,
`firstName` varchar(50) NOT NULL,
`extension` varchar(50),
`email` varchar(50) NOT NULL,
`officeCode` int(11),
`reportsTo` int(11),
`jobTitle` varchar(50),
CONSTRAINT `PK_employees` PRIMARY KEY (`employeeNumber`),
CONSTRAINT `UQ_employees_email` UNIQUE (`email`),
CONSTRAINT `FK_offices_employees` FOREIGN KEY (`officeCode`) REFERENCES `offices` (`officeCode`),
CONSTRAINT `FK_employees_employees` FOREIGN KEY (`reportsTo`) REFERENCES `employees`(`employeeNumber`)
);
CREATE TABLE `offices`(
`officeCode` int(11) unsigned NOT NULL AUTO_INCREMENT,
`city` varchar(25) NOT NULL,
`phone` varchar(10) NOT NULL,
`addressLine1` varchar(75) NOT NULL,
`addressLine2` varchar(75) DEFAULT NULL,
`state` varchar(15) NOT NULL,
`country` varchar(15) NOT NULL,
`postalCode` int(8) NOT NULL,
`territory` varchar(15) NOT NULL,
CONSTRAINT `PK_offices` PRIMARY KEY (`officeCode`),
CONSTRAINT `UQ_offices_phone` UNIQUE (`phone`)
);
CREATE TABLE `products`(
`productCode` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`productName` varchar(50) NOT NULL,
`productLine` int(11) UNSIGNED NOT NULL,
`productScale` varchar(25),
`productVendor` varchar(30) NOT NULL,
`productDescription` varchar(100),
`quantityInStock` int(11) UNSIGNED NOT NULL,
`buyPrice` decimal(11, 2) UNSIGNED NOT NULL,
`MSRP` decimal(11, 2) UNSIGNED NOT NULL,
CONSTRAINT `PK_products` PRIMARY KEY (`productCode`),
CONSTRAINT `UQ_products_productName` UNIQUE (`productName`),
CONSTRAINT `FK_productLines_products` FOREIGN KEY (`productLine`) REFERENCES `productLines` (`productLines`)
);
CREATE TABLE `productLines` (
`productLine` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`textDescription` varchar(255),
`htmlDescription` varchar(255),
`image` varchar(255),
CONSTRAINT `PK_productLines` PRIMARY KEY (`productLine`),
);
CREATE TABLE `orderDetails` (
`orderNumber` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`productCode` int(11) UNSIGNED NOT NULL,
`quantityOrdered` int(11) UNSIGNED NOT NULL,
`priceEach` decimal(11,2) NOT NULL,
`orderLineNumber` int(11),
CONSTRAINT `PK_orderDetails` PRIMARY KEY (`orderNumber`, `productCode`),
CONSTRAINT `FK_orderDetails_productCode` FOREIGN KEY (`productCode`) REFERENCES `products` (`productCode`)
);
CREATE TABLE `orders` (
`orderNumber` int(11) UNSIGNED NOT NULL,
`orderDate` datetime NOT NULL,
`requiredDate` datetime,
`shippedDate` datetime NOT NULL,
`status` varchar(255) NOT NULL,
`comments` varchar(255),
`customerNumber` int(11) UNSIGNED NOT NULL,
CONSTRAINT `PK_orders` PRIMARY KEY (`orderNumber`),
CONSTRAINT `FK_orderDetails_orders` FOREIGN KEY (`orderNumber`) REFERENCES `orderDetails` (`orderNumber`),
CONSTRAINT `FK_customers_orders` FOREIGN KEY (`customerNumber`) REFERENCES `customers` (`customerNumber`)
);
CREATE TABLE `customers` (
`customerNumber` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`customerName` varchar(50) NOT NULL,
`contactLastName` varchar(25),
`contactFirstName` varchar(25),
`phone` varchar(10) NOT NULL,
`addressLine1` varchar(100) NOT NULL,
`addressLine2` varchar(100),
`city` varchar(15) NOT NULL,
`state` varchar(15) NOT NULL,
`postalCode` varchar(6) NOT NULL,
`country` varchar(15) NOT NULL,
`salesRepEmployeeNumber` int(11) UNSIGNED,
`creditLimit` decimal(11, 2) UNSIGNED,
CONSTRAINT `PK_customers` PRIMARY KEY (`customerNumber`),
CONSTRAINT `UQ_customers_phone` UNIQUE (`phone`)
CONSTRAINT `FK_employees_customers` FOREIGN KEY (`salesRepEmployeeNumber`) REFERENCES `employees` (`employeeNumber`),
);
CREATE TABLE `payments`(
`customerNumber` int(11) UNSIGNED NOT NULL,
`checkNumber` int(11) UNSIGNED,
`paymentDate` datetime NOT NULL,
`amount` decimal(11, 2) NOT NULL,
CONSTRAINT `PK_payments` PRIMARY KEY (`customerNumber`, `checkNumber`),
CONSTRAINT `UQ_payments_checkNumber` UNIQUE (`checkNumber`)
CONSTRAINT `FK_customers_payments` FOREIGN KEY (`customerNumber`) REFERENCES `customers` (`customerNumber`),
)
|
|
|
|
CREATE DATABASE IF NOT EXISTS product_details CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; show databases; USE product_details; CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLE |
|
CREATE TABLE CREATE TABLE CONSTRAINT CREATE TABLE CONSTRAINT CREATE TABLE CONSTRAINT ) CREATE TABLE
CONSTRAINT ) CREATE TABLE CONSTRAINT ) |
|
CREATE DATABASE IF NOT EXISTS first_db; CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLE ) |
|
|
CREATE database IF NOT EXISTS USE CREATE TABLE IF NOT EXISTS ); CREATE TABLE IF NOT EXISTS ); CREATE TABLE IF NOT EXISTS CREATE TABLE IF NOT EXISTS CREATE TABLE IF NOT EXISTS CREATE TABLE IF NOT EXISTS CREATE TABLE IF NOT EXISTS |
|
|
|
|
|
|
|
CREATE TABLE `customers` (
`customerNumber` int(12) UNSIGNED NOT NULL AUTO_INCREMENT,
`customerName` varchar(25) NOT NULL,
`contactLastName` varchar(25) DEFAULT NULL,
`contactFirstName` varchar(25) DEFAULT NULL,
`phone` varchar(25) DEFAULT NULL,
`addressLine1` varchar(30) DEFAULT NULL,
`addressLine2` varchar(30) DEFAULT NULL,
`city` varchar(30) DEFAULT NULL,
`state` varchar(30) DEFAULT NULL,
`postalCode` int DEFAULT NULL,
`country` varchar(30) DEFAULT NULL,
`salesRepEmployeeNumber` int DEFAULT NULL,
`creditLimit` int DEFAULT NULL,
CONSTRAINT `PK_customer` PRIMARY KEY (`customerNumber`)
);
CREATE TABLE `orders` (
`orderNumber` int(12) UNSIGNED NOT NULL AUTO_INCREMENT,
`orderDate` datetime DEFAULT NULL,
`requiredDate` datetime DEFAULT NULL,
`shippedDate` datetime DEFAULT NULL,
`status` varchar(30) DEFAULT NULL,
`comments` varchar(30) DEFAULT NULL,
`customerNumber` int(12) UNSIGNED NOT NULL,
CONSTRAINT `PK_order` PRIMARY KEY (`orderNumber`),
CONSTRAINT `FK_customer` FOREIGN KEY (`customerNumber`)
REFERENCES customers(`customerNumber`)
);
CREATE TABLE `products` (
`productCode` int(12) UNSIGNED NOT NULL AUTO_INCREMENT,
`productName` varchar(30) NOT NULL,
`productLine` varchar(40) DEFAULT NULL,
`producScale` varchar(30) DEFAULT NULL,
`productVendor` varchar(30) DEFAULT NULL,
`productDescription` varchar(50) DEFAULT NULL,
`quantityInStock` int UNSIGNED ,
`buyPrice` int UNSIGNED NOT NULL,
`MSRP` int UNSIGNED NOT NULL,
CONSTRAINT `PK_product` PRIMARY KEY (`productCode`)
) |
CREATE SCHEMA IF NOT EXISTS `company` DEFAULT CHARACTER SET utf8mb4 ;
USE `company` ;CREATE TABLE IF NOT EXISTS `company`.`customers` (
`customerNumber` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`customerName` VARCHAR(50) NULL,
`contactLastName` VARCHAR(25) NOT NULL,
`contactFirstName` VARCHAR(25) NOT NULL,
`phone` VARCHAR(13) NOT NULL,
`addressLine1` VARCHAR(40) NOT NULL,
`addressLine2` VARCHAR(30) NULL DEFAULT NULL,
`city` VARCHAR(25) NOT NULL,
`state` VARCHAR(20) NULL,
`postalCode` VARCHAR(8) NULL DEFAULT NULL,
`country` VARCHAR(56) NULL,
`salesRepEmployeeNumber` INT UNSIGNED NOT NULL,
`creditLimit` INT NULL,
PRIMARY KEY (`customerNumber`))
ENGINE = InnoDB;
CREATE UNIQUE INDEX `customerNumber_UNIQUE` ON `company`.`customers` (`customerNumber` DESC) VISIBLE;
CREATE UNIQUE INDEX `customerName_UNIQUE` ON `company`.`customers` (`customerName` DESC) VISIBLE;
CREATE TABLE IF NOT EXISTS `company`.`orders` (
`orderNumber` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`orderDate` DATETIME NOT NULL,
`requiredDate` DATE NOT NULL,
`shippedDate` DATE NULL DEFAULT NULL,
`status` VARCHAR(40) NOT NULL,
`comments` VARCHAR(70) NULL DEFAULT NULL,
`customerNumber` INT UNSIGNED NOT NULL,
PRIMARY KEY (`orderNumber`, `customerNumber`),
CONSTRAINT `fk_orders_customers`
FOREIGN KEY (`customerNumber`)
REFERENCES `company`.`customers` (`customerNumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE UNIQUE INDEX `orderNumber_UNIQUE` ON `company`.`orders` (`orderNumber` DESC) VISIBLE;
CREATE INDEX `fk_orders_customers_idx` ON `company`.`orders` (`customerNumber` DESC) VISIBLE;
CREATE TABLE IF NOT EXISTS `company`.`products` (
`productCode` VARCHAR(20) NOT NULL,
`productName` VARCHAR(50) NOT NULL,
`productLine` VARCHAR(20) NULL DEFAULT NULL,
`productScale` VARCHAR(10) NULL DEFAULT NULL,
`productVendor` VARCHAR(30) NOT NULL,
`productDescription` VARCHAR(255) NOT NULL,
`quantityInStock` INT UNSIGNED NOT NULL,
`buyPrice` DECIMAL(14,2) UNSIGNED NOT NULL,
`MSRP` DECIMAL(14,2) UNSIGNED NOT NULL,
PRIMARY KEY (`productCode`))
ENGINE = InnoDB;
CREATE UNIQUE INDEX `productCode_UNIQUE` ON `company`.`products` (`productCode` DESC) VISIBLE;
|
CREATE TABLE `orders` (
`orderNumber` int(11) UNSIGNED NOT NULL,
`orderDate` datetime NOT NULL,
`requiredDate` datetime,
`shippedDate` datetime NOT NULL,
`status` varchar(255) NOT NULL,
`comments` varchar(255),
`customerNumber` int(11) UNSIGNED NOT NULL,
CONSTRAINT `PK_orders` PRIMARY KEY (`orderNumber`),
CONSTRAINT `FK_orderDetails_orders` FOREIGN KEY (`orderNumber`) REFERENCES `orderDetails` (`orderNumber`),
CONSTRAINT `FK_customers_orders` FOREIGN KEY (`customerNumber`) REFERENCES `customers` (`customerNumber`)
);
CREATE TABLE `customers` (
`customerNumber` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`customerName` varchar(50) NOT NULL,
`contactLastName` varchar(25),
`contactFirstName` varchar(25),
`phone` varchar(10) NOT NULL,
`addressLine1` varchar(100) NOT NULL,
`addressLine2` varchar(100),
`city` varchar(15) NOT NULL,
`state` varchar(15) NOT NULL,
`postalCode` varchar(6) NOT NULL,
`country` varchar(15) NOT NULL,
`salesRepEmployeeNumber` int(11) UNSIGNED,
`creditLimit` decimal(11, 2) UNSIGNED,
CONSTRAINT `PK_customers` PRIMARY KEY (`customerNumber`),
CONSTRAINT `UQ_customers_phone` UNIQUE (`phone`)
CONSTRAINT `FK_employees_customers` FOREIGN KEY (`salesRepEmployeeNumber`) REFERENCES `employees` (`employeeNumber`),
);
CREATE TABLE `payments`(
`customerNumber` int(11) UNSIGNED NOT NULL,
`checkNumber` int(11) UNSIGNED,
`paymentDate` datetime NOT NULL,
`amount` decimal(11, 2) NOT NULL,
CONSTRAINT `PK_payments` PRIMARY KEY (`customerNumber`, `checkNumber`),
CONSTRAINT `UQ_payments_checkNumber` UNIQUE (`checkNumber`)
CONSTRAINT `FK_customers_payments` FOREIGN KEY (`customerNumber`) REFERENCES `customers` (`customerNumber`),
) |
|
CREATE TABLE `customers` (
`customerNumber` int(12) UNSIGNED NOT NULL AUTO_INCREMENT,
`customerName` varchar(25) NOT NULL,
`contactLastName` varchar(25) DEFAULT NULL,
`contactFirstName` varchar(25) DEFAULT NULL,
`phone` varchar(25) DEFAULT NULL,
`addressLine1` varchar(30) DEFAULT NULL,
`addressLine2` varchar(30) DEFAULT NULL,
`city` varchar(30) DEFAULT NULL,
`state` varchar(30) DEFAULT NULL,
`postalCode` int DEFAULT NULL,
`country` varchar(30) DEFAULT NULL,
`salesRepEmployeeNumber` int DEFAULT NULL,
`creditLimit` int DEFAULT NULL,
CONSTRAINT `PK_customer` PRIMARY KEY (`customerNumber`)
);
CREATE TABLE `orders` (
`orderNumber` int(12) UNSIGNED NOT NULL AUTO_INCREMENT,
`orderDate` datetime DEFAULT NULL,
`requiredDate` datetime DEFAULT NULL,
`shippedDate` datetime DEFAULT NULL,
`status` varchar(30) DEFAULT NULL,
`comments` varchar(30) DEFAULT NULL,
`customerNumber` int(12) UNSIGNED NOT NULL,
CONSTRAINT `PK_order` PRIMARY KEY (`orderNumber`),
CONSTRAINT `FK_customer` FOREIGN KEY (`customerNumber`)
REFERENCES customers(`customerNumber`)
);
CREATE TABLE `products` (
`productCode` int(12) UNSIGNED NOT NULL AUTO_INCREMENT,
`productName` varchar(30) NOT NULL,
`productLine` varchar(40) DEFAULT NULL,
`producScale` varchar(30) DEFAULT NULL,
`productVendor` varchar(30) DEFAULT NULL,
`productDescription` varchar(50) DEFAULT NULL,
`quantityInStock` int UNSIGNED ,
`buyPrice` int UNSIGNED NOT NULL,
`MSRP` int UNSIGNED NOT NULL,
CONSTRAINT `PK_product` PRIMARY KEY (`productCode`)
) |
CREATE
); INSERT
Retrieve all data from the three tables respectively.
Update any employee’s extension and email.
Delete a customer record where the customer is from USA.
|
|
CREATE - CREATE TABLE offices( CREATE TABLE customers ( INSERT- INSERT INTO employees ( insert into offices ( officeCode, insert into offices ( officeCode, insert into customers ( customerNumber, insert into customers ( customerNumber, RETRIEVE- UPDATE- DELETE- |
|
|
CREATE database IF NOT EXISTS Company CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; USE Company; CREATE TABLE IF NOT EXISTS productLines( ); CREATE TABLE IF NOT EXISTS offices ( CREATE TABLE IF NOT EXISTS products( ); CREATE TABLE IF NOT EXISTS employees( CREATE TABLE IF NOT EXISTS customers( CREATE TABLE IF NOT EXISTS payments( CREATE TABLE IF NOT EXISTS orders ( |
|
CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLE CREATE TABLE |
|
CREATE TABLE IF NOT EXISTS productlines ( CREATE TABLE IF NOT EXISTS payments ( |
|
Hi @akash-coded , Please find the DDL statements as below. CREATE TABLE IF NOT EXISTS CREATE TABLE IF NOT EXISTS CREATE TABLE IF NOT EXISTS CREATE TABLE IF NOT EXISTS CREATE TABLE IF NOT EXISTS CREATE TABLE IF NOT EXISTS CREATE TABLE IF NOT EXISTS CREATE TABLE IF NOT EXISTS |
|
Hi @akash-coded, Please find my solution attached. |
|
CREATE DATABASE IF NOT EXISTS |

Uh oh!
There was an error while loading. Please reload this page.
Write DDL statements to create a DB and tables as per the given schema:
All reactions