SQL Product Table Creation - Incorrect Syntax - sql

I am trying to create a products table running the following SQL query:
CREATE TABLE ‘products’ (
‘id’ INT NOT NULL AUTO_INCREMENT ,
‘name’ VARCHAR( 255 ) NOT NULL ,
‘shortdesc’ VARCHAR( 255 ) NOT NULL ,
‘longdesc’ TEXT NOT NULL ,
‘thumbnail’ VARCHAR( 255 ) NOT NULL ,
‘image’ VARCHAR( 255 ) NOT NULL ,
‘sizes’ ENUM( ‘s’, ‘m’, ‘l’, ‘xl’ ) NOT NULL ,
‘colors’ ENUM( ‘red’, ‘blue’, ‘green’, ‘brown’, ‘white’, ‘black’ ) NOT NULL ,
‘grouping’ VARCHAR( 16 ) NOT NULL ,
‘status’ ENUM( ‘active’, ‘inactive’ ) NOT NULL ,
‘category_id’ INT NOT NULL ,
‘featured’ ENUM (‘true’, ‘false’) NOT NULL,
‘price’ FLOAT( 4, 2 ) NOT NULL,
PRIMARY KEY ( ‘id’ )
) TYPE = MYISAM ;
However this error occurs:
#1064 - 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 '‘s’, ‘m’, ‘l’, ‘xl’ ) NOT NULL , ‘colors’ ENUM( ‘red’, ' at line 8
Which type should I use? I am following an example from a book.

