1

I'm trying to learn Database Management. This is the code I created based on the textbook example:

    drop database `Pine_Valley_Furniture_Company`;

CREATE DATABASE `Pine_Valley_Furniture_Company` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

USE `Pine_Valley_Furniture_Company`;

CREATE TABLE IF NOT EXISTS `CUSTOMER` (
  `ID` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT = 100,       -- I made the decision to start the auto-increment from 100 to reserve ids below 100 for special customers (company subsidiaries, government clients) or special placeholder (error) values 
  `Name` varchar(25) COLLATE utf8_unicode_ci NOT NULL,      -- Can't have a nameless customer, so 'NOT NULL'
  `Address` varchar(30) COLLATE utf8_unicode_ci,            -- Address fields are not required, since a customer might do a pick up at the store, so I didn't use 'NOT NULL' for these fields
  `City` varchar(20) COLLATE utf8_unicode_ci,
  `State_` char(2) COLLATE utf8_unicode_ci,                 -- 'state' is a reserved keyword, so I used 'state_' for the column name instead
  `Postalcode` varchar(10) COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


INSERT INTO `CUSTOMER` (`ID`, `Name`, `Address`, `City`, `State_`, `Postalcode`) VALUES(101, 'Mike Smith', '11 Maple drive', 'Nashua', 'NH', '03060');
INSERT INTO `CUSTOMER` (`ID`, `Name`, `Address`, `City`, `State_`, `Postalcode`) VALUES(102, 'Boston College', '505 Amherst St', 'Boston', 'MA', '03063');
INSERT INTO `CUSTOMER` (`ID`, `Name`, `Address`, `City`, `State_`, `Postalcode`) VALUES(103, 'Annie Chang', '75 Circle ln', 'Annieborough', 'MA', '03555');
INSERT INTO `CUSTOMER` (`ID`, `Name`, `Address`, `City`, `State_`, `Postalcode`) VALUES(104, 'Sam Bond', '1 First drive', 'Pleasantville', 'NH', '03555');
INSERT INTO `CUSTOMER` (`ID`, `Name`, `Address`, `City`, `State_`, `Postalcode`) VALUES(105, 'Mr. Cookie Monster', '606 Market St', 'Celebration', 'FL', '34747');




CREATE TABLE IF NOT EXISTS `ORDER_LINE` (
  `OrderID` int NOT NULL AUTO_INCREMENT=100 PRIMARY KEY,        -- I made the decision to start the auto-increment from 100 to reserve ids below 100 for special orders or special placeholder (error) values 
  `ProductID` int NOT NULL,
  `Orderedquantity` int NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


INSERT INTO `ORDER_LINE` (`OrderID`, `ProductID`, `Orderedquantity`) VALUES(100, 10015, 3);
INSERT INTO `ORDER_LINE` (`OrderID`, `ProductID`, `Orderedquantity`) VALUES(101, 20315, 1);
INSERT INTO `ORDER_LINE` (`OrderID`, `ProductID`, `Orderedquantity`) VALUES(102, 20010, 1);
INSERT INTO `ORDER_LINE` (`OrderID`, `ProductID`, `Orderedquantity`) VALUES(103, 10010, 1);
INSERT INTO `ORDER_LINE` (`OrderID`, `ProductID`, `Orderedquantity`) VALUES(104, 18013, 2);




CREATE TABLE IF NOT EXISTS `ORDER_` (                           -- Named it 'ORDER_' instead of 'ORDER', since ORDER is a reserved keyword
  `OrderID` int NOT NULL AUTO_INCREMENT=100 PRIMARY KEY,        -- I made the decision to start the auto-increment from 100 to reserve ids below 100 for special orders or special placeholder (error) values 
  `CustomerID` int NOT NULL,
  `OrderDate` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


INSERT INTO `ORDER_` (`OrderID`, `CustomerID`, `OrderDate`) VALUES(101, 101, '2018-09-03T14:25:10.487');
INSERT INTO `ORDER_` (`OrderID`, `CustomerID`, `OrderDate`) VALUES(102, 102, '2018-09-03T15:20:22.988'); -- the first two customers to register also placed the 1st two orders, hence order id and customer id for the 1st two twoples are equal (customer id 101 and order id is 101)
INSERT INTO `ORDER_` (`OrderID`, `CustomerID`, `OrderDate`) VALUES(103, 101, '2018-09-03T16:45:11.883'); -- customer 101 placed another order
INSERT INTO `ORDER_` (`OrderID`, `CustomerID`, `OrderDate`) VALUES(104, 103, '2018-09-03T18:01:19.001');
INSERT INTO `ORDER_` (`OrderID`, `CustomerID`, `OrderDate`) VALUES(105, 104, '2018-09-03T18:26:55.089');




CREATE TABLE IF NOT EXISTS `PRODUCT` (
  `ProductID` int NOT NULL AUTO_INCREMENT=10000 PRIMARY KEY,        -- I made the decision to start the auto-increment from 10000 to reserve ids below 10000 for special products or special placeholder (error) values 
  `Description` varchar(50) COLLATE utf8_unicode_ci,
  `Finish` varchar(20) COLLATE utf8_unicode_ci,
  `StandardPrice` numeric(6,2)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


INSERT INTO `PRODUCT` (`ProductID`, `Description`, `Finish`, `StandardPrice`) VALUES(10000, 'Small Dining Table', 'Oak', 799.97);
INSERT INTO `PRODUCT` (`ProductID`, `Description`, `Finish`, `StandardPrice`) VALUES(10001, 'Medium Dining Table', 'Oak', 899.97);
INSERT INTO `PRODUCT` (`ProductID`, `Description`, `Finish`, `StandardPrice`) VALUES(10002, 'Large Dining Table', 'Oak', 999.97);
INSERT INTO `PRODUCT` (`ProductID`, `Description`, `Finish`, `StandardPrice`) VALUES(10003, 'Royal Dining Table', 'Oak', 1099.97);
INSERT INTO `PRODUCT` (`ProductID`, `Description`, `Finish`, `StandardPrice`) VALUES(10004, 'Small Dining Table', 'Maple', 799.97);



desc table CUSTOMER;
desc table ORDER_LINE;
desc table ORDER_;
desc table PRODUCT;

drop table CUSTOMER;
drop table ORDER_LINE;
drop table ORDER_;
drop table PRODUCT;

The example from the textbook is as follows:

DROP DATABSE `shirts4mike`;
CREATE DATABASE `shirts4mike` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

USE `shirts4mike`;

CREATE TABLE IF NOT EXISTS `products` (
  `sku` int(11) DEFAULT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `img` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `price` decimal(10,2) DEFAULT NULL,
  `paypal` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(101, 'Logo Shirt, Red', 'img/shirts/shirt-101.jpg', 18.00, '9P7DLECFD4LKE');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(102, 'Mike the Frog Shirt, Black', 'img/shirts/shirt-102.jpg', 20.00, 'SXKPTHN2EES3J');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(103, 'Mike the Frog Shirt, Blue', 'img/shirts/shirt-103.jpg', 20.00, '7T8LK5WXT5Q9J');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(104, 'Logo Shirt, Green', 'img/shirts/shirt-104.jpg', 18.00, 'YKVL5F87E8PCS');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(105, 'Mike the Frog Shirt, Yellow', 'img/shirts/shirt-105.jpg', 25.00, '4CLP2SCVYM288');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(106, 'Logo Shirt, Gray', 'img/shirts/shirt-106.jpg', 20.00, 'TNAZ2RGYYJ396');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(107, 'Logo Shirt, Teal', 'img/shirts/shirt-107.jpg', 20.00, 'S5FMPJN6Y2C32');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(108, 'Mike the Frog Shirt, Orange', 'img/shirts/shirt-108.jpg', 25.00, 'JMFK7P7VEHS44');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(109, 'Get Coding Shirt, Gray', 'img/shirts/shirt-109.jpg', 20.00, 'B5DAJHWHDA4RC');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(110, 'HTML5 Shirt, Orange', 'img/shirts/shirt-110.jpg', 22.00, '6T2LVA8EDZR8L');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(111, 'CSS3 Shirt, Gray', 'img/shirts/shirt-111.jpg', 22.00, 'MA2WQGE2KCWDS');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(112, 'HTML5 Shirt, Blue', 'img/shirts/shirt-112.jpg', 22.00, 'FWR955VF5PALA');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(113, 'CSS3 Shirt, Black', 'img/shirts/shirt-113.jpg', 22.00, '4ELH2M2FW7272');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(114, 'PHP Shirt, Yellow', 'img/shirts/shirt-114.jpg', 24.00, 'AT3XQ3ZVP2DZG');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(115, 'PHP Shirt, Purple', 'img/shirts/shirt-115.jpg', 24.00, 'LYESEKV9JWE3A');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(116, 'PHP Shirt, Green', 'img/shirts/shirt-116.jpg', 24.00, 'KT7MRRJUXZR34');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(117, 'Get Coding Shirt, Red', 'img/shirts/shirt-117.jpg', 20.00, '5UXJG8PXRXFKE');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(118, 'Mike the Frog Shirt, Purple', 'img/shirts/shirt-118.jpg', 25.00, 'KHP8PYPDZZFTA');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(119, 'CSS3 Shirt, Purple', 'img/shirts/shirt-119.jpg', 22.00, 'BFJRFE24L93NW');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(120, 'HTML5 Shirt, Red', 'img/shirts/shirt-120.jpg', 22.00, 'RUVJSBR9FXXWQ');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(121, 'Get Coding Shirt, Blue', 'img/shirts/shirt-121.jpg', 20.00, 'PGN6ULGFZTXL4');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(122, 'PHP Shirt, Gray', 'img/shirts/shirt-122.jpg', 24.00, 'PYR4QH97W2TSJ');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(123, 'Mike the Frog Shirt, Green', 'img/shirts/shirt-123.jpg', 25.00, 'STDAUJJTSPT54');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(124, 'Logo Shirt, Yellow', 'img/shirts/shirt-124.jpg', 20.00, '2R2U74KWU5RXG');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(125, 'CSS3 Shirt, Blue', 'img/shirts/shirt-125.jpg', 22.00, 'GJG7F8EW3XFAS');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(126, 'Doctype Shirt, Green', 'img/shirts/shirt-126.jpg', 25.00, 'QW2LFRYGU7L4Q');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(127, 'Logo Shirt, Purple', 'img/shirts/shirt-127.jpg', 20.00, 'GFV6QVRMJU7F8');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(128, 'Doctype Shirt, Purple', 'img/shirts/shirt-128.jpg', 25.00, 'BARQMHMB565PN');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(129, 'Get Coding Shirt, Green', 'img/shirts/shirt-129.jpg', 20.00, 'DH9GXABU3P8GS');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(130, 'HTML5 Shirt, Teal', 'img/shirts/shirt-130.jpg', 22.00, '4LZ3EUVCBENE4');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(131, 'Logo Shirt, Orange', 'img/shirts/shirt-131.jpg', 20.00, '7BNDYJBKWD364');
INSERT INTO `products` (`sku`, `name`, `img`, `price`, `paypal`) VALUES(132, 'Mike the Frog Shirt, Red', 'img/shirts/shirt-132.jpg', 25.00, 'Y6EQRE445MYYW');

When I try to run either of these scripts, via command line mysql.exe --user=root --password -s < "E:\Database Design and Management\Lab 2\shirts4mike-1.sql" it gives me a bunch of errors BOTH for my script AND the textbook example.

 ERROR 1064 (42000) at line 7: 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 '= 100,   `Name` varchar(25) COLLATE
 utf8_unicode_ci NOT NULL,   `Address` v' at line 2

If I remove auto increment from everywhere , it still barks

ERROR 1064 (42000) at line 7: 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 '= 100,   `Name` varchar(25) COLLATE
utf8_unicode_ci NOT NULL,   `Address` v' at line 2

Please, help! What am I doing wrong (assuming I am, considering that the textbook code gives errors as well)? I'm on my own, no instructor guidance. It's very confusing.

sticky bit
  • 36,626
  • 12
  • 31
  • 42
InfiniteLoop
  • 387
  • 1
  • 3
  • 18
  • The example from the textbook does not seem to correlate at all with your code, so I'm not sure why you posted it. (One is named `product`, the other `products`. One has `productID`, the other `sku`. The textbook example does not contain AUTO INCREMENT, so it can't cause that error. There are many other differences as well. ) You've got several places where that same error (near `= 100`) could happen. Did you try reading the manual for the CREATE TABLE statement in the documentation? You can find it with the Google search expression *mariadb documentation* or *mysql documentation*. – Ken White Sep 11 '18 at 22:39
  • Unrelated to your syntax problem: Don't use "special" IDs to designate special records. Use an extra column per "specialty" to flag whether a row is special in some way. – sticky bit Sep 11 '18 at 22:46
  • Because the instructor told us to use it as an example. It's like our 3rd lesson this semester, we haven't written ANY sql code before this (not even a single line), just worked with workbench gui tool a little and now we have to write this. – InfiniteLoop Sep 11 '18 at 23:08
  • On top of everything, the course uses one of those custom college textbooks compiled by one of our former professors and it's out of stock at the college book store. No book and no guidance (the instructor doesn't have sql coding experience as far as I know). – InfiniteLoop Sep 11 '18 at 23:08
  • It's so frustrating for the students that several of my classmates already withdrew from the course. I'm determined to finish it no matter what, but it's hard to go without a textbook available at the moment and without professor's guidance (she said "just use shirts4mike file as an example" and gave us like 2-3 links to online materials, but they don't discuss any of these things. So, I'm both googling hard and trying to get help (hints) on here. – InfiniteLoop Sep 11 '18 at 23:08
  • The online MySQL manual has a [Tutorial](https://dev.mysql.com/doc/refman/5.7/en/tutorial.html). It also has detailed reference documentation on [CREATE TABLE](https://dev.mysql.com/doc/refman/5.7/en/create-table.html) and all other statements. The reference doc is pretty dense reading, but it's thorough! – Bill Karwin Sep 11 '18 at 23:22
  • Remove `AUTO_INCREMENT = 100` -- it is confusing and mostly useless. – Rick James Oct 04 '18 at 22:49

1 Answers1

3
`ID` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT = 100, 

This is not syntax that MySQL recognizes. Did your textbook say you could declare the initial AI value there?

If you want to start the auto-increment at 100, do this:

CREATE TABLE IF NOT EXISTS `CUSTOMER` (
    `ID` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    ... other columns ...
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

You can also change the AI value for a table any time after you create it:

ALTER TABLE `CUSTOMER` AUTO_INCREMENT=200;

If you have rows of data in the table, altering the AI value this way won't go below the largest id value that exists in the table.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Thank you for your response. Re: what the textbook says: See my comments to my original question (the course uses a custom textbook which is not available for purchase at the moment. It's just me and google right now.) Here is the screenshot of the handout (to clarify on what I am trying to do): https://imgur.com/51ic7rU – InfiniteLoop Sep 11 '18 at 23:17
  • Thank you so much, Bill! It works now. But what if I want different columns within one table to auto_incriment starting at different initial counter values? I've looked at the link to the tutorial you provided, but I don't believe it mentions this scenario. – InfiniteLoop Sep 12 '18 at 00:28
  • [CREATE TABLE Syntax](https://dev.mysql.com/doc/refman/5.7/en/create-table.html) says: "There can be only one AUTO_INCREMENT column per table, it must be indexed, and it cannot have a DEFAULT value." – Bill Karwin Sep 12 '18 at 00:31
  • Thank you! I wish you were our professor! If it's not too much trouble, is there a way to have output of "describe table" formatted nicely and include the name of the table before each table's description? My output is correct, but ugly (the type description of one column is aligned to the name of another, etc.), includes no headers ("field", "type", "null", "default", etc.) and all the descriptions just mash the columns together without identifying which table they belong to: https://imgur.com/HdloVAc – InfiniteLoop Sep 12 '18 at 00:59
  • I found this answer, but it involves a relatively complex script, way beyond our baby steps: https://stackoverflow.com/questions/38665946/convert-mysql-schema-to-github-wiki/38679580#38679580 – InfiniteLoop Sep 12 '18 at 01:00
  • I'd use `SHOW CREATE TABLE CUSTOMER\G` This outputs exactly the same syntax it would take to recreate the table, formatted nicely. It includes details that aren't shown with DESCRIBE TABLE, like indexes and constraints and table options. – Bill Karwin Sep 12 '18 at 01:15
  • Thank you! That takes care of the table names, but is there any way to make it tabulated? https://imgur.com/oIkggS8 So that int of one row is aligned with int of another row. Pri of one row is aligned with Pri of another row, NULL of one row is aligned with NULL of another one. And so on. – InfiniteLoop Sep 12 '18 at 01:23
  • Not with that SHOW statement. It always outputs just one space in between words. You can edit it in a text editor before turning it in, but I wouldn't bother, unless your professor requires it. – Bill Karwin Sep 12 '18 at 01:30