After some playing I think I can say it is the ‘’ characters that are throwing it off. I ran this without issue:
CREATE TABLE `products2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`sizes` enum('s','m','l','xl') NOT NULL,
`colors` enum('red','blue','green','brown','white','black') NOT NULL,
`shortdesc` varchar(255) NOT NULL,
`longdesc` text NOT NULL,
`thumbnail` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`grouping` varchar(16) NOT NULL,
`status` enum('active','inactive') NOT NULL,
`category_id` int(11) NOT NULL,
`featured` enum('true','false') NOT NULL,
`price` float(4,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Using backticks and apostrophes instead of curly quotation marks seems to work for me
CREATE TABLE `products` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 255 ) NOT NULL ,
`shortdesc` VARCHAR( 255 ) NOT NULL ,
`longdesc` TEXT NOT NULL ,
`thumbnail` VARCHAR( 255 ) NOT NULL ,
`image` VARCHAR( 255 ) NOT NULL ,
`sizes` ENUM( 's', 'm', 'l', 'xl' ) NOT NULL ,
`colors` ENUM( 'red', 'blue', 'green', 'brown', 'white', 'black' ) NOT NULL ,
`grouping` VARCHAR( 16 ) NOT NULL ,
`status` ENUM( 'active', 'inactive' ) NOT NULL ,
`category_id` INT NOT NULL ,
`featured` ENUM ('true', 'false') NOT NULL,
`price` FLOAT( 4, 2 ) NOT NULL,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;

Related

Error when creating multiple columns in one table with same values

When I try to exeecute the folliwng in SQL, I get error, I am trying to add multiple columns with same value to one table.
CREATE TABLE IF NOT EXISTS `vendor` (
`product_id` varchar(255) NOT NULL
), `Vendor_SKU_or_Stock_Number` varchar(255) NOT NULL
), `Brand_Name` varchar(255) NOT NULL
), `Image_URL5`varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ERROR: #1064 - 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 ' Vendor_SKU_or_Stock_Number varchar(255) NOT NULL
), Brand_Name varchar(255)' at line 13
Please help
You have too many closing parenthesis, and your table name and field names should not be quoted.
Try this:
CREATE TABLE IF NOT EXISTS vendor (
product_id varchar(255) NOT NULL
, Vendor_SKU_or_Stock_Number varchar(255) NOT NULL
, Brand_Name varchar(255) NOT NULL
, Image_URL5 varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Removed ")"
CREATE TABLE IF NOT EXISTS `vendor` (
`product_id` varchar(255) NOT NULL
, `Vendor_SKU_or_Stock_Number` varchar(255) NOT NULL
, `Brand_Name` varchar(255) NOT NULL
, `Image_URL5`varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Try this
CREATE TABLE IF NOT EXISTS `vendor` (
`product_id` varchar(255) NOT NULL
, `Vendor_SKU_or_Stock_Number` varchar(255) NOT NULL
, `Brand_Name` varchar(255) NOT NULL
, `Image_URL5`varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Try this
CREATE TABLE IF NOT EXISTS vendor (
product_id varchar(255) NOT NULL,
vendor_SKU_or_Stock_Number varchar(255) NOT NULL,
brand_Name varchar(255) NOT NULL,
image_URL5 varchar(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARACTER SET=latin1;

Derby and SQL from Workbench

I've a project at school and i must use a Derby DB.
I'm using eclipse and the SQL Scrapbook to execute my SQL script.
Here is the problem:
My SQL script was generated by Mysql Workbench, we did a schemas and then exported it to a .sql
When i try to execute it in the SQL Scrapbook, I have MANY errors, the sql seems not to be adapted to derby (for example, AUTOINCREMENT is now (START WITH 1, INCREMENT BY 1).
Here is my .sql :
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL';
DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NOT NULL ,
`lastname` VARCHAR(45) NOT NULL ,
`email` VARCHAR(45) NOT NULL ,
`adress` VARCHAR(45) NOT NULL ,
`city` VARCHAR(45) NOT NULL ,
`zip` VARCHAR(45) NOT NULL ,
`login` VARCHAR(45) NOT NULL ,
`password` VARCHAR(45) NOT NULL ,
`admin` TINYINT(1) NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
-- -----------------------------------------------------
-- Table `mydb`.`Movie`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Movie` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Movie` (
`id` INT NOT NULL AUTO_INCREMENT ,
`title` VARCHAR(45) NOT NULL ,
`resume` VARCHAR(500) NULL ,
`genre` VARCHAR(60) NULL ,
`grade` INT(11) NULL ,
`review_pub` VARCHAR(200) NULL ,
`review_gen` VARCHAR(200) NULL ,
`poster` VARCHAR(100) NULL ,
`duration` INT(11) NULL ,
`release_date` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Projection`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Projection` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Projection` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT ' ' ,
`date` DATE NULL ,
`length` INT(11) NULL ,
`Movie_id` INT NOT NULL ,
`price` DECIMAL(10,0) NULL ,
`location` VARCHAR(45) NOT NULL ,
`place_nbr` INT(11) NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_Projection_Movie` (`Movie_id` ASC) ,
CONSTRAINT `fk_Projection_Movie`
FOREIGN KEY (`Movie_id` )
REFERENCES `mydb`.`Movie` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`command`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`command` ;
CREATE TABLE IF NOT EXISTS `mydb`.`command` (
`id` INT NOT NULL AUTO_INCREMENT ,
`Projection_id` INT NOT NULL ,
`User_id` INT NOT NULL ,
`paid` TINYINT(1) NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_command_Projection1` (`Projection_id` ASC) ,
INDEX `fk_command_User1` (`User_id` ASC) ,
CONSTRAINT `fk_command_Projection1`
FOREIGN KEY (`Projection_id` )
REFERENCES `mydb`.`Projection` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_command_User1`
FOREIGN KEY (`User_id` )
REFERENCES `mydb`.`User` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
Thank you
Try using DdlUtils (http://db.apache.org/ddlutils/) to export your schema from your MySQL database, then you can import that same schema to Derby.

How can I insert a new column after x on a table?

I have a table called users:
CREATE TABLE `users` (
`UID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(45) NOT NULL ,
`password` VARCHAR(200) NOT NULL ,
`name` VARCHAR(45) NOT NULL ,
`email` VARCHAR(255) NOT NULL ,
`phone` VARCHAR(30) NOT NULL ,
`logo` VARCHAR(255) NULL ,
`address` VARCHAR(100) NULL ,
`information` TEXT NULL ,
`announcement` TEXT NULL ,
`role` INT UNSIGNED NULL ,
`yahoo` VARCHAR(100) NULL ,
`twitter` VARCHAR(20) NULL ,
`timelogin` DATETIME NULL,
PRIMARY KEY (`UID`) ,
UNIQUE INDEX `UID_UNIQUE` (`UID` ASC) ,
UNIQUE INDEX `name_UNIQUE` (`name` ASC) ,
UNIQUE INDEX `logo_UNIQUE` (`logo` ASC) ,
UNIQUE INDEX `username_UNIQUE` (`username` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
the problem is that I want to add or insert a new row like
`image_seal` VARCHAR(255) NULL ,
after logo, can we do something like that in mysql ? append-thing?
Thanks
Adam Ramadhan
ALTER TABLE users ADD COLUMN image_seal VARCHAR(255) NULL AFTER logo;
Check here for Syntax of ALTER TABLE
It seems you want to add a new column to your table, not a row.
From the MySQL reference it seems you can use the keyword AFTER.
ALTER TABLE users ADD COLUMN image_seal VARCHAR(255) AFTER logo;
should do the trick.
What are you using to administer your database? SQLBuddy, PHPmyAdmin and others can do this for you, without you needing to run queries.

problem in getting count

Here is my table complete schema , i want to get the each category sale count ,
How to get that each category sale count ,
CREATE TABLE IF NOT EXISTS `tblbasket` (
`BID` int(20) NOT NULL
AUTO_INCREMENT,
`BasketSessionID` varchar(100) NOT NULL DEFAULT '0',
`ProductCode` varchar(50) NOT NULL,
`Quantity` int(20) NOT NULL,
`AfterDiscount` double NOT NULL DEFAULT '0',
`ProductCost` double NOT NULL,
`SaleMode` varchar(10) NOT NULL DEFAULT 'p' COMMENT 'p Price f Free',
`BillType` varchar(5) NOT NULL DEFAULT 's' COMMENT 's sale r Returns',
`EntryUser` int(20) NOT NULL,
`EntryTimestamp` int(50) NOT NULL,
`EntryDate` datetime NOT NULL,
`UpdatedUser` int(20) NOT NULL,
`UpdatedDate` datetime NOT NULL,
`Status` int(3) NOT NULL DEFAULT '1',
PRIMARY KEY (`BID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
CREATE TABLE IF NOT EXISTS `tblcategory` (
`CatID` int(11) NOT NULL AUTO_INCREMENT,
`CatName` varchar(20) NOT NULL,
`Discount` int(11) NOT NULL,
`EntryUser` int(10) NOT NULL,
`EntryDate` datetime NOT NULL,
`UpdateUser` int(20) NOT NULL,
`UpdatedDate` datetime NOT NULL,
`Status` int(3) NOT NULL DEFAULT '1',
PRIMARY KEY (`CatID`),
KEY `CatName` (`CatName`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
CREATE TABLE IF NOT EXISTS `tblsale` ( `SaleID` int(20) NOT NULL AUTO_INCREMENT,
`BillNo` varchar(30) NOT NULL DEFAULT '0',
`BasketSessionID` varchar(200) NOT NULL,
`AfterDiscount` double NOT NULL,
`ProductCost` double NOT NULL, `VAT` int(20) NOT NULL DEFAULT '0',
`BillType` varchar(5) NOT NULL DEFAULT 's' COMMENT 'same like tblbasket table',
`EntryUser` int(20) NOT NULL,
`EntryDate` datetime NOT NULL,
`UpdatedUser` int(20) NOT NULL,
`UpdateDate` datetime NOT NULL,
`OfficeNotesTS` text NOT NULL,
`Status` int(5) NOT NULL DEFAULT '1',
PRIMARY KEY (`SaleID`),
UNIQUE KEY `BillNo` (`BillNo`),
UNIQUE KEY `BasketSessionID` (`BasketSessionID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE IF NOT EXISTS `tblstock` (
`StockID` int(20) NOT NULL AUTO_INCREMENT,
`ProductCode` varchar(50) NOT NULL,
`ManufacturerID` int(20) NOT NULL,
`CategoryID` int(20) NOT NULL,
`ProductTitle` varchar(150) NOT NULL,
`Timestamp` int(50) NOT NULL,
`EntryUser` int(20) NOT NULL,
`EntryDate` datetime NOT NULL,
`UpdateUser` int(20) NOT NULL,
`UpdatedDate` datetime NOT NULL,
`Status` int(3) NOT NULL DEFAULT '1',
PRIMARY KEY (`StockID`),
KEY `ManufacturerID` (`ManufacturerID`),
KEY `CategoryID` (`CategoryID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2175 ;
CREATE TABLE IF NOT EXISTS `tblstockhistory` (
`STID` int(20) NOT NULL AUTO_INCREMENT,
`StockID` int(20) NOT NULL,
`SupplierID` int(20) NOT NULL,
`InvoiceNO` varchar(20) NOT NULL,
`InvoiceDate` datetime NOT NULL,
`InvoiceAmount` float NOT NULL DEFAULT '0',
`Quantity` int(20) NOT NULL,
`Cost` float NOT NULL,
`MRP` float NOT NULL,
`Discount` int(20) NOT NULL DEFAULT '0',
`VAT` float NOT NULL,
`EntryUser` int(10) NOT NULL,
`EntryDate` datetime NOT NULL,
`UpdateUser` int(20) NOT NULL,
`UpdatedDate` datetime NOT NULL,
`Status` int(10) NOT NULL DEFAULT '1',
PRIMARY KEY (`STID`),
KEY `StockID` (`StockID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2190 ;
ALTER TABLE `tblstock`
ADD CONSTRAINT `tblStock_ibfk_1`
FOREIGN KEY (`ManufacturerID`)
REFERENCES `tblmanufacturer` (`ManufacturerID`);
ALTER TABLE `tblstockhistory`
ADD CONSTRAINT `tblStockHistory_ibfk_1`
FOREIGN KEY (`StockID`)
REFERENCES `tblstock` (`StockID`);
try something like that (its hard to understand without foreign key, and little explain ):
SELECT st.CategoryID,SUM(b.ProductCost*b.Quantity) FROM tblsale s
LEFT JOIN tblbasket b ON b.BasketSessionID = s.BasketSessionID
LEFT JOIN tblstock st ON st.ProductCode = b.ProductCode GROUP BY st.CategoryID

Problem with SQL syntax (probably simple)

I'm doing some custom database work for a module for Drupal and I get the following SQL error when trying to create my table:
user warning: 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 'DEFAULT NULL, rooms INT DEFAULT NULL, adults INT DEFAULT NULL, children' at line 14 query: CREATE TABLE dr_enquiry ( eId INT unsigned NOT NULL auto_increment, eKey VARCHAR(16) NOT NULL, dateSent INT NOT NULL DEFAULT 0, status VARCHAR(30) NOT NULL DEFAULT 'Unanswered', custName VARCHAR(50) NOT NULL, custEmail VARCHAR(200) NOT NULL, custPhone VARCHAR(25) NOT NULL, custCountry VARCHAR(40) NOT NULL, custIP VARCHAR(11) DEFAULT NULL, offerName VARCHAR(100) NOT NULL, offerURL VARCHAR(200) NOT NULL, arrival DATETIME DEFAULT NULL, departure DEFAULT NULL, rooms INT DEFAULT NULL, adults INT DEFAULT NULL, children INT DEFAULT NULL, childAges VARCHAR(32) DEFAULT NULL, toddlers INT DEFAULT NULL, toddlerAges VARCHAR(32) DEFAULT NULL, catering VARCHAR(255) DEFAULT NULL, comments VARCHAR(255) DEFAULT NULL, agent VARCHAR(100) DEFAULT NULL, voucher VARCHAR(100) DEFAULT NULL, PRIMARY KEY (eId) ) /*!40100 DEFAULT CHARACTER SET UTF8 */ in /home/travelco/public_html/includes/database.inc on line 550.
The 'departure' field doesn't seem to have a type eg varchar, mediumint etc. Try adding one and see if that solves your problem :)
Nullability is not set using a DEFAULT constraint. Thus, the compiler is balking at each instance of DEFAULT NULL. Thus, the create statement should look like:
CREATE TABLE dr_enquiry (
eId INT unsigned NOT NULL auto_increment
, eKey VARCHAR(16) NOT NULL
, dateSent INT NOT NULL DEFAULT 0
, status VARCHAR(30) NOT NULL DEFAULT 'Unanswered'
, custName VARCHAR(50) NOT NULL
, custEmail VARCHAR(200) NOT NULL
, custPhone VARCHAR(25) NOT NULL
, custCountry VARCHAR(40) NOT NULL
, custIP VARCHAR(11) NULL
, offerName VARCHAR(100) NOT NULL
, offerURL VARCHAR(200) NOT NULL
, arrival DATETIME NULL
, departure DATETIME NULL
, rooms INT NULL
, adults INT NULL
, children INT NULL
, childAges VARCHAR(32) NULL
, toddlers INT NULL
, toddlerAges VARCHAR(32) NULL
, catering VARCHAR(255) NULL
, comments VARCHAR(255) NULL
, agent VARCHAR(100) NULL
, voucher VARCHAR(100) NULL
, PRIMARY KEY (eId)
)
(Oh and departure did not have a datatype as richsage mentioned. I presumed it was DateTime.